|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2019 the original author or authors. |
| 2 | + * Copyright 2014-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
33 | 33 | import org.springframework.http.MediaType;
|
34 | 34 | import org.springframework.http.ReactiveHttpInputMessage;
|
35 | 35 | import org.springframework.http.codec.FormHttpMessageReader;
|
| 36 | +import org.springframework.http.codec.HttpMessageReader; |
36 | 37 | import org.springframework.http.codec.multipart.FilePart;
|
37 | 38 | import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
|
38 | 39 | import org.springframework.http.codec.multipart.Part;
|
|
59 | 60 | */
|
60 | 61 | class WebTestClientRequestConverter implements RequestConverter<ExchangeResult> {
|
61 | 62 |
|
| 63 | + private static final String DEFAULT_PART_HTTP_MESSAGE_READER = "org.springframework.http.codec.multipart.DefaultPartHttpMessageReader"; |
| 64 | + |
62 | 65 | private static final ResolvableType FORM_DATA_TYPE = ResolvableType.forClassWithGenerics(MultiValueMap.class,
|
63 | 66 | String.class, String.class);
|
64 | 67 |
|
@@ -91,17 +94,35 @@ private Parameters extractParameters(ExchangeResult result) {
|
91 | 94 | }
|
92 | 95 |
|
93 | 96 | private List<OperationRequestPart> extractRequestParts(ExchangeResult result) {
|
94 |
| - if (!ClassUtils.isPresent("org.synchronoss.cloud.nio.multipart.NioMultipartParserListener", |
95 |
| - getClass().getClassLoader())) { |
| 97 | + HttpMessageReader<Part> partHttpMessageReader = findPartHttpMessageReader(); |
| 98 | + if (partHttpMessageReader == null) { |
96 | 99 | return Collections.emptyList();
|
97 | 100 | }
|
98 |
| - return new MultipartHttpMessageReader(new SynchronossPartHttpMessageReader()) |
| 101 | + return new MultipartHttpMessageReader(partHttpMessageReader) |
99 | 102 | .readMono(ResolvableType.forClass(Part.class), new ExchangeResultReactiveHttpInputMessage(result),
|
100 | 103 | Collections.emptyMap())
|
101 | 104 | .onErrorReturn(new LinkedMultiValueMap<>()).block().values().stream()
|
102 | 105 | .flatMap((parts) -> parts.stream().map(this::createOperationRequestPart)).collect(Collectors.toList());
|
103 | 106 | }
|
104 | 107 |
|
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + private HttpMessageReader<Part> findPartHttpMessageReader() { |
| 110 | + if (ClassUtils.isPresent(DEFAULT_PART_HTTP_MESSAGE_READER, getClass().getClassLoader())) { |
| 111 | + try { |
| 112 | + return (HttpMessageReader<Part>) Class |
| 113 | + .forName(DEFAULT_PART_HTTP_MESSAGE_READER, true, getClass().getClassLoader()).newInstance(); |
| 114 | + } |
| 115 | + catch (Exception ex) { |
| 116 | + // Continue |
| 117 | + } |
| 118 | + } |
| 119 | + if (ClassUtils.isPresent("org.synchronoss.cloud.nio.multipart.NioMultipartParserListener", |
| 120 | + getClass().getClassLoader())) { |
| 121 | + return new SynchronossPartHttpMessageReader(); |
| 122 | + } |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
105 | 126 | private OperationRequestPart createOperationRequestPart(Part part) {
|
106 | 127 | ByteArrayOutputStream content = readPartBodyContent(part);
|
107 | 128 | return new OperationRequestPartFactory().create(part.name(),
|
|
0 commit comments