Fine-tuned assertions and related polishing
This commit is contained in:
parent
8848ec73ab
commit
41ab177b6c
|
@ -414,7 +414,6 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||||
* Private constructor that can create read-only {@code HttpHeader} instances.
|
* Private constructor that can create read-only {@code HttpHeader} instances.
|
||||||
*/
|
*/
|
||||||
private HttpHeaders(Map<String, List<String>> headers, boolean readOnly) {
|
private HttpHeaders(Map<String, List<String>> headers, boolean readOnly) {
|
||||||
Assert.notNull(headers, "'headers' must not be null");
|
|
||||||
if (readOnly) {
|
if (readOnly) {
|
||||||
Map<String, List<String>> map = new LinkedCaseInsensitiveMap<>(headers.size(), Locale.ENGLISH);
|
Map<String, List<String>> map = new LinkedCaseInsensitiveMap<>(headers.size(), Locale.ENGLISH);
|
||||||
headers.forEach((key, valueList) -> map.put(key, Collections.unmodifiableList(valueList)));
|
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.
|
* Return a {@code HttpHeaders} object that can only be read, not written to.
|
||||||
*/
|
*/
|
||||||
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
|
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
|
||||||
|
Assert.notNull(headers, "HttpHeaders must not be null");
|
||||||
return (headers.readOnly ? headers : new HttpHeaders(headers, true));
|
return (headers.readOnly ? headers : new HttpHeaders(headers, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -114,7 +114,7 @@ public interface ClientRequest {
|
||||||
/**
|
/**
|
||||||
* Create a builder with the given method and url.
|
* Create a builder with the given method and url.
|
||||||
* @param method the HTTP method (GET, POST, etc)
|
* @param method the HTTP method (GET, POST, etc)
|
||||||
* @param url the URL
|
* @param url the url (as a URI instance)
|
||||||
* @return the created builder
|
* @return the created builder
|
||||||
* @deprecated in favor of {@link #create(HttpMethod, URI)}
|
* @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.
|
* Create a request builder with the given method and url.
|
||||||
* @param method the HTTP method (GET, POST, etc)
|
* @param method the HTTP method (GET, POST, etc)
|
||||||
* @param url the URL
|
* @param url the url (as a URI instance)
|
||||||
* @return the created builder
|
* @return the created builder
|
||||||
*/
|
*/
|
||||||
static Builder create(HttpMethod method, URI url) {
|
static Builder create(HttpMethod method, URI url) {
|
||||||
|
|
|
@ -49,28 +49,30 @@ import org.springframework.web.reactive.function.BodyInserters;
|
||||||
*/
|
*/
|
||||||
final class DefaultClientRequestBuilder implements ClientRequest.Builder {
|
final class DefaultClientRequestBuilder implements ClientRequest.Builder {
|
||||||
|
|
||||||
|
private HttpMethod method;
|
||||||
|
|
||||||
|
private URI url;
|
||||||
|
|
||||||
private final HttpHeaders headers = new HttpHeaders();
|
private final HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
|
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
|
||||||
|
|
||||||
private final Map<String, Object> attributes = new LinkedHashMap<>();
|
private final Map<String, Object> attributes = new LinkedHashMap<>();
|
||||||
|
|
||||||
private HttpMethod method;
|
private BodyInserter<?, ? super ClientHttpRequest> body = BodyInserters.empty();
|
||||||
|
|
||||||
private URI url;
|
|
||||||
|
|
||||||
private BodyInserter<?, ? super ClientHttpRequest> inserter = BodyInserters.empty();
|
|
||||||
|
|
||||||
|
|
||||||
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
|
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
|
||||||
method(method);
|
Assert.notNull(method, "HttpMethod must not be null");
|
||||||
url(url);
|
Assert.notNull(url, "URI must not be null");
|
||||||
|
this.method = method;
|
||||||
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultClientRequestBuilder(ClientRequest other) {
|
public DefaultClientRequestBuilder(ClientRequest other) {
|
||||||
Assert.notNull(other, "ClientRequest must not be null");
|
Assert.notNull(other, "ClientRequest must not be null");
|
||||||
method(other.method());
|
this.method = other.method();
|
||||||
url(other.url());
|
this.url = other.url();
|
||||||
headers(headers -> headers.addAll(other.headers()));
|
headers(headers -> headers.addAll(other.headers()));
|
||||||
cookies(cookies -> cookies.addAll(other.cookies()));
|
cookies(cookies -> cookies.addAll(other.cookies()));
|
||||||
attributes(attributes -> attributes.putAll(other.attributes()));
|
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(publisher, "'publisher' must not be null");
|
||||||
Assert.notNull(elementClass, "'elementClass' 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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +138,7 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
|
||||||
Assert.notNull(publisher, "'publisher' must not be null");
|
Assert.notNull(publisher, "'publisher' must not be null");
|
||||||
Assert.notNull(typeReference, "'typeReference' 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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,14 +156,13 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientRequest.Builder body(BodyInserter<?, ? super ClientHttpRequest> inserter) {
|
public ClientRequest.Builder body(BodyInserter<?, ? super ClientHttpRequest> inserter) {
|
||||||
this.inserter = inserter;
|
this.body = inserter;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientRequest build() {
|
public ClientRequest build() {
|
||||||
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies,
|
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies, this.body, this.attributes);
|
||||||
this.inserter, this.attributes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,20 +176,19 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
|
||||||
|
|
||||||
private final MultiValueMap<String, String> cookies;
|
private final MultiValueMap<String, String> cookies;
|
||||||
|
|
||||||
private final BodyInserter<?, ? super ClientHttpRequest> inserter;
|
private final BodyInserter<?, ? super ClientHttpRequest> body;
|
||||||
|
|
||||||
private final Map<String, Object> attributes;
|
private final Map<String, Object> attributes;
|
||||||
|
|
||||||
public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
|
public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
|
||||||
MultiValueMap<String, String> cookies,
|
MultiValueMap<String, String> cookies, BodyInserter<?, ? super ClientHttpRequest> body,
|
||||||
BodyInserter<?, ? super ClientHttpRequest> inserter,
|
|
||||||
Map<String, Object> attributes) {
|
Map<String, Object> attributes) {
|
||||||
|
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
|
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
|
||||||
this.cookies = CollectionUtils.unmodifiableMultiValueMap(cookies);
|
this.cookies = CollectionUtils.unmodifiableMultiValueMap(cookies);
|
||||||
this.inserter = inserter;
|
this.body = body;
|
||||||
this.attributes = Collections.unmodifiableMap(attributes);
|
this.attributes = Collections.unmodifiableMap(attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,7 +214,7 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BodyInserter<?, ? super ClientHttpRequest> body() {
|
public BodyInserter<?, ? super ClientHttpRequest> body() {
|
||||||
return this.inserter;
|
return this.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
@Override
|
||||||
public List<HttpMessageWriter<?>> messageWriters() {
|
public List<HttpMessageWriter<?>> messageWriters() {
|
||||||
return strategies.messageWriters();
|
return strategies.messageWriters();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ServerHttpRequest> serverRequest() {
|
public Optional<ServerHttpRequest> serverRequest() {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> hints() {
|
public Map<String, Object> hints() {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
|
|
|
@ -42,16 +42,16 @@ import org.springframework.util.MultiValueMap;
|
||||||
*/
|
*/
|
||||||
final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||||
|
|
||||||
|
private ExchangeStrategies strategies;
|
||||||
|
|
||||||
|
private HttpStatus statusCode = HttpStatus.OK;
|
||||||
|
|
||||||
private final HttpHeaders headers = new HttpHeaders();
|
private final HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
|
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
|
||||||
|
|
||||||
private HttpStatus statusCode = HttpStatus.OK;
|
|
||||||
|
|
||||||
private Flux<DataBuffer> body = Flux.empty();
|
private Flux<DataBuffer> body = Flux.empty();
|
||||||
|
|
||||||
private ExchangeStrategies strategies;
|
|
||||||
|
|
||||||
|
|
||||||
public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
|
public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
|
||||||
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
||||||
|
@ -84,7 +84,6 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
|
public ClientResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
|
||||||
Assert.notNull(headersConsumer, "Consumer must not be null");
|
|
||||||
headersConsumer.accept(this.headers);
|
headersConsumer.accept(this.headers);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -99,7 +98,6 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
public ClientResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
||||||
Assert.notNull(cookiesConsumer, "Consumer must not be null");
|
|
||||||
cookiesConsumer.accept(this.cookies);
|
cookiesConsumer.accept(this.cookies);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,13 +63,14 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||||
@Nullable
|
@Nullable
|
||||||
private ClientHttpConnector connector;
|
private ClientHttpConnector connector;
|
||||||
|
|
||||||
private ExchangeStrategies exchangeStrategies = ExchangeStrategies.withDefaults();
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private ExchangeFunction exchangeFunction;
|
private ExchangeFunction exchangeFunction;
|
||||||
|
|
||||||
|
private ExchangeStrategies exchangeStrategies;
|
||||||
|
|
||||||
|
|
||||||
public DefaultWebClientBuilder() {
|
public DefaultWebClientBuilder() {
|
||||||
|
this.exchangeStrategies = ExchangeStrategies.withDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
|
public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
|
||||||
|
@ -90,8 +91,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||||
new LinkedMultiValueMap<>(other.defaultCookies) : null);
|
new LinkedMultiValueMap<>(other.defaultCookies) : null);
|
||||||
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
|
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
|
||||||
this.connector = other.connector;
|
this.connector = other.connector;
|
||||||
this.exchangeStrategies = other.exchangeStrategies;
|
|
||||||
this.exchangeFunction = other.exchangeFunction;
|
this.exchangeFunction = other.exchangeFunction;
|
||||||
|
this.exchangeStrategies = other.exchangeStrategies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -181,15 +182,15 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
|
public WebClient.Builder exchangeFunction(ExchangeFunction exchangeFunction) {
|
||||||
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
this.exchangeFunction = exchangeFunction;
|
||||||
this.exchangeStrategies = strategies;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WebClient.Builder exchangeFunction(ExchangeFunction exchangeFunction) {
|
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
|
||||||
this.exchangeFunction = exchangeFunction;
|
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
||||||
|
this.exchangeStrategies = strategies;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,9 +208,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||||
|
|
||||||
private static @Nullable HttpHeaders unmodifiableCopy(@Nullable HttpHeaders original) {
|
private static @Nullable HttpHeaders unmodifiableCopy(@Nullable HttpHeaders original) {
|
||||||
if (original != null) {
|
if (original != null) {
|
||||||
HttpHeaders copy = new HttpHeaders();
|
return HttpHeaders.readOnlyHttpHeaders(original);
|
||||||
copy.putAll(original);
|
|
||||||
return HttpHeaders.readOnlyHttpHeaders(copy);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -243,7 +242,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||||
else if (this.connector != null) {
|
else if (this.connector != null) {
|
||||||
return ExchangeFunctions.create(this.connector, this.exchangeStrategies);
|
return ExchangeFunctions.create(this.connector, this.exchangeStrategies);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
return ExchangeFunctions.create(new ReactorClientHttpConnector(), this.exchangeStrategies);
|
return ExchangeFunctions.create(new ReactorClientHttpConnector(), this.exchangeStrategies);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 {
|
public interface ExchangeStrategies {
|
||||||
|
|
||||||
// Instance methods
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the {@link HttpMessageReader}s to be used for request body conversion.
|
* Return the {@link HttpMessageReader}s to be used for request body conversion.
|
||||||
* @return the stream of message readers
|
* @return the stream of message readers
|
||||||
|
@ -60,8 +58,6 @@ public interface ExchangeStrategies {
|
||||||
return builder().build();
|
return builder().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Builder methods
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a mutable builder for a {@code ExchangeStrategies} with default initialization.
|
* Return a mutable builder for a {@code ExchangeStrategies} with default initialization.
|
||||||
* @return the builder
|
* @return the builder
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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);
|
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
|
* Provide a pre-configured {@link ExchangeFunction} instance. This is
|
||||||
* an alternative to and effectively overrides the following:
|
* an alternative to and effectively overrides the following:
|
||||||
|
@ -307,6 +298,15 @@ public interface WebClient {
|
||||||
*/
|
*/
|
||||||
Builder exchangeFunction(ExchangeFunction exchangeFunction);
|
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}
|
* Clone this {@code WebClient.Builder}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -96,7 +96,6 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EntityResponse.Builder<T> cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
public EntityResponse.Builder<T> cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
||||||
Assert.notNull(cookiesConsumer, "Consumer must not be null");
|
|
||||||
cookiesConsumer.accept(this.cookies);
|
cookiesConsumer.accept(this.cookies);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,6 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RenderingResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
public RenderingResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
||||||
Assert.notNull(cookiesConsumer, "Consumer must not be null");
|
|
||||||
cookiesConsumer.accept(this.cookies);
|
cookiesConsumer.accept(this.cookies);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,14 +76,10 @@ class DefaultServerRequest implements ServerRequest {
|
||||||
|
|
||||||
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
|
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
|
||||||
this.exchange = exchange;
|
this.exchange = exchange;
|
||||||
this.messageReaders = unmodifiableCopy(messageReaders);
|
this.messageReaders = Collections.unmodifiableList(new ArrayList<>(messageReaders));
|
||||||
this.headers = new DefaultHeaders();
|
this.headers = new DefaultHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
|
|
||||||
return Collections.unmodifiableList(new ArrayList<>(list));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String methodName() {
|
public String methodName() {
|
||||||
|
|
|
@ -97,7 +97,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerResponse.BodyBuilder headers(Consumer<HttpHeaders> headersConsumer) {
|
public ServerResponse.BodyBuilder headers(Consumer<HttpHeaders> headersConsumer) {
|
||||||
Assert.notNull(headersConsumer, "Consumer must not be null");
|
|
||||||
headersConsumer.accept(this.headers);
|
headersConsumer.accept(this.headers);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -111,7 +110,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerResponse.BodyBuilder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
public ServerResponse.BodyBuilder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
|
||||||
Assert.notNull(cookiesConsumer, "Consumer must not be null");
|
|
||||||
cookiesConsumer.accept(this.cookies);
|
cookiesConsumer.accept(this.cookies);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -201,9 +199,8 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||||
public Mono<ServerResponse> build(
|
public Mono<ServerResponse> build(
|
||||||
BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>> writeFunction) {
|
BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>> writeFunction) {
|
||||||
|
|
||||||
Assert.notNull(writeFunction, "BiFunction must not be null");
|
|
||||||
return Mono.just(
|
return Mono.just(
|
||||||
new WriterFunctionServerResponse(this.statusCode, this.headers, this.cookies, writeFunction));
|
new WriterFunctionResponse(this.statusCode, this.headers, this.cookies, writeFunction));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -250,15 +247,12 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<ServerResponse> body(BodyInserter<?, ? super ServerHttpResponse> inserter) {
|
public Mono<ServerResponse> body(BodyInserter<?, ? super ServerHttpResponse> inserter) {
|
||||||
Assert.notNull(inserter, "BodyInserter must not be null");
|
|
||||||
return Mono.just(
|
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
|
@Override
|
||||||
public Mono<ServerResponse> render(String name, Object... modelAttributes) {
|
public Mono<ServerResponse> render(String name, Object... modelAttributes) {
|
||||||
Assert.hasLength(name, "Name must not be empty");
|
|
||||||
|
|
||||||
return new DefaultRenderingResponseBuilder(name)
|
return new DefaultRenderingResponseBuilder(name)
|
||||||
.headers(this.headers)
|
.headers(this.headers)
|
||||||
.status(this.statusCode)
|
.status(this.statusCode)
|
||||||
|
@ -269,8 +263,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<ServerResponse> render(String name, Map<String, ?> model) {
|
public Mono<ServerResponse> render(String name, Map<String, ?> model) {
|
||||||
Assert.hasLength(name, "Name must not be empty");
|
|
||||||
|
|
||||||
return new DefaultRenderingResponseBuilder(name)
|
return new DefaultRenderingResponseBuilder(name)
|
||||||
.headers(this.headers)
|
.headers(this.headers)
|
||||||
.status(this.statusCode)
|
.status(this.statusCode)
|
||||||
|
@ -290,24 +282,12 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||||
|
|
||||||
private final MultiValueMap<String, ResponseCookie> cookies;
|
private final MultiValueMap<String, ResponseCookie> cookies;
|
||||||
|
|
||||||
protected AbstractServerResponse(int statusCode, HttpHeaders headers,
|
protected AbstractServerResponse(
|
||||||
MultiValueMap<String, ResponseCookie> cookies) {
|
int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies) {
|
||||||
|
|
||||||
this.statusCode = statusCode;
|
this.statusCode = statusCode;
|
||||||
this.headers = readOnlyCopy(headers);
|
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
|
||||||
this.cookies = readOnlyCopy(cookies);
|
this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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;
|
private final BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction;
|
||||||
|
|
||||||
public WriterFunctionServerResponse(int statusCode, HttpHeaders headers,
|
public WriterFunctionResponse(int statusCode, HttpHeaders headers,
|
||||||
MultiValueMap<String, ResponseCookie> cookies,
|
MultiValueMap<String, ResponseCookie> cookies,
|
||||||
BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) {
|
BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) {
|
||||||
|
|
||||||
super(statusCode, headers, cookies);
|
super(statusCode, headers, cookies);
|
||||||
|
Assert.notNull(writeFunction, "BiFunction must not be null");
|
||||||
this.writeFunction = writeFunction;
|
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 BodyInserter<T, ? super ServerHttpResponse> inserter;
|
||||||
|
|
||||||
private final Map<String, Object> hints;
|
private final Map<String, Object> hints;
|
||||||
|
|
||||||
public BodyInserterServerResponse(int statusCode, HttpHeaders headers,
|
public BodyInserterResponse(int statusCode, HttpHeaders headers,
|
||||||
MultiValueMap<String, ResponseCookie> cookies,
|
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);
|
super(statusCode, headers, cookies);
|
||||||
this.inserter = inserter;
|
Assert.notNull(body, "BodyInserter must not be null");
|
||||||
|
this.inserter = body;
|
||||||
this.hints = hints;
|
this.hints = hints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 {
|
public interface HandlerStrategies {
|
||||||
|
|
||||||
// Instance methods
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the {@link HttpMessageReader}s to be used for request body conversion.
|
* Return the {@link HttpMessageReader}s to be used for request body conversion.
|
||||||
* @return the message readers
|
* @return the message readers
|
||||||
|
@ -90,8 +88,6 @@ public interface HandlerStrategies {
|
||||||
return builder().build();
|
return builder().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Builder methods
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a mutable builder for a {@code HandlerStrategies} with default initialization.
|
* Return a mutable builder for a {@code HandlerStrategies} with default initialization.
|
||||||
* @return the builder
|
* @return the builder
|
||||||
|
|
|
@ -248,7 +248,6 @@ public interface ServerRequest {
|
||||||
/**
|
/**
|
||||||
* Return the form data from the body of the request if the Content-Type is
|
* 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.
|
* {@code "application/x-www-form-urlencoded"} or an empty map otherwise.
|
||||||
*
|
|
||||||
* <p><strong>Note:</strong> calling this method causes the request body to
|
* <p><strong>Note:</strong> calling this method causes the request body to
|
||||||
* be read and parsed in full and the resulting {@code MultiValueMap} is
|
* be read and parsed in full and the resulting {@code MultiValueMap} is
|
||||||
* cached so that this method is safe to call more than once.
|
* 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
|
* Return the parts of a multipart request if the Content-Type is
|
||||||
* {@code "multipart/form-data"} or an empty map otherwise.
|
* {@code "multipart/form-data"} or an empty map otherwise.
|
||||||
*
|
|
||||||
* <p><strong>Note:</strong> calling this method causes the request body to
|
* <p><strong>Note:</strong> calling this method causes the request body to
|
||||||
* be read and parsed in full and the resulting {@code MultiValueMap} is
|
* be read and parsed in full and the resulting {@code MultiValueMap} is
|
||||||
* cached so that this method is safe to call more than once.
|
* cached so that this method is safe to call more than once.
|
||||||
|
@ -266,6 +264,7 @@ public interface ServerRequest {
|
||||||
Mono<MultiValueMap<String, Part>> multipartData();
|
Mono<MultiValueMap<String, Part>> multipartData();
|
||||||
|
|
||||||
|
|
||||||
|
// Static methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code ServerRequest} based on the given {@code ServerWebExchange} and
|
* Create a new {@code ServerRequest} based on the given {@code ServerWebExchange} and
|
||||||
|
|
|
@ -56,8 +56,6 @@ import org.springframework.web.server.ServerWebExchange;
|
||||||
*/
|
*/
|
||||||
public interface ServerResponse {
|
public interface ServerResponse {
|
||||||
|
|
||||||
// Instance methods
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the status code of this response.
|
* Return the status code of this response.
|
||||||
*/
|
*/
|
||||||
|
@ -82,7 +80,7 @@ public interface ServerResponse {
|
||||||
Mono<Void> writeTo(ServerWebExchange exchange, Context context);
|
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.
|
* 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.
|
* Create a builder with a {@linkplain HttpStatus#NOT_FOUND 404 Not Found} status.
|
||||||
*
|
|
||||||
* @return the created builder
|
* @return the created builder
|
||||||
*/
|
*/
|
||||||
static HeadersBuilder<?> notFound() {
|
static HeadersBuilder<?> notFound() {
|
||||||
|
|
Loading…
Reference in New Issue