ServerWebExchange provides access to form data
The ServerWebExchange now has a getFormData() method that delegates to FormHttpMessageReader for the parsing and then caches the result so it may be used multiples times during request processing. Issue: SPR-14541
This commit is contained in:
@@ -24,9 +24,6 @@ import java.util.TreeMap;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -42,11 +39,6 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
*/
|
||||
public class WebExchangeDataBinder extends WebDataBinder {
|
||||
|
||||
private static final ResolvableType MULTIVALUE_MAP_TYPE = ResolvableType.forClass(MultiValueMap.class);
|
||||
|
||||
|
||||
private HttpMessageReader<MultiValueMap<String, String>> formReader = null;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance, with default object name.
|
||||
@@ -69,15 +61,6 @@ public class WebExchangeDataBinder extends WebDataBinder {
|
||||
}
|
||||
|
||||
|
||||
public void setFormReader(HttpMessageReader<MultiValueMap<String, String>> formReader) {
|
||||
this.formReader = formReader;
|
||||
}
|
||||
|
||||
public HttpMessageReader<MultiValueMap<String, String>> getFormReader() {
|
||||
return this.formReader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Bind the URL query parameters or form data of the body of the given request
|
||||
* to this binder's target. The request body is parsed if the content-type
|
||||
@@ -90,7 +73,8 @@ public class WebExchangeDataBinder extends WebDataBinder {
|
||||
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
Mono<MultiValueMap<String, String>> queryParams = Mono.just(request.getQueryParams());
|
||||
Mono<MultiValueMap<String, String>> formParams = getFormParams(exchange);
|
||||
Mono<MultiValueMap<String, String>> formParams =
|
||||
exchange.getFormData().defaultIfEmpty(new LinkedMultiValueMap<>());
|
||||
|
||||
return Mono.zip(this::mergeParams, queryParams, formParams)
|
||||
.map(this::getParamsToBind)
|
||||
@@ -102,17 +86,6 @@ public class WebExchangeDataBinder extends WebDataBinder {
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<MultiValueMap<String, String>> getFormParams(ServerWebExchange exchange) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
MediaType contentType = request.getHeaders().getContentType();
|
||||
if (this.formReader.canRead(MULTIVALUE_MAP_TYPE, contentType)) {
|
||||
return this.formReader.readMono(MULTIVALUE_MAP_TYPE, request, Collections.emptyMap());
|
||||
}
|
||||
else {
|
||||
return Mono.just(new LinkedMultiValueMap<>());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private MultiValueMap<String, String> mergeParams(Object[] paramMaps) {
|
||||
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
|
||||
|
||||
@@ -23,6 +23,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Default implementation of
|
||||
@@ -43,6 +44,8 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
|
||||
private Mono<WebSession> session;
|
||||
|
||||
private Mono<MultiValueMap<String, String>> formData;
|
||||
|
||||
|
||||
public DefaultServerWebExchangeMutativeBuilder(ServerWebExchange delegate) {
|
||||
Assert.notNull(delegate, "'delegate' is required.");
|
||||
@@ -74,10 +77,16 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerWebExchange.MutativeBuilder setFormData(Mono<MultiValueMap<String, String>> formData) {
|
||||
this.formData = formData;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerWebExchange build() {
|
||||
return new MutativeDecorator(this.delegate,
|
||||
this.request, this.response, this.user, this.session);
|
||||
return new MutativeDecorator(this.delegate, this.request, this.response,
|
||||
this.user, this.session, this.formData);
|
||||
}
|
||||
|
||||
|
||||
@@ -95,16 +104,19 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
|
||||
private final Mono<WebSession> session;
|
||||
|
||||
private final Mono<MultiValueMap<String, String>> formData;
|
||||
|
||||
|
||||
public MutativeDecorator(ServerWebExchange delegate,
|
||||
ServerHttpRequest request, ServerHttpResponse response, Principal user,
|
||||
Mono<WebSession> session) {
|
||||
Mono<WebSession> session, Mono<MultiValueMap<String, String>> formData) {
|
||||
|
||||
super(delegate);
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.user = user;
|
||||
this.session = session;
|
||||
this.formData = formData;
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +140,11 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
public <T extends Principal> Optional<T> getPrincipal() {
|
||||
return (this.user != null ? Optional.of((T) this.user) : getDelegate().getPrincipal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MultiValueMap<String, String>> getFormData() {
|
||||
return (this.formData != null ? this.formData : getDelegate().getFormData());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Contract for an HTTP request-response interaction. Provides access to the HTTP
|
||||
@@ -74,6 +75,12 @@ public interface ServerWebExchange {
|
||||
*/
|
||||
<T extends Principal> Optional<T> getPrincipal();
|
||||
|
||||
/**
|
||||
* Return the form data from the body of the request or an empty {@code Mono}
|
||||
* if the Content-Type is not "application/x-www-form-urlencoded".
|
||||
*/
|
||||
Mono<MultiValueMap<String, String>> getFormData();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the one of the {@code checkNotModified} methods
|
||||
* in this contract were used and they returned true.
|
||||
@@ -155,6 +162,11 @@ public interface ServerWebExchange {
|
||||
*/
|
||||
MutativeBuilder setSession(Mono<WebSession> session);
|
||||
|
||||
/**
|
||||
* Set the form data.
|
||||
*/
|
||||
MutativeBuilder setFormData(Mono<MultiValueMap<String, String>> formData);
|
||||
|
||||
/**
|
||||
* Build an immutable wrapper that returning the mutated properties.
|
||||
*/
|
||||
|
||||
@@ -25,6 +25,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* A convenient base class for classes that need to wrap another
|
||||
@@ -59,52 +60,57 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
|
||||
|
||||
@Override
|
||||
public ServerHttpRequest getRequest() {
|
||||
return this.getDelegate().getRequest();
|
||||
return getDelegate().getRequest();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerHttpResponse getResponse() {
|
||||
return this.getDelegate().getResponse();
|
||||
return getDelegate().getResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAttributes() {
|
||||
return this.getDelegate().getAttributes();
|
||||
return getDelegate().getAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Optional<T> getAttribute(String name) {
|
||||
return this.getDelegate().getAttribute(name);
|
||||
return getDelegate().getAttribute(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<WebSession> getSession() {
|
||||
return this.getDelegate().getSession();
|
||||
return getDelegate().getSession();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Principal> Optional<T> getPrincipal() {
|
||||
return this.getDelegate().getPrincipal();
|
||||
return getDelegate().getPrincipal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MultiValueMap<String, String>> getFormData() {
|
||||
return getDelegate().getFormData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNotModified() {
|
||||
return this.getDelegate().isNotModified();
|
||||
return getDelegate().isNotModified();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNotModified(Instant lastModified) {
|
||||
return this.getDelegate().checkNotModified(lastModified);
|
||||
return getDelegate().checkNotModified(lastModified);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNotModified(String etag) {
|
||||
return this.getDelegate().checkNotModified(etag);
|
||||
return getDelegate().checkNotModified(etag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNotModified(String etag, Instant lastModified) {
|
||||
return this.getDelegate().checkNotModified(etag, lastModified);
|
||||
return getDelegate().checkNotModified(etag, lastModified);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.security.Principal;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -27,12 +28,17 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.FormHttpMessageReader;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebSession;
|
||||
@@ -48,6 +54,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
|
||||
private static final List<HttpMethod> SAFE_METHODS = Arrays.asList(HttpMethod.GET, HttpMethod.HEAD);
|
||||
|
||||
private static final FormHttpMessageReader FORM_READER = new FormHttpMessageReader();
|
||||
|
||||
private static final ResolvableType MULTIVALUE_TYPE =
|
||||
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class);
|
||||
|
||||
|
||||
private final ServerHttpRequest request;
|
||||
|
||||
@@ -57,6 +68,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
|
||||
private final Mono<WebSession> sessionMono;
|
||||
|
||||
private final Mono<MultiValueMap<String, String>> formDataMono;
|
||||
|
||||
private volatile boolean notModified;
|
||||
|
||||
|
||||
@@ -66,9 +79,25 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
Assert.notNull(request, "'request' is required");
|
||||
Assert.notNull(response, "'response' is required");
|
||||
Assert.notNull(response, "'sessionManager' is required");
|
||||
Assert.notNull(response, "'formReader' is required");
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.sessionMono = sessionManager.getSession(this).cache();
|
||||
this.formDataMono = initFormData(request);
|
||||
}
|
||||
|
||||
private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request) {
|
||||
MediaType contentType;
|
||||
try {
|
||||
contentType = request.getHeaders().getContentType();
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
|
||||
return FORM_READER.readMono(MULTIVALUE_TYPE, request, Collections.emptyMap()).cache();
|
||||
}
|
||||
}
|
||||
catch (InvalidMediaTypeException ex) {
|
||||
// Ignore
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -110,6 +139,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MultiValueMap<String, String>> getFormData() {
|
||||
return this.formDataMono;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNotModified() {
|
||||
return this.notModified;
|
||||
|
||||
Reference in New Issue
Block a user