-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
RequestEntity.java
756 lines (653 loc) · 21.1 KB
/
RequestEntity.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*
* 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.http;
import java.lang.reflect.Type;
import java.net.URI;
import java.nio.charset.Charset;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Consumer;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
/**
* Extension of {@link HttpEntity} that also exposes the HTTP method and the
* target URL. For use in the {@code RestTemplate} to prepare requests with
* and in {@code @Controller} methods to represent request input.
*
* <p>Example use with the {@code RestTemplate}:
* <pre class="code">
* MyRequest body = ...
* RequestEntity<MyRequest> request = RequestEntity
* .post("https://example.com/{foo}", "bar")
* .accept(MediaType.APPLICATION_JSON)
* .body(body);
* ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
* </pre>
*
* <p>Example use in an {@code @Controller}:
* <pre class="code">
* @RequestMapping("/handle")
* public void handle(RequestEntity<String> request) {
* HttpMethod method = request.getMethod();
* URI url = request.getUrl();
* String body = request.getBody();
* }
* </pre>
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Parviz Rozikov
* @since 4.1
* @param <T> the body type
* @see #getMethod()
* @see #getUrl()
* @see org.springframework.web.client.RestOperations#exchange(RequestEntity, Class)
* @see ResponseEntity
*/
public class RequestEntity<T> extends HttpEntity<T> {
@Nullable
private final HttpMethod method;
@Nullable
private final URI url;
@Nullable
private final Type type;
/**
* Constructor with method and URL but without body nor headers.
* @param method the method
* @param url the URL
*/
public RequestEntity(HttpMethod method, URI url) {
this(null, null, method, url, null);
}
/**
* Constructor with method, URL and body but without headers.
* @param body the body
* @param method the method
* @param url the URL
*/
public RequestEntity(@Nullable T body, HttpMethod method, URI url) {
this(body, null, method, url, null);
}
/**
* Constructor with method, URL, body and type but without headers.
* @param body the body
* @param method the method
* @param url the URL
* @param type the type used for generic type resolution
* @since 4.3
*/
public RequestEntity(@Nullable T body, HttpMethod method, URI url, Type type) {
this(body, null, method, url, type);
}
/**
* Constructor with method, URL and headers but without body.
* @param headers the headers
* @param method the method
* @param url the URL
*/
public RequestEntity(MultiValueMap<String, String> headers, HttpMethod method, URI url) {
this(null, headers, method, url, null);
}
/**
* Constructor with method, URL, headers and body.
* @param body the body
* @param headers the headers
* @param method the method
* @param url the URL
*/
public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers,
@Nullable HttpMethod method, URI url) {
this(body, headers, method, url, null);
}
/**
* Constructor with method, URL, headers, body and type.
* @param body the body
* @param headers the headers
* @param method the method
* @param url the URL
* @param type the type used for generic type resolution
* @since 4.3
*/
public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers,
@Nullable HttpMethod method, @Nullable URI url, @Nullable Type type) {
super(body, headers);
this.method = method;
this.url = url;
this.type = type;
}
/**
* Return the HTTP method of the request.
* @return the HTTP method as an {@code HttpMethod} enum value
*/
@Nullable
public HttpMethod getMethod() {
return this.method;
}
/**
* Return the {@link URI} for the target HTTP endpoint.
* <p><strong>Note:</strong> This method raises
* {@link UnsupportedOperationException} if the {@code RequestEntity} was
* created with a URI template and variables rather than with a {@link URI}
* instance. This is because a URI cannot be created without further input
* on how to expand template and encode the URI. In such cases, the
* {@code URI} is prepared by the
* {@link org.springframework.web.client.RestTemplate} with the help of the
* {@link org.springframework.web.util.UriTemplateHandler} it is configured with.
*/
public URI getUrl() {
if (this.url == null) {
throw new UnsupportedOperationException(
"The RequestEntity was created with a URI template and variables, " +
"and there is not enough information on how to correctly expand and " +
"encode the URI template. This will be done by the RestTemplate instead " +
"with help from the UriTemplateHandler it is configured with.");
}
return this.url;
}
/**
* Return the type of the request's body.
* @return the request's body type, or {@code null} if not known
* @since 4.3
*/
@Nullable
public Type getType() {
if (this.type == null) {
T body = getBody();
if (body != null) {
return body.getClass();
}
}
return this.type;
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!super.equals(other)) {
return false;
}
return (other instanceof RequestEntity<?> otherEntity &&
ObjectUtils.nullSafeEquals(this.method, otherEntity.method) &&
ObjectUtils.nullSafeEquals(this.url, otherEntity.url));
}
@Override
public int hashCode() {
int hashCode = super.hashCode();
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.method);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.url);
return hashCode;
}
@Override
public String toString() {
return format(getMethod(), getUrl().toString(), getBody(), getHeaders());
}
static <T> String format(@Nullable HttpMethod httpMethod, String url, @Nullable T body, HttpHeaders headers) {
StringBuilder builder = new StringBuilder("<");
builder.append(httpMethod);
builder.append(' ');
builder.append(url);
builder.append(',');
if (body != null) {
builder.append(body);
builder.append(',');
}
builder.append(headers);
builder.append('>');
return builder.toString();
}
// Static builder methods
/**
* Create a builder with the given method and url.
* @param method the HTTP method (GET, POST, etc)
* @param url the URL
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, URI url) {
return new DefaultBodyBuilder(method, url);
}
/**
* Create a builder with the given HTTP method, URI template, and variables.
* @param method the HTTP method (GET, POST, etc)
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static BodyBuilder method(HttpMethod method, String uriTemplate, Object... uriVariables) {
return new DefaultBodyBuilder(method, uriTemplate, uriVariables);
}
/**
* Create a builder with the given HTTP method, URI template, and variables.
* @param method the HTTP method (GET, POST, etc)
* @param uriTemplate the uri template to use
* @return the created builder
* @since 5.3
*/
public static BodyBuilder method(HttpMethod method, String uriTemplate, Map<String, ?> uriVariables) {
return new DefaultBodyBuilder(method, uriTemplate, uriVariables);
}
/**
* Create an HTTP GET builder with the given url.
* @param url the URL
* @return the created builder
*/
public static HeadersBuilder<?> get(URI url) {
return method(HttpMethod.GET, url);
}
/**
* Create an HTTP GET builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> get(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.GET, uriTemplate, uriVariables);
}
/**
* Create an HTTP HEAD builder with the given url.
* @param url the URL
* @return the created builder
*/
public static HeadersBuilder<?> head(URI url) {
return method(HttpMethod.HEAD, url);
}
/**
* Create an HTTP HEAD builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> head(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.HEAD, uriTemplate, uriVariables);
}
/**
* Create an HTTP POST builder with the given url.
* @param url the URL
* @return the created builder
*/
public static BodyBuilder post(URI url) {
return method(HttpMethod.POST, url);
}
/**
* Create an HTTP POST builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static BodyBuilder post(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.POST, uriTemplate, uriVariables);
}
/**
* Create an HTTP PUT builder with the given url.
* @param url the URL
* @return the created builder
*/
public static BodyBuilder put(URI url) {
return method(HttpMethod.PUT, url);
}
/**
* Create an HTTP PUT builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static BodyBuilder put(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.PUT, uriTemplate, uriVariables);
}
/**
* Create an HTTP PATCH builder with the given url.
* @param url the URL
* @return the created builder
*/
public static BodyBuilder patch(URI url) {
return method(HttpMethod.PATCH, url);
}
/**
* Create an HTTP PATCH builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static BodyBuilder patch(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.PATCH, uriTemplate, uriVariables);
}
/**
* Create an HTTP DELETE builder with the given url.
* @param url the URL
* @return the created builder
*/
public static HeadersBuilder<?> delete(URI url) {
return method(HttpMethod.DELETE, url);
}
/**
* Create an HTTP DELETE builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> delete(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.DELETE, uriTemplate, uriVariables);
}
/**
* Creates an HTTP OPTIONS builder with the given url.
* @param url the URL
* @return the created builder
*/
public static HeadersBuilder<?> options(URI url) {
return method(HttpMethod.OPTIONS, url);
}
/**
* Creates an HTTP OPTIONS builder with the given string base uri template.
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> options(String uriTemplate, Object... uriVariables) {
return method(HttpMethod.OPTIONS, uriTemplate, uriVariables);
}
/**
* Defines a builder that adds headers to the request entity.
* @param <B> the builder subclass
*/
public interface HeadersBuilder<B extends HeadersBuilder<B>> {
/**
* Add the given, single header value under the given name.
* @param headerName the header name
* @param headerValues the header value(s)
* @return this builder
* @see HttpHeaders#add(String, String)
*/
B header(String headerName, String... headerValues);
/**
* Copy the given headers into the entity's headers map.
* @param headers the existing HttpHeaders to copy from
* @return this builder
* @since 5.2
* @see HttpHeaders#add(String, String)
*/
B headers(@Nullable HttpHeaders headers);
/**
* Manipulate this entity'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
* @since 5.2
*/
B headers(Consumer<HttpHeaders> headersConsumer);
/**
* Set the list of acceptable {@linkplain MediaType media types}, as
* specified by the {@code Accept} header.
* @param acceptableMediaTypes the acceptable media types
*/
B accept(MediaType... acceptableMediaTypes);
/**
* Set the list of acceptable {@linkplain Charset charsets}, as specified
* by the {@code Accept-Charset} header.
* @param acceptableCharsets the acceptable charsets
*/
B acceptCharset(Charset... acceptableCharsets);
/**
* Set the value of the {@code If-Modified-Since} header.
* @param ifModifiedSince the new value of the header
* @since 5.1.4
*/
B ifModifiedSince(ZonedDateTime ifModifiedSince);
/**
* Set the value of the {@code If-Modified-Since} header.
* @param ifModifiedSince the new value of the header
* @since 5.1.4
*/
B ifModifiedSince(Instant ifModifiedSince);
/**
* Set the value of the {@code If-Modified-Since} header.
* <p>The date should be specified as the number of milliseconds since
* January 1, 1970 GMT.
* @param ifModifiedSince the new value of the header
*/
B ifModifiedSince(long ifModifiedSince);
/**
* Set the values of the {@code If-None-Match} header.
* @param ifNoneMatches the new value of the header
*/
B ifNoneMatch(String... ifNoneMatches);
/**
* Builds the request entity with no body.
* @return the request entity
* @see BodyBuilder#body(Object)
*/
RequestEntity<Void> build();
}
/**
* Defines a builder that adds a body to the response entity.
*/
public interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
/**
* Set the length of the body in bytes, as specified by the
* {@code Content-Length} header.
* @param contentLength the content length
* @return this builder
* @see HttpHeaders#setContentLength(long)
*/
BodyBuilder contentLength(long contentLength);
/**
* Set the {@linkplain MediaType media type} of the body, as specified
* by the {@code Content-Type} header.
* @param contentType the content type
* @return this builder
* @see HttpHeaders#setContentType(MediaType)
*/
BodyBuilder contentType(MediaType contentType);
/**
* Set the body of the request entity and build the RequestEntity.
* @param <T> the type of the body
* @param body the body of the request entity
* @return the built request entity
*/
<T> RequestEntity<T> body(T body);
/**
* Set the body and type of the request entity and build the RequestEntity.
* @param <T> the type of the body
* @param body the body of the request entity
* @param type the type of the body, useful for generic type resolution
* @return the built request entity
* @since 4.3
*/
<T> RequestEntity<T> body(T body, Type type);
}
private static class DefaultBodyBuilder implements BodyBuilder {
private final HttpMethod method;
private final HttpHeaders headers = new HttpHeaders();
@Nullable
private final URI uri;
@Nullable
private final String uriTemplate;
@Nullable
private final Object[] uriVarsArray;
@Nullable
private final Map<String, ?> uriVarsMap;
DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.uri = url;
this.uriTemplate = null;
this.uriVarsArray = null;
this.uriVarsMap = null;
}
DefaultBodyBuilder(HttpMethod method, String uriTemplate, Object... uriVars) {
this.method = method;
this.uri = null;
this.uriTemplate = uriTemplate;
this.uriVarsArray = uriVars;
this.uriVarsMap = null;
}
DefaultBodyBuilder(HttpMethod method, String uriTemplate, Map<String, ?> uriVars) {
this.method = method;
this.uri = null;
this.uriTemplate = uriTemplate;
this.uriVarsArray = null;
this.uriVarsMap = uriVars;
}
@Override
public BodyBuilder header(String headerName, String... headerValues) {
for (String headerValue : headerValues) {
this.headers.add(headerName, headerValue);
}
return this;
}
@Override
public BodyBuilder headers(@Nullable HttpHeaders headers) {
if (headers != null) {
this.headers.putAll(headers);
}
return this;
}
@Override
public BodyBuilder headers(Consumer<HttpHeaders> headersConsumer) {
headersConsumer.accept(this.headers);
return this;
}
@Override
public BodyBuilder accept(MediaType... acceptableMediaTypes) {
this.headers.setAccept(Arrays.asList(acceptableMediaTypes));
return this;
}
@Override
public BodyBuilder acceptCharset(Charset... acceptableCharsets) {
this.headers.setAcceptCharset(Arrays.asList(acceptableCharsets));
return this;
}
@Override
public BodyBuilder contentLength(long contentLength) {
this.headers.setContentLength(contentLength);
return this;
}
@Override
public BodyBuilder contentType(MediaType contentType) {
this.headers.setContentType(contentType);
return this;
}
@Override
public BodyBuilder ifModifiedSince(ZonedDateTime ifModifiedSince) {
this.headers.setIfModifiedSince(ifModifiedSince);
return this;
}
@Override
public BodyBuilder ifModifiedSince(Instant ifModifiedSince) {
this.headers.setIfModifiedSince(ifModifiedSince);
return this;
}
@Override
public BodyBuilder ifModifiedSince(long ifModifiedSince) {
this.headers.setIfModifiedSince(ifModifiedSince);
return this;
}
@Override
public BodyBuilder ifNoneMatch(String... ifNoneMatches) {
this.headers.setIfNoneMatch(Arrays.asList(ifNoneMatches));
return this;
}
@Override
public RequestEntity<Void> build() {
return buildInternal(null, null);
}
@Override
public <T> RequestEntity<T> body(T body) {
return buildInternal(body, null);
}
@Override
public <T> RequestEntity<T> body(T body, Type type) {
return buildInternal(body, type);
}
private <T> RequestEntity<T> buildInternal(@Nullable T body, @Nullable Type type) {
if (this.uri != null) {
return new RequestEntity<>(body, this.headers, this.method, this.uri, type);
}
else if (this.uriTemplate != null){
return new UriTemplateRequestEntity<>(body, this.headers, this.method, type,
this.uriTemplate, this.uriVarsArray, this.uriVarsMap);
}
else {
throw new IllegalStateException("Neither URI nor URI template");
}
}
}
/**
* RequestEntity initialized with a URI template and variables instead of a {@link URI}.
* @since 5.3
* @param <T> the body type
*/
public static class UriTemplateRequestEntity<T> extends RequestEntity<T> {
private final String uriTemplate;
@Nullable
private final Object[] uriVarsArray;
@Nullable
private final Map<String, ?> uriVarsMap;
UriTemplateRequestEntity(
@Nullable T body, @Nullable MultiValueMap<String, String> headers,
@Nullable HttpMethod method, @Nullable Type type, String uriTemplate,
@Nullable Object[] uriVarsArray, @Nullable Map<String, ?> uriVarsMap) {
super(body, headers, method, null, type);
this.uriTemplate = uriTemplate;
this.uriVarsArray = uriVarsArray;
this.uriVarsMap = uriVarsMap;
}
public String getUriTemplate() {
return this.uriTemplate;
}
@Nullable
public Object[] getVars() {
return this.uriVarsArray;
}
@Nullable
public Map<String, ?> getVarsMap() {
return this.uriVarsMap;
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!super.equals(other)) {
return false;
}
return (other instanceof UriTemplateRequestEntity<?> otherEntity &&
ObjectUtils.nullSafeEquals(this.uriTemplate, otherEntity.uriTemplate) &&
ObjectUtils.nullSafeEquals(this.uriVarsArray, otherEntity.uriVarsArray) &&
ObjectUtils.nullSafeEquals(this.uriVarsMap, otherEntity.uriVarsMap));
}
@Override
public int hashCode() {
return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.uriTemplate));
}
@Override
public String toString() {
return format(getMethod(), getUriTemplate(), getBody(), getHeaders());
}
}
}