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 package org.apache.hc.client5.http.config;
29
30 import java.util.Collection;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.http.HttpHost;
36 import org.apache.hc.core5.util.TimeValue;
37 import org.apache.hc.core5.util.Timeout;
38
39
40
41
42 @Contract(threading = ThreadingBehavior.IMMUTABLE)
43 public class RequestConfig implements Cloneable {
44
45 private static final Timeout DEFAULT_CONNECTION_REQUEST_TIMEOUT = Timeout.ofMinutes(3);
46 private static final TimeValue DEFAULT_CONN_KEEP_ALIVE = TimeValue.ofMinutes(3);
47
48 public static final RequestConfig DEFAULT = new Builder().build();
49
50 private final boolean expectContinueEnabled;
51 private final HttpHost proxy;
52 private final String cookieSpec;
53 private final boolean redirectsEnabled;
54 private final boolean circularRedirectsAllowed;
55 private final int maxRedirects;
56 private final boolean authenticationEnabled;
57 private final Collection<String> targetPreferredAuthSchemes;
58 private final Collection<String> proxyPreferredAuthSchemes;
59 private final Timeout connectionRequestTimeout;
60 private final Timeout connectTimeout;
61 private final Timeout responseTimeout;
62 private final TimeValue connectionKeepAlive;
63 private final boolean contentCompressionEnabled;
64 private final boolean hardCancellationEnabled;
65
66
67
68
69 protected RequestConfig() {
70 this(false, null, null, false, false, 0, false, null, null,
71 DEFAULT_CONNECTION_REQUEST_TIMEOUT, null, null, DEFAULT_CONN_KEEP_ALIVE, false, false);
72 }
73
74 RequestConfig(
75 final boolean expectContinueEnabled,
76 final HttpHost proxy,
77 final String cookieSpec,
78 final boolean redirectsEnabled,
79 final boolean circularRedirectsAllowed,
80 final int maxRedirects,
81 final boolean authenticationEnabled,
82 final Collection<String> targetPreferredAuthSchemes,
83 final Collection<String> proxyPreferredAuthSchemes,
84 final Timeout connectionRequestTimeout,
85 final Timeout connectTimeout,
86 final Timeout responseTimeout,
87 final TimeValue connectionKeepAlive,
88 final boolean contentCompressionEnabled,
89 final boolean hardCancellationEnabled) {
90 super();
91 this.expectContinueEnabled = expectContinueEnabled;
92 this.proxy = proxy;
93 this.cookieSpec = cookieSpec;
94 this.redirectsEnabled = redirectsEnabled;
95 this.circularRedirectsAllowed = circularRedirectsAllowed;
96 this.maxRedirects = maxRedirects;
97 this.authenticationEnabled = authenticationEnabled;
98 this.targetPreferredAuthSchemes = targetPreferredAuthSchemes;
99 this.proxyPreferredAuthSchemes = proxyPreferredAuthSchemes;
100 this.connectionRequestTimeout = connectionRequestTimeout;
101 this.connectTimeout = connectTimeout;
102 this.responseTimeout = responseTimeout;
103 this.connectionKeepAlive = connectionKeepAlive;
104 this.contentCompressionEnabled = contentCompressionEnabled;
105 this.hardCancellationEnabled = hardCancellationEnabled;
106 }
107
108
109
110
111 public boolean isExpectContinueEnabled() {
112 return expectContinueEnabled;
113 }
114
115
116
117
118
119
120
121 @Deprecated
122 public HttpHost getProxy() {
123 return proxy;
124 }
125
126
127
128
129 public String getCookieSpec() {
130 return cookieSpec;
131 }
132
133
134
135
136 public boolean isRedirectsEnabled() {
137 return redirectsEnabled;
138 }
139
140
141
142
143 public boolean isCircularRedirectsAllowed() {
144 return circularRedirectsAllowed;
145 }
146
147
148
149
150 public int getMaxRedirects() {
151 return maxRedirects;
152 }
153
154
155
156
157 public boolean isAuthenticationEnabled() {
158 return authenticationEnabled;
159 }
160
161
162
163
164 public Collection<String> getTargetPreferredAuthSchemes() {
165 return targetPreferredAuthSchemes;
166 }
167
168
169
170
171 public Collection<String> getProxyPreferredAuthSchemes() {
172 return proxyPreferredAuthSchemes;
173 }
174
175
176
177
178 public Timeout getConnectionRequestTimeout() {
179 return connectionRequestTimeout;
180 }
181
182
183
184
185
186
187 @Deprecated
188 public Timeout getConnectTimeout() {
189 return connectTimeout;
190 }
191
192
193
194
195 public Timeout getResponseTimeout() {
196 return responseTimeout;
197 }
198
199
200
201
202 public TimeValue getConnectionKeepAlive() {
203 return connectionKeepAlive;
204 }
205
206
207
208
209 public boolean isContentCompressionEnabled() {
210 return contentCompressionEnabled;
211 }
212
213
214
215
216 public boolean isHardCancellationEnabled() {
217 return hardCancellationEnabled;
218 }
219
220 @Override
221 protected RequestConfig clone() throws CloneNotSupportedException {
222 return (RequestConfig) super.clone();
223 }
224
225 @Override
226 public String toString() {
227 final StringBuilder builder = new StringBuilder();
228 builder.append("[");
229 builder.append("expectContinueEnabled=").append(expectContinueEnabled);
230 builder.append(", proxy=").append(proxy);
231 builder.append(", cookieSpec=").append(cookieSpec);
232 builder.append(", redirectsEnabled=").append(redirectsEnabled);
233 builder.append(", maxRedirects=").append(maxRedirects);
234 builder.append(", circularRedirectsAllowed=").append(circularRedirectsAllowed);
235 builder.append(", authenticationEnabled=").append(authenticationEnabled);
236 builder.append(", targetPreferredAuthSchemes=").append(targetPreferredAuthSchemes);
237 builder.append(", proxyPreferredAuthSchemes=").append(proxyPreferredAuthSchemes);
238 builder.append(", connectionRequestTimeout=").append(connectionRequestTimeout);
239 builder.append(", connectTimeout=").append(connectTimeout);
240 builder.append(", responseTimeout=").append(responseTimeout);
241 builder.append(", connectionKeepAlive=").append(connectionKeepAlive);
242 builder.append(", contentCompressionEnabled=").append(contentCompressionEnabled);
243 builder.append(", hardCancellationEnabled=").append(hardCancellationEnabled);
244 builder.append("]");
245 return builder.toString();
246 }
247
248 public static RequestConfig.Builder custom() {
249 return new Builder();
250 }
251 @SuppressWarnings("deprecation")
252 public static RequestConfig.Builder copy(final RequestConfig config) {
253 return new Builder()
254 .setExpectContinueEnabled(config.isExpectContinueEnabled())
255 .setProxy(config.getProxy())
256 .setCookieSpec(config.getCookieSpec())
257 .setRedirectsEnabled(config.isRedirectsEnabled())
258 .setCircularRedirectsAllowed(config.isCircularRedirectsAllowed())
259 .setMaxRedirects(config.getMaxRedirects())
260 .setAuthenticationEnabled(config.isAuthenticationEnabled())
261 .setTargetPreferredAuthSchemes(config.getTargetPreferredAuthSchemes())
262 .setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
263 .setConnectionRequestTimeout(config.getConnectionRequestTimeout())
264 .setConnectTimeout(config.getConnectTimeout())
265 .setResponseTimeout(config.getResponseTimeout())
266 .setConnectionKeepAlive(config.getConnectionKeepAlive())
267 .setContentCompressionEnabled(config.isContentCompressionEnabled())
268 .setHardCancellationEnabled(config.isHardCancellationEnabled());
269 }
270
271 public static class Builder {
272
273 private boolean expectContinueEnabled;
274 private HttpHost proxy;
275 private String cookieSpec;
276 private boolean redirectsEnabled;
277 private boolean circularRedirectsAllowed;
278 private int maxRedirects;
279 private boolean authenticationEnabled;
280 private Collection<String> targetPreferredAuthSchemes;
281 private Collection<String> proxyPreferredAuthSchemes;
282 private Timeout connectionRequestTimeout;
283 private Timeout connectTimeout;
284 private Timeout responseTimeout;
285 private TimeValue connectionKeepAlive;
286 private boolean contentCompressionEnabled;
287 private boolean hardCancellationEnabled;
288
289 Builder() {
290 super();
291 this.redirectsEnabled = true;
292 this.maxRedirects = 50;
293 this.authenticationEnabled = true;
294 this.connectionRequestTimeout = DEFAULT_CONNECTION_REQUEST_TIMEOUT;
295 this.contentCompressionEnabled = true;
296 this.hardCancellationEnabled = true;
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 public Builder setExpectContinueEnabled(final boolean expectContinueEnabled) {
322 this.expectContinueEnabled = expectContinueEnabled;
323 return this;
324 }
325
326
327
328
329
330
331
332
333
334
335 @Deprecated
336 public Builder setProxy(final HttpHost proxy) {
337 this.proxy = proxy;
338 return this;
339 }
340
341
342
343
344
345
346
347
348 public Builder setCookieSpec(final String cookieSpec) {
349 this.cookieSpec = cookieSpec;
350 return this;
351 }
352
353
354
355
356
357
358
359 public Builder setRedirectsEnabled(final boolean redirectsEnabled) {
360 this.redirectsEnabled = redirectsEnabled;
361 return this;
362 }
363
364
365
366
367
368
369
370
371
372 public Builder setCircularRedirectsAllowed(final boolean circularRedirectsAllowed) {
373 this.circularRedirectsAllowed = circularRedirectsAllowed;
374 return this;
375 }
376
377
378
379
380
381
382
383
384 public Builder setMaxRedirects(final int maxRedirects) {
385 this.maxRedirects = maxRedirects;
386 return this;
387 }
388
389
390
391
392
393
394
395 public Builder setAuthenticationEnabled(final boolean authenticationEnabled) {
396 this.authenticationEnabled = authenticationEnabled;
397 return this;
398 }
399
400
401
402
403
404
405
406
407 public Builder setTargetPreferredAuthSchemes(final Collection<String> targetPreferredAuthSchemes) {
408 this.targetPreferredAuthSchemes = targetPreferredAuthSchemes;
409 return this;
410 }
411
412
413
414
415
416
417
418
419 public Builder setProxyPreferredAuthSchemes(final Collection<String> proxyPreferredAuthSchemes) {
420 this.proxyPreferredAuthSchemes = proxyPreferredAuthSchemes;
421 return this;
422 }
423
424
425
426
427
428
429
430 public Builder setConnectionRequestTimeout(final Timeout connectionRequestTimeout) {
431 this.connectionRequestTimeout = connectionRequestTimeout;
432 return this;
433 }
434
435
436
437
438 public Builder setConnectionRequestTimeout(final long connectionRequestTimeout, final TimeUnit timeUnit) {
439 this.connectionRequestTimeout = Timeout.of(connectionRequestTimeout, timeUnit);
440 return this;
441 }
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456 @Deprecated
457 public Builder setConnectTimeout(final Timeout connectTimeout) {
458 this.connectTimeout = connectTimeout;
459 return this;
460 }
461
462
463
464
465
466
467 @Deprecated
468 public Builder setConnectTimeout(final long connectTimeout, final TimeUnit timeUnit) {
469 this.connectTimeout = Timeout.of(connectTimeout, timeUnit);
470 return this;
471 }
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489 public Builder setResponseTimeout(final Timeout responseTimeout) {
490 this.responseTimeout = responseTimeout;
491 return this;
492 }
493
494
495
496
497 public Builder setResponseTimeout(final long responseTimeout, final TimeUnit timeUnit) {
498 this.responseTimeout = Timeout.of(responseTimeout, timeUnit);
499 return this;
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515 public Builder setConnectionKeepAlive(final TimeValue connectionKeepAlive) {
516 this.connectionKeepAlive = connectionKeepAlive;
517 return this;
518 }
519
520
521
522
523 public Builder setDefaultKeepAlive(final long defaultKeepAlive, final TimeUnit timeUnit) {
524 this.connectionKeepAlive = TimeValue.of(defaultKeepAlive, timeUnit);
525 return this;
526 }
527
528
529
530
531
532
533
534
535
536 public Builder setContentCompressionEnabled(final boolean contentCompressionEnabled) {
537 this.contentCompressionEnabled = contentCompressionEnabled;
538 return this;
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 public Builder setHardCancellationEnabled(final boolean hardCancellationEnabled) {
569 this.hardCancellationEnabled = hardCancellationEnabled;
570 return this;
571 }
572
573 public RequestConfig build() {
574 return new RequestConfig(
575 expectContinueEnabled,
576 proxy,
577 cookieSpec,
578 redirectsEnabled,
579 circularRedirectsAllowed,
580 maxRedirects,
581 authenticationEnabled,
582 targetPreferredAuthSchemes,
583 proxyPreferredAuthSchemes,
584 connectionRequestTimeout != null ? connectionRequestTimeout : DEFAULT_CONNECTION_REQUEST_TIMEOUT,
585 connectTimeout,
586 responseTimeout,
587 connectionKeepAlive != null ? connectionKeepAlive : DEFAULT_CONN_KEEP_ALIVE,
588 contentCompressionEnabled,
589 hardCancellationEnabled);
590 }
591
592 }
593
594 }