Support for "request parameters"
ServerWebExchange now provides access to "requestParams" as a MulitValueMap with query parameters and form data combined. The combined map is then used for the params condition of @RequestMapping purposes () and also for @RequestParam arguments. Issue: SPR-15000
This commit is contained in:
@@ -76,11 +76,18 @@ public interface ServerWebExchange {
|
||||
<T extends Principal> Mono<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".
|
||||
* Return the form data from the body of the request if the Content-Type is
|
||||
* {@code "application/x-www-form-urlencoded"} or an empty map.
|
||||
*/
|
||||
Mono<MultiValueMap<String, String>> getFormData();
|
||||
|
||||
/**
|
||||
* Return a combined map that represents both
|
||||
* {@link ServerHttpRequest#getQueryParams()} and {@link #getFormData()}
|
||||
* or an empty map.
|
||||
*/
|
||||
Mono<MultiValueMap<String, String>> getRequestParams();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the one of the {@code checkNotModified} methods
|
||||
* in this contract were used and they returned true.
|
||||
|
||||
@@ -93,6 +93,11 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
|
||||
return getDelegate().getFormData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MultiValueMap<String, String>> getRequestParams() {
|
||||
return getDelegate().getRequestParams();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNotModified() {
|
||||
return getDelegate().isNotModified();
|
||||
|
||||
@@ -38,6 +38,8 @@ 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.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -56,9 +58,13 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
|
||||
private static final FormHttpMessageReader FORM_READER = new FormHttpMessageReader();
|
||||
|
||||
private static final ResolvableType MULTIVALUE_TYPE =
|
||||
private static final ResolvableType FORM_DATA_VALUE_TYPE =
|
||||
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class);
|
||||
|
||||
private static final Mono<MultiValueMap<String, String>> EMPTY_FORM_DATA =
|
||||
Mono.just(CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<String, String>(0)))
|
||||
.cache();
|
||||
|
||||
|
||||
private final ServerHttpRequest request;
|
||||
|
||||
@@ -70,6 +76,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
|
||||
private final Mono<MultiValueMap<String, String>> formDataMono;
|
||||
|
||||
private final Mono<MultiValueMap<String, String>> requestParamsMono;
|
||||
|
||||
private volatile boolean notModified;
|
||||
|
||||
|
||||
@@ -80,10 +88,12 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
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);
|
||||
this.requestParamsMono = initRequestParams(request, this.formDataMono);
|
||||
}
|
||||
|
||||
private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request) {
|
||||
@@ -91,13 +101,28 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
try {
|
||||
contentType = request.getHeaders().getContentType();
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
|
||||
return FORM_READER.readMono(MULTIVALUE_TYPE, request, Collections.emptyMap()).cache();
|
||||
Map<String, Object> hints = Collections.emptyMap();
|
||||
return FORM_READER.readMono(FORM_DATA_VALUE_TYPE, request, hints).cache();
|
||||
}
|
||||
}
|
||||
catch (InvalidMediaTypeException ex) {
|
||||
// Ignore
|
||||
}
|
||||
return Mono.empty();
|
||||
return EMPTY_FORM_DATA;
|
||||
}
|
||||
|
||||
private static Mono<MultiValueMap<String, String>> initRequestParams(
|
||||
ServerHttpRequest request, Mono<MultiValueMap<String, String>> formDataMono) {
|
||||
|
||||
return formDataMono
|
||||
.map(formData -> {
|
||||
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
|
||||
result.putAll(request.getQueryParams());
|
||||
result.putAll(formData);
|
||||
return CollectionUtils.unmodifiableMultiValueMap(result);
|
||||
})
|
||||
.defaultIfEmpty(request.getQueryParams())
|
||||
.cache();
|
||||
}
|
||||
|
||||
|
||||
@@ -144,6 +169,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
return this.formDataMono;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MultiValueMap<String, String>> getRequestParams() {
|
||||
return this.requestParamsMono;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNotModified() {
|
||||
return this.notModified;
|
||||
|
||||
Reference in New Issue
Block a user