Skip to content

Commit ecb2e97

Browse files
committed
Merge changes for SPR-15124
2 parents cb0d992 + d057c3d commit ecb2e97

24 files changed

+1525
-543
lines changed

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

Lines changed: 15 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@
1717
package org.springframework.web.reactive.function.client;
1818

1919
import java.net.URI;
20-
import java.nio.charset.Charset;
21-
import java.time.ZonedDateTime;
2220

2321
import org.reactivestreams.Publisher;
2422
import reactor.core.publisher.Mono;
2523

2624
import org.springframework.http.HttpHeaders;
2725
import org.springframework.http.HttpMethod;
28-
import org.springframework.http.MediaType;
2926
import org.springframework.http.client.reactive.ClientHttpRequest;
3027
import org.springframework.util.Assert;
3128
import org.springframework.util.MultiValueMap;
3229
import org.springframework.web.reactive.function.BodyInserter;
33-
import org.springframework.web.util.DefaultUriTemplateHandler;
34-
import org.springframework.web.util.UriTemplateHandler;
3530

3631
/**
37-
* Represents a typed, immutable, client-side HTTP request, as executed by the {@link WebClient}.
38-
* Instances of this interface are created via static builder methods:
39-
* {@link #method(HttpMethod, String, Object...)}, {@link #GET(String, Object...)}, etc.
32+
* Represents a typed, immutable, client-side HTTP request, as executed by the
33+
* {@link WebClient}. Instances of this interface can be created via static
34+
* builder methods in this class.
4035
*
36+
* <p>Note that applications are more likely to perform requests through
37+
* {@link WebClientOperations} rather than using this directly.
38+
* :
4139
* @param <T> the type of the body that this request contains
4240
* @author Brian Clozel
4341
* @author Arjen Poutsma
4442
* @since 5.0
4543
*/
4644
public interface ClientRequest<T> {
4745

48-
// Instance methods
49-
5046
/**
5147
* Return the HTTP method.
5248
*/
@@ -81,6 +77,7 @@ public interface ClientRequest<T> {
8177
*/
8278
Mono<Void> writeTo(ClientHttpRequest request, WebClientStrategies strategies);
8379

80+
8481
// Static builder methods
8582

8683
/**
@@ -89,7 +86,7 @@ public interface ClientRequest<T> {
8986
* @param other the request to copy the method, URI, headers, and cookies from
9087
* @return the created builder
9188
*/
92-
static BodyBuilder from(ClientRequest<?> other) {
89+
static Builder from(ClientRequest<?> other) {
9390
Assert.notNull(other, "'other' must not be null");
9491
return new DefaultClientRequestBuilder(other.method(), other.url())
9592
.headers(other.headers())
@@ -102,100 +99,15 @@ static BodyBuilder from(ClientRequest<?> other) {
10299
* @param url the URL
103100
* @return the created builder
104101
*/
105-
static BodyBuilder method(HttpMethod method, URI url) {
106-
return new DefaultClientRequestBuilder(method, url);
107-
}
108-
109-
/**
110-
* Create a builder with the given method and url template.
111-
* @param method the HTTP method (GET, POST, etc)
112-
* @param urlTemplate the URL template
113-
* @param uriVariables optional variables to expand the template
114-
* @return the created builder
115-
*/
116-
static BodyBuilder method(HttpMethod method, String urlTemplate, Object... uriVariables) {
117-
UriTemplateHandler templateHandler = new DefaultUriTemplateHandler();
118-
URI url = templateHandler.expand(urlTemplate, uriVariables);
102+
static Builder method(HttpMethod method, URI url) {
119103
return new DefaultClientRequestBuilder(method, url);
120104
}
121105

122-
/**
123-
* Create an HTTP GET builder with the given url template.
124-
* @param urlTemplate the URL template
125-
* @param uriVariables optional variables to expand the template
126-
* @return the created builder
127-
*/
128-
static HeadersBuilder<?> GET(String urlTemplate, Object... uriVariables) {
129-
return method(HttpMethod.GET, urlTemplate, uriVariables);
130-
}
131-
132-
/**
133-
* Create an HTTP HEAD builder with the given url template.
134-
* @param urlTemplate the URL template
135-
* @param uriVariables optional variables to expand the template
136-
* @return the created builder
137-
*/
138-
static HeadersBuilder<?> HEAD(String urlTemplate, Object... uriVariables) {
139-
return method(HttpMethod.HEAD, urlTemplate, uriVariables);
140-
}
141-
142-
/**
143-
* Create an HTTP POST builder with the given url template.
144-
* @param urlTemplate the URL template
145-
* @param uriVariables optional variables to expand the template
146-
* @return the created builder
147-
*/
148-
static BodyBuilder POST(String urlTemplate, Object... uriVariables) {
149-
return method(HttpMethod.POST, urlTemplate, uriVariables);
150-
}
151-
152-
/**
153-
* Create an HTTP PUT builder with the given url template.
154-
* @param urlTemplate the URL template
155-
* @param uriVariables optional variables to expand the template
156-
* @return the created builder
157-
*/
158-
static BodyBuilder PUT(String urlTemplate, Object... uriVariables) {
159-
return method(HttpMethod.PUT, urlTemplate, uriVariables);
160-
}
161-
162-
/**
163-
* Create an HTTP PATCH builder with the given url template.
164-
* @param urlTemplate the URL template
165-
* @param uriVariables optional variables to expand the template
166-
* @return the created builder
167-
*/
168-
static BodyBuilder PATCH(String urlTemplate, Object... uriVariables) {
169-
return method(HttpMethod.PATCH, urlTemplate, uriVariables);
170-
}
171-
172-
/**
173-
* Create an HTTP DELETE builder with the given url template.
174-
* @param urlTemplate the URL template
175-
* @param uriVariables optional variables to expand the template
176-
* @return the created builder
177-
*/
178-
static HeadersBuilder<?> DELETE(String urlTemplate, Object... uriVariables) {
179-
return method(HttpMethod.DELETE, urlTemplate, uriVariables);
180-
}
181-
182-
/**
183-
* Creates an HTTP OPTIONS builder with the given url template.
184-
* @param urlTemplate the URL template
185-
* @param uriVariables optional variables to expand the template
186-
* @return the created builder
187-
*/
188-
static HeadersBuilder<?> OPTIONS(String urlTemplate, Object... uriVariables) {
189-
return method(HttpMethod.OPTIONS, urlTemplate, uriVariables);
190-
}
191-
192106

193107
/**
194-
* Defines a builder that adds headers to the request.
195-
*
196-
* @param <B> the builder subclass
108+
* Defines a builder for a request.
197109
*/
198-
interface HeadersBuilder<B extends HeadersBuilder<B>> {
110+
interface Builder {
199111

200112
/**
201113
* Add the given, single header value under the given name.
@@ -204,95 +116,37 @@ interface HeadersBuilder<B extends HeadersBuilder<B>> {
204116
* @return this builder
205117
* @see HttpHeaders#add(String, String)
206118
*/
207-
B header(String headerName, String... headerValues);
119+
Builder header(String headerName, String... headerValues);
208120

209121
/**
210122
* Copy the given headers into the entity's headers map.
211123
*
212124
* @param headers the existing HttpHeaders to copy from
213125
* @return this builder
214126
*/
215-
B headers(HttpHeaders headers);
216-
217-
/**
218-
* Set the list of acceptable {@linkplain MediaType media types}, as
219-
* specified by the {@code Accept} header.
220-
* @param acceptableMediaTypes the acceptable media types
221-
* @return this builder
222-
*/
223-
B accept(MediaType... acceptableMediaTypes);
224-
225-
/**
226-
* Set the list of acceptable {@linkplain Charset charsets}, as specified
227-
* by the {@code Accept-Charset} header.
228-
* @param acceptableCharsets the acceptable charsets
229-
* @return this builder
230-
*/
231-
B acceptCharset(Charset... acceptableCharsets);
232-
233-
/**
234-
* Set the value of the {@code If-Modified-Since} header.
235-
* <p>The date should be specified as the number of milliseconds since
236-
* January 1, 1970 GMT.
237-
* @param ifModifiedSince the new value of the header
238-
* @return this builder
239-
*/
240-
B ifModifiedSince(ZonedDateTime ifModifiedSince);
241-
242-
/**
243-
* Set the values of the {@code If-None-Match} header.
244-
* @param ifNoneMatches the new value of the header
245-
* @return this builder
246-
*/
247-
B ifNoneMatch(String... ifNoneMatches);
127+
Builder headers(HttpHeaders headers);
248128

249129
/**
250130
* Add a cookie with the given name and value.
251131
* @param name the cookie name
252132
* @param value the cookie value
253133
* @return this builder
254134
*/
255-
B cookie(String name, String value);
135+
Builder cookie(String name, String value);
256136

257137
/**
258138
* Copy the given cookies into the entity's cookies map.
259139
*
260140
* @param cookies the existing cookies to copy from
261141
* @return this builder
262142
*/
263-
B cookies(MultiValueMap<String, String> cookies);
143+
Builder cookies(MultiValueMap<String, String> cookies);
264144

265145
/**
266146
* Builds the request entity with no body.
267147
* @return the request entity
268148
*/
269149
ClientRequest<Void> build();
270-
}
271-
272-
273-
/**
274-
* Defines a builder that adds a body to the request entity.
275-
*/
276-
interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
277-
278-
279-
/**
280-
* Set the length of the body in bytes, as specified by the
281-
* {@code Content-Length} header.
282-
* @param contentLength the content length
283-
* @return this builder
284-
* @see HttpHeaders#setContentLength(long)
285-
*/
286-
BodyBuilder contentLength(long contentLength);
287-
288-
/**
289-
* Set the {@linkplain MediaType media type} of the body, as specified
290-
* by the {@code Content-Type} header.
291-
* @param contentType the content type
292-
* @return this builder
293-
* @see HttpHeaders#setContentType(MediaType)
294-
*/
295-
BodyBuilder contentType(MediaType contentType);
296150

297151
/**
298152
* Set the body of the request to the given {@code BodyInserter} and return it.
@@ -314,5 +168,4 @@ interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
314168

315169
}
316170

317-
318171
}

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

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
package org.springframework.web.reactive.function.client;
1818

1919
import java.net.URI;
20-
import java.nio.charset.Charset;
21-
import java.time.ZoneId;
22-
import java.time.ZonedDateTime;
23-
import java.time.format.DateTimeFormatter;
24-
import java.util.Arrays;
2520
import java.util.Collections;
2621
import java.util.Map;
2722
import java.util.function.Supplier;
@@ -33,7 +28,6 @@
3328
import org.springframework.http.HttpCookie;
3429
import org.springframework.http.HttpHeaders;
3530
import org.springframework.http.HttpMethod;
36-
import org.springframework.http.MediaType;
3731
import org.springframework.http.client.reactive.ClientHttpRequest;
3832
import org.springframework.http.codec.HttpMessageWriter;
3933
import org.springframework.util.Assert;
@@ -44,12 +38,12 @@
4438
import org.springframework.web.reactive.function.BodyInserters;
4539

4640
/**
47-
* Default implementation of {@link ClientRequest.BodyBuilder}.
41+
* Default implementation of {@link ClientRequest.Builder}.
4842
*
4943
* @author Arjen Poutsma
5044
* @since 5.0
5145
*/
52-
class DefaultClientRequestBuilder implements ClientRequest.BodyBuilder {
46+
class DefaultClientRequestBuilder implements ClientRequest.Builder {
5347

5448
private final HttpMethod method;
5549

@@ -66,55 +60,29 @@ public DefaultClientRequestBuilder(HttpMethod method, URI url) {
6660
}
6761

6862
@Override
69-
public ClientRequest.BodyBuilder header(String headerName, String... headerValues) {
63+
public ClientRequest.Builder header(String headerName, String... headerValues) {
7064
for (String headerValue : headerValues) {
7165
this.headers.add(headerName, headerValue);
7266
}
7367
return this;
7468
}
7569

7670
@Override
77-
public ClientRequest.BodyBuilder headers(HttpHeaders headers) {
71+
public ClientRequest.Builder headers(HttpHeaders headers) {
7872
if (headers != null) {
7973
this.headers.putAll(headers);
8074
}
8175
return this;
8276
}
8377

8478
@Override
85-
public ClientRequest.BodyBuilder accept(MediaType... acceptableMediaTypes) {
86-
this.headers.setAccept(Arrays.asList(acceptableMediaTypes));
87-
return this;
88-
}
89-
90-
@Override
91-
public ClientRequest.BodyBuilder acceptCharset(Charset... acceptableCharsets) {
92-
this.headers.setAcceptCharset(Arrays.asList(acceptableCharsets));
93-
return this;
94-
}
95-
96-
@Override
97-
public ClientRequest.BodyBuilder ifModifiedSince(ZonedDateTime ifModifiedSince) {
98-
ZonedDateTime gmt = ifModifiedSince.withZoneSameInstant(ZoneId.of("GMT"));
99-
String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt);
100-
this.headers.set(HttpHeaders.IF_MODIFIED_SINCE, headerValue);
101-
return this;
102-
}
103-
104-
@Override
105-
public ClientRequest.BodyBuilder ifNoneMatch(String... ifNoneMatches) {
106-
this.headers.setIfNoneMatch(Arrays.asList(ifNoneMatches));
107-
return this;
108-
}
109-
110-
@Override
111-
public ClientRequest.BodyBuilder cookie(String name, String value) {
79+
public ClientRequest.Builder cookie(String name, String value) {
11280
this.cookies.add(name, value);
11381
return this;
11482
}
11583

11684
@Override
117-
public ClientRequest.BodyBuilder cookies(MultiValueMap<String, String> cookies) {
85+
public ClientRequest.Builder cookies(MultiValueMap<String, String> cookies) {
11886
if (cookies != null) {
11987
this.cookies.putAll(cookies);
12088
}
@@ -126,18 +94,6 @@ public ClientRequest<Void> build() {
12694
return body(BodyInserters.empty());
12795
}
12896

129-
@Override
130-
public ClientRequest.BodyBuilder contentLength(long contentLength) {
131-
this.headers.setContentLength(contentLength);
132-
return this;
133-
}
134-
135-
@Override
136-
public ClientRequest.BodyBuilder contentType(MediaType contentType) {
137-
this.headers.setContentType(contentType);
138-
return this;
139-
}
140-
14197
@Override
14298
public <T> ClientRequest<T> body(BodyInserter<T, ? super ClientHttpRequest> inserter) {
14399
Assert.notNull(inserter, "'inserter' must not be null");

0 commit comments

Comments
 (0)