/
HttpRequestValues.java
473 lines (391 loc) · 13.3 KB
/
HttpRequestValues.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright 2002-2022 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.service.invoker;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.http.codec.FormHttpMessageWriter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
/**
* Container for HTTP request values extracted from an
* {@link org.springframework.web.service.annotation.HttpExchange @HttpExchange}-annotated
* method and argument values passed to it. This is then given to
* {@link HttpClientAdapter} to adapt to the underlying HTTP client.
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public final class HttpRequestValues {
private static final MultiValueMap<String, String> EMPTY_COOKIES_MAP =
CollectionUtils.toMultiValueMap(Collections.emptyMap());
@Nullable
private final HttpMethod httpMethod;
@Nullable
private final URI uri;
@Nullable
private final String uriTemplate;
private final Map<String, String> uriVariables;
private final HttpHeaders headers;
private final MultiValueMap<String, String> cookies;
private final Map<String, Object> attributes;
@Nullable
private final Object bodyValue;
@Nullable
private final Publisher<?> body;
@Nullable
private final ParameterizedTypeReference<?> bodyElementType;
private HttpRequestValues(@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVariables,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue,
@Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> bodyElementType) {
Assert.isTrue(uri != null || uriTemplate != null, "Neither URI nor URI template");
this.httpMethod = httpMethod;
this.uri = uri;
this.uriTemplate = uriTemplate;
this.uriVariables = uriVariables;
this.headers = headers;
this.cookies = cookies;
this.attributes = attributes;
this.bodyValue = bodyValue;
this.body = body;
this.bodyElementType = bodyElementType;
}
/**
* Return the HTTP method to use for the request.
*/
@Nullable
public HttpMethod getHttpMethod() {
return this.httpMethod;
}
/**
* Return the URL to use.
* <p>Typically, this comes from a {@link URI} method argument, which provides
* the caller with the option to override the {@link #getUriTemplate()
* uriTemplate} from class and method {@code HttpExchange} annotations.
* annotation.
*/
@Nullable
public URI getUri() {
return this.uri;
}
/**
* Return the URL template for the request. This comes from the values in
* class and method {@code HttpExchange} annotations.
*/
@Nullable
public String getUriTemplate() {
return this.uriTemplate;
}
/**
* Return the URL template variables, or an empty map.
*/
public Map<String, String> getUriVariables() {
return this.uriVariables;
}
/**
* Return the headers for the request, if any.
*/
public HttpHeaders getHeaders() {
return this.headers;
}
/**
* Return the cookies for the request, or an empty map.
*/
public MultiValueMap<String, String> getCookies() {
return this.cookies;
}
/**
* Return the attributes associated with the request, or an empty map.
*/
public Map<String, Object> getAttributes() {
return this.attributes;
}
/**
* Return the request body as a value to be serialized, if set.
* <p>This is mutually exclusive with {@link #getBody()}.
* Only one of the two or neither is set.
*/
@Nullable
public Object getBodyValue() {
return this.bodyValue;
}
/**
* Return the request body as a Publisher.
* <p>This is mutually exclusive with {@link #getBodyValue()}.
* Only one of the two or neither is set.
*/
@Nullable
public Publisher<?> getBody() {
return this.body;
}
/**
* Return the element type for a {@linkplain #getBody() Publisher body}.
*/
@Nullable
public ParameterizedTypeReference<?> getBodyElementType() {
return this.bodyElementType;
}
public static Builder builder() {
return new Builder();
}
/**
* Builder for {@link HttpRequestValues}.
*/
public final static class Builder {
private static final Function<MultiValueMap<String, String>, byte[]> FORM_DATA_SERIALIZER = new FormDataSerializer();
@Nullable
private HttpMethod httpMethod;
@Nullable
private URI uri;
@Nullable
private String uriTemplate;
@Nullable
private Map<String, String> uriVars;
@Nullable
private HttpHeaders headers;
@Nullable
private MultiValueMap<String, String> cookies;
@Nullable
private MultiValueMap<String, String> requestParams;
@Nullable
private MultipartBodyBuilder multipartBuilder;
@Nullable
private Map<String, Object> attributes;
@Nullable
private Object bodyValue;
@Nullable
private Publisher<?> body;
@Nullable
private ParameterizedTypeReference<?> bodyElementType;
/**
* Set the HTTP method for the request.
*/
public Builder setHttpMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
return this;
}
/**
* Set the URL to use. When set, this overrides the
* {@linkplain #setUriTemplate(String) URI template} from the
* {@code HttpExchange} annotation.
*/
public Builder setUri(URI uri) {
this.uri = uri;
return this;
}
/**
* Set the request URL as a String template.
*/
public Builder setUriTemplate(String uriTemplate) {
this.uriTemplate = uriTemplate;
return this;
}
/**
* Add a URI variable name-value pair.
*/
public Builder setUriVariable(String name, String value) {
this.uriVars = (this.uriVars != null ? this.uriVars : new LinkedHashMap<>());
this.uriVars.put(name, value);
return this;
}
/**
* Set the media types for the request {@code Accept} header.
*/
public Builder setAccept(List<MediaType> acceptableMediaTypes) {
initHeaders().setAccept(acceptableMediaTypes);
return this;
}
/**
* Set the media type for the request {@code Content-Type} header.
*/
public Builder setContentType(MediaType contentType) {
initHeaders().setContentType(contentType);
return this;
}
/**
* Add the given header name and values.
*/
public Builder addHeader(String headerName, String... headerValues) {
for (String headerValue : headerValues) {
initHeaders().add(headerName, headerValue);
}
return this;
}
private HttpHeaders initHeaders() {
this.headers = (this.headers != null ? this.headers : new HttpHeaders());
return this.headers;
}
/**
* Add the given cookie name and values.
*/
public Builder addCookie(String name, String... values) {
this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>());
for (String value : values) {
this.cookies.add(name, value);
}
return this;
}
/**
* Add the given request parameter name and values.
* <p>When {@code "content-type"} is set to
* {@code "application/x-www-form-urlencoded"}, request parameters are
* encoded in the request body. Otherwise, they are added as URL query
* parameters.
*/
public Builder addRequestParameter(String name, String... values) {
this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>());
for (String value : values) {
this.requestParams.add(name, value);
}
return this;
}
/**
* Add a part to a multipart request. The part value may be as described
* in {@link MultipartBodyBuilder#part(String, Object)}.
*/
public Builder addRequestPart(String name, Object part) {
this.multipartBuilder = (this.multipartBuilder != null ? this.multipartBuilder : new MultipartBodyBuilder());
this.multipartBuilder.part(name, part);
return this;
}
/**
* Variant of {@link #addRequestPart(String, Object)} that allows the
* part value to be produced by a {@link Publisher}.
*/
public <T, P extends Publisher<T>> Builder addRequestPart(String name, P publisher, ResolvableType type) {
this.multipartBuilder = (this.multipartBuilder != null ? this.multipartBuilder : new MultipartBodyBuilder());
this.multipartBuilder.asyncPart(name, publisher, ParameterizedTypeReference.forType(type.getType()));
return this;
}
/**
* Configure an attribute to associate with the request.
* @param name the attribute name
* @param value the attribute value
*/
public Builder addAttribute(String name, Object value) {
this.attributes = (this.attributes != null ? this.attributes : new HashMap<>());
this.attributes.put(name, value);
return this;
}
/**
* Set the request body as a concrete value to be serialized.
* <p>This is mutually exclusive with, and resets any previously set
* {@linkplain #setBody(Publisher, ParameterizedTypeReference) body Publisher}.
*/
public void setBodyValue(Object bodyValue) {
this.bodyValue = bodyValue;
this.body = null;
this.bodyElementType = null;
}
/**
* Set the request body as a concrete value to be serialized.
* <p>This is mutually exclusive with, and resets any previously set
* {@linkplain #setBodyValue(Object) body value}.
*/
public <T, P extends Publisher<T>> void setBody(P body, ParameterizedTypeReference<T> elementTye) {
this.body = body;
this.bodyElementType = elementTye;
this.bodyValue = null;
}
/**
* Build the {@link HttpRequestValues} instance.
*/
public HttpRequestValues build() {
URI uri = this.uri;
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
Object bodyValue = this.bodyValue;
if (!CollectionUtils.isEmpty(this.requestParams)) {
boolean isFormData = (this.headers != null &&
MediaType.APPLICATION_FORM_URLENCODED.equals(this.headers.getContentType()));
if (isFormData) {
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request params, not both");
bodyValue = FORM_DATA_SERIALIZER.apply(this.requestParams);
}
else if (uri != null) {
uri = UriComponentsBuilder.fromUri(uri)
.queryParams(UriUtils.encodeQueryParams(this.requestParams))
.build(true)
.toUri();
}
else {
uriVars = (uriVars.isEmpty() ? new HashMap<>() : uriVars);
uriTemplate = appendQueryParams(uriTemplate, uriVars, this.requestParams);
}
}
else if (this.multipartBuilder != null) {
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request parts, not both");
bodyValue = this.multipartBuilder.build();
}
HttpHeaders headers = HttpHeaders.EMPTY;
if (this.headers != null) {
headers = new HttpHeaders();
headers.putAll(this.headers);
}
MultiValueMap<String, String> cookies = (this.cookies != null ?
new LinkedMultiValueMap<>(this.cookies) : EMPTY_COOKIES_MAP);
Map<String, Object> attributes = (this.attributes != null ?
new HashMap<>(this.attributes) : Collections.emptyMap());
return new HttpRequestValues(
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes,
bodyValue, this.body, this.bodyElementType);
}
private String appendQueryParams(
String uriTemplate, Map<String, String> uriVars, MultiValueMap<String, String> requestParams) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uriTemplate);
int i = 0;
for (Map.Entry<String, List<String>> entry : requestParams.entrySet()) {
String nameVar = "queryParam" + i;
uriVars.put(nameVar, entry.getKey());
for (int j = 0; j < entry.getValue().size(); j++) {
String valueVar = nameVar + "[" + j + "]";
uriVars.put(valueVar, entry.getValue().get(j));
uriComponentsBuilder.queryParam("{" + nameVar + "}", "{" + valueVar + "}");
}
i++;
}
return uriComponentsBuilder.build().toUriString();
}
}
private static class FormDataSerializer
extends FormHttpMessageWriter implements Function<MultiValueMap<String, String>, byte[]> {
@Override
public byte[] apply(MultiValueMap<String, String> requestParams) {
Charset charset = StandardCharsets.UTF_8;
return serializeForm(requestParams, charset).getBytes(charset);
}
}
}