Remove ServerWebExchange#getRequestParams

Issue: SPR-15508
This commit is contained in:
Rossen Stoyanchev
2017-05-02 16:08:50 -04:00
parent 7f19e57d9d
commit 1881727b37
16 changed files with 115 additions and 314 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.web.bind.support;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -24,10 +23,10 @@ import java.util.TreeMap;
import reactor.core.publisher.Mono;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.http.codec.multipart.Part;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -39,6 +38,7 @@ import org.springframework.web.server.ServerWebExchange;
*/
public class WebExchangeDataBinder extends WebDataBinder {
/**
* Create a new instance, with default object name.
* @param target the target object to bind onto (or {@code null} if the
@@ -61,62 +61,52 @@ public class WebExchangeDataBinder extends WebDataBinder {
/**
* 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
* is {@code "application/x-www-form-urlencoded"}.
* Bind query params, form data, and or multipart form data to the binder target.
* @param exchange the current exchange.
* @return a {@code Mono<Void>} when binding is complete
*/
public Mono<Void> bind(ServerWebExchange exchange) {
return exchange.getRequestParams()
.map(this::getParamsToBind)
.doOnNext(values -> values.putAll(getMultipartFiles(exchange)))
.doOnNext(values -> values.putAll(getExtraValuesToBind(exchange)))
.flatMap(values -> {
doBind(new MutablePropertyValues(values));
return Mono.empty();
return getValuesToBind(exchange)
.doOnNext(values -> doBind(new MutablePropertyValues(values)))
.then();
}
/**
* Protected method to obtain the values for data binding. By default this
* method delegates to {@link #extractValuesToBind(ServerWebExchange)}.
*/
protected Mono<Map<String, Object>> getValuesToBind(ServerWebExchange exchange) {
return extractValuesToBind(exchange);
}
/**
* Combine query params and form data for multipart form data from the body
* of the request into a {@code Map<String, Object>} of values to use for
* data binding purposes.
*
* @param exchange the current exchange
* @return a {@code Mono} with the values to bind
*/
public static Mono<Map<String, Object>> extractValuesToBind(ServerWebExchange exchange) {
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
Mono<MultiValueMap<String, String>> formData = exchange.getFormData();
Mono<MultiValueMap<String, Part>> multipartData = exchange.getMultipartData();
return Mono.when(Mono.just(queryParams), formData, multipartData)
.map(tuple -> {
Map<String, Object> result = new TreeMap<>();
tuple.getT1().forEach((key, values) -> addBindValue(result, key, values));
tuple.getT2().forEach((key, values) -> addBindValue(result, key, values));
tuple.getT3().forEach((key, values) -> addBindValue(result, key, values));
return result;
});
}
private Map<String, Object> getParamsToBind(MultiValueMap<String, String> params) {
Map<String, Object> result = new TreeMap<>();
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
String name = entry.getKey();
List<String> values = entry.getValue();
if (CollectionUtils.isEmpty(values)) {
// Do nothing, no values found at all.
}
else if (values.size() == 1) {
result.put(name, values.get(0));
}
else {
result.put(name, values);
}
private static void addBindValue(Map<String, Object> params, String key, List<?> values) {
if (!CollectionUtils.isEmpty(values)) {
params.put(key, values.size() == 1 ? values.get(0) : values);
}
return result;
}
/**
* Bind all multipart files contained in the given request, if any (in case
* of a multipart request).
* <p>Multipart files will only be added to the property values if they
* are not empty or if we're configured to bind empty multipart files too.
* @param exchange the current exchange
* @return Map of field name String to MultipartFile object
*/
protected Map<String, List<MultipartFile>> getMultipartFiles(ServerWebExchange exchange) {
// TODO
return Collections.emptyMap();
}
/**
* Extension point that subclasses can use to add extra bind values for a
* request. Invoked before {@link #doBind(MutablePropertyValues)}.
* <p>The default implementation is empty.
* @param exchange the current exchange
*/
protected Map<String, ?> getExtraValuesToBind(ServerWebExchange exchange) {
return Collections.emptyMap();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* 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.
@@ -80,24 +80,23 @@ public interface ServerWebExchange {
/**
* 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 otherwise.
* This method may be called multiple times.
*
* <p><strong>Note:</strong> calling this method causes the request body to
* be read and parsed in full and the resulting {@code MultiValueMap} is
* cached so that this method is safe to call more than once.
*/
Mono<MultiValueMap<String, String>> getFormData();
/**
* Return the parts of a multipart request if the Content-Type is
* {@code "multipart/form-data"} or an empty map otherwise.
* This method may be called multiple times.
*
* <p><strong>Note:</strong> calling this method causes the request body to
* be read and parsed in full and the resulting {@code MultiValueMap} is
* cached so that this method is safe to call more than once.
*/
Mono<MultiValueMap<String, Part>> getMultipartData();
/**
* 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.

View File

@@ -99,11 +99,6 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
return getDelegate().getMultipartData();
}
@Override
public Mono<MultiValueMap<String, String>> getRequestParams() {
return getDelegate().getRequestParams();
}
@Override
public boolean isNotModified() {
return getDelegate().isNotModified();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* 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.
@@ -88,8 +88,6 @@ public class DefaultServerWebExchange implements ServerWebExchange {
private final Mono<MultiValueMap<String, Part>> multipartDataMono;
private final Mono<MultiValueMap<String, String>> requestParamsMono;
private volatile boolean notModified;
@@ -109,8 +107,6 @@ public class DefaultServerWebExchange implements ServerWebExchange {
this.sessionMono = sessionManager.getSession(this).cache();
this.formDataMono = initFormData(request, codecConfigurer);
this.multipartDataMono = initMultipartData(request, codecConfigurer);
this.requestParamsMono = initRequestParams(request, this.formDataMono);
}
@SuppressWarnings("unchecked")
@@ -165,20 +161,6 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return EMPTY_MULTIPART_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();
}
@Override
public ServerHttpRequest getRequest() {
@@ -228,11 +210,6 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return this.multipartDataMono;
}
@Override
public Mono<MultiValueMap<String, String>> getRequestParams() {
return this.requestParamsMono;
}
@Override
public boolean isNotModified() {
return this.notModified;