Skip to content

Commit 3e47f45

Browse files
committed
Fine-tuned assertions and related polishing in WebFlux builders
(cherry picked from commit 9bff5b4)
1 parent a9548f9 commit 3e47f45

32 files changed

+114
-179
lines changed

spring-web/src/main/java/org/springframework/http/client/MultipartBodyBuilder.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,10 @@ public PartBuilder part(String name, Object part, @Nullable MediaType contentTyp
125125
* @param elementClass the type of elements contained in the publisher
126126
* @return builder that allows for further customization of part headers
127127
*/
128-
public <T, P extends Publisher<T>> PartBuilder asyncPart(String name, P publisher,
129-
Class<T> elementClass) {
130-
131-
Assert.notNull(elementClass, "'elementClass' must not be null");
132-
ResolvableType elementType = ResolvableType.forClass(elementClass);
128+
public <T, P extends Publisher<T>> PartBuilder asyncPart(String name, P publisher, Class<T> elementClass) {
133129
Assert.hasLength(name, "'name' must not be empty");
134130
Assert.notNull(publisher, "'publisher' must not be null");
135-
Assert.notNull(elementType, "'elementType' must not be null");
131+
Assert.notNull(elementClass, "'elementClass' must not be null");
136132

137133
HttpHeaders headers = new HttpHeaders();
138134
PublisherPartBuilder<T, P> builder = new PublisherPartBuilder<>(headers, publisher, elementClass);
@@ -150,14 +146,12 @@ public <T, P extends Publisher<T>> PartBuilder asyncPart(String name, P publishe
150146
* @param typeReference the type of elements contained in the publisher
151147
* @return builder that allows for further customization of part headers
152148
*/
153-
public <T, P extends Publisher<T>> PartBuilder asyncPart(String name, P publisher,
154-
ParameterizedTypeReference<T> typeReference) {
149+
public <T, P extends Publisher<T>> PartBuilder asyncPart(
150+
String name, P publisher, ParameterizedTypeReference<T> typeReference) {
155151

156-
Assert.notNull(typeReference, "'typeReference' must not be null");
157-
ResolvableType elementType1 = ResolvableType.forType(typeReference);
158152
Assert.hasLength(name, "'name' must not be empty");
159153
Assert.notNull(publisher, "'publisher' must not be null");
160-
Assert.notNull(elementType1, "'typeReference' must not be null");
154+
Assert.notNull(typeReference, "'typeReference' must not be null");
161155

162156
HttpHeaders headers = new HttpHeaders();
163157
PublisherPartBuilder<T, P> builder = new PublisherPartBuilder<>(headers, publisher, typeReference);
@@ -210,7 +204,6 @@ private static class DefaultPartBuilder implements PartBuilder {
210204
@Nullable
211205
protected final Object body;
212206

213-
214207
public DefaultPartBuilder(HttpHeaders headers, @Nullable Object body) {
215208
this.headers = headers;
216209
this.body = body;
@@ -224,7 +217,6 @@ public PartBuilder header(String headerName, String... headerValues) {
224217

225218
@Override
226219
public PartBuilder headers(Consumer<HttpHeaders> headersConsumer) {
227-
Assert.notNull(headersConsumer, "'headersConsumer' must not be null");
228220
headersConsumer.accept(this.headers);
229221
return this;
230222
}
@@ -239,7 +231,6 @@ private static class PublisherPartBuilder<S, P extends Publisher<S>> extends Def
239231

240232
private final ResolvableType resolvableType;
241233

242-
243234
public PublisherPartBuilder(HttpHeaders headers, P body, Class<S> elementClass) {
244235
super(headers, body);
245236
this.resolvableType = ResolvableType.forClass(elementClass);
@@ -255,12 +246,11 @@ public PublisherPartBuilder(PublisherEntity<S, P> other) {
255246
this.resolvableType = other.getResolvableType();
256247
}
257248

258-
259249
@Override
260250
@SuppressWarnings("unchecked")
261251
public HttpEntity<?> build() {
262252
P publisher = (P) this.body;
263-
Assert.state(publisher != null, "'publisher' must not be null");
253+
Assert.state(publisher != null, "Publisher must not be null");
264254
return new PublisherEntity<>(this.headers, publisher, this.resolvableType);
265255
}
266256
}

spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java

Lines changed: 2 additions & 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.http.server.reactive;
1718

1819
import java.util.function.BiFunction;
@@ -32,7 +33,6 @@
3233
*/
3334
public class HttpHeadResponseDecorator extends ServerHttpResponseDecorator {
3435

35-
3636
public HttpHeadResponseDecorator(ServerHttpResponse delegate) {
3737
super(delegate);
3838
}
@@ -45,9 +45,7 @@ public HttpHeadResponseDecorator(ServerHttpResponse delegate) {
4545
*/
4646
@Override
4747
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
48-
4948
// After Reactor Netty #171 is fixed we can return without delegating
50-
5149
return getDelegate().writeWith(
5250
Flux.from(body)
5351
.reduce(0, (current, buffer) -> {
@@ -61,7 +59,6 @@ public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
6159

6260
/**
6361
* Invoke {@link #setComplete()} without writing.
64-
*
6562
* <p>RFC 7302 allows HTTP HEAD response without content-length and it's not
6663
* something that can be computed on a streaming response.
6764
*/

spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java

Lines changed: 2 additions & 2 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.
@@ -43,7 +43,7 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest {
4343

4444

4545
public ServerHttpRequestDecorator(ServerHttpRequest delegate) {
46-
Assert.notNull(delegate, "ServerHttpRequest delegate is required.");
46+
Assert.notNull(delegate, "Delegate is required");
4747
this.delegate = delegate;
4848
}
4949

spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponseDecorator.java

Lines changed: 2 additions & 2 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.
@@ -42,7 +42,7 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse {
4242

4343

4444
public ServerHttpResponseDecorator(ServerHttpResponse delegate) {
45-
Assert.notNull(delegate, "ServerHttpResponse delegate is required.");
45+
Assert.notNull(delegate, "Delegate is required");
4646
this.delegate = delegate;
4747
}
4848

spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ public PathContainer extractPathWithinPattern(PathContainer path) {
310310
}
311311
}
312312
resultPath = PathContainer.parsePath(buf.toString());
313-
} else if (startIndex >= endIndex) {
313+
}
314+
else if (startIndex >= endIndex) {
314315
resultPath = PathContainer.parsePath("");
315316
}
316317
else {

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static FormInserter<String> fromFormData(MultiValueMap<String, String> fo
212212
* @return the inserter that allows adding more form data
213213
*/
214214
public static FormInserter<String> fromFormData(String name, String value) {
215-
Assert.notNull(name, "'key' must not be null");
215+
Assert.notNull(name, "'name' must not be null");
216216
Assert.notNull(value, "'value' must not be null");
217217
return new DefaultFormInserter().with(name, value);
218218
}
@@ -221,11 +221,9 @@ public static FormInserter<String> fromFormData(String name, String value) {
221221
* Return a {@link MultipartInserter} that writes the given
222222
* {@code MultiValueMap} as multipart data. Values in the map can be an
223223
* Object or an {@link HttpEntity}.
224-
*
225224
* <p>Note that you can also build the multipart data externally with
226225
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
227226
* {@code syncBody(Object)} shortcut method in {@code WebClient}.
228-
*
229227
* @param multipartData the form data to write to the output message
230228
* @return the inserter that allows adding more parts
231229
* @see MultipartBodyBuilder
@@ -239,40 +237,32 @@ public static MultipartInserter fromMultipartData(MultiValueMap<String, ?> multi
239237
* Return a {@link MultipartInserter} that writes the given parts,
240238
* as multipart data. Values in the map can be an Object or an
241239
* {@link HttpEntity}.
242-
*
243240
* <p>Note that you can also build the multipart data externally with
244241
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
245242
* {@code syncBody(Object)} shortcut method in {@code WebClient}.
246-
*
247243
* @param name the part name
248244
* @param value the part value, an Object or {@code HttpEntity}
249245
* @return the inserter that allows adding more parts
250246
*/
251247
public static MultipartInserter fromMultipartData(String name, Object value) {
252-
Assert.notNull(name, "'key' must not be null");
248+
Assert.notNull(name, "'name' must not be null");
253249
Assert.notNull(value, "'value' must not be null");
254250
return new DefaultMultipartInserter().with(name, value);
255251
}
256252

257253
/**
258254
* Return a {@link MultipartInserter} that writes the given asynchronous parts,
259255
* as multipart data.
260-
*
261256
* <p>Note that you can also build the multipart data externally with
262257
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
263258
* {@code syncBody(Object)} shortcut method in {@code WebClient}.
264-
*
265259
* @param name the part name
266260
* @param publisher the publisher that forms the part value
267261
* @param elementClass the class contained in the {@code publisher}
268262
* @return the inserter that allows adding more parts
269263
*/
270-
public static <T, P extends Publisher<T>> MultipartInserter fromMultipartAsyncData(String name,
271-
P publisher, Class<T> elementClass) {
272-
273-
Assert.notNull(name, "'key' must not be null");
274-
Assert.notNull(publisher, "'publisher' must not be null");
275-
Assert.notNull(elementClass, "'elementClass' must not be null");
264+
public static <T, P extends Publisher<T>> MultipartInserter fromMultipartAsyncData(
265+
String name, P publisher, Class<T> elementClass) {
276266

277267
return new DefaultMultipartInserter().withPublisher(name, publisher, elementClass);
278268
}
@@ -281,22 +271,16 @@ public static <T, P extends Publisher<T>> MultipartInserter fromMultipartAsyncDa
281271
* Variant of {@link #fromMultipartAsyncData(String, Publisher, Class)} that
282272
* accepts a {@link ParameterizedTypeReference} for the element type, which
283273
* allows specifying generic type information.
284-
*
285274
* <p>Note that you can also build the multipart data externally with
286275
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
287276
* {@code syncBody(Object)} shortcut method in {@code WebClient}.
288-
*
289277
* @param name the part name
290278
* @param publisher the publisher that forms the part value
291279
* @param typeReference the type contained in the {@code publisher}
292280
* @return the inserter that allows adding more parts
293281
*/
294-
public static <T, P extends Publisher<T>> MultipartInserter fromMultipartAsyncData(String name,
295-
P publisher, ParameterizedTypeReference<T> typeReference) {
296-
297-
Assert.notNull(name, "'key' must not be null");
298-
Assert.notNull(publisher, "'publisher' must not be null");
299-
Assert.notNull(typeReference, "'typeReference' must not be null");
282+
public static <T, P extends Publisher<T>> MultipartInserter fromMultipartAsyncData(
283+
String name, P publisher, ParameterizedTypeReference<T> typeReference) {
300284

301285
return new DefaultMultipartInserter().withPublisher(name, publisher, typeReference);
302286
}
@@ -312,7 +296,7 @@ public static <T, P extends Publisher<T>> MultipartInserter fromMultipartAsyncDa
312296
public static <T extends Publisher<DataBuffer>> BodyInserter<T, ReactiveHttpOutputMessage> fromDataBuffers(
313297
T publisher) {
314298

315-
Assert.notNull(publisher, "'publisher' must not be null");
299+
Assert.notNull(publisher, "Publisher must not be null");
316300
return (outputMessage, context) -> outputMessage.writeWith(publisher);
317301
}
318302

@@ -333,9 +317,9 @@ private static <T, P extends Publisher<?>, M extends ReactiveHttpOutputMessage>
333317
return messageWriter.write(body, bodyType, bodyType, contentType,
334318
serverRequest.get(), (ServerHttpResponse) outputMessage,
335319
context.hints());
336-
} else {
337-
return messageWriter.write(body, bodyType, contentType, outputMessage,
338-
context.hints());
320+
}
321+
else {
322+
return messageWriter.write(body, bodyType, contentType, outputMessage, context.hints());
339323
}
340324
})
341325
.orElseGet(() -> {
@@ -500,4 +484,5 @@ public Mono<Void> insert(ClientHttpRequest outputMessage, Context context) {
500484
outputMessage, context.hints());
501485
}
502486
}
487+
503488
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.http.HttpHeaders;
2929
import org.springframework.http.HttpMethod;
3030
import org.springframework.http.client.reactive.ClientHttpRequest;
31-
import org.springframework.util.Assert;
3231
import org.springframework.util.MultiValueMap;
3332
import org.springframework.web.reactive.function.BodyInserter;
3433

@@ -109,7 +108,6 @@ default Optional<Object> attribute(String name) {
109108
* @return the created builder
110109
*/
111110
static Builder from(ClientRequest other) {
112-
Assert.notNull(other, "'other' must not be null");
113111
return new DefaultClientRequestBuilder(other);
114112
}
115113

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.http.client.reactive.ClientHttpResponse;
3636
import org.springframework.http.codec.HttpMessageReader;
3737
import org.springframework.http.codec.HttpMessageWriter;
38-
import org.springframework.util.Assert;
3938
import org.springframework.util.MultiValueMap;
4039
import org.springframework.web.reactive.function.BodyExtractor;
4140

@@ -160,7 +159,6 @@ public interface ClientResponse {
160159
* @return the created builder
161160
*/
162161
static Builder from(ClientResponse other) {
163-
Assert.notNull(other, "Other ClientResponse must not be null");
164162
return new DefaultClientResponseBuilder(other);
165163
}
166164

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* @author Arjen Poutsma
4848
* @since 5.0
4949
*/
50-
class DefaultClientRequestBuilder implements ClientRequest.Builder {
50+
final class DefaultClientRequestBuilder implements ClientRequest.Builder {
5151

5252
private final HttpHeaders headers = new HttpHeaders();
5353

@@ -63,18 +63,21 @@ class DefaultClientRequestBuilder implements ClientRequest.Builder {
6363

6464

6565
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
66-
this.method = method;
67-
this.url = url;
66+
method(method);
67+
url(url);
6868
}
6969

7070
public DefaultClientRequestBuilder(ClientRequest other) {
71-
this(other.method(), other.url());
71+
Assert.notNull(other, "ClientRequest must not be null");
72+
method(other.method());
73+
url(other.url());
7274
headers(headers -> headers.addAll(other.headers()));
7375
cookies(cookies -> cookies.addAll(other.cookies()));
7476
attributes(attributes -> attributes.putAll(other.attributes()));
7577
body(other.body());
7678
}
7779

80+
7881
@Override
7982
public ClientRequest.Builder method(HttpMethod method) {
8083
Assert.notNull(method, "'method' must not be null");
@@ -99,7 +102,6 @@ public ClientRequest.Builder header(String headerName, String... headerValues) {
99102

100103
@Override
101104
public ClientRequest.Builder headers(Consumer<HttpHeaders> headersConsumer) {
102-
Assert.notNull(headersConsumer, "'headersConsumer' must not be null");
103105
headersConsumer.accept(this.headers);
104106
return this;
105107
}
@@ -114,7 +116,6 @@ public ClientRequest.Builder cookie(String name, String... values) {
114116

115117
@Override
116118
public ClientRequest.Builder cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer) {
117-
Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null");
118119
cookiesConsumer.accept(this.cookies);
119120
return this;
120121
}
@@ -129,8 +130,8 @@ public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class
129130
}
130131

131132
@Override
132-
public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher,
133-
ParameterizedTypeReference<S> typeReference) {
133+
public <S, P extends Publisher<S>> ClientRequest.Builder body(
134+
P publisher, ParameterizedTypeReference<S> typeReference) {
134135

135136
Assert.notNull(publisher, "'publisher' must not be null");
136137
Assert.notNull(typeReference, "'typeReference' must not be null");
@@ -147,7 +148,6 @@ public ClientRequest.Builder attribute(String name, Object value) {
147148

148149
@Override
149150
public ClientRequest.Builder attributes(Consumer<Map<String, Object>> attributesConsumer) {
150-
Assert.notNull(attributesConsumer, "'attributesConsumer' must not be null");
151151
attributesConsumer.accept(this.attributes);
152152
return this;
153153
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @author Arjen Poutsma
4141
* @since 5.0.5
4242
*/
43-
class DefaultClientResponseBuilder implements ClientResponse.Builder {
43+
final class DefaultClientResponseBuilder implements ClientResponse.Builder {
4444

4545
private final HttpHeaders headers = new HttpHeaders();
4646

@@ -59,7 +59,8 @@ public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
5959
}
6060

6161
public DefaultClientResponseBuilder(ClientResponse other) {
62-
this(other.strategies());
62+
Assert.notNull(other, "ClientResponse must not be null");
63+
this.strategies = other.strategies();
6364
statusCode(other.statusCode());
6465
headers(headers -> headers.addAll(other.headers().asHttpHeaders()));
6566
cookies(cookies -> cookies.addAll(other.cookies()));

0 commit comments

Comments
 (0)