Skip to content

Commit 41ab177

Browse files
committed
Fine-tuned assertions and related polishing
1 parent 8848ec7 commit 41ab177

14 files changed

+66
-108
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ public HttpHeaders() {
414414
* Private constructor that can create read-only {@code HttpHeader} instances.
415415
*/
416416
private HttpHeaders(Map<String, List<String>> headers, boolean readOnly) {
417-
Assert.notNull(headers, "'headers' must not be null");
418417
if (readOnly) {
419418
Map<String, List<String>> map = new LinkedCaseInsensitiveMap<>(headers.size(), Locale.ENGLISH);
420419
headers.forEach((key, valueList) -> map.put(key, Collections.unmodifiableList(valueList)));
@@ -1556,6 +1555,7 @@ public String toString() {
15561555
* Return a {@code HttpHeaders} object that can only be read, not written to.
15571556
*/
15581557
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
1558+
Assert.notNull(headers, "HttpHeaders must not be null");
15591559
return (headers.readOnly ? headers : new HttpHeaders(headers, true));
15601560
}
15611561

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static Builder from(ClientRequest other) {
114114
/**
115115
* Create a builder with the given method and url.
116116
* @param method the HTTP method (GET, POST, etc)
117-
* @param url the URL
117+
* @param url the url (as a URI instance)
118118
* @return the created builder
119119
* @deprecated in favor of {@link #create(HttpMethod, URI)}
120120
*/
@@ -126,7 +126,7 @@ static Builder method(HttpMethod method, URI url) {
126126
/**
127127
* Create a request builder with the given method and url.
128128
* @param method the HTTP method (GET, POST, etc)
129-
* @param url the URL
129+
* @param url the url (as a URI instance)
130130
* @return the created builder
131131
*/
132132
static Builder create(HttpMethod method, URI url) {

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,30 @@
4949
*/
5050
final class DefaultClientRequestBuilder implements ClientRequest.Builder {
5151

52+
private HttpMethod method;
53+
54+
private URI url;
55+
5256
private final HttpHeaders headers = new HttpHeaders();
5357

5458
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
5559

5660
private final Map<String, Object> attributes = new LinkedHashMap<>();
5761

58-
private HttpMethod method;
59-
60-
private URI url;
61-
62-
private BodyInserter<?, ? super ClientHttpRequest> inserter = BodyInserters.empty();
62+
private BodyInserter<?, ? super ClientHttpRequest> body = BodyInserters.empty();
6363

6464

6565
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
66-
method(method);
67-
url(url);
66+
Assert.notNull(method, "HttpMethod must not be null");
67+
Assert.notNull(url, "URI must not be null");
68+
this.method = method;
69+
this.url = url;
6870
}
6971

7072
public DefaultClientRequestBuilder(ClientRequest other) {
7173
Assert.notNull(other, "ClientRequest must not be null");
72-
method(other.method());
73-
url(other.url());
74+
this.method = other.method();
75+
this.url = other.url();
7476
headers(headers -> headers.addAll(other.headers()));
7577
cookies(cookies -> cookies.addAll(other.cookies()));
7678
attributes(attributes -> attributes.putAll(other.attributes()));
@@ -125,7 +127,7 @@ public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class
125127
Assert.notNull(publisher, "'publisher' must not be null");
126128
Assert.notNull(elementClass, "'elementClass' must not be null");
127129

128-
this.inserter = BodyInserters.fromPublisher(publisher, elementClass);
130+
this.body = BodyInserters.fromPublisher(publisher, elementClass);
129131
return this;
130132
}
131133

@@ -136,7 +138,7 @@ public <S, P extends Publisher<S>> ClientRequest.Builder body(
136138
Assert.notNull(publisher, "'publisher' must not be null");
137139
Assert.notNull(typeReference, "'typeReference' must not be null");
138140

139-
this.inserter = BodyInserters.fromPublisher(publisher, typeReference);
141+
this.body = BodyInserters.fromPublisher(publisher, typeReference);
140142
return this;
141143
}
142144

@@ -154,14 +156,13 @@ public ClientRequest.Builder attributes(Consumer<Map<String, Object>> attributes
154156

155157
@Override
156158
public ClientRequest.Builder body(BodyInserter<?, ? super ClientHttpRequest> inserter) {
157-
this.inserter = inserter;
159+
this.body = inserter;
158160
return this;
159161
}
160162

161163
@Override
162164
public ClientRequest build() {
163-
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies,
164-
this.inserter, this.attributes);
165+
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies, this.body, this.attributes);
165166
}
166167

167168

@@ -175,20 +176,19 @@ private static class BodyInserterRequest implements ClientRequest {
175176

176177
private final MultiValueMap<String, String> cookies;
177178

178-
private final BodyInserter<?, ? super ClientHttpRequest> inserter;
179+
private final BodyInserter<?, ? super ClientHttpRequest> body;
179180

180181
private final Map<String, Object> attributes;
181182

182183
public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
183-
MultiValueMap<String, String> cookies,
184-
BodyInserter<?, ? super ClientHttpRequest> inserter,
184+
MultiValueMap<String, String> cookies, BodyInserter<?, ? super ClientHttpRequest> body,
185185
Map<String, Object> attributes) {
186186

187187
this.method = method;
188188
this.url = url;
189189
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
190190
this.cookies = CollectionUtils.unmodifiableMultiValueMap(cookies);
191-
this.inserter = inserter;
191+
this.body = body;
192192
this.attributes = Collections.unmodifiableMap(attributes);
193193
}
194194

@@ -214,7 +214,7 @@ public MultiValueMap<String, String> cookies() {
214214

215215
@Override
216216
public BodyInserter<?, ? super ClientHttpRequest> body() {
217-
return this.inserter;
217+
return this.body;
218218
}
219219

220220
@Override
@@ -240,17 +240,15 @@ public Mono<Void> writeTo(ClientHttpRequest request, ExchangeStrategies strategi
240240
}));
241241
}
242242

243-
return this.inserter.insert(request, new BodyInserter.Context() {
243+
return this.body.insert(request, new BodyInserter.Context() {
244244
@Override
245245
public List<HttpMessageWriter<?>> messageWriters() {
246246
return strategies.messageWriters();
247247
}
248-
249248
@Override
250249
public Optional<ServerHttpRequest> serverRequest() {
251250
return Optional.empty();
252251
}
253-
254252
@Override
255253
public Map<String, Object> hints() {
256254
return Collections.emptyMap();

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@
4242
*/
4343
final class DefaultClientResponseBuilder implements ClientResponse.Builder {
4444

45+
private ExchangeStrategies strategies;
46+
47+
private HttpStatus statusCode = HttpStatus.OK;
48+
4549
private final HttpHeaders headers = new HttpHeaders();
4650

4751
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
4852

49-
private HttpStatus statusCode = HttpStatus.OK;
50-
5153
private Flux<DataBuffer> body = Flux.empty();
5254

53-
private ExchangeStrategies strategies;
54-
5555

5656
public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
5757
Assert.notNull(strategies, "ExchangeStrategies must not be null");
@@ -84,7 +84,6 @@ public ClientResponse.Builder header(String headerName, String... headerValues)
8484

8585
@Override
8686
public ClientResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
87-
Assert.notNull(headersConsumer, "Consumer must not be null");
8887
headersConsumer.accept(this.headers);
8988
return this;
9089
}
@@ -99,7 +98,6 @@ public DefaultClientResponseBuilder cookie(String name, String... values) {
9998

10099
@Override
101100
public ClientResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
102-
Assert.notNull(cookiesConsumer, "Consumer must not be null");
103101
cookiesConsumer.accept(this.cookies);
104102
return this;
105103
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
6363
@Nullable
6464
private ClientHttpConnector connector;
6565

66-
private ExchangeStrategies exchangeStrategies = ExchangeStrategies.withDefaults();
67-
6866
@Nullable
6967
private ExchangeFunction exchangeFunction;
7068

69+
private ExchangeStrategies exchangeStrategies;
70+
7171

7272
public DefaultWebClientBuilder() {
73+
this.exchangeStrategies = ExchangeStrategies.withDefaults();
7374
}
7475

7576
public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
@@ -90,8 +91,8 @@ public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
9091
new LinkedMultiValueMap<>(other.defaultCookies) : null);
9192
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
9293
this.connector = other.connector;
93-
this.exchangeStrategies = other.exchangeStrategies;
9494
this.exchangeFunction = other.exchangeFunction;
95+
this.exchangeStrategies = other.exchangeStrategies;
9596
}
9697

9798

@@ -181,15 +182,15 @@ private List<ExchangeFilterFunction> initFilters() {
181182
}
182183

183184
@Override
184-
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
185-
Assert.notNull(strategies, "ExchangeStrategies must not be null");
186-
this.exchangeStrategies = strategies;
185+
public WebClient.Builder exchangeFunction(ExchangeFunction exchangeFunction) {
186+
this.exchangeFunction = exchangeFunction;
187187
return this;
188188
}
189189

190190
@Override
191-
public WebClient.Builder exchangeFunction(ExchangeFunction exchangeFunction) {
192-
this.exchangeFunction = exchangeFunction;
191+
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
192+
Assert.notNull(strategies, "ExchangeStrategies must not be null");
193+
this.exchangeStrategies = strategies;
193194
return this;
194195
}
195196

@@ -207,9 +208,7 @@ public WebClient build() {
207208

208209
private static @Nullable HttpHeaders unmodifiableCopy(@Nullable HttpHeaders original) {
209210
if (original != null) {
210-
HttpHeaders copy = new HttpHeaders();
211-
copy.putAll(original);
212-
return HttpHeaders.readOnlyHttpHeaders(copy);
211+
return HttpHeaders.readOnlyHttpHeaders(original);
213212
}
214213
else {
215214
return null;
@@ -243,7 +242,6 @@ private ExchangeFunction initExchangeFunction() {
243242
else if (this.connector != null) {
244243
return ExchangeFunctions.create(this.connector, this.exchangeStrategies);
245244
}
246-
247245
else {
248246
return ExchangeFunctions.create(new ReactorClientHttpConnector(), this.exchangeStrategies);
249247
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeStrategies.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,8 +35,6 @@
3535
*/
3636
public interface ExchangeStrategies {
3737

38-
// Instance methods
39-
4038
/**
4139
* Return the {@link HttpMessageReader}s to be used for request body conversion.
4240
* @return the stream of message readers
@@ -60,8 +58,6 @@ static ExchangeStrategies withDefaults() {
6058
return builder().build();
6159
}
6260

63-
// Builder methods
64-
6561
/**
6662
* Return a mutable builder for a {@code ExchangeStrategies} with default initialization.
6763
* @return the builder

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -285,15 +285,6 @@ interface Builder {
285285
*/
286286
Builder filters(Consumer<List<ExchangeFilterFunction>> filtersConsumer);
287287

288-
/**
289-
* Configure the {@link ExchangeStrategies} to use.
290-
* <p>By default {@link ExchangeStrategies#withDefaults()} is used.
291-
* @param strategies the strategies to use
292-
* @see #clientConnector(ClientHttpConnector)
293-
* @see #exchangeFunction(ExchangeFunction)
294-
*/
295-
Builder exchangeStrategies(ExchangeStrategies strategies);
296-
297288
/**
298289
* Provide a pre-configured {@link ExchangeFunction} instance. This is
299290
* an alternative to and effectively overrides the following:
@@ -307,6 +298,15 @@ interface Builder {
307298
*/
308299
Builder exchangeFunction(ExchangeFunction exchangeFunction);
309300

301+
/**
302+
* Configure the {@link ExchangeStrategies} to use.
303+
* <p>By default {@link ExchangeStrategies#withDefaults()} is used.
304+
* @param strategies the strategies to use
305+
* @see #clientConnector(ClientHttpConnector)
306+
* @see #exchangeFunction(ExchangeFunction)
307+
*/
308+
Builder exchangeStrategies(ExchangeStrategies strategies);
309+
310310
/**
311311
* Clone this {@code WebClient.Builder}
312312
*/

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public EntityResponse.Builder<T> cookie(ResponseCookie cookie) {
9696

9797
@Override
9898
public EntityResponse.Builder<T> cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
99-
Assert.notNull(cookiesConsumer, "Consumer must not be null");
10099
cookiesConsumer.accept(this.cookies);
101100
return this;
102101
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public RenderingResponse.Builder cookie(ResponseCookie cookie) {
9999

100100
@Override
101101
public RenderingResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
102-
Assert.notNull(cookiesConsumer, "Consumer must not be null");
103102
cookiesConsumer.accept(this.cookies);
104103
return this;
105104
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,10 @@ class DefaultServerRequest implements ServerRequest {
7676

7777
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
7878
this.exchange = exchange;
79-
this.messageReaders = unmodifiableCopy(messageReaders);
79+
this.messageReaders = Collections.unmodifiableList(new ArrayList<>(messageReaders));
8080
this.headers = new DefaultHeaders();
8181
}
8282

83-
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
84-
return Collections.unmodifiableList(new ArrayList<>(list));
85-
}
86-
8783

8884
@Override
8985
public String methodName() {

0 commit comments

Comments
 (0)