-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
ClientRequest.java
281 lines (242 loc) · 8.79 KB
/
ClientRequest.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
/*
* Copyright 2002-2021 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.net.URI;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserter;
/**
* Represents a typed, immutable, client-side HTTP request, as executed by the
* {@link ExchangeFunction}. Instances of this interface can be created via static
* builder methods.
*
* <p>Note that applications are more likely to perform requests through
* {@link WebClient} rather than using this directly.
*
* @author Brian Clozel
* @author Arjen Poutsma
* @since 5.0
*/
public interface ClientRequest {
/**
* Name of {@link #attributes() attribute} whose value can be used to
* correlate log messages for this request. Use {@link #logPrefix()} to
* obtain a consistently formatted prefix based on this attribute.
* @since 5.1
* @see #logPrefix()
*/
String LOG_ID_ATTRIBUTE = ClientRequest.class.getName() + ".LOG_ID";
/**
* Return the HTTP method.
*/
HttpMethod method();
/**
* Return the request URI.
*/
URI url();
/**
* Return the headers of this request.
*/
HttpHeaders headers();
/**
* Return the cookies of this request.
*/
MultiValueMap<String, String> cookies();
/**
* Return the body inserter of this request.
*/
BodyInserter<?, ? super ClientHttpRequest> body();
/**
* Return the request attribute value if present.
* @param name the attribute name
* @return the attribute value
*/
default Optional<Object> attribute(String name) {
return Optional.ofNullable(attributes().get(name));
}
/**
* Return the attributes of this request.
*/
Map<String, Object> attributes();
/**
* Return consumer(s) configured to access to the {@link ClientHttpRequest}.
* @since 5.3
*/
@Nullable
Consumer<ClientHttpRequest> httpRequest();
/**
* Return a log message prefix to use to correlate messages for this request.
* The prefix is based on the value of the attribute {@link #LOG_ID_ATTRIBUTE
* LOG_ID_ATTRIBUTE} surrounded with "[" and "]".
* @return the log message prefix or an empty String if the
* {@link #LOG_ID_ATTRIBUTE LOG_ID_ATTRIBUTE} is not set.
* @since 5.1
*/
String logPrefix();
/**
* Write this request to the given {@link ClientHttpRequest}.
* @param request the client http request to write to
* @param strategies the strategies to use when writing
* @return {@code Mono<Void>} to indicate when writing is complete
*/
Mono<Void> writeTo(ClientHttpRequest request, ExchangeStrategies strategies);
// Static builder methods
/**
* Create a builder with the method, URI, headers, cookies, attributes, and body of the given request.
* @param other the request to copy the method, URI, headers, cookies, attributes, and body from
* @return the created builder
*/
static Builder from(ClientRequest other) {
return new DefaultClientRequestBuilder(other);
}
/**
* Create a builder with the given method and url.
* @param method the HTTP method (GET, POST, etc)
* @param url the url (as a URI instance)
* @return the created builder
* @deprecated in favor of {@link #create(HttpMethod, URI)}
*/
@Deprecated
static Builder method(HttpMethod method, URI url) {
return new DefaultClientRequestBuilder(method, url);
}
/**
* Create a request builder with the given method and url.
* @param method the HTTP method (GET, POST, etc)
* @param url the url (as a URI instance)
* @return the created builder
*/
static Builder create(HttpMethod method, URI url) {
return new DefaultClientRequestBuilder(method, url);
}
/**
* Defines a builder for a request.
*/
interface Builder {
/**
* Set the method of the request.
* @param method the new method
* @return this builder
* @since 5.0.1
*/
Builder method(HttpMethod method);
/**
* Set the url of the request.
* @param url the new url
* @return this builder
* @since 5.0.1
*/
Builder url(URI url);
/**
* 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 request's headers with the given consumer. 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 request's cookies with the given consumer. The
* map provided to the consumer is "live", so that the consumer can be used to
* {@linkplain MultiValueMap#set(Object, Object) overwrite} existing header 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, String>> cookiesConsumer);
/**
* Set the body of the request to the given {@code BodyInserter}.
* @param inserter the {@code BodyInserter} that writes to the request
* @return this builder
*/
Builder body(BodyInserter<?, ? super ClientHttpRequest> inserter);
/**
* Set the body of the request to the given {@code Publisher} and return it.
* @param publisher the {@code Publisher} to write to the request
* @param elementClass the class of elements contained in the publisher
* @param <S> the type of the elements contained in the publisher
* @param <P> the type of the {@code Publisher}
* @return the built request
*/
<S, P extends Publisher<S>> Builder body(P publisher, Class<S> elementClass);
/**
* Set the body of the request to the given {@code Publisher} and return it.
* @param publisher the {@code Publisher} to write to the request
* @param typeReference a type reference describing the elements contained in the publisher
* @param <S> the type of the elements contained in the publisher
* @param <P> the type of the {@code Publisher}
* @return the built request
*/
<S, P extends Publisher<S>> Builder body(P publisher, ParameterizedTypeReference<S> typeReference);
/**
* Set the attribute with the given name to the given value.
* @param name the name of the attribute to add
* @param value the value of the attribute to add
* @return this builder
*/
Builder attribute(String name, Object value);
/**
* Manipulate the request attributes with the given consumer. The attributes provided to
* the consumer are "live", so that the consumer can be used to inspect attributes,
* remove attributes, or use any of the other map-provided methods.
* @param attributesConsumer a function that consumes the attributes
* @return this builder
*/
Builder attributes(Consumer<Map<String, Object>> attributesConsumer);
/**
* Callback for access to the {@link ClientHttpRequest} that in turn
* provides access to the native request of the underlying HTTP library.
* This could be useful for setting advanced, per-request options that
* exposed by the underlying library.
* @param requestConsumer a consumer to access the
* {@code ClientHttpRequest} with
* @return this builder
* @since 5.3
*/
Builder httpRequest(Consumer<ClientHttpRequest> requestConsumer);
/**
* Build the request.
*/
ClientRequest build();
}
}