forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientResponse.java
347 lines (303 loc) · 11.2 KB
/
ClientResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.client;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.function.Consumer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
/**
* Represents an HTTP response, as returned by {@link WebClient} and also
* {@link ExchangeFunction}. Provides access to the response status and headers,
* and also methods to consume the response body.
*
* <p><strong>NOTE:</strong> When given access to a {@link ClientResponse},
* through the {@code WebClient}
* {@link WebClient.RequestHeadersSpec#exchange() exchange()} method,
* you must always use one of the body or toEntity methods to ensure resources
* are released and avoid potential issues with HTTP connection pooling.
* You can use {@code bodyToMono(Void.class)} if no response content is
* expected. However keep in mind that if the response does have content, the
* connection will be closed and will not be placed back in the pool.
*
* @author Brian Clozel
* @author Arjen Poutsma
* @since 5.0
*/
public interface ClientResponse {
/**
* Return the HTTP status code as an {@link HttpStatus} enum value.
* @return the HTTP status as an HttpStatus enum value (never {@code null})
* @throws IllegalArgumentException in case of an unknown HTTP status code
* @since #getRawStatusCode()
* @see HttpStatus#valueOf(int)
*/
HttpStatus statusCode();
/**
* Return the (potentially non-standard) status code of this response.
* @return the HTTP status as an integer value
* @since 5.1
* @see #statusCode()
* @see HttpStatus#resolve(int)
*/
int rawStatusCode();
/**
* Return the headers of this response.
*/
Headers headers();
/**
* Return the cookies of this response.
*/
MultiValueMap<String, ResponseCookie> cookies();
/**
* Return the strategies used to convert the body of this response.
*/
ExchangeStrategies strategies();
/**
* Extract the body with the given {@code BodyExtractor}.
* @param extractor the {@code BodyExtractor} that reads from the response
* @param <T> the type of the body returned
* @return the extracted body
*/
<T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor);
/**
* Extract the body to a {@code Mono}.
* @param elementClass the class of element in the {@code Mono}
* @param <T> the element type
* @return a mono containing the body of the given type {@code T}
*/
<T> Mono<T> bodyToMono(Class<? extends T> elementClass);
/**
* Extract the body to a {@code Mono}.
* @param typeReference a type reference describing the expected response body type
* @param <T> the element type
* @return a mono containing the body of the given type {@code T}
*/
<T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference);
/**
* Extract the body to a {@code Flux}.
* @param elementClass the class of element in the {@code Flux}
* @param <T> the element type
* @return a flux containing the body of the given type {@code T}
*/
<T> Flux<T> bodyToFlux(Class<? extends T> elementClass);
/**
* Extract the body to a {@code Flux}.
* @param typeReference a type reference describing the expected response body type
* @param <T> the element type
* @return a flux containing the body of the given type {@code T}
*/
<T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference);
/**
* Return this response as a delayed {@code ResponseEntity}.
* @param bodyType the expected response body type
* @param <T> response body type
* @return {@code Mono} with the {@code ResponseEntity}
*/
<T> Mono<ResponseEntity<T>> toEntity(Class<T> bodyType);
/**
* Return this response as a delayed {@code ResponseEntity}.
* @param typeReference a type reference describing the expected response body type
* @param <T> response body type
* @return {@code Mono} with the {@code ResponseEntity}
*/
<T> Mono<ResponseEntity<T>> toEntity(ParameterizedTypeReference<T> typeReference);
/**
* Return this response as a delayed list of {@code ResponseEntity}s.
* @param elementType the expected response body list element type
* @param <T> the type of elements in the list
* @return {@code Mono} with the list of {@code ResponseEntity}s
*/
<T> Mono<ResponseEntity<List<T>>> toEntityList(Class<T> elementType);
/**
* Return this response as a delayed list of {@code ResponseEntity}s.
* @param typeReference a type reference describing the expected response body type
* @param <T> the type of elements in the list
* @return {@code Mono} with the list of {@code ResponseEntity}s
*/
<T> Mono<ResponseEntity<List<T>>> toEntityList(ParameterizedTypeReference<T> typeReference);
// Static builder methods
/**
* Create a builder with the status, headers, and cookies of the given response.
* @param other the response to copy the status, headers, and cookies from
* @return the created builder
*/
static Builder from(ClientResponse other) {
return new DefaultClientResponseBuilder(other);
}
/**
* Create a response builder with the given status code and using default strategies for
* reading the body.
* @param statusCode the status code
* @return the created builder
*/
static Builder create(HttpStatus statusCode) {
return create(statusCode, ExchangeStrategies.withDefaults());
}
/**
* Create a response builder with the given status code and strategies for reading the body.
* @param statusCode the status code
* @param strategies the strategies
* @return the created builder
*/
static Builder create(HttpStatus statusCode, ExchangeStrategies strategies) {
return new DefaultClientResponseBuilder(strategies).statusCode(statusCode);
}
/**
* Create a response builder with the given raw status code and strategies for reading the body.
* @param statusCode the status code
* @param strategies the strategies
* @return the created builder
* @since 5.1.9
*/
static Builder create(int statusCode, ExchangeStrategies strategies) {
return new DefaultClientResponseBuilder(strategies).rawStatusCode(statusCode);
}
/**
* Create a response builder with the given status code and message body readers.
* @param statusCode the status code
* @param messageReaders the message readers
* @return the created builder
*/
static Builder create(HttpStatus statusCode, List<HttpMessageReader<?>> messageReaders) {
return create(statusCode, new ExchangeStrategies() {
@Override
public List<HttpMessageReader<?>> messageReaders() {
return messageReaders;
}
@Override
public List<HttpMessageWriter<?>> messageWriters() {
// not used in the response
return Collections.emptyList();
}
});
}
/**
* Represents the headers of the HTTP response.
* @see ClientResponse#headers()
*/
interface Headers {
/**
* Return the length of the body in bytes, as specified by the
* {@code Content-Length} header.
*/
OptionalLong contentLength();
/**
* Return the {@linkplain MediaType media type} of the body, as specified
* by the {@code Content-Type} header.
*/
Optional<MediaType> contentType();
/**
* Return the header value(s), if any, for the header of the given name.
* <p>Return an empty list if no header values are found.
* @param headerName the header name
*/
List<String> header(String headerName);
/**
* Return the headers as an {@link HttpHeaders} instance.
*/
HttpHeaders asHttpHeaders();
}
/**
* Defines a builder for a response.
*/
interface Builder {
/**
* Set the status code of the response.
* @param statusCode the new status code
* @return this builder
*/
Builder statusCode(HttpStatus statusCode);
/**
* Set the raw status code of the response.
* @param statusCode the new status code
* @return this builder
* @since 5.1.9
*/
Builder rawStatusCode(int statusCode);
/**
* Add the given header value(s) under the given name.
* @param headerName the header name
* @param headerValues the header value(s)
* @return this builder
* @see HttpHeaders#add(String, String)
*/
Builder header(String headerName, String... headerValues);
/**
* Manipulate this response's headers with the given consumer.
* <p>The headers provided to the consumer are "live", so that the consumer
* can be used to {@linkplain HttpHeaders#set(String, String) overwrite}
* existing header values, {@linkplain HttpHeaders#remove(Object) remove}
* values, or use any of the other {@link HttpHeaders} methods.
* @param headersConsumer a function that consumes the {@code HttpHeaders}
* @return this builder
*/
Builder headers(Consumer<HttpHeaders> headersConsumer);
/**
* Add a cookie with the given name and value(s).
* @param name the cookie name
* @param values the cookie value(s)
* @return this builder
*/
Builder cookie(String name, String... values);
/**
* Manipulate this response's cookies with the given consumer.
* <p>The map provided to the consumer is "live", so that the consumer can be used to
* {@linkplain MultiValueMap#set(Object, Object) overwrite} existing cookie values,
* {@linkplain MultiValueMap#remove(Object) remove} values, or use any of the other
* {@link MultiValueMap} methods.
* @param cookiesConsumer a function that consumes the cookies map
* @return this builder
*/
Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer);
/**
* Set the body of the response.
* <p>Calling this methods will
* {@linkplain org.springframework.core.io.buffer.DataBufferUtils#release(DataBuffer) release}
* the existing body of the builder.
* @param body the new body
* @return this builder
*/
Builder body(Flux<DataBuffer> body);
/**
* Set the body of the response to the UTF-8 encoded bytes of the given string.
* <p>Calling this methods will
* {@linkplain org.springframework.core.io.buffer.DataBufferUtils#release(DataBuffer) release}
* the existing body of the builder.
* @param body the new body
* @return this builder
*/
Builder body(String body);
/**
* Build the response.
*/
ClientResponse build();
}
}