GH-597 Add support for handling MultipartFile(s)
This initial fix ensures that functions can process single MultipartFile as well as multiple. Resolves #597
This commit is contained in:
@@ -18,8 +18,11 @@ package org.springframework.cloud.function.web.mvc;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
|
||||
@@ -28,12 +31,20 @@ import org.springframework.cloud.function.web.RequestProcessor.FunctionWrapper;
|
||||
import org.springframework.cloud.function.web.constants.WebRequestConstants;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.ResponseEntity.BodyBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -48,14 +59,44 @@ public class FunctionController {
|
||||
this.processor = processor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping(path = "/**", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE,
|
||||
MediaType.MULTIPART_FORM_DATA_VALUE })
|
||||
@ResponseBody
|
||||
public Mono<ResponseEntity<?>> form(WebRequest request) {
|
||||
FunctionWrapper wrapper = wrapper(request);
|
||||
|
||||
if (((ServletWebRequest) request).getRequest() instanceof StandardMultipartHttpServletRequest) {
|
||||
MultiValueMap<String, MultipartFile> multiFileMap = ((StandardMultipartHttpServletRequest) ((ServletWebRequest) request)
|
||||
.getRequest()).getMultiFileMap();
|
||||
if (!CollectionUtils.isEmpty(multiFileMap)) {
|
||||
List<Message<MultipartFile>> files = multiFileMap.values().stream().flatMap(v -> v.stream())
|
||||
.map(file -> MessageBuilder.withPayload(file).copyHeaders(wrapper.headers()).build())
|
||||
.collect(Collectors.toList());
|
||||
FunctionInvocationWrapper function = wrapper.function();
|
||||
|
||||
Publisher<?> result = (Publisher<?>) function.apply(Flux.fromIterable(files));
|
||||
BodyBuilder builder = ResponseEntity.ok();
|
||||
if (result instanceof Flux) {
|
||||
result = Flux.from(result).map(message -> ((Message<?>) message).getPayload()).collectList();
|
||||
}
|
||||
return Mono.from(result).flatMap(body -> Mono.just(builder.body(body)));
|
||||
}
|
||||
}
|
||||
return this.processor.post(wrapper, null, false);
|
||||
}
|
||||
|
||||
// @PostMapping(path = "/**", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE,
|
||||
// MediaType.MULTIPART_FORM_DATA_VALUE })
|
||||
// public Mono<ResponseEntity<?>> handleFileUpload(@RequestParam("file") MultipartFile file, WebRequest request) {
|
||||
// FunctionWrapper wrapper = wrapper(request);
|
||||
//
|
||||
// Object result = wrapper.function().apply(file);
|
||||
//
|
||||
// return Mono.just(ResponseEntity.status(HttpStatus.OK).body(result));
|
||||
// }
|
||||
|
||||
@PostMapping(path = "/**")
|
||||
@ResponseBody
|
||||
public Mono<ResponseEntity<?>> post(WebRequest request,
|
||||
|
||||
Reference in New Issue
Block a user