Support @RequestBody Flux<Part> in WebFlux

This commit turns the Synchronoss NIO Multipart HttpMessageReader into
a reader of Flux<Part> and creates a separate reader that aggregates
the parts into a MultiValueMap<String, Part>.

Issue: SPR-14546
This commit is contained in:
Rossen Stoyanchev
2017-05-03 17:25:49 -04:00
parent d43dfc7bae
commit b5089ac092
9 changed files with 198 additions and 57 deletions

View File

@@ -21,7 +21,8 @@ import java.util.List;
import org.springframework.core.codec.Encoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.codec.multipart.SynchronossMultipartHttpMessageReader;
import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
import org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader;
import org.springframework.util.ClassUtils;
/**
@@ -65,7 +66,9 @@ class DefaultServerCodecConfigurer extends DefaultCodecConfigurer implements Ser
super.addTypedReadersTo(result);
addReaderTo(result, FormHttpMessageReader::new);
if (synchronossMultipartPresent) {
addReaderTo(result, SynchronossMultipartHttpMessageReader::new);
SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
addReaderTo(result, () -> partReader);
addReaderTo(result, () -> new MultipartHttpMessageReader(partReader));
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2002-2017 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
*
* http://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.http.codec.multipart;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* {@code HttpMessageReader} for reading {@code "multipart/form-data"} requests
* into a {@code MultiValueMap<String, Part>}.
*
* <p>Note that this reader depends on access to an
* {@code HttpMessageReader<Part>} for the actual parsing of multipart content.
* The purpose of this reader is to collect the parts into a map.
*
* @author Rossen Stoyanchev
* @since 5.0
*/
public class MultipartHttpMessageReader implements HttpMessageReader<MultiValueMap<String, Part>> {
private static final ResolvableType MULTIPART_VALUE_TYPE = ResolvableType.forClassWithGenerics(
MultiValueMap.class, String.class, Part.class);
private final HttpMessageReader<Part> partReader;
public MultipartHttpMessageReader(HttpMessageReader<Part> partReader) {
Assert.notNull(partReader, "'partReader' is required");
this.partReader = partReader;
}
@Override
public List<MediaType> getReadableMediaTypes() {
return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
}
@Override
public boolean canRead(ResolvableType elementType, MediaType mediaType) {
return MULTIPART_VALUE_TYPE.isAssignableFrom(elementType) &&
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
}
@Override
public Flux<MultiValueMap<String, Part>> read(ResolvableType elementType,
ReactiveHttpInputMessage message, Map<String, Object> hints) {
return Flux.from(readMono(elementType, message, hints));
}
@Override
public Mono<MultiValueMap<String, Part>> readMono(ResolvableType elementType,
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
return this.partReader.read(elementType, inputMessage, hints)
.collectMultimap(Part::getName).map(this::toMultiValueMap);
}
private LinkedMultiValueMap<String, Part> toMultiValueMap(Map<String, Collection<Part>> map) {
return new LinkedMultiValueMap<>(map.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> toList(e.getValue()))));
}
private List<Part> toList(Collection<Part> collection) {
return collection instanceof List ? (List<Part>) collection : new ArrayList<>(collection);
}
}

View File

@@ -25,15 +25,12 @@ import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.synchronoss.cloud.nio.multipart.Multipart;
import org.synchronoss.cloud.nio.multipart.MultipartContext;
@@ -55,25 +52,24 @@ import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
/**
* {@code HttpMessageReader} for {@code "multipart/form-data"} requests based
* on the Synchronoss NIO Multipart library.
* {@code HttpMessageReader} for parsing {@code "multipart/form-data"} requests
* to a stream of {@link Part}'s using the Synchronoss NIO Multipart library.
*
* <p>This reader can be provided to {@link MultipartHttpMessageReader} in order
* to aggregate all parts into a Map.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @author Arjen Poutsma
* @since 5.0
* @see <a href="https://github.com/synchronoss/nio-multipart">Synchronoss NIO Multipart</a>
* @see MultipartHttpMessageReader
*/
public class SynchronossMultipartHttpMessageReader implements HttpMessageReader<MultiValueMap<String, Part>> {
private static final ResolvableType MULTIPART_VALUE_TYPE = ResolvableType.forClassWithGenerics(
MultiValueMap.class, String.class, Part.class);
public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part> {
@Override
@@ -83,34 +79,25 @@ public class SynchronossMultipartHttpMessageReader implements HttpMessageReader<
@Override
public boolean canRead(ResolvableType elementType, MediaType mediaType) {
return MULTIPART_VALUE_TYPE.isAssignableFrom(elementType) &&
return Part.class.equals(elementType.resolve(Object.class)) &&
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
}
@Override
public Flux<MultiValueMap<String, Part>> read(ResolvableType elementType,
ReactiveHttpInputMessage message, Map<String, Object> hints) {
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
return Flux.from(readMono(elementType, message, hints));
return Flux.create(new SynchronossPartGenerator(message));
}
@Override
public Mono<MultiValueMap<String, Part>> readMono(ResolvableType elementType,
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
return Flux.create(new SynchronossPartGenerator(inputMessage))
.collectMultimap(Part::getName).map(this::toMultiValueMap);
}
private LinkedMultiValueMap<String, Part> toMultiValueMap(Map<String, Collection<Part>> map) {
return new LinkedMultiValueMap<>(map.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> toList(e.getValue()))));
}
private List<Part> toList(Collection<Part> collection) {
return collection instanceof List ? (List<Part>) collection : new ArrayList<>(collection);
return Mono.error(new UnsupportedOperationException(
"This reader does not support reading a single element."));
}