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.http.impl.client;
29
30 import java.io.Closeable;
31 import java.io.IOException;
32 import java.net.ProxySelector;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.concurrent.TimeUnit;
40
41 import javax.net.ssl.HostnameVerifier;
42 import javax.net.ssl.SSLContext;
43 import javax.net.ssl.SSLSocketFactory;
44
45 import org.apache.http.ConnectionReuseStrategy;
46 import org.apache.http.Header;
47 import org.apache.http.HttpHost;
48 import org.apache.http.HttpRequestInterceptor;
49 import org.apache.http.HttpResponseInterceptor;
50 import org.apache.http.auth.AuthSchemeProvider;
51 import org.apache.http.client.AuthenticationStrategy;
52 import org.apache.http.client.BackoffManager;
53 import org.apache.http.client.ConnectionBackoffStrategy;
54 import org.apache.http.client.CookieStore;
55 import org.apache.http.client.CredentialsProvider;
56 import org.apache.http.client.HttpRequestRetryHandler;
57 import org.apache.http.client.RedirectStrategy;
58 import org.apache.http.client.ServiceUnavailableRetryStrategy;
59 import org.apache.http.client.UserTokenHandler;
60 import org.apache.http.client.config.AuthSchemes;
61 import org.apache.http.client.config.RequestConfig;
62 import org.apache.http.client.entity.InputStreamFactory;
63 import org.apache.http.client.protocol.RequestAcceptEncoding;
64 import org.apache.http.client.protocol.RequestAddCookies;
65 import org.apache.http.client.protocol.RequestAuthCache;
66 import org.apache.http.client.protocol.RequestClientConnControl;
67 import org.apache.http.client.protocol.RequestDefaultHeaders;
68 import org.apache.http.client.protocol.RequestExpectContinue;
69 import org.apache.http.client.protocol.ResponseContentEncoding;
70 import org.apache.http.client.protocol.ResponseProcessCookies;
71 import org.apache.http.config.ConnectionConfig;
72 import org.apache.http.config.Lookup;
73 import org.apache.http.config.RegistryBuilder;
74 import org.apache.http.config.SocketConfig;
75 import org.apache.http.conn.ConnectionKeepAliveStrategy;
76 import org.apache.http.conn.DnsResolver;
77 import org.apache.http.conn.HttpClientConnectionManager;
78 import org.apache.http.conn.SchemePortResolver;
79 import org.apache.http.conn.routing.HttpRoutePlanner;
80 import org.apache.http.conn.socket.ConnectionSocketFactory;
81 import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
82 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
83 import org.apache.http.conn.ssl.DefaultHostnameVerifier;
84 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
85 import org.apache.http.conn.ssl.X509HostnameVerifier;
86 import org.apache.http.conn.util.PublicSuffixMatcher;
87 import org.apache.http.conn.util.PublicSuffixMatcherLoader;
88 import org.apache.http.cookie.CookieSpecProvider;
89 import org.apache.http.impl.NoConnectionReuseStrategy;
90 import org.apache.http.impl.auth.BasicSchemeFactory;
91 import org.apache.http.impl.auth.DigestSchemeFactory;
92 import org.apache.http.impl.auth.KerberosSchemeFactory;
93 import org.apache.http.impl.auth.NTLMSchemeFactory;
94 import org.apache.http.impl.auth.SPNegoSchemeFactory;
95 import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
96 import org.apache.http.impl.conn.DefaultRoutePlanner;
97 import org.apache.http.impl.conn.DefaultSchemePortResolver;
98 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
99 import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
100 import org.apache.http.impl.execchain.BackoffStrategyExec;
101 import org.apache.http.impl.execchain.ClientExecChain;
102 import org.apache.http.impl.execchain.MainClientExec;
103 import org.apache.http.impl.execchain.ProtocolExec;
104 import org.apache.http.impl.execchain.RedirectExec;
105 import org.apache.http.impl.execchain.RetryExec;
106 import org.apache.http.impl.execchain.ServiceUnavailableRetryExec;
107 import org.apache.http.protocol.HttpProcessor;
108 import org.apache.http.protocol.HttpProcessorBuilder;
109 import org.apache.http.protocol.HttpRequestExecutor;
110 import org.apache.http.protocol.ImmutableHttpProcessor;
111 import org.apache.http.protocol.RequestContent;
112 import org.apache.http.protocol.RequestTargetHost;
113 import org.apache.http.protocol.RequestUserAgent;
114 import org.apache.http.ssl.SSLContexts;
115 import org.apache.http.util.TextUtils;
116 import org.apache.http.util.VersionInfo;
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 public class HttpClientBuilder {
158
159 private HttpRequestExecutor requestExec;
160 private HostnameVerifier hostnameVerifier;
161 private LayeredConnectionSocketFactory sslSocketFactory;
162 private SSLContext sslContext;
163 private HttpClientConnectionManager connManager;
164 private boolean connManagerShared;
165 private SchemePortResolver schemePortResolver;
166 private ConnectionReuseStrategy reuseStrategy;
167 private ConnectionKeepAliveStrategy keepAliveStrategy;
168 private AuthenticationStrategy targetAuthStrategy;
169 private AuthenticationStrategy proxyAuthStrategy;
170 private UserTokenHandler userTokenHandler;
171 private HttpProcessor httpprocessor;
172 private DnsResolver dnsResolver;
173
174 private LinkedList<HttpRequestInterceptor> requestFirst;
175 private LinkedList<HttpRequestInterceptor> requestLast;
176 private LinkedList<HttpResponseInterceptor> responseFirst;
177 private LinkedList<HttpResponseInterceptor> responseLast;
178
179 private HttpRequestRetryHandler retryHandler;
180 private HttpRoutePlanner routePlanner;
181 private RedirectStrategy redirectStrategy;
182 private ConnectionBackoffStrategy connectionBackoffStrategy;
183 private BackoffManager backoffManager;
184 private ServiceUnavailableRetryStrategy serviceUnavailStrategy;
185 private Lookup<AuthSchemeProvider> authSchemeRegistry;
186 private Lookup<CookieSpecProvider> cookieSpecRegistry;
187 private Map<String, InputStreamFactory> contentDecoderMap;
188 private CookieStore cookieStore;
189 private CredentialsProvider credentialsProvider;
190 private String userAgent;
191 private HttpHost proxy;
192 private Collection<? extends Header> defaultHeaders;
193 private SocketConfig defaultSocketConfig;
194 private ConnectionConfig defaultConnectionConfig;
195 private RequestConfig defaultRequestConfig;
196 private boolean evictExpiredConnections;
197 private boolean evictIdleConnections;
198 private long maxIdleTime;
199 private TimeUnit maxIdleTimeUnit;
200
201 private boolean systemProperties;
202 private boolean redirectHandlingDisabled;
203 private boolean automaticRetriesDisabled;
204 private boolean contentCompressionDisabled;
205 private boolean cookieManagementDisabled;
206 private boolean authCachingDisabled;
207 private boolean connectionStateDisabled;
208 private boolean defaultUserAgentDisabled;
209
210 private int maxConnTotal = 0;
211 private int maxConnPerRoute = 0;
212
213 private long connTimeToLive = -1;
214 private TimeUnit connTimeToLiveTimeUnit = TimeUnit.MILLISECONDS;
215
216 private List<Closeable> closeables;
217
218 private PublicSuffixMatcher publicSuffixMatcher;
219
220 public static HttpClientBuilder create() {
221 return new HttpClientBuilder();
222 }
223
224 protected HttpClientBuilder() {
225 super();
226 }
227
228
229
230
231 public final HttpClientBuilder setRequestExecutor(final HttpRequestExecutor requestExec) {
232 this.requestExec = requestExec;
233 return this;
234 }
235
236
237
238
239
240
241
242
243
244
245
246 @Deprecated
247 public final HttpClientBuilder setHostnameVerifier(final X509HostnameVerifier hostnameVerifier) {
248 this.hostnameVerifier = hostnameVerifier;
249 return this;
250 }
251
252
253
254
255
256
257
258
259
260
261
262 public final HttpClientBuilder setSSLHostnameVerifier(final HostnameVerifier hostnameVerifier) {
263 this.hostnameVerifier = hostnameVerifier;
264 return this;
265 }
266
267
268
269
270
271
272
273
274
275
276 public final HttpClientBuilder setPublicSuffixMatcher(final PublicSuffixMatcher publicSuffixMatcher) {
277 this.publicSuffixMatcher = publicSuffixMatcher;
278 return this;
279 }
280
281
282
283
284
285
286
287
288
289
290
291 @Deprecated
292 public final HttpClientBuilder setSslcontext(final SSLContext sslcontext) {
293 return setSSLContext(sslcontext);
294 }
295
296
297
298
299
300
301
302
303
304 public final HttpClientBuilder setSSLContext(final SSLContext sslContext) {
305 this.sslContext = sslContext;
306 return this;
307 }
308
309
310
311
312
313
314
315
316 public final HttpClientBuilder setSSLSocketFactory(
317 final LayeredConnectionSocketFactory sslSocketFactory) {
318 this.sslSocketFactory = sslSocketFactory;
319 return this;
320 }
321
322
323
324
325
326
327
328
329 public final HttpClientBuilder setMaxConnTotal(final int maxConnTotal) {
330 this.maxConnTotal = maxConnTotal;
331 return this;
332 }
333
334
335
336
337
338
339
340
341 public final HttpClientBuilder setMaxConnPerRoute(final int maxConnPerRoute) {
342 this.maxConnPerRoute = maxConnPerRoute;
343 return this;
344 }
345
346
347
348
349
350
351
352
353 public final HttpClientBuilder setDefaultSocketConfig(final SocketConfig config) {
354 this.defaultSocketConfig = config;
355 return this;
356 }
357
358
359
360
361
362
363
364
365 public final HttpClientBuilder setDefaultConnectionConfig(final ConnectionConfig config) {
366 this.defaultConnectionConfig = config;
367 return this;
368 }
369
370
371
372
373
374
375
376
377
378
379 public final HttpClientBuilder setConnectionTimeToLive(final long connTimeToLive, final TimeUnit connTimeToLiveTimeUnit) {
380 this.connTimeToLive = connTimeToLive;
381 this.connTimeToLiveTimeUnit = connTimeToLiveTimeUnit;
382 return this;
383 }
384
385
386
387
388 public final HttpClientBuilder setConnectionManager(
389 final HttpClientConnectionManager connManager) {
390 this.connManager = connManager;
391 return this;
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408 public final HttpClientBuilder setConnectionManagerShared(
409 final boolean shared) {
410 this.connManagerShared = shared;
411 return this;
412 }
413
414
415
416
417 public final HttpClientBuilder setConnectionReuseStrategy(
418 final ConnectionReuseStrategy reuseStrategy) {
419 this.reuseStrategy = reuseStrategy;
420 return this;
421 }
422
423
424
425
426 public final HttpClientBuilder setKeepAliveStrategy(
427 final ConnectionKeepAliveStrategy keepAliveStrategy) {
428 this.keepAliveStrategy = keepAliveStrategy;
429 return this;
430 }
431
432
433
434
435
436 public final HttpClientBuilder setTargetAuthenticationStrategy(
437 final AuthenticationStrategy targetAuthStrategy) {
438 this.targetAuthStrategy = targetAuthStrategy;
439 return this;
440 }
441
442
443
444
445
446 public final HttpClientBuilder setProxyAuthenticationStrategy(
447 final AuthenticationStrategy proxyAuthStrategy) {
448 this.proxyAuthStrategy = proxyAuthStrategy;
449 return this;
450 }
451
452
453
454
455
456
457
458
459 public final HttpClientBuilder setUserTokenHandler(final UserTokenHandler userTokenHandler) {
460 this.userTokenHandler = userTokenHandler;
461 return this;
462 }
463
464
465
466
467 public final HttpClientBuilder disableConnectionState() {
468 connectionStateDisabled = true;
469 return this;
470 }
471
472
473
474
475 public final HttpClientBuilder setSchemePortResolver(
476 final SchemePortResolver schemePortResolver) {
477 this.schemePortResolver = schemePortResolver;
478 return this;
479 }
480
481
482
483
484
485
486
487
488 public final HttpClientBuilder setUserAgent(final String userAgent) {
489 this.userAgent = userAgent;
490 return this;
491 }
492
493
494
495
496
497
498
499
500 public final HttpClientBuilder setDefaultHeaders(final Collection<? extends Header> defaultHeaders) {
501 this.defaultHeaders = defaultHeaders;
502 return this;
503 }
504
505
506
507
508
509
510
511
512 public final HttpClientBuilder addInterceptorFirst(final HttpResponseInterceptor itcp) {
513 if (itcp == null) {
514 return this;
515 }
516 if (responseFirst == null) {
517 responseFirst = new LinkedList<HttpResponseInterceptor>();
518 }
519 responseFirst.addFirst(itcp);
520 return this;
521 }
522
523
524
525
526
527
528
529
530 public final HttpClientBuilder addInterceptorLast(final HttpResponseInterceptor itcp) {
531 if (itcp == null) {
532 return this;
533 }
534 if (responseLast == null) {
535 responseLast = new LinkedList<HttpResponseInterceptor>();
536 }
537 responseLast.addLast(itcp);
538 return this;
539 }
540
541
542
543
544
545
546
547 public final HttpClientBuilder addInterceptorFirst(final HttpRequestInterceptor itcp) {
548 if (itcp == null) {
549 return this;
550 }
551 if (requestFirst == null) {
552 requestFirst = new LinkedList<HttpRequestInterceptor>();
553 }
554 requestFirst.addFirst(itcp);
555 return this;
556 }
557
558
559
560
561
562
563
564 public final HttpClientBuilder addInterceptorLast(final HttpRequestInterceptor itcp) {
565 if (itcp == null) {
566 return this;
567 }
568 if (requestLast == null) {
569 requestLast = new LinkedList<HttpRequestInterceptor>();
570 }
571 requestLast.addLast(itcp);
572 return this;
573 }
574
575
576
577
578
579
580
581 public final HttpClientBuilder disableCookieManagement() {
582 this.cookieManagementDisabled = true;
583 return this;
584 }
585
586
587
588
589
590
591
592 public final HttpClientBuilder disableContentCompression() {
593 contentCompressionDisabled = true;
594 return this;
595 }
596
597
598
599
600
601
602
603 public final HttpClientBuilder disableAuthCaching() {
604 this.authCachingDisabled = true;
605 return this;
606 }
607
608
609
610
611 public final HttpClientBuilder setHttpProcessor(final HttpProcessor httpprocessor) {
612 this.httpprocessor = httpprocessor;
613 return this;
614 }
615
616
617
618
619
620
621 public final HttpClientBuilder setDnsResolver(final DnsResolver dnsResolver) {
622 this.dnsResolver = dnsResolver;
623 return this;
624 }
625
626
627
628
629
630
631
632 public final HttpClientBuilder setRetryHandler(final HttpRequestRetryHandler retryHandler) {
633 this.retryHandler = retryHandler;
634 return this;
635 }
636
637
638
639
640 public final HttpClientBuilder disableAutomaticRetries() {
641 automaticRetriesDisabled = true;
642 return this;
643 }
644
645
646
647
648
649
650
651 public final HttpClientBuilder setProxy(final HttpHost proxy) {
652 this.proxy = proxy;
653 return this;
654 }
655
656
657
658
659 public final HttpClientBuilder setRoutePlanner(final HttpRoutePlanner routePlanner) {
660 this.routePlanner = routePlanner;
661 return this;
662 }
663
664
665
666
667
668
669
670
671 public final HttpClientBuilder setRedirectStrategy(final RedirectStrategy redirectStrategy) {
672 this.redirectStrategy = redirectStrategy;
673 return this;
674 }
675
676
677
678
679 public final HttpClientBuilder disableRedirectHandling() {
680 redirectHandlingDisabled = true;
681 return this;
682 }
683
684
685
686
687 public final HttpClientBuilder setConnectionBackoffStrategy(
688 final ConnectionBackoffStrategy connectionBackoffStrategy) {
689 this.connectionBackoffStrategy = connectionBackoffStrategy;
690 return this;
691 }
692
693
694
695
696 public final HttpClientBuilder setBackoffManager(final BackoffManager backoffManager) {
697 this.backoffManager = backoffManager;
698 return this;
699 }
700
701
702
703
704 public final HttpClientBuilder setServiceUnavailableRetryStrategy(
705 final ServiceUnavailableRetryStrategy serviceUnavailStrategy) {
706 this.serviceUnavailStrategy = serviceUnavailStrategy;
707 return this;
708 }
709
710
711
712
713
714 public final HttpClientBuilder setDefaultCookieStore(final CookieStore cookieStore) {
715 this.cookieStore = cookieStore;
716 return this;
717 }
718
719
720
721
722
723
724 public final HttpClientBuilder setDefaultCredentialsProvider(
725 final CredentialsProvider credentialsProvider) {
726 this.credentialsProvider = credentialsProvider;
727 return this;
728 }
729
730
731
732
733
734
735 public final HttpClientBuilder setDefaultAuthSchemeRegistry(
736 final Lookup<AuthSchemeProvider> authSchemeRegistry) {
737 this.authSchemeRegistry = authSchemeRegistry;
738 return this;
739 }
740
741
742
743
744
745
746
747
748
749 public final HttpClientBuilder setDefaultCookieSpecRegistry(
750 final Lookup<CookieSpecProvider> cookieSpecRegistry) {
751 this.cookieSpecRegistry = cookieSpecRegistry;
752 return this;
753 }
754
755
756
757
758
759
760 public final HttpClientBuilder setContentDecoderRegistry(
761 final Map<String, InputStreamFactory> contentDecoderMap) {
762 this.contentDecoderMap = contentDecoderMap;
763 return this;
764 }
765
766
767
768
769
770
771 public final HttpClientBuilder setDefaultRequestConfig(final RequestConfig config) {
772 this.defaultRequestConfig = config;
773 return this;
774 }
775
776
777
778
779
780 public final HttpClientBuilder useSystemProperties() {
781 this.systemProperties = true;
782 return this;
783 }
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803 public final HttpClientBuilder evictExpiredConnections() {
804 evictExpiredConnections = true;
805 return this;
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 @Deprecated
834 public final HttpClientBuilder evictIdleConnections(final Long maxIdleTime, final TimeUnit maxIdleTimeUnit) {
835 return evictIdleConnections(maxIdleTime.longValue(), maxIdleTimeUnit);
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 public final HttpClientBuilder evictIdleConnections(final long maxIdleTime, final TimeUnit maxIdleTimeUnit) {
862 this.evictIdleConnections = true;
863 this.maxIdleTime = maxIdleTime;
864 this.maxIdleTimeUnit = maxIdleTimeUnit;
865 return this;
866 }
867
868
869
870
871
872
873 public final HttpClientBuilder disableDefaultUserAgent() {
874 this.defaultUserAgentDisabled = true;
875 return this;
876 }
877
878
879
880
881
882
883
884
885
886
887
888
889 protected ClientExecChain createMainExec(
890 final HttpRequestExecutor requestExec,
891 final HttpClientConnectionManager connManager,
892 final ConnectionReuseStrategy reuseStrategy,
893 final ConnectionKeepAliveStrategy keepAliveStrategy,
894 final HttpProcessor proxyHttpProcessor,
895 final AuthenticationStrategy targetAuthStrategy,
896 final AuthenticationStrategy proxyAuthStrategy,
897 final UserTokenHandler userTokenHandler)
898 {
899 return new MainClientExec(
900 requestExec,
901 connManager,
902 reuseStrategy,
903 keepAliveStrategy,
904 proxyHttpProcessor,
905 targetAuthStrategy,
906 proxyAuthStrategy,
907 userTokenHandler);
908 }
909
910
911
912
913 protected ClientExecChainin.html#ClientExecChain">ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
914 return mainExec;
915 }
916
917
918
919
920 protected ClientExecChaintml#ClientExecChain">ClientExecChain decorateProtocolExec(final ClientExecChain protocolExec) {
921 return protocolExec;
922 }
923
924
925
926
927 protected void addCloseable(final Closeable closeable) {
928 if (closeable == null) {
929 return;
930 }
931 if (closeables == null) {
932 closeables = new ArrayList<Closeable>();
933 }
934 closeables.add(closeable);
935 }
936
937 private static String[] split(final String s) {
938 if (TextUtils.isBlank(s)) {
939 return null;
940 }
941 return s.split(" *, *");
942 }
943
944 public CloseableHttpClient build() {
945
946
947 PublicSuffixMatcher publicSuffixMatcherCopy = this.publicSuffixMatcher;
948 if (publicSuffixMatcherCopy == null) {
949 publicSuffixMatcherCopy = PublicSuffixMatcherLoader.getDefault();
950 }
951
952 HttpRequestExecutor requestExecCopy = this.requestExec;
953 if (requestExecCopy == null) {
954 requestExecCopy = new HttpRequestExecutor();
955 }
956 HttpClientConnectionManager connManagerCopy = this.connManager;
957 if (connManagerCopy == null) {
958 LayeredConnectionSocketFactory sslSocketFactoryCopy = this.sslSocketFactory;
959 if (sslSocketFactoryCopy == null) {
960 final String[] supportedProtocols = systemProperties ? split(
961 System.getProperty("https.protocols")) : null;
962 final String[] supportedCipherSuites = systemProperties ? split(
963 System.getProperty("https.cipherSuites")) : null;
964 HostnameVerifier hostnameVerifierCopy = this.hostnameVerifier;
965 if (hostnameVerifierCopy == null) {
966 hostnameVerifierCopy = new DefaultHostnameVerifier(publicSuffixMatcherCopy);
967 }
968 if (sslContext != null) {
969 sslSocketFactoryCopy = new SSLConnectionSocketFactory(
970 sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifierCopy);
971 } else {
972 if (systemProperties) {
973 sslSocketFactoryCopy = new SSLConnectionSocketFactory(
974 (SSLSocketFactory) SSLSocketFactory.getDefault(),
975 supportedProtocols, supportedCipherSuites, hostnameVerifierCopy);
976 } else {
977 sslSocketFactoryCopy = new SSLConnectionSocketFactory(
978 SSLContexts.createDefault(),
979 hostnameVerifierCopy);
980 }
981 }
982 }
983 @SuppressWarnings("resource")
984 final PoolingHttpClientConnectionManagerger.html#PoolingHttpClientConnectionManager">PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
985 RegistryBuilder.<ConnectionSocketFactory>create()
986 .register("http", PlainConnectionSocketFactory.getSocketFactory())
987 .register("https", sslSocketFactoryCopy)
988 .build(),
989 null,
990 null,
991 dnsResolver,
992 connTimeToLive,
993 connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
994 if (defaultSocketConfig != null) {
995 poolingmgr.setDefaultSocketConfig(defaultSocketConfig);
996 }
997 if (defaultConnectionConfig != null) {
998 poolingmgr.setDefaultConnectionConfig(defaultConnectionConfig);
999 }
1000 if (systemProperties) {
1001 String s = System.getProperty("http.keepAlive", "true");
1002 if ("true".equalsIgnoreCase(s)) {
1003 s = System.getProperty("http.maxConnections", "5");
1004 final int max = Integer.parseInt(s);
1005 poolingmgr.setDefaultMaxPerRoute(max);
1006 poolingmgr.setMaxTotal(2 * max);
1007 }
1008 }
1009 if (maxConnTotal > 0) {
1010 poolingmgr.setMaxTotal(maxConnTotal);
1011 }
1012 if (maxConnPerRoute > 0) {
1013 poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
1014 }
1015 connManagerCopy = poolingmgr;
1016 }
1017 ConnectionReuseStrategy reuseStrategyCopy = this.reuseStrategy;
1018 if (reuseStrategyCopy == null) {
1019 if (systemProperties) {
1020 final String s = System.getProperty("http.keepAlive", "true");
1021 if ("true".equalsIgnoreCase(s)) {
1022 reuseStrategyCopy = DefaultClientConnectionReuseStrategy.INSTANCE;
1023 } else {
1024 reuseStrategyCopy = NoConnectionReuseStrategy.INSTANCE;
1025 }
1026 } else {
1027 reuseStrategyCopy = DefaultClientConnectionReuseStrategy.INSTANCE;
1028 }
1029 }
1030 ConnectionKeepAliveStrategy keepAliveStrategyCopy = this.keepAliveStrategy;
1031 if (keepAliveStrategyCopy == null) {
1032 keepAliveStrategyCopy = DefaultConnectionKeepAliveStrategy.INSTANCE;
1033 }
1034 AuthenticationStrategy targetAuthStrategyCopy = this.targetAuthStrategy;
1035 if (targetAuthStrategyCopy == null) {
1036 targetAuthStrategyCopy = TargetAuthenticationStrategy.INSTANCE;
1037 }
1038 AuthenticationStrategy proxyAuthStrategyCopy = this.proxyAuthStrategy;
1039 if (proxyAuthStrategyCopy == null) {
1040 proxyAuthStrategyCopy = ProxyAuthenticationStrategy.INSTANCE;
1041 }
1042 UserTokenHandler userTokenHandlerCopy = this.userTokenHandler;
1043 if (userTokenHandlerCopy == null) {
1044 if (!connectionStateDisabled) {
1045 userTokenHandlerCopy = DefaultUserTokenHandler.INSTANCE;
1046 } else {
1047 userTokenHandlerCopy = NoopUserTokenHandler.INSTANCE;
1048 }
1049 }
1050
1051 String userAgentCopy = this.userAgent;
1052 if (userAgentCopy == null) {
1053 if (systemProperties) {
1054 userAgentCopy = System.getProperty("http.agent");
1055 }
1056 if (userAgentCopy == null && !defaultUserAgentDisabled) {
1057 userAgentCopy = VersionInfo.getUserAgent("Apache-HttpClient",
1058 "org.apache.http.client", getClass());
1059 }
1060 }
1061
1062 ClientExecChain execChain = createMainExec(
1063 requestExecCopy,
1064 connManagerCopy,
1065 reuseStrategyCopy,
1066 keepAliveStrategyCopy,
1067 new ImmutableHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)),
1068 targetAuthStrategyCopy,
1069 proxyAuthStrategyCopy,
1070 userTokenHandlerCopy);
1071
1072 execChain = decorateMainExec(execChain);
1073
1074 HttpProcessor httpprocessorCopy = this.httpprocessor;
1075 if (httpprocessorCopy == null) {
1076
1077 final HttpProcessorBuilder b = HttpProcessorBuilder.create();
1078 if (requestFirst != null) {
1079 for (final HttpRequestInterceptor i: requestFirst) {
1080 b.addFirst(i);
1081 }
1082 }
1083 if (responseFirst != null) {
1084 for (final HttpResponseInterceptor i: responseFirst) {
1085 b.addFirst(i);
1086 }
1087 }
1088 b.addAll(
1089 new RequestDefaultHeaders(defaultHeaders),
1090 new RequestContent(),
1091 new RequestTargetHost(),
1092 new RequestClientConnControl(),
1093 new RequestUserAgent(userAgentCopy),
1094 new RequestExpectContinue());
1095 if (!cookieManagementDisabled) {
1096 b.add(new RequestAddCookies());
1097 }
1098 if (!contentCompressionDisabled) {
1099 if (contentDecoderMap != null) {
1100 final List<String> encodings = new ArrayList<String>(contentDecoderMap.keySet());
1101 Collections.sort(encodings);
1102 b.add(new RequestAcceptEncoding(encodings));
1103 } else {
1104 b.add(new RequestAcceptEncoding());
1105 }
1106 }
1107 if (!authCachingDisabled) {
1108 b.add(new RequestAuthCache());
1109 }
1110 if (!cookieManagementDisabled) {
1111 b.add(new ResponseProcessCookies());
1112 }
1113 if (!contentCompressionDisabled) {
1114 if (contentDecoderMap != null) {
1115 final RegistryBuilder<InputStreamFactory> b2 = RegistryBuilder.create();
1116 for (final Map.Entry<String, InputStreamFactory> entry: contentDecoderMap.entrySet()) {
1117 b2.register(entry.getKey(), entry.getValue());
1118 }
1119 b.add(new ResponseContentEncoding(b2.build()));
1120 } else {
1121 b.add(new ResponseContentEncoding());
1122 }
1123 }
1124 if (requestLast != null) {
1125 for (final HttpRequestInterceptor i: requestLast) {
1126 b.addLast(i);
1127 }
1128 }
1129 if (responseLast != null) {
1130 for (final HttpResponseInterceptor i: responseLast) {
1131 b.addLast(i);
1132 }
1133 }
1134 httpprocessorCopy = b.build();
1135 }
1136 execChain = new ProtocolExec(execChain, httpprocessorCopy);
1137
1138 execChain = decorateProtocolExec(execChain);
1139
1140
1141 if (!automaticRetriesDisabled) {
1142 HttpRequestRetryHandler retryHandlerCopy = this.retryHandler;
1143 if (retryHandlerCopy == null) {
1144 retryHandlerCopy = DefaultHttpRequestRetryHandler.INSTANCE;
1145 }
1146 execChain = new RetryExec(execChain, retryHandlerCopy);
1147 }
1148
1149 HttpRoutePlanner routePlannerCopy = this.routePlanner;
1150 if (routePlannerCopy == null) {
1151 SchemePortResolver schemePortResolverCopy = this.schemePortResolver;
1152 if (schemePortResolverCopy == null) {
1153 schemePortResolverCopy = DefaultSchemePortResolver.INSTANCE;
1154 }
1155 if (proxy != null) {
1156 routePlannerCopy = new DefaultProxyRoutePlanner(proxy, schemePortResolverCopy);
1157 } else if (systemProperties) {
1158 routePlannerCopy = new SystemDefaultRoutePlanner(
1159 schemePortResolverCopy, ProxySelector.getDefault());
1160 } else {
1161 routePlannerCopy = new DefaultRoutePlanner(schemePortResolverCopy);
1162 }
1163 }
1164
1165
1166 final ServiceUnavailableRetryStrategy serviceUnavailStrategyCopy = this.serviceUnavailStrategy;
1167 if (serviceUnavailStrategyCopy != null) {
1168 execChain = new ServiceUnavailableRetryExec(execChain, serviceUnavailStrategyCopy);
1169 }
1170
1171
1172 if (!redirectHandlingDisabled) {
1173 RedirectStrategy redirectStrategyCopy = this.redirectStrategy;
1174 if (redirectStrategyCopy == null) {
1175 redirectStrategyCopy = DefaultRedirectStrategy.INSTANCE;
1176 }
1177 execChain = new RedirectExec(execChain, routePlannerCopy, redirectStrategyCopy);
1178 }
1179
1180
1181 if (this.backoffManager != null && this.connectionBackoffStrategy != null) {
1182 execChain = new BackoffStrategyExec(execChain, this.connectionBackoffStrategy, this.backoffManager);
1183 }
1184
1185 Lookup<AuthSchemeProvider> authSchemeRegistryCopy = this.authSchemeRegistry;
1186 if (authSchemeRegistryCopy == null) {
1187 authSchemeRegistryCopy = RegistryBuilder.<AuthSchemeProvider>create()
1188 .register(AuthSchemes.BASIC, new BasicSchemeFactory())
1189 .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
1190 .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
1191 .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
1192 .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
1193 .build();
1194 }
1195 Lookup<CookieSpecProvider> cookieSpecRegistryCopy = this.cookieSpecRegistry;
1196 if (cookieSpecRegistryCopy == null) {
1197 cookieSpecRegistryCopy = CookieSpecRegistries.createDefault(publicSuffixMatcherCopy);
1198 }
1199
1200 CookieStore defaultCookieStore = this.cookieStore;
1201 if (defaultCookieStore == null) {
1202 defaultCookieStore = new BasicCookieStore();
1203 }
1204
1205 CredentialsProvider defaultCredentialsProvider = this.credentialsProvider;
1206 if (defaultCredentialsProvider == null) {
1207 if (systemProperties) {
1208 defaultCredentialsProvider = new SystemDefaultCredentialsProvider();
1209 } else {
1210 defaultCredentialsProvider = new BasicCredentialsProvider();
1211 }
1212 }
1213
1214 List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null;
1215 if (!this.connManagerShared) {
1216 if (closeablesCopy == null) {
1217 closeablesCopy = new ArrayList<Closeable>(1);
1218 }
1219 final HttpClientConnectionManager cm = connManagerCopy;
1220
1221 if (evictExpiredConnections || evictIdleConnections) {
1222 final IdleConnectionEvictortml#IdleConnectionEvictor">IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm,
1223 maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS,
1224 maxIdleTime, maxIdleTimeUnit);
1225 closeablesCopy.add(new Closeable() {
1226
1227 @Override
1228 public void close() throws IOException {
1229 connectionEvictor.shutdown();
1230 try {
1231 connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS);
1232 } catch (final InterruptedException interrupted) {
1233 Thread.currentThread().interrupt();
1234 }
1235 }
1236
1237 });
1238 connectionEvictor.start();
1239 }
1240 closeablesCopy.add(new Closeable() {
1241
1242 @Override
1243 public void close() throws IOException {
1244 cm.shutdown();
1245 }
1246
1247 });
1248 }
1249
1250 return new InternalHttpClient(
1251 execChain,
1252 connManagerCopy,
1253 routePlannerCopy,
1254 cookieSpecRegistryCopy,
1255 authSchemeRegistryCopy,
1256 defaultCookieStore,
1257 defaultCredentialsProvider,
1258 defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
1259 closeablesCopy);
1260 }
1261
1262 }