Fine-tuned assertions and related polishing

This commit is contained in:
Juergen Hoeller 2018-05-05 14:59:13 +02:00
parent 8848ec73ab
commit 41ab177b6c
14 changed files with 66 additions and 108 deletions

View File

@ -414,7 +414,6 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Private constructor that can create read-only {@code HttpHeader} instances.
*/
private HttpHeaders(Map<String, List<String>> headers, boolean readOnly) {
Assert.notNull(headers, "'headers' must not be null");
if (readOnly) {
Map<String, List<String>> map = new LinkedCaseInsensitiveMap<>(headers.size(), Locale.ENGLISH);
headers.forEach((key, valueList) -> map.put(key, Collections.unmodifiableList(valueList)));
@ -1556,6 +1555,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Return a {@code HttpHeaders} object that can only be read, not written to.
*/
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
Assert.notNull(headers, "HttpHeaders must not be null");
return (headers.readOnly ? headers : new HttpHeaders(headers, true));
}

View File

@ -114,7 +114,7 @@ public interface ClientRequest {
/**
* Create a builder with the given method and url.
* @param method the HTTP method (GET, POST, etc)
* @param url the URL
* @param url the url (as a URI instance)
* @return the created builder
* @deprecated in favor of {@link #create(HttpMethod, URI)}
*/
@ -126,7 +126,7 @@ public interface ClientRequest {
/**
* Create a request builder with the given method and url.
* @param method the HTTP method (GET, POST, etc)
* @param url the URL
* @param url the url (as a URI instance)
* @return the created builder
*/
static Builder create(HttpMethod method, URI url) {

View File

@ -49,28 +49,30 @@ import org.springframework.web.reactive.function.BodyInserters;
*/
final class DefaultClientRequestBuilder implements ClientRequest.Builder {
private HttpMethod method;
private URI url;
private final HttpHeaders headers = new HttpHeaders();
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
private final Map<String, Object> attributes = new LinkedHashMap<>();
private HttpMethod method;
private URI url;
private BodyInserter<?, ? super ClientHttpRequest> inserter = BodyInserters.empty();
private BodyInserter<?, ? super ClientHttpRequest> body = BodyInserters.empty();
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
method(method);
url(url);
Assert.notNull(method, "HttpMethod must not be null");
Assert.notNull(url, "URI must not be null");
this.method = method;
this.url = url;
}
public DefaultClientRequestBuilder(ClientRequest other) {
Assert.notNull(other, "ClientRequest must not be null");
method(other.method());
url(other.url());
this.method = other.method();
this.url = other.url();
headers(headers -> headers.addAll(other.headers()));
cookies(cookies -> cookies.addAll(other.cookies()));
attributes(attributes -> attributes.putAll(other.attributes()));
@ -125,7 +127,7 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(elementClass, "'elementClass' must not be null");
this.inserter = BodyInserters.fromPublisher(publisher, elementClass);
this.body = BodyInserters.fromPublisher(publisher, elementClass);
return this;
}
@ -136,7 +138,7 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(typeReference, "'typeReference' must not be null");
this.inserter = BodyInserters.fromPublisher(publisher, typeReference);
this.body = BodyInserters.fromPublisher(publisher, typeReference);
return this;
}
@ -154,14 +156,13 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
@Override
public ClientRequest.Builder body(BodyInserter<?, ? super ClientHttpRequest> inserter) {
this.inserter = inserter;
this.body = inserter;
return this;
}
@Override
public ClientRequest build() {
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies,
this.inserter, this.attributes);
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies, this.body, this.attributes);
}
@ -175,20 +176,19 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
private final MultiValueMap<String, String> cookies;
private final BodyInserter<?, ? super ClientHttpRequest> inserter;
private final BodyInserter<?, ? super ClientHttpRequest> body;
private final Map<String, Object> attributes;
public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
MultiValueMap<String, String> cookies,
BodyInserter<?, ? super ClientHttpRequest> inserter,
MultiValueMap<String, String> cookies, BodyInserter<?, ? super ClientHttpRequest> body,
Map<String, Object> attributes) {
this.method = method;
this.url = url;
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
this.cookies = CollectionUtils.unmodifiableMultiValueMap(cookies);
this.inserter = inserter;
this.body = body;
this.attributes = Collections.unmodifiableMap(attributes);
}
@ -214,7 +214,7 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
@Override
public BodyInserter<?, ? super ClientHttpRequest> body() {
return this.inserter;
return this.body;
}
@Override
@ -240,17 +240,15 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
}));
}
return this.inserter.insert(request, new BodyInserter.Context() {
return this.body.insert(request, new BodyInserter.Context() {
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return strategies.messageWriters();
}
@Override
public Optional<ServerHttpRequest> serverRequest() {
return Optional.empty();
}
@Override
public Map<String, Object> hints() {
return Collections.emptyMap();

View File

@ -42,16 +42,16 @@ import org.springframework.util.MultiValueMap;
*/
final class DefaultClientResponseBuilder implements ClientResponse.Builder {
private ExchangeStrategies strategies;
private HttpStatus statusCode = HttpStatus.OK;
private final HttpHeaders headers = new HttpHeaders();
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
private HttpStatus statusCode = HttpStatus.OK;
private Flux<DataBuffer> body = Flux.empty();
private ExchangeStrategies strategies;
public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
Assert.notNull(strategies, "ExchangeStrategies must not be null");
@ -84,7 +84,6 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
@Override
public ClientResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
Assert.notNull(headersConsumer, "Consumer must not be null");
headersConsumer.accept(this.headers);
return this;
}
@ -99,7 +98,6 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
@Override
public ClientResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
Assert.notNull(cookiesConsumer, "Consumer must not be null");
cookiesConsumer.accept(this.cookies);
return this;
}

View File

@ -63,13 +63,14 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
@Nullable
private ClientHttpConnector connector;
private ExchangeStrategies exchangeStrategies = ExchangeStrategies.withDefaults();
@Nullable
private ExchangeFunction exchangeFunction;
private ExchangeStrategies exchangeStrategies;
public DefaultWebClientBuilder() {
this.exchangeStrategies = ExchangeStrategies.withDefaults();
}
public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
@ -90,8 +91,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
new LinkedMultiValueMap<>(other.defaultCookies) : null);
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
this.connector = other.connector;
this.exchangeStrategies = other.exchangeStrategies;
this.exchangeFunction = other.exchangeFunction;
this.exchangeStrategies = other.exchangeStrategies;
}
@ -181,15 +182,15 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
}
@Override
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
Assert.notNull(strategies, "ExchangeStrategies must not be null");
this.exchangeStrategies = strategies;
public WebClient.Builder exchangeFunction(ExchangeFunction exchangeFunction) {
this.exchangeFunction = exchangeFunction;
return this;
}
@Override
public WebClient.Builder exchangeFunction(ExchangeFunction exchangeFunction) {
this.exchangeFunction = exchangeFunction;
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
Assert.notNull(strategies, "ExchangeStrategies must not be null");
this.exchangeStrategies = strategies;
return this;
}
@ -207,9 +208,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
private static @Nullable HttpHeaders unmodifiableCopy(@Nullable HttpHeaders original) {
if (original != null) {
HttpHeaders copy = new HttpHeaders();
copy.putAll(original);
return HttpHeaders.readOnlyHttpHeaders(copy);
return HttpHeaders.readOnlyHttpHeaders(original);
}
else {
return null;
@ -243,7 +242,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
else if (this.connector != null) {
return ExchangeFunctions.create(this.connector, this.exchangeStrategies);
}
else {
return ExchangeFunctions.create(new ReactorClientHttpConnector(), this.exchangeStrategies);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -35,8 +35,6 @@ import org.springframework.http.codec.HttpMessageWriter;
*/
public interface ExchangeStrategies {
// Instance methods
/**
* Return the {@link HttpMessageReader}s to be used for request body conversion.
* @return the stream of message readers
@ -60,8 +58,6 @@ public interface ExchangeStrategies {
return builder().build();
}
// Builder methods
/**
* Return a mutable builder for a {@code ExchangeStrategies} with default initialization.
* @return the builder

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -285,15 +285,6 @@ public interface WebClient {
*/
Builder filters(Consumer<List<ExchangeFilterFunction>> filtersConsumer);
/**
* Configure the {@link ExchangeStrategies} to use.
* <p>By default {@link ExchangeStrategies#withDefaults()} is used.
* @param strategies the strategies to use
* @see #clientConnector(ClientHttpConnector)
* @see #exchangeFunction(ExchangeFunction)
*/
Builder exchangeStrategies(ExchangeStrategies strategies);
/**
* Provide a pre-configured {@link ExchangeFunction} instance. This is
* an alternative to and effectively overrides the following:
@ -307,6 +298,15 @@ public interface WebClient {
*/
Builder exchangeFunction(ExchangeFunction exchangeFunction);
/**
* Configure the {@link ExchangeStrategies} to use.
* <p>By default {@link ExchangeStrategies#withDefaults()} is used.
* @param strategies the strategies to use
* @see #clientConnector(ClientHttpConnector)
* @see #exchangeFunction(ExchangeFunction)
*/
Builder exchangeStrategies(ExchangeStrategies strategies);
/**
* Clone this {@code WebClient.Builder}
*/

View File

@ -96,7 +96,6 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
@Override
public EntityResponse.Builder<T> cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
Assert.notNull(cookiesConsumer, "Consumer must not be null");
cookiesConsumer.accept(this.cookies);
return this;
}

View File

@ -99,7 +99,6 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
@Override
public RenderingResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
Assert.notNull(cookiesConsumer, "Consumer must not be null");
cookiesConsumer.accept(this.cookies);
return this;
}

View File

@ -76,14 +76,10 @@ class DefaultServerRequest implements ServerRequest {
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
this.exchange = exchange;
this.messageReaders = unmodifiableCopy(messageReaders);
this.messageReaders = Collections.unmodifiableList(new ArrayList<>(messageReaders));
this.headers = new DefaultHeaders();
}
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
return Collections.unmodifiableList(new ArrayList<>(list));
}
@Override
public String methodName() {

View File

@ -97,7 +97,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
public ServerResponse.BodyBuilder headers(Consumer<HttpHeaders> headersConsumer) {
Assert.notNull(headersConsumer, "Consumer must not be null");
headersConsumer.accept(this.headers);
return this;
}
@ -111,7 +110,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
public ServerResponse.BodyBuilder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
Assert.notNull(cookiesConsumer, "Consumer must not be null");
cookiesConsumer.accept(this.cookies);
return this;
}
@ -201,9 +199,8 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
public Mono<ServerResponse> build(
BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>> writeFunction) {
Assert.notNull(writeFunction, "BiFunction must not be null");
return Mono.just(
new WriterFunctionServerResponse(this.statusCode, this.headers, this.cookies, writeFunction));
new WriterFunctionResponse(this.statusCode, this.headers, this.cookies, writeFunction));
}
@Override
@ -250,15 +247,12 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
public Mono<ServerResponse> body(BodyInserter<?, ? super ServerHttpResponse> inserter) {
Assert.notNull(inserter, "BodyInserter must not be null");
return Mono.just(
new BodyInserterServerResponse<>(this.statusCode, this.headers, this.cookies, inserter, this.hints));
new BodyInserterResponse<>(this.statusCode, this.headers, this.cookies, inserter, this.hints));
}
@Override
public Mono<ServerResponse> render(String name, Object... modelAttributes) {
Assert.hasLength(name, "Name must not be empty");
return new DefaultRenderingResponseBuilder(name)
.headers(this.headers)
.status(this.statusCode)
@ -269,8 +263,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
public Mono<ServerResponse> render(String name, Map<String, ?> model) {
Assert.hasLength(name, "Name must not be empty");
return new DefaultRenderingResponseBuilder(name)
.headers(this.headers)
.status(this.statusCode)
@ -290,24 +282,12 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
private final MultiValueMap<String, ResponseCookie> cookies;
protected AbstractServerResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies) {
protected AbstractServerResponse(
int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies) {
this.statusCode = statusCode;
this.headers = readOnlyCopy(headers);
this.cookies = readOnlyCopy(cookies);
}
private static HttpHeaders readOnlyCopy(HttpHeaders headers) {
HttpHeaders copy = new HttpHeaders();
copy.putAll(headers);
return HttpHeaders.readOnlyHttpHeaders(copy);
}
private static <K, V> MultiValueMap<K,V> readOnlyCopy(MultiValueMap<K, V> map) {
MultiValueMap<K, V> copy = new LinkedMultiValueMap<>();
copy.putAll(map);
return CollectionUtils.unmodifiableMultiValueMap(copy);
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(cookies));
}
@Override
@ -367,15 +347,16 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
}
private static final class WriterFunctionServerResponse extends AbstractServerResponse {
private static final class WriterFunctionResponse extends AbstractServerResponse {
private final BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction;
public WriterFunctionServerResponse(int statusCode, HttpHeaders headers,
public WriterFunctionResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies,
BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) {
super(statusCode, headers, cookies);
Assert.notNull(writeFunction, "BiFunction must not be null");
this.writeFunction = writeFunction;
}
@ -386,18 +367,19 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
}
private static final class BodyInserterServerResponse<T> extends AbstractServerResponse {
private static final class BodyInserterResponse<T> extends AbstractServerResponse {
private final BodyInserter<T, ? super ServerHttpResponse> inserter;
private final Map<String, Object> hints;
public BodyInserterServerResponse(int statusCode, HttpHeaders headers,
public BodyInserterResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies,
BodyInserter<T, ? super ServerHttpResponse> inserter, Map<String, Object> hints) {
BodyInserter<T, ? super ServerHttpResponse> body, Map<String, Object> hints) {
super(statusCode, headers, cookies);
this.inserter = inserter;
Assert.notNull(body, "BodyInserter must not be null");
this.inserter = body;
this.hints = hints;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -41,8 +41,6 @@ import org.springframework.web.server.i18n.LocaleContextResolver;
*/
public interface HandlerStrategies {
// Instance methods
/**
* Return the {@link HttpMessageReader}s to be used for request body conversion.
* @return the message readers
@ -90,8 +88,6 @@ public interface HandlerStrategies {
return builder().build();
}
// Builder methods
/**
* Return a mutable builder for a {@code HandlerStrategies} with default initialization.
* @return the builder

View File

@ -248,7 +248,6 @@ public interface ServerRequest {
/**
* 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.
*
* <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.
@ -258,7 +257,6 @@ public interface ServerRequest {
/**
* Return the parts of a multipart request if the Content-Type is
* {@code "multipart/form-data"} or an empty map otherwise.
*
* <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.
@ -266,6 +264,7 @@ public interface ServerRequest {
Mono<MultiValueMap<String, Part>> multipartData();
// Static methods
/**
* Create a new {@code ServerRequest} based on the given {@code ServerWebExchange} and

View File

@ -56,8 +56,6 @@ import org.springframework.web.server.ServerWebExchange;
*/
public interface ServerResponse {
// Instance methods
/**
* Return the status code of this response.
*/
@ -82,7 +80,7 @@ public interface ServerResponse {
Mono<Void> writeTo(ServerWebExchange exchange, Context context);
// Static builder methods
// Static methods
/**
* Create a builder with the status code and headers of the given response.
@ -190,7 +188,6 @@ public interface ServerResponse {
/**
* Create a builder with a {@linkplain HttpStatus#NOT_FOUND 404 Not Found} status.
*
* @return the created builder
*/
static HeadersBuilder<?> notFound() {