-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathHttpRequest.java
1244 lines (1135 loc) · 38.9 KB
/
HttpRequest.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 (c) 2010 Google Inc.
*
* 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
*
* http://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 com.google.api.client.http;
import com.google.api.client.util.Beta;
import com.google.api.client.util.LoggingStreamingContent;
import com.google.api.client.util.ObjectParser;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.Sleeper;
import com.google.api.client.util.StreamingContent;
import com.google.api.client.util.StringUtils;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.opencensus.common.Scope;
import io.opencensus.contrib.http.util.HttpTraceAttributeConstants;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Span;
import io.opencensus.trace.Tracer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* HTTP request.
*
* <p>Implementation is not thread-safe.
*
* @since 1.0
* @author Yaniv Inbar
*/
public final class HttpRequest {
/**
* Current version of the Google API Client Library for Java.
*
* @since 1.8
*/
public static final String VERSION = getVersion();
/**
* User agent suffix for all requests.
*
* <p>Includes a {@code "(gzip)"} suffix in case the server -- as Google's servers may do --
* checks the {@code User-Agent} header to try to detect if the client accepts gzip-encoded
* responses.
*
* @since 1.4
*/
public static final String USER_AGENT_SUFFIX = "Google-HTTP-Java-Client/" + VERSION + " (gzip)";
/**
* The default number of retries that will be allowed to execute before the request will be
* terminated.
*
* @see #getNumberOfRetries
* @since 1.22
*/
public static final int DEFAULT_NUMBER_OF_RETRIES = 10;
/**
* HTTP request execute interceptor to intercept the start of {@link #execute()} (before executing
* the HTTP request) or {@code null} for none.
*/
private HttpExecuteInterceptor executeInterceptor;
/** HTTP request headers. */
private HttpHeaders headers = new HttpHeaders();
/**
* HTTP response headers.
*
* <p>For example, this can be used if you want to use a subclass of {@link HttpHeaders} called
* MyHeaders to process the response:
*
* <pre>
* static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
* MyHeaders responseHeaders = new MyHeaders();
* request.responseHeaders = responseHeaders;
* HttpResponse response = request.execute();
* return responseHeaders.someCustomHeader;
* }
* </pre>
*/
private HttpHeaders responseHeaders = new HttpHeaders();
/**
* The number of retries that will be allowed to execute before the request will be terminated or
* {@code 0} to not retry requests. Retries occur as a result of either {@link
* HttpUnsuccessfulResponseHandler} or {@link HttpIOExceptionHandler} which handles abnormal HTTP
* response or the I/O exception.
*/
private int numRetries = DEFAULT_NUMBER_OF_RETRIES;
/**
* Determines the limit to the content size that will be logged during {@link #execute()}.
*
* <p>Content will only be logged if {@link #isLoggingEnabled} is {@code true}.
*
* <p>If the content size is greater than this limit then it will not be logged.
*
* <p>Can be set to {@code 0} to disable content logging. This is useful for example if content
* has sensitive data such as authentication information.
*
* <p>Defaults to 16KB.
*/
private int contentLoggingLimit = 0x4000;
/** Determines whether logging should be enabled for this request. Defaults to {@code true}. */
private boolean loggingEnabled = true;
/** Determines whether logging in form of curl commands should be enabled for this request. */
private boolean curlLoggingEnabled = true;
/** HTTP request content or {@code null} for none. */
private HttpContent content;
/** HTTP transport. */
private final HttpTransport transport;
/** HTTP request method or {@code null} for none. */
private String requestMethod;
/** HTTP request URL. */
private GenericUrl url;
/** Timeout in milliseconds to establish a connection or {@code 0} for an infinite timeout. */
private int connectTimeout = 20 * 1000;
/**
* Timeout in milliseconds to read data from an established connection or {@code 0} for an
* infinite timeout.
*/
private int readTimeout = 20 * 1000;
/** Timeout in milliseconds to set POST/PUT data or {@code 0} for an infinite timeout. */
private int writeTimeout = 0;
/** HTTP unsuccessful (non-2XX) response handler or {@code null} for none. */
private HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler;
/** HTTP I/O exception handler or {@code null} for none. */
@Beta private HttpIOExceptionHandler ioExceptionHandler;
/** HTTP response interceptor or {@code null} for none. */
private HttpResponseInterceptor responseInterceptor;
/** Parser used to parse responses. */
private ObjectParser objectParser;
/** HTTP content encoding or {@code null} for none. */
private HttpEncoding encoding;
/** The {@link BackOffPolicy} to use between retry attempts or {@code null} for none. */
@Deprecated @Beta private BackOffPolicy backOffPolicy;
/** Whether to automatically follow redirects ({@code true} by default). */
private boolean followRedirects = true;
/** Whether to use raw redirect URLs ({@code false} by default). */
private boolean useRawRedirectUrls = false;
/**
* Whether to throw an exception at the end of {@link #execute()} on an HTTP error code (non-2XX)
* after all retries and response handlers have been exhausted ({@code true} by default).
*/
private boolean throwExceptionOnExecuteError = true;
/**
* Whether to retry the request if an {@link IOException} is encountered in {@link
* LowLevelHttpRequest#execute()}.
*/
@Deprecated @Beta private boolean retryOnExecuteIOException = false;
/**
* Whether to not add the suffix {@link #USER_AGENT_SUFFIX} to the User-Agent header.
*
* <p>It is {@code false} by default.
*/
private boolean suppressUserAgentSuffix;
/** Sleeper. */
private Sleeper sleeper = Sleeper.DEFAULT;
/** OpenCensus tracing component. */
private final Tracer tracer = OpenCensusUtils.getTracer();
/**
* Determines whether {@link HttpResponse#getContent()} of this request should return raw input
* stream or not.
*
* <p>It is {@code false} by default.
*/
private boolean responseReturnRawInputStream = false;
/**
* @param transport HTTP transport
* @param requestMethod HTTP request method or {@code null} for none
*/
HttpRequest(HttpTransport transport, String requestMethod) {
this.transport = transport;
setRequestMethod(requestMethod);
}
/**
* Returns the HTTP transport.
*
* @since 1.5
*/
public HttpTransport getTransport() {
return transport;
}
/**
* Returns the HTTP request method or {@code null} for none.
*
* @since 1.12
*/
public String getRequestMethod() {
return requestMethod;
}
/**
* Sets the HTTP request method or {@code null} for none.
*
* @since 1.12
*/
public HttpRequest setRequestMethod(String requestMethod) {
Preconditions.checkArgument(requestMethod == null || HttpMediaType.matchesToken(requestMethod));
this.requestMethod = requestMethod;
return this;
}
/**
* Returns the HTTP request URL.
*
* @since 1.5
*/
public GenericUrl getUrl() {
return url;
}
/**
* Sets the HTTP request URL.
*
* @since 1.5
*/
public HttpRequest setUrl(GenericUrl url) {
this.url = Preconditions.checkNotNull(url);
return this;
}
/**
* Returns the HTTP request content or {@code null} for none.
*
* @since 1.5
*/
public HttpContent getContent() {
return content;
}
/**
* Sets the HTTP request content or {@code null} for none.
*
* @since 1.5
*/
public HttpRequest setContent(HttpContent content) {
this.content = content;
return this;
}
/**
* Returns the HTTP content encoding or {@code null} for none.
*
* @since 1.14
*/
public HttpEncoding getEncoding() {
return encoding;
}
/**
* Sets the HTTP content encoding or {@code null} for none.
*
* @since 1.14
*/
public HttpRequest setEncoding(HttpEncoding encoding) {
this.encoding = encoding;
return this;
}
/**
* {@link Beta} <br>
* Returns the {@link BackOffPolicy} to use between retry attempts or {@code null} for none.
*
* @since 1.7
* @deprecated (scheduled to be removed in 1.18). {@link
* #setUnsuccessfulResponseHandler(HttpUnsuccessfulResponseHandler)} with a new {@link
* HttpBackOffUnsuccessfulResponseHandler} instead.
*/
@Deprecated
@Beta
public BackOffPolicy getBackOffPolicy() {
return backOffPolicy;
}
/**
* {@link Beta} <br>
* Sets the {@link BackOffPolicy} to use between retry attempts or {@code null} for none.
*
* @since 1.7
* @deprecated (scheduled to be removed in 1.18). Use {@link
* #setUnsuccessfulResponseHandler(HttpUnsuccessfulResponseHandler)} with a new {@link
* HttpBackOffUnsuccessfulResponseHandler} instead.
*/
@Deprecated
@Beta
public HttpRequest setBackOffPolicy(BackOffPolicy backOffPolicy) {
this.backOffPolicy = backOffPolicy;
return this;
}
/**
* Returns the limit to the content size that will be logged during {@link #execute()}.
*
* <p>If the content size is greater than this limit then it will not be logged.
*
* <p>Content will only be logged if {@link #isLoggingEnabled} is {@code true}.
*
* <p>Can be set to {@code 0} to disable content logging. This is useful for example if content
* has sensitive data such as authentication information.
*
* <p>Defaults to 16KB.
*
* @since 1.7
*/
public int getContentLoggingLimit() {
return contentLoggingLimit;
}
/**
* Set the limit to the content size that will be logged during {@link #execute()}.
*
* <p>If the content size is greater than this limit then it will not be logged.
*
* <p>Content will only be logged if {@link #isLoggingEnabled} is {@code true}.
*
* <p>Can be set to {@code 0} to disable content logging. This is useful for example if content
* has sensitive data such as authentication information.
*
* <p>Defaults to 16KB.
*
* @since 1.7
*/
public HttpRequest setContentLoggingLimit(int contentLoggingLimit) {
Preconditions.checkArgument(
contentLoggingLimit >= 0, "The content logging limit must be non-negative.");
this.contentLoggingLimit = contentLoggingLimit;
return this;
}
/**
* Returns whether logging should be enabled for this request.
*
* <p>Defaults to {@code true}.
*
* @since 1.9
*/
public boolean isLoggingEnabled() {
return loggingEnabled;
}
/**
* Sets whether logging should be enabled for this request.
*
* <p>Defaults to {@code true}.
*
* @since 1.9
*/
public HttpRequest setLoggingEnabled(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
return this;
}
/**
* Returns whether logging in form of curl commands is enabled for this request.
*
* @since 1.11
*/
public boolean isCurlLoggingEnabled() {
return curlLoggingEnabled;
}
/**
* Sets whether logging in form of curl commands should be enabled for this request.
*
* <p>Defaults to {@code true}.
*
* @since 1.11
*/
public HttpRequest setCurlLoggingEnabled(boolean curlLoggingEnabled) {
this.curlLoggingEnabled = curlLoggingEnabled;
return this;
}
/**
* Returns the timeout in milliseconds to establish a connection or {@code 0} for an infinite
* timeout.
*
* @since 1.5
*/
public int getConnectTimeout() {
return connectTimeout;
}
/**
* Sets the timeout in milliseconds to establish a connection or {@code 0} for an infinite
* timeout.
*
* <p>By default it is 20000 (20 seconds).
*
* @since 1.5
*/
public HttpRequest setConnectTimeout(int connectTimeout) {
Preconditions.checkArgument(connectTimeout >= 0);
this.connectTimeout = connectTimeout;
return this;
}
/**
* Returns the timeout in milliseconds to read data from an established connection or {@code 0}
* for an infinite timeout.
*
* <p>By default it is 20000 (20 seconds).
*
* @since 1.5
*/
public int getReadTimeout() {
return readTimeout;
}
/**
* Sets the timeout in milliseconds to read data from an established connection or {@code 0} for
* an infinite timeout.
*
* @since 1.5
*/
public HttpRequest setReadTimeout(int readTimeout) {
Preconditions.checkArgument(readTimeout >= 0);
this.readTimeout = readTimeout;
return this;
}
/**
* Returns the timeout in milliseconds to send POST/PUT data or {@code 0} for an infinite timeout.
*
* <p>By default it is 0 (infinite).
*
* @since 1.27
*/
public int getWriteTimeout() {
return writeTimeout;
}
/**
* Sets the timeout in milliseconds to send POST/PUT data or {@code 0} for an infinite timeout.
*
* @since 1.27
*/
public HttpRequest setWriteTimeout(int writeTimeout) {
Preconditions.checkArgument(writeTimeout >= 0);
this.writeTimeout = writeTimeout;
return this;
}
/**
* Returns the HTTP request headers.
*
* @since 1.5
*/
public HttpHeaders getHeaders() {
return headers;
}
/**
* Sets the HTTP request headers.
*
* <p>By default, this is a new unmodified instance of {@link HttpHeaders}.
*
* @since 1.5
*/
public HttpRequest setHeaders(HttpHeaders headers) {
this.headers = Preconditions.checkNotNull(headers);
return this;
}
/**
* Returns the HTTP response headers.
*
* @since 1.5
*/
public HttpHeaders getResponseHeaders() {
return responseHeaders;
}
/**
* Sets the HTTP response headers.
*
* <p>By default, this is a new unmodified instance of {@link HttpHeaders}.
*
* <p>For example, this can be used if you want to use a subclass of {@link HttpHeaders} called
* MyHeaders to process the response:
*
* <pre>
* static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
* MyHeaders responseHeaders = new MyHeaders();
* request.responseHeaders = responseHeaders;
* HttpResponse response = request.execute();
* return responseHeaders.someCustomHeader;
* }
* </pre>
*
* @since 1.5
*/
public HttpRequest setResponseHeaders(HttpHeaders responseHeaders) {
this.responseHeaders = Preconditions.checkNotNull(responseHeaders);
return this;
}
/**
* Returns the HTTP request execute interceptor to intercept the start of {@link #execute()}
* (before executing the HTTP request) or {@code null} for none.
*
* @since 1.5
*/
public HttpExecuteInterceptor getInterceptor() {
return executeInterceptor;
}
/**
* Sets the HTTP request execute interceptor to intercept the start of {@link #execute()} (before
* executing the HTTP request) or {@code null} for none.
*
* @since 1.5
*/
public HttpRequest setInterceptor(HttpExecuteInterceptor interceptor) {
this.executeInterceptor = interceptor;
return this;
}
/**
* Returns the HTTP unsuccessful (non-2XX) response handler or {@code null} for none.
*
* @since 1.5
*/
public HttpUnsuccessfulResponseHandler getUnsuccessfulResponseHandler() {
return unsuccessfulResponseHandler;
}
/**
* Sets the HTTP unsuccessful (non-2XX) response handler or {@code null} for none.
*
* @since 1.5
*/
public HttpRequest setUnsuccessfulResponseHandler(
HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler) {
this.unsuccessfulResponseHandler = unsuccessfulResponseHandler;
return this;
}
/**
* {@link Beta} <br>
* Returns the HTTP I/O exception handler or {@code null} for none.
*
* @since 1.15
*/
@Beta
public HttpIOExceptionHandler getIOExceptionHandler() {
return ioExceptionHandler;
}
/**
* {@link Beta} <br>
* Sets the HTTP I/O exception handler or {@code null} for none.
*
* @since 1.15
*/
@Beta
public HttpRequest setIOExceptionHandler(HttpIOExceptionHandler ioExceptionHandler) {
this.ioExceptionHandler = ioExceptionHandler;
return this;
}
/**
* Returns the HTTP response interceptor or {@code null} for none.
*
* @since 1.13
*/
public HttpResponseInterceptor getResponseInterceptor() {
return responseInterceptor;
}
/**
* Sets the HTTP response interceptor or {@code null} for none.
*
* @since 1.13
*/
public HttpRequest setResponseInterceptor(HttpResponseInterceptor responseInterceptor) {
this.responseInterceptor = responseInterceptor;
return this;
}
/**
* Returns the number of retries that will be allowed to execute before the request will be
* terminated or {@code 0} to not retry requests. Retries occur as a result of either {@link
* HttpUnsuccessfulResponseHandler} or {@link HttpIOExceptionHandler} which handles abnormal HTTP
* response or the I/O exception.
*
* @since 1.5
*/
public int getNumberOfRetries() {
return numRetries;
}
/**
* Sets the number of retries that will be allowed to execute before the request will be
* terminated or {@code 0} to not retry requests. Retries occur as a result of either {@link
* HttpUnsuccessfulResponseHandler} or {@link HttpIOExceptionHandler} which handles abnormal HTTP
* response or the I/O exception.
*
* <p>The default value is {@link #DEFAULT_NUMBER_OF_RETRIES}.
*
* @since 1.5
*/
public HttpRequest setNumberOfRetries(int numRetries) {
Preconditions.checkArgument(numRetries >= 0);
this.numRetries = numRetries;
return this;
}
/**
* Sets the {@link ObjectParser} used to parse the response to this request or {@code null} for
* none.
*
* <p>This parser will be preferred over any registered HttpParser.
*
* @since 1.10
*/
public HttpRequest setParser(ObjectParser parser) {
this.objectParser = parser;
return this;
}
/**
* Returns the {@link ObjectParser} used to parse the response or {@code null} for none.
*
* @since 1.10
*/
public final ObjectParser getParser() {
return objectParser;
}
/**
* Returns whether to follow redirects automatically.
*
* @since 1.6
*/
public boolean getFollowRedirects() {
return followRedirects;
}
/**
* Sets whether to follow redirects automatically.
*
* <p>The default value is {@code true}.
*
* @since 1.6
*/
public HttpRequest setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
/** Return whether to use raw redirect URLs. */
public boolean getUseRawRedirectUrls() {
return useRawRedirectUrls;
}
/**
* Sets whether to use raw redirect URLs.
*
* <p>The default value is {@code false}.
*/
public HttpRequest setUseRawRedirectUrls(boolean useRawRedirectUrls) {
this.useRawRedirectUrls = useRawRedirectUrls;
return this;
}
/**
* Returns whether to throw an exception at the end of {@link #execute()} on an HTTP error code
* (non-2XX) after all retries and response handlers have been exhausted.
*
* @since 1.7
*/
public boolean getThrowExceptionOnExecuteError() {
return throwExceptionOnExecuteError;
}
/**
* Sets whether to throw an exception at the end of {@link #execute()} on a HTTP error code
* (non-2XX) after all retries and response handlers have been exhausted.
*
* <p>The default value is {@code true}.
*
* @since 1.7
*/
public HttpRequest setThrowExceptionOnExecuteError(boolean throwExceptionOnExecuteError) {
this.throwExceptionOnExecuteError = throwExceptionOnExecuteError;
return this;
}
/**
* {@link Beta} <br>
* Returns whether to retry the request if an {@link IOException} is encountered in {@link
* LowLevelHttpRequest#execute()}.
*
* @since 1.9
* @deprecated (scheduled to be removed in 1.18) Use {@link
* #setIOExceptionHandler(HttpIOExceptionHandler)} instead.
*/
@Deprecated
@Beta
public boolean getRetryOnExecuteIOException() {
return retryOnExecuteIOException;
}
/**
* {@link Beta} <br>
* Sets whether to retry the request if an {@link IOException} is encountered in {@link
* LowLevelHttpRequest#execute()}.
*
* <p>The default value is {@code false}.
*
* @since 1.9
* @deprecated (scheduled to be removed in 1.18) Use {@link
* #setIOExceptionHandler(HttpIOExceptionHandler)} instead.
*/
@Deprecated
@Beta
public HttpRequest setRetryOnExecuteIOException(boolean retryOnExecuteIOException) {
this.retryOnExecuteIOException = retryOnExecuteIOException;
return this;
}
/**
* Returns whether to not add the suffix {@link #USER_AGENT_SUFFIX} to the User-Agent header.
*
* @since 1.11
*/
public boolean getSuppressUserAgentSuffix() {
return suppressUserAgentSuffix;
}
/**
* Sets whether to not add the suffix {@link #USER_AGENT_SUFFIX} to the User-Agent header.
*
* <p>The default value is {@code false}.
*
* @since 1.11
*/
public HttpRequest setSuppressUserAgentSuffix(boolean suppressUserAgentSuffix) {
this.suppressUserAgentSuffix = suppressUserAgentSuffix;
return this;
}
/**
* Returns whether {@link HttpResponse#getContent()} should return raw input stream for this
* request.
*
* @since 1.29
*/
public boolean getResponseReturnRawInputStream() {
return responseReturnRawInputStream;
}
/**
* Sets whether {@link HttpResponse#getContent()} should return raw input stream for this request.
*
* <p>The default value is {@code false}.
*
* @since 1.29
*/
public HttpRequest setResponseReturnRawInputStream(boolean responseReturnRawInputStream) {
this.responseReturnRawInputStream = responseReturnRawInputStream;
return this;
}
/**
* Execute the HTTP request and returns the HTTP response.
*
* <p>Note that regardless of the returned status code, the HTTP response content has not been
* parsed yet, and must be parsed by the calling code.
*
* <p>Note that when calling to this method twice or more, the state of this HTTP request object
* isn't cleared, so the request will continue where it was left. For example, the state of the
* {@link HttpUnsuccessfulResponseHandler} attached to this HTTP request will remain the same as
* it was left after last execute.
*
* <p>Almost all details of the request and response are logged if {@link Level#CONFIG} is
* loggable. The only exception is the value of the {@code Authorization} header which is only
* logged if {@link Level#ALL} is loggable.
*
* <p>Callers should call {@link HttpResponse#disconnect} when the returned HTTP response object
* is no longer needed. However, {@link HttpResponse#disconnect} does not have to be called if the
* response stream is properly closed. Example usage:
*
* <pre>
* HttpResponse response = request.execute();
* try {
* // process the HTTP response object
* } finally {
* response.disconnect();
* }
* </pre>
*
* @return HTTP response for an HTTP success response (or HTTP error response if {@link
* #getThrowExceptionOnExecuteError()} is {@code false})
* @throws HttpResponseException for an HTTP error response (only if {@link
* #getThrowExceptionOnExecuteError()} is {@code true})
* @see HttpResponse#isSuccessStatusCode()
*/
@SuppressWarnings("deprecation")
public HttpResponse execute() throws IOException {
boolean retryRequest = false;
Preconditions.checkArgument(numRetries >= 0);
int retriesRemaining = numRetries;
if (backOffPolicy != null) {
// Reset the BackOffPolicy at the start of each execute.
backOffPolicy.reset();
}
HttpResponse response = null;
IOException executeException;
Preconditions.checkNotNull(requestMethod);
Preconditions.checkNotNull(url);
Span span =
tracer
.spanBuilder(OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE)
.setRecordEvents(OpenCensusUtils.isRecordEvent())
.startSpan();
do {
span.addAnnotation("retry #" + (numRetries - retriesRemaining));
// Cleanup any unneeded response from a previous iteration
if (response != null) {
response.ignore();
}
response = null;
executeException = null;
// run the interceptor
if (executeInterceptor != null) {
executeInterceptor.intercept(this);
}
// build low-level HTTP request
String urlString = url.build();
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_METHOD, requestMethod);
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_HOST, url.getHost());
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_PATH, url.getRawPath());
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_URL, urlString);
LowLevelHttpRequest lowLevelHttpRequest = transport.buildRequest(requestMethod, urlString);
Logger logger = HttpTransport.LOGGER;
boolean loggable = loggingEnabled && logger.isLoggable(Level.CONFIG);
StringBuilder logbuf = null;
StringBuilder curlbuf = null;
// log method and URL
if (loggable) {
logbuf = new StringBuilder();
logbuf.append("-------------- REQUEST --------------").append(StringUtils.LINE_SEPARATOR);
logbuf
.append(requestMethod)
.append(' ')
.append(urlString)
.append(StringUtils.LINE_SEPARATOR);
// setup curl logging
if (curlLoggingEnabled) {
curlbuf = new StringBuilder("curl -v --compressed");
if (!requestMethod.equals(HttpMethods.GET)) {
curlbuf.append(" -X ").append(requestMethod);
}
}
}
// add to user agent
String originalUserAgent = headers.getUserAgent();
if (!suppressUserAgentSuffix) {
if (originalUserAgent == null) {
headers.setUserAgent(USER_AGENT_SUFFIX);
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_USER_AGENT, USER_AGENT_SUFFIX);
} else {
String newUserAgent = originalUserAgent + " " + USER_AGENT_SUFFIX;
headers.setUserAgent(newUserAgent);
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_USER_AGENT, newUserAgent);
}
}
OpenCensusUtils.propagateTracingContext(span, headers);
// headers
HttpHeaders.serializeHeaders(headers, logbuf, curlbuf, logger, lowLevelHttpRequest);
if (!suppressUserAgentSuffix) {
// set the original user agent back so that retries do not keep appending to it
headers.setUserAgent(originalUserAgent);
}
// content
StreamingContent streamingContent = content;
final boolean contentRetrySupported = streamingContent == null || content.retrySupported();
if (streamingContent != null) {
final String contentEncoding;
long contentLength = -1;
final String contentType = content.getType();
// log content
if (loggable) {
streamingContent =
new LoggingStreamingContent(
streamingContent, HttpTransport.LOGGER, Level.CONFIG, contentLoggingLimit);
}
// encoding
if (encoding == null) {
contentEncoding = null;
contentLength = content.getLength();
} else {
contentEncoding = encoding.getName();
streamingContent = new HttpEncodingStreamingContent(streamingContent, encoding);
}
// append content headers to log buffer
if (loggable) {
if (contentType != null) {
String header = "Content-Type: " + contentType;
logbuf.append(header).append(StringUtils.LINE_SEPARATOR);
if (curlbuf != null) {
curlbuf.append(" -H '" + header + "'");
}
}
if (contentEncoding != null) {
String header = "Content-Encoding: " + contentEncoding;
logbuf.append(header).append(StringUtils.LINE_SEPARATOR);
if (curlbuf != null) {
curlbuf.append(" -H '" + header + "'");
}
}
if (contentLength >= 0) {
String header = "Content-Length: " + contentLength;
logbuf.append(header).append(StringUtils.LINE_SEPARATOR);
// do not log @ curl as the user will most likely manipulate the content
}
}
if (curlbuf != null) {
curlbuf.append(" -d '@-'");
}
// send content information to low-level HTTP request
lowLevelHttpRequest.setContentType(contentType);
lowLevelHttpRequest.setContentEncoding(contentEncoding);
lowLevelHttpRequest.setContentLength(contentLength);
lowLevelHttpRequest.setStreamingContent(streamingContent);
}
// log from buffer
if (loggable) {
logger.config(logbuf.toString());
if (curlbuf != null) {
curlbuf.append(" -- '");
curlbuf.append(urlString.replaceAll("\'", "'\"'\"'"));
curlbuf.append("'");
if (streamingContent != null) {
curlbuf.append(" << $$$");
}
logger.config(curlbuf.toString());
}
}
// We need to make sure our content type can support retry
// null content is inherently able to be retried