Skip to content

Commit 70076c8

Browse files
committed
GH-597 Add support for handling MultipartFile(s)
This initial fix ensures that functions can process single MultipartFile as well as multiple. Resolves #597
1 parent 3cea034 commit 70076c8

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/mvc/FunctionController.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import java.util.Arrays;
2020
import java.util.Iterator;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
2123

2224
import org.reactivestreams.Publisher;
25+
import reactor.core.publisher.Flux;
2326
import reactor.core.publisher.Mono;
2427

2528
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
@@ -28,12 +31,20 @@
2831
import org.springframework.cloud.function.web.constants.WebRequestConstants;
2932
import org.springframework.http.MediaType;
3033
import org.springframework.http.ResponseEntity;
34+
import org.springframework.http.ResponseEntity.BodyBuilder;
35+
import org.springframework.messaging.Message;
36+
import org.springframework.messaging.support.MessageBuilder;
3137
import org.springframework.stereotype.Component;
38+
import org.springframework.util.CollectionUtils;
39+
import org.springframework.util.MultiValueMap;
3240
import org.springframework.web.bind.annotation.GetMapping;
3341
import org.springframework.web.bind.annotation.PostMapping;
3442
import org.springframework.web.bind.annotation.RequestBody;
3543
import org.springframework.web.bind.annotation.ResponseBody;
44+
import org.springframework.web.context.request.ServletWebRequest;
3645
import org.springframework.web.context.request.WebRequest;
46+
import org.springframework.web.multipart.MultipartFile;
47+
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
3748

3849
/**
3950
* @author Dave Syer
@@ -48,14 +59,44 @@ public FunctionController(RequestProcessor processor) {
4859
this.processor = processor;
4960
}
5061

62+
63+
5164
@PostMapping(path = "/**", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE,
5265
MediaType.MULTIPART_FORM_DATA_VALUE })
5366
@ResponseBody
5467
public Mono<ResponseEntity<?>> form(WebRequest request) {
5568
FunctionWrapper wrapper = wrapper(request);
69+
70+
if (((ServletWebRequest) request).getRequest() instanceof StandardMultipartHttpServletRequest) {
71+
MultiValueMap<String, MultipartFile> multiFileMap = ((StandardMultipartHttpServletRequest) ((ServletWebRequest) request)
72+
.getRequest()).getMultiFileMap();
73+
if (!CollectionUtils.isEmpty(multiFileMap)) {
74+
List<Message<MultipartFile>> files = multiFileMap.values().stream().flatMap(v -> v.stream())
75+
.map(file -> MessageBuilder.withPayload(file).copyHeaders(wrapper.headers()).build())
76+
.collect(Collectors.toList());
77+
FunctionInvocationWrapper function = wrapper.function();
78+
79+
Publisher<?> result = (Publisher<?>) function.apply(Flux.fromIterable(files));
80+
BodyBuilder builder = ResponseEntity.ok();
81+
if (result instanceof Flux) {
82+
result = Flux.from(result).map(message -> ((Message<?>) message).getPayload()).collectList();
83+
}
84+
return Mono.from(result).flatMap(body -> Mono.just(builder.body(body)));
85+
}
86+
}
5687
return this.processor.post(wrapper, null, false);
5788
}
5889

90+
// @PostMapping(path = "/**", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE,
91+
// MediaType.MULTIPART_FORM_DATA_VALUE })
92+
// public Mono<ResponseEntity<?>> handleFileUpload(@RequestParam("file") MultipartFile file, WebRequest request) {
93+
// FunctionWrapper wrapper = wrapper(request);
94+
//
95+
// Object result = wrapper.function().apply(file);
96+
//
97+
// return Mono.just(ResponseEntity.status(HttpStatus.OK).body(result));
98+
// }
99+
59100
@PostMapping(path = "/**")
60101
@ResponseBody
61102
public Mono<ResponseEntity<?>> post(WebRequest request,
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.web.mvc;
18+
19+
import java.net.URI;
20+
import java.util.List;
21+
import java.util.function.Function;
22+
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.boot.SpringApplication;
28+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
29+
import org.springframework.boot.test.web.client.TestRestTemplate;
30+
import org.springframework.cloud.function.json.JsonMapper;
31+
import org.springframework.context.ApplicationContext;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.core.io.ClassPathResource;
34+
import org.springframework.http.HttpEntity;
35+
import org.springframework.http.HttpHeaders;
36+
import org.springframework.http.HttpMethod;
37+
import org.springframework.http.MediaType;
38+
import org.springframework.http.ResponseEntity;
39+
import org.springframework.util.LinkedMultiValueMap;
40+
import org.springframework.util.SocketUtils;
41+
import org.springframework.web.multipart.MultipartFile;
42+
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
/**
45+
*
46+
* @author Oleg Zhurakousky
47+
*
48+
*/
49+
public class MultipartFileTests {
50+
51+
@BeforeEach
52+
public void init() throws Exception {
53+
String port = "" + SocketUtils.findAvailableTcpPort();
54+
System.setProperty("server.port", port);
55+
}
56+
57+
@AfterEach
58+
public void close() throws Exception {
59+
System.clearProperty("server.port");
60+
}
61+
62+
@Test
63+
public void testMultipartFileUpload() throws Exception {
64+
ApplicationContext context = SpringApplication.run(TestConfiguration.class);
65+
JsonMapper mapper = context.getBean(JsonMapper.class);
66+
TestRestTemplate template = new TestRestTemplate();
67+
String port = System.getProperty("server.port");
68+
69+
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
70+
map.add("file", new ClassPathResource("META-INF/spring.factories"));
71+
HttpHeaders headers = new HttpHeaders();
72+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
73+
74+
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
75+
map, headers);
76+
ResponseEntity<String> result = template.exchange(new URI("http://localhost:" + port + "/uppercase"),
77+
HttpMethod.POST, requestEntity, String.class);
78+
List<String> resultCollection = mapper.fromJson(result.getBody(), List.class);
79+
assertThat(resultCollection.get(0)).isEqualTo("SPRING.FACTORIES");
80+
}
81+
82+
@Test
83+
public void testMultipartFilesUpload() throws Exception {
84+
ApplicationContext context = SpringApplication.run(TestConfiguration.class);
85+
JsonMapper mapper = context.getBean(JsonMapper.class);
86+
TestRestTemplate template = new TestRestTemplate();
87+
String port = System.getProperty("server.port");
88+
89+
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
90+
map.add("fileA", new ClassPathResource("META-INF/spring.factories"));
91+
map.add("fileB", new ClassPathResource("static/test.html"));
92+
HttpHeaders headers = new HttpHeaders();
93+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
94+
95+
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
96+
map, headers);
97+
ResponseEntity<String> result = template.exchange(new URI("http://localhost:" + port + "/uppercase"),
98+
HttpMethod.POST, requestEntity, String.class);
99+
List<String> resultCollection = mapper.fromJson(result.getBody(), List.class);
100+
assertThat(resultCollection.get(0)).isEqualTo("SPRING.FACTORIES");
101+
assertThat(resultCollection.get(1)).isEqualTo("TEST.HTML");
102+
}
103+
104+
@EnableAutoConfiguration
105+
protected static class TestConfiguration {
106+
107+
@Bean
108+
public Function<MultipartFile, String> uppercase() {
109+
return value -> {
110+
return value.getOriginalFilename().toUpperCase();
111+
};
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)