-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MultipartFile support to HTTP interface client
See gh-30728
- Loading branch information
1 parent
3f40452
commit e69a1d2
Showing
4 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
.../src/main/java/org/springframework/web/service/invoker/MultipartFileArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.web.service.invoker; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
/** | ||
* {@link HttpServiceArgumentResolver} for arguments of type {@link MultipartFile}. | ||
* The arguments should not be annotated. To allow for non-required arguments, | ||
* the {@link MultipartFile} parameters can also be wrapped with {@link Optional}. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 6.1 | ||
*/ | ||
public class MultipartFileArgumentResolver extends AbstractNamedValueArgumentResolver { | ||
|
||
private static final String MULTIPART_FILE_LABEL = "multipart file"; | ||
|
||
@Override | ||
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { | ||
if (!parameter.nestedIfOptional().getNestedParameterType().equals(MultipartFile.class)) { | ||
return null; | ||
} | ||
return new NamedValueInfo("", true, null, MULTIPART_FILE_LABEL, true); | ||
|
||
} | ||
|
||
@Override | ||
protected void addRequestValue(String name, Object value, MethodParameter parameter, | ||
HttpRequestValues.Builder requestValues) { | ||
Assert.state(value instanceof MultipartFile, | ||
"The value has to be of type 'MultipartFile'"); | ||
|
||
MultipartFile file = (MultipartFile) value; | ||
requestValues.addRequestPart(name, toHttpEntity(name, file)); | ||
} | ||
|
||
private HttpEntity<Resource> toHttpEntity(String name, MultipartFile file) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
if (file.getOriginalFilename() != null) { | ||
headers.setContentDispositionFormData(name, file.getOriginalFilename()); | ||
} | ||
if (file.getContentType() != null) { | ||
headers.add(HttpHeaders.CONTENT_TYPE, file.getContentType()); | ||
} | ||
return new HttpEntity<>(file.getResource(), headers); | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
...test/java/org/springframework/web/service/invoker/MultipartFileArgumentResolverTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.web.service.invoker; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.http.ContentDisposition; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.bind.annotation.RequestPart; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.service.annotation.PostExchange; | ||
import org.springframework.web.testfixture.servlet.MockMultipartFile; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Unit tests for {@link MultipartFileArgumentResolver}. | ||
* Tests for base class functionality of this resolver can be found in {@link NamedValueArgumentResolverTests}. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
class MultipartFileArgumentResolverTests { | ||
|
||
private final TestHttpClientAdapter clientAdapter = new TestHttpClientAdapter(); | ||
|
||
private TestClient client; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder(this.clientAdapter).build(); | ||
this.client = proxyFactory.createClient(TestClient.class); | ||
} | ||
|
||
@Test | ||
void multipartFile() { | ||
String fileName = "testFileName"; | ||
String originalFileName = "originalTestFileName"; | ||
MultipartFile testFile = new MockMultipartFile(fileName, originalFileName, | ||
MediaType.APPLICATION_JSON_VALUE, "test".getBytes()); | ||
this.client.postMultipartFile(testFile); | ||
|
||
Object body = clientAdapter.getRequestValues().getBodyValue(); | ||
|
||
assertThat(body).isInstanceOf(MultiValueMap.class); | ||
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) body; | ||
assertThat(map.size()).isEqualTo(1); | ||
assertThat(map.getFirst("file")).isNotNull(); | ||
HttpEntity<?> fileEntity = map.getFirst("file"); | ||
assertThat(fileEntity.getBody()).isEqualTo(testFile.getResource()); | ||
HttpHeaders headers = fileEntity.getHeaders(); | ||
assertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON); | ||
ContentDisposition contentDisposition = headers.getContentDisposition(); | ||
assertThat(contentDisposition.getType()).isEqualTo("form-data"); | ||
assertThat(contentDisposition.getName()).isEqualTo("file"); | ||
assertThat(contentDisposition.getFilename()).isEqualTo(originalFileName); | ||
} | ||
|
||
@Test | ||
void optionalMultipartFile() { | ||
this.client.postOptionalMultipartFile(Optional.empty(), "anotherPart"); | ||
|
||
Object body = clientAdapter.getRequestValues().getBodyValue(); | ||
|
||
assertThat(body).isInstanceOf(MultiValueMap.class); | ||
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) body; | ||
assertThat(map.size()).isEqualTo(1); | ||
assertThat(map.getFirst("anotherPart")).isNotNull(); | ||
} | ||
|
||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
private interface TestClient { | ||
|
||
@PostExchange | ||
void postMultipartFile(MultipartFile file); | ||
|
||
@PostExchange | ||
void postOptionalMultipartFile(Optional<MultipartFile> file, @RequestPart String anotherPart); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters