Add multipart support to ServerWebExchange

Issue: SPR-14546
This commit is contained in:
Sebastien Deleuze
2017-04-28 12:03:55 +02:00
parent 8e272bc5b0
commit 4bfd04b3c5
4 changed files with 172 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ import java.util.function.Consumer;
import reactor.core.publisher.Mono;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.MultiValueMap;
@@ -82,6 +83,12 @@ public interface ServerWebExchange {
*/
Mono<MultiValueMap<String, String>> getFormData();
/**
* Return the form parts from the body of the request or an empty {@code Mono}
* if the Content-Type is not "multipart/form-data".
*/
Mono<MultiValueMap<String, Part>> getMultipartData();
/**
* Return a combined map that represents both
* {@link ServerHttpRequest#getQueryParams()} and {@link #getFormData()}

View File

@@ -22,6 +22,7 @@ import java.util.Optional;
import reactor.core.publisher.Mono;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert;
@@ -93,6 +94,11 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
return getDelegate().getFormData();
}
@Override
public Mono<MultiValueMap<String, Part>> getMultipartData() {
return getDelegate().getMultipartData();
}
@Override
public Mono<MultiValueMap<String, String>> getRequestParams() {
return getDelegate().getRequestParams();

View File

@@ -36,6 +36,7 @@ import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert;
@@ -48,6 +49,7 @@ import org.springframework.web.server.WebSession;
import org.springframework.web.server.session.WebSessionManager;
import static org.springframework.http.MediaType.*;
import static org.springframework.http.codec.multipart.MultipartHttpMessageReader.*;
/**
* Default implementation of {@link ServerWebExchange}.
@@ -66,6 +68,10 @@ public class DefaultServerWebExchange implements ServerWebExchange {
Mono.just(CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<String, String>(0)))
.cache();
private static final Mono<MultiValueMap<String, Part>> EMPTY_MULTIPART_DATA =
Mono.just(CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<String, Part>(0)))
.cache();
private final ServerHttpRequest request;
@@ -77,6 +83,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
private final Mono<MultiValueMap<String, String>> formDataMono;
private final Mono<MultiValueMap<String, Part>> multipartDataMono;
private final Mono<MultiValueMap<String, String>> requestParamsMono;
private volatile boolean notModified;
@@ -97,6 +105,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
this.response = response;
this.sessionMono = sessionManager.getSession(this).cache();
this.formDataMono = initFormData(request, codecConfigurer);
this.multipartDataMono = initMultipartData(request, codecConfigurer);
this.requestParamsMono = initRequestParams(request, this.formDataMono);
}
@@ -126,6 +135,31 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return EMPTY_FORM_DATA;
}
@SuppressWarnings("unchecked")
private static Mono<MultiValueMap<String, Part>> initMultipartData(
ServerHttpRequest request, ServerCodecConfigurer codecConfigurer) {
MediaType contentType;
try {
contentType = request.getHeaders().getContentType();
if (MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
return ((HttpMessageReader<MultiValueMap<String, Part>>)codecConfigurer
.getReaders()
.stream()
.filter(messageReader -> messageReader.canRead(MULTIPART_VALUE_TYPE, MULTIPART_FORM_DATA))
.findFirst()
.orElseThrow(() -> new IllegalStateException("Could not find HttpMessageReader that supports " + MULTIPART_FORM_DATA)))
.readMono(FORM_DATA_VALUE_TYPE, request, Collections.emptyMap())
.switchIfEmpty(EMPTY_MULTIPART_DATA)
.cache();
}
}
catch (InvalidMediaTypeException ex) {
// Ignore
}
return EMPTY_MULTIPART_DATA;
}
private static Mono<MultiValueMap<String, String>> initRequestParams(
ServerHttpRequest request, Mono<MultiValueMap<String, String>> formDataMono) {
@@ -184,6 +218,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return this.formDataMono;
}
@Override
public Mono<MultiValueMap<String, Part>> getMultipartData() {
return this.multipartDataMono;
}
@Override
public Mono<MultiValueMap<String, String>> getRequestParams() {
return this.requestParamsMono;