-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
TestRestTemplate.java
1072 lines (995 loc) · 45.9 KB
/
TestRestTemplate.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-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.boot.test.web.client;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RootUriTemplateHandler;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.RequestEntity.UriTemplateRequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.Assert;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriTemplateHandler;
/**
* Convenient alternative of {@link RestTemplate} that is suitable for integration tests.
* They are fault tolerant, and optionally can carry Basic authentication headers. If
* Apache Http Client 4.3.2 or better is available (recommended) it will be used as the
* client, and by default configured to ignore cookies and redirects.
* <p>
* Note: To prevent injection problems this class intentionally does not extend
* {@link RestTemplate}. If you need access to the underlying {@link RestTemplate} use
* {@link #getRestTemplate()}.
* <p>
* If you are using the
* {@link org.springframework.boot.test.context.SpringBootTest @SpringBootTest} annotation
* with an embedded server, a {@link TestRestTemplate} is automatically available and can
* be {@code @Autowired} into your test. If you need customizations (for example to adding
* additional message converters) use a {@link RestTemplateBuilder} {@code @Bean}.
*
* @author Dave Syer
* @author Phillip Webb
* @author Andy Wilkinson
* @author Kristine Jetzke
* @author Dmytro Nosan
* @since 1.4.0
*/
public class TestRestTemplate {
private final RestTemplateBuilder builder;
private final HttpClientOption[] httpClientOptions;
private final RestTemplate restTemplate;
/**
* Create a new {@link TestRestTemplate} instance.
* @param restTemplateBuilder builder used to configure underlying
* {@link RestTemplate}
* @since 1.4.1
*/
public TestRestTemplate(RestTemplateBuilder restTemplateBuilder) {
this(restTemplateBuilder, null, null);
}
/**
* Create a new {@link TestRestTemplate} instance.
* @param httpClientOptions client options to use if the Apache HTTP Client is used
*/
public TestRestTemplate(HttpClientOption... httpClientOptions) {
this(null, null, httpClientOptions);
}
/**
* Create a new {@link TestRestTemplate} instance with the specified credentials.
* @param username the username to use (or {@code null})
* @param password the password (or {@code null})
* @param httpClientOptions client options to use if the Apache HTTP Client is used
*/
public TestRestTemplate(String username, String password, HttpClientOption... httpClientOptions) {
this(new RestTemplateBuilder(), username, password, httpClientOptions);
}
/**
* Create a new {@link TestRestTemplate} instance with the specified credentials.
* @param builder builder used to configure underlying {@link RestTemplate}
* @param username the username to use (or {@code null})
* @param password the password (or {@code null})
* @param httpClientOptions client options to use if the Apache HTTP Client is used
* @since 2.0.0
*/
public TestRestTemplate(RestTemplateBuilder builder, String username, String password,
HttpClientOption... httpClientOptions) {
Assert.notNull(builder, "Builder must not be null");
this.builder = builder;
this.httpClientOptions = httpClientOptions;
if (httpClientOptions != null) {
ClientHttpRequestFactory requestFactory = builder.buildRequestFactory();
if (requestFactory instanceof HttpComponentsClientHttpRequestFactory) {
builder = builder
.requestFactory(() -> new CustomHttpComponentsClientHttpRequestFactory(httpClientOptions));
}
}
if (username != null || password != null) {
builder = builder.basicAuthentication(username, password);
}
this.restTemplate = builder.build();
this.restTemplate.setErrorHandler(new NoOpResponseErrorHandler());
}
/**
* Configure the {@link UriTemplateHandler} to use to expand URI templates. By default
* the {@link DefaultUriBuilderFactory} is used which relies on Spring's URI template
* support and exposes several useful properties that customize its behavior for
* encoding and for prepending a common base URL. An alternative implementation may be
* used to plug an external URI template library.
* @param handler the URI template handler to use
*/
public void setUriTemplateHandler(UriTemplateHandler handler) {
this.restTemplate.setUriTemplateHandler(handler);
}
/**
* Returns the root URI applied by a {@link RootUriTemplateHandler} or {@code ""} if
* the root URI is not available.
* @return the root URI
*/
public String getRootUri() {
UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler();
if (uriTemplateHandler instanceof RootUriTemplateHandler) {
return ((RootUriTemplateHandler) uriTemplateHandler).getRootUri();
}
return "";
}
/**
* Retrieve a representation by doing a GET on the specified URL. The response (if
* any) is converted and returned.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param responseType the type of the return value
* @param urlVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error on client-side HTTP error
* @see RestTemplate#getForObject(String, Class, Object...)
*/
public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException {
return this.restTemplate.getForObject(url, responseType, urlVariables);
}
/**
* Retrieve a representation by doing a GET on the URI template. The response (if any)
* is converted and returned.
* <p>
* URI Template variables are expanded using the given map.
* @param url the URL
* @param responseType the type of the return value
* @param urlVariables the map containing variables for the URI template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#getForObject(String, Class, Object...)
*/
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> urlVariables)
throws RestClientException {
return this.restTemplate.getForObject(url, responseType, urlVariables);
}
/**
* Retrieve a representation by doing a GET on the URL . The response (if any) is
* converted and returned.
* @param url the URL
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#getForObject(java.net.URI, java.lang.Class)
*/
public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
return this.restTemplate.getForObject(applyRootUriIfNecessary(url), responseType);
}
/**
* Retrieve an entity by doing a GET on the specified URL. The response is converted
* and stored in an {@link ResponseEntity}.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param responseType the type of the return value
* @param urlVariables the variables to expand the template
* @param <T> the type of the return value
* @return the entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#getForEntity(java.lang.String, java.lang.Class,
* java.lang.Object[])
*/
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables)
throws RestClientException {
return this.restTemplate.getForEntity(url, responseType, urlVariables);
}
/**
* Retrieve a representation by doing a GET on the URI template. The response is
* converted and stored in an {@link ResponseEntity}.
* <p>
* URI Template variables are expanded using the given map.
* @param url the URL
* @param responseType the type of the return value
* @param urlVariables the map containing variables for the URI template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#getForEntity(java.lang.String, java.lang.Class, java.util.Map)
*/
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)
throws RestClientException {
return this.restTemplate.getForEntity(url, responseType, urlVariables);
}
/**
* Retrieve a representation by doing a GET on the URL . The response is converted and
* stored in an {@link ResponseEntity}.
* @param url the URL
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#getForEntity(java.net.URI, java.lang.Class)
*/
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {
return this.restTemplate.getForEntity(applyRootUriIfNecessary(url), responseType);
}
/**
* Retrieve all headers of the resource specified by the URI template.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param urlVariables the variables to expand the template
* @return all HTTP headers of that resource
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#headForHeaders(java.lang.String, java.lang.Object[])
*/
public HttpHeaders headForHeaders(String url, Object... urlVariables) throws RestClientException {
return this.restTemplate.headForHeaders(url, urlVariables);
}
/**
* Retrieve all headers of the resource specified by the URI template.
* <p>
* URI Template variables are expanded using the given map.
* @param url the URL
* @param urlVariables the map containing variables for the URI template
* @return all HTTP headers of that resource
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#headForHeaders(java.lang.String, java.util.Map)
*/
public HttpHeaders headForHeaders(String url, Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.headForHeaders(url, urlVariables);
}
/**
* Retrieve all headers of the resource specified by the URL.
* @param url the URL
* @return all HTTP headers of that resource
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#headForHeaders(java.net.URI)
*/
public HttpHeaders headForHeaders(URI url) throws RestClientException {
return this.restTemplate.headForHeaders(applyRootUriIfNecessary(url));
}
/**
* Create a new resource by POSTing the given object to the URI template, and returns
* the value of the {@code Location} header. This header typically indicates where the
* new resource is stored.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param urlVariables the variables to expand the template
* @return the value for the {@code Location} header
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForLocation(java.lang.String, java.lang.Object,
* java.lang.Object[])
*/
public URI postForLocation(String url, Object request, Object... urlVariables) throws RestClientException {
return this.restTemplate.postForLocation(url, request, urlVariables);
}
/**
* Create a new resource by POSTing the given object to the URI template, and returns
* the value of the {@code Location} header. This header typically indicates where the
* new resource is stored.
* <p>
* URI Template variables are expanded using the given map.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param urlVariables the variables to expand the template
* @return the value for the {@code Location} header
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForLocation(java.lang.String, java.lang.Object,
* java.util.Map)
*/
public URI postForLocation(String url, Object request, Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.postForLocation(url, request, urlVariables);
}
/**
* Create a new resource by POSTing the given object to the URL, and returns the value
* of the {@code Location} header. This header typically indicates where the new
* resource is stored.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @return the value for the {@code Location} header
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForLocation(java.net.URI, java.lang.Object)
*/
public URI postForLocation(URI url, Object request) throws RestClientException {
return this.restTemplate.postForLocation(applyRootUriIfNecessary(url), request);
}
/**
* Create a new resource by POSTing the given object to the URI template, and returns
* the representation found in the response.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the type of the return value
* @param urlVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForObject(java.lang.String, java.lang.Object,
* java.lang.Class, java.lang.Object[])
*/
public <T> T postForObject(String url, Object request, Class<T> responseType, Object... urlVariables)
throws RestClientException {
return this.restTemplate.postForObject(url, request, responseType, urlVariables);
}
/**
* Create a new resource by POSTing the given object to the URI template, and returns
* the representation found in the response.
* <p>
* URI Template variables are expanded using the given map.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the type of the return value
* @param urlVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForObject(java.lang.String, java.lang.Object,
* java.lang.Class, java.util.Map)
*/
public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> urlVariables)
throws RestClientException {
return this.restTemplate.postForObject(url, request, responseType, urlVariables);
}
/**
* Create a new resource by POSTing the given object to the URL, and returns the
* representation found in the response.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForObject(java.net.URI, java.lang.Object, java.lang.Class)
*/
public <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException {
return this.restTemplate.postForObject(applyRootUriIfNecessary(url), request, responseType);
}
/**
* Create a new resource by POSTing the given object to the URI template, and returns
* the response as {@link ResponseEntity}.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the response type to return
* @param urlVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForEntity(java.lang.String, java.lang.Object,
* java.lang.Class, java.lang.Object[])
*/
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType,
Object... urlVariables) throws RestClientException {
return this.restTemplate.postForEntity(url, request, responseType, urlVariables);
}
/**
* Create a new resource by POSTing the given object to the URI template, and returns
* the response as {@link HttpEntity}.
* <p>
* URI Template variables are expanded using the given map.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the response type to return
* @param urlVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForEntity(java.lang.String, java.lang.Object,
* java.lang.Class, java.util.Map)
*/
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType,
Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.postForEntity(url, request, responseType, urlVariables);
}
/**
* Create a new resource by POSTing the given object to the URL, and returns the
* response as {@link ResponseEntity}.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the response type to return
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#postForEntity(java.net.URI, java.lang.Object, java.lang.Class)
*/
public <T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType)
throws RestClientException {
return this.restTemplate.postForEntity(applyRootUriIfNecessary(url), request, responseType);
}
/**
* Create or update a resource by PUTting the given object to the URI.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be PUT, may be {@code null}
* @param urlVariables the variables to expand the template
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#put(java.lang.String, java.lang.Object, java.lang.Object[])
*/
public void put(String url, Object request, Object... urlVariables) throws RestClientException {
this.restTemplate.put(url, request, urlVariables);
}
/**
* Creates a new resource by PUTting the given object to URI template.
* <p>
* URI Template variables are expanded using the given map.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be PUT, may be {@code null}
* @param urlVariables the variables to expand the template
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#put(java.lang.String, java.lang.Object, java.util.Map)
*/
public void put(String url, Object request, Map<String, ?> urlVariables) throws RestClientException {
this.restTemplate.put(url, request, urlVariables);
}
/**
* Creates a new resource by PUTting the given object to URL.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be PUT, may be {@code null}
* @throws RestClientException on client-side HTTP error
* @see HttpEntity
* @see RestTemplate#put(java.net.URI, java.lang.Object)
*/
public void put(URI url, Object request) throws RestClientException {
this.restTemplate.put(applyRootUriIfNecessary(url), request);
}
/**
* Update a resource by PATCHing the given object to the URI template, and returns the
* representation found in the response.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be PATCHed, may be {@code null}
* @param responseType the type of the return value
* @param uriVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @since 1.4.4
* @see HttpEntity
*/
public <T> T patchForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException {
return this.restTemplate.patchForObject(url, request, responseType, uriVariables);
}
/**
* Update a resource by PATCHing the given object to the URI template, and returns the
* representation found in the response.
* <p>
* URI Template variables are expanded using the given map.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be PATCHed, may be {@code null}
* @param responseType the type of the return value
* @param uriVariables the variables to expand the template
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @since 1.4.4
* @see HttpEntity
*/
public <T> T patchForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException {
return this.restTemplate.patchForObject(url, request, responseType, uriVariables);
}
/**
* Update a resource by PATCHing the given object to the URL, and returns the
* representation found in the response.
* <p>
* The {@code request} parameter can be a {@link HttpEntity} in order to add
* additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed, may be {@code null}
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the converted object
* @throws RestClientException on client-side HTTP error
* @since 1.4.4
* @see HttpEntity
*/
public <T> T patchForObject(URI url, Object request, Class<T> responseType) throws RestClientException {
return this.restTemplate.patchForObject(applyRootUriIfNecessary(url), request, responseType);
}
/**
* Delete the resources at the specified URI.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param urlVariables the variables to expand in the template
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#delete(java.lang.String, java.lang.Object[])
*/
public void delete(String url, Object... urlVariables) throws RestClientException {
this.restTemplate.delete(url, urlVariables);
}
/**
* Delete the resources at the specified URI.
* <p>
* URI Template variables are expanded using the given map.
* @param url the URL
* @param urlVariables the variables to expand the template
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#delete(java.lang.String, java.util.Map)
*/
public void delete(String url, Map<String, ?> urlVariables) throws RestClientException {
this.restTemplate.delete(url, urlVariables);
}
/**
* Delete the resources at the specified URL.
* @param url the URL
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#delete(java.net.URI)
*/
public void delete(URI url) throws RestClientException {
this.restTemplate.delete(applyRootUriIfNecessary(url));
}
/**
* Return the value of the Allow header for the given URI.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param urlVariables the variables to expand in the template
* @return the value of the allow header
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#optionsForAllow(java.lang.String, java.lang.Object[])
*/
public Set<HttpMethod> optionsForAllow(String url, Object... urlVariables) throws RestClientException {
return this.restTemplate.optionsForAllow(url, urlVariables);
}
/**
* Return the value of the Allow header for the given URI.
* <p>
* URI Template variables are expanded using the given map.
* @param url the URL
* @param urlVariables the variables to expand in the template
* @return the value of the allow header
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#optionsForAllow(java.lang.String, java.util.Map)
*/
public Set<HttpMethod> optionsForAllow(String url, Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.optionsForAllow(url, urlVariables);
}
/**
* Return the value of the Allow header for the given URL.
* @param url the URL
* @return the value of the allow header
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#optionsForAllow(java.net.URI)
*/
public Set<HttpMethod> optionsForAllow(URI url) throws RestClientException {
return this.restTemplate.optionsForAllow(applyRootUriIfNecessary(url));
}
/**
* Execute the HTTP method to the given URI template, writing the given request entity
* to the request, and returns the response as {@link ResponseEntity}.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request, may
* be {@code null}
* @param responseType the type of the return value
* @param urlVariables the variables to expand in the template
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(java.lang.String, org.springframework.http.HttpMethod,
* org.springframework.http.HttpEntity, java.lang.Class, java.lang.Object[])
*/
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
Class<T> responseType, Object... urlVariables) throws RestClientException {
return this.restTemplate.exchange(url, method, requestEntity, responseType, urlVariables);
}
/**
* Execute the HTTP method to the given URI template, writing the given request entity
* to the request, and returns the response as {@link ResponseEntity}.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request, may
* be {@code null}
* @param responseType the type of the return value
* @param urlVariables the variables to expand in the template
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(java.lang.String, org.springframework.http.HttpMethod,
* org.springframework.http.HttpEntity, java.lang.Class, java.util.Map)
*/
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
Class<T> responseType, Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.exchange(url, method, requestEntity, responseType, urlVariables);
}
/**
* Execute the HTTP method to the given URI template, writing the given request entity
* to the request, and returns the response as {@link ResponseEntity}.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request, may
* be {@code null}
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(java.net.URI, org.springframework.http.HttpMethod,
* org.springframework.http.HttpEntity, java.lang.Class)
*/
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity,
Class<T> responseType) throws RestClientException {
return this.restTemplate.exchange(applyRootUriIfNecessary(url), method, requestEntity, responseType);
}
/**
* Execute the HTTP method to the given URI template, writing the given request entity
* to the request, and returns the response as {@link ResponseEntity}. The given
* {@link ParameterizedTypeReference} is used to pass generic type information:
* <pre class="code">
* ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {};
* ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);
* </pre>
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request, may
* be {@code null}
* @param responseType the type of the return value
* @param urlVariables the variables to expand in the template
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(java.lang.String, org.springframework.http.HttpMethod,
* org.springframework.http.HttpEntity,
* org.springframework.core.ParameterizedTypeReference, java.lang.Object[])
*/
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType, Object... urlVariables) throws RestClientException {
return this.restTemplate.exchange(url, method, requestEntity, responseType, urlVariables);
}
/**
* Execute the HTTP method to the given URI template, writing the given request entity
* to the request, and returns the response as {@link ResponseEntity}. The given
* {@link ParameterizedTypeReference} is used to pass generic type information:
* <pre class="code">
* ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {};
* ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);
* </pre>
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request, may
* be {@code null}
* @param responseType the type of the return value
* @param urlVariables the variables to expand in the template
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(java.lang.String, org.springframework.http.HttpMethod,
* org.springframework.http.HttpEntity,
* org.springframework.core.ParameterizedTypeReference, java.util.Map)
*/
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType, Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.exchange(url, method, requestEntity, responseType, urlVariables);
}
/**
* Execute the HTTP method to the given URI template, writing the given request entity
* to the request, and returns the response as {@link ResponseEntity}. The given
* {@link ParameterizedTypeReference} is used to pass generic type information:
* <pre class="code">
* ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {};
* ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);
* </pre>
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request, may
* be {@code null}
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(java.net.URI, org.springframework.http.HttpMethod,
* org.springframework.http.HttpEntity,
* org.springframework.core.ParameterizedTypeReference)
*/
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType) throws RestClientException {
return this.restTemplate.exchange(applyRootUriIfNecessary(url), method, requestEntity, responseType);
}
/**
* Execute the request specified in the given {@link RequestEntity} and return the
* response as {@link ResponseEntity}. Typically used in combination with the static
* builder methods on {@code RequestEntity}, for instance: <pre class="code">
* MyRequest body = ...
* RequestEntity request = RequestEntity.post(new URI("https://example.com/foo")).accept(MediaType.APPLICATION_JSON).body(body);
* ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
* </pre>
* @param requestEntity the entity to write to the request
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(org.springframework.http.RequestEntity, java.lang.Class)
*/
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType)
throws RestClientException {
return this.restTemplate.exchange(createRequestEntityWithRootAppliedUri(requestEntity), responseType);
}
/**
* Execute the request specified in the given {@link RequestEntity} and return the
* response as {@link ResponseEntity}. The given {@link ParameterizedTypeReference} is
* used to pass generic type information: <pre class="code">
* MyRequest body = ...
* RequestEntity request = RequestEntity.post(new URI("https://example.com/foo")).accept(MediaType.APPLICATION_JSON).body(body);
* ParameterizedTypeReference<List<MyResponse>> myBean = new ParameterizedTypeReference<List<MyResponse>>() {};
* ResponseEntity<List<MyResponse>> response = template.exchange(request, myBean);
* </pre>
* @param requestEntity the entity to write to the request
* @param responseType the type of the return value
* @param <T> the type of the return value
* @return the response as entity
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#exchange(org.springframework.http.RequestEntity,
* org.springframework.core.ParameterizedTypeReference)
*/
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
throws RestClientException {
return this.restTemplate.exchange(createRequestEntityWithRootAppliedUri(requestEntity), responseType);
}
/**
* Execute the HTTP method to the given URI template, preparing the request with the
* {@link RequestCallback}, and reading the response with a {@link ResponseExtractor}.
* <p>
* URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestCallback object that prepares the request
* @param responseExtractor object that extracts the return value from the response
* @param urlVariables the variables to expand in the template
* @param <T> the type of the return value
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#execute(java.lang.String, org.springframework.http.HttpMethod,
* org.springframework.web.client.RequestCallback,
* org.springframework.web.client.ResponseExtractor, java.lang.Object[])
*/
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException {
return this.restTemplate.execute(url, method, requestCallback, responseExtractor, urlVariables);
}
/**
* Execute the HTTP method to the given URI template, preparing the request with the
* {@link RequestCallback}, and reading the response with a {@link ResponseExtractor}.
* <p>
* URI Template variables are expanded using the given URI variables map.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestCallback object that prepares the request
* @param responseExtractor object that extracts the return value from the response
* @param urlVariables the variables to expand in the template
* @param <T> the type of the return value
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#execute(java.lang.String, org.springframework.http.HttpMethod,
* org.springframework.web.client.RequestCallback,
* org.springframework.web.client.ResponseExtractor, java.util.Map)
*/
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables) throws RestClientException {
return this.restTemplate.execute(url, method, requestCallback, responseExtractor, urlVariables);
}
/**
* Execute the HTTP method to the given URL, preparing the request with the
* {@link RequestCallback}, and reading the response with a {@link ResponseExtractor}.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestCallback object that prepares the request
* @param responseExtractor object that extracts the return value from the response
* @param <T> the type of the return value
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
* @throws RestClientException on client-side HTTP error
* @see RestTemplate#execute(java.net.URI, org.springframework.http.HttpMethod,
* org.springframework.web.client.RequestCallback,
* org.springframework.web.client.ResponseExtractor)
*/
public <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
return this.restTemplate.execute(applyRootUriIfNecessary(url), method, requestCallback, responseExtractor);
}
/**
* Returns the underlying {@link RestTemplate} that is actually used to perform the
* REST operations.
* @return the restTemplate
*/
public RestTemplate getRestTemplate() {
return this.restTemplate;
}
/**
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
* except that it will send basic authorization headers using the given
* {@code username} and {@code password}. The request factory used is a new instance
* of the underlying {@link RestTemplate}'s request factory type (when possible).
* @param username the username
* @param password the password
* @return the new template
* @since 1.4.1
*/
public TestRestTemplate withBasicAuth(String username, String password) {
TestRestTemplate template = new TestRestTemplate(this.builder, username, password, this.httpClientOptions);
template.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
return template;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private RequestEntity<?> createRequestEntityWithRootAppliedUri(RequestEntity<?> requestEntity) {
return new RequestEntity(requestEntity.getBody(), requestEntity.getHeaders(), requestEntity.getMethod(),
applyRootUriIfNecessary(resolveUri(requestEntity)), requestEntity.getType());
}
private URI applyRootUriIfNecessary(URI uri) {
UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler();
if ((uriTemplateHandler instanceof RootUriTemplateHandler) && uri.toString().startsWith("/")) {
return URI.create(((RootUriTemplateHandler) uriTemplateHandler).getRootUri() + uri.toString());
}
return uri;
}
private URI resolveUri(RequestEntity<?> entity) {
if (entity instanceof UriTemplateRequestEntity) {
UriTemplateRequestEntity<?> templatedUriEntity = (UriTemplateRequestEntity<?>) entity;
if (templatedUriEntity.getVars() != null) {
return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(),
templatedUriEntity.getVars());
}
else if (templatedUriEntity.getVarsMap() != null) {
return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(),
templatedUriEntity.getVarsMap());
}
throw new IllegalStateException(
"No variables specified for URI template: " + templatedUriEntity.getUriTemplate());
}
return entity.getUrl();
}
/**
* Options used to customize the Apache HTTP Client.
*/
public enum HttpClientOption {