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.IOException;
31 import java.lang.reflect.UndeclaredThrowableException;
32 import java.net.URI;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.http.ConnectionReuseStrategy;
37 import org.apache.http.HttpEntity;
38 import org.apache.http.HttpException;
39 import org.apache.http.HttpHost;
40 import org.apache.http.HttpRequest;
41 import org.apache.http.HttpRequestInterceptor;
42 import org.apache.http.HttpResponse;
43 import org.apache.http.HttpResponseInterceptor;
44 import org.apache.http.annotation.GuardedBy;
45 import org.apache.http.annotation.ThreadSafe;
46 import org.apache.http.auth.AuthSchemeRegistry;
47 import org.apache.http.client.AuthenticationHandler;
48 import org.apache.http.client.AuthenticationStrategy;
49 import org.apache.http.client.BackoffManager;
50 import org.apache.http.client.ClientProtocolException;
51 import org.apache.http.client.ConnectionBackoffStrategy;
52 import org.apache.http.client.CookieStore;
53 import org.apache.http.client.CredentialsProvider;
54 import org.apache.http.client.HttpClient;
55 import org.apache.http.client.HttpRequestRetryHandler;
56 import org.apache.http.client.RedirectHandler;
57 import org.apache.http.client.RedirectStrategy;
58 import org.apache.http.client.RequestDirector;
59 import org.apache.http.client.ResponseHandler;
60 import org.apache.http.client.UserTokenHandler;
61 import org.apache.http.client.methods.HttpUriRequest;
62 import org.apache.http.client.params.AuthPolicy;
63 import org.apache.http.client.params.ClientPNames;
64 import org.apache.http.client.params.CookiePolicy;
65 import org.apache.http.client.protocol.ClientContext;
66 import org.apache.http.client.utils.URIUtils;
67 import org.apache.http.conn.ClientConnectionManager;
68 import org.apache.http.conn.ClientConnectionManagerFactory;
69 import org.apache.http.conn.ConnectionKeepAliveStrategy;
70 import org.apache.http.conn.routing.HttpRoute;
71 import org.apache.http.conn.routing.HttpRoutePlanner;
72 import org.apache.http.conn.scheme.SchemeRegistry;
73 import org.apache.http.cookie.CookieSpecRegistry;
74 import org.apache.http.impl.DefaultConnectionReuseStrategy;
75 import org.apache.http.impl.auth.BasicSchemeFactory;
76 import org.apache.http.impl.auth.DigestSchemeFactory;
77 import org.apache.http.impl.auth.KerberosSchemeFactory;
78 import org.apache.http.impl.auth.NTLMSchemeFactory;
79 import org.apache.http.impl.auth.SPNegoSchemeFactory;
80 import org.apache.http.impl.conn.BasicClientConnectionManager;
81 import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
82 import org.apache.http.impl.conn.SchemeRegistryFactory;
83 import org.apache.http.impl.cookie.BestMatchSpecFactory;
84 import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
85 import org.apache.http.impl.cookie.IgnoreSpecFactory;
86 import org.apache.http.impl.cookie.NetscapeDraftSpecFactory;
87 import org.apache.http.impl.cookie.RFC2109SpecFactory;
88 import org.apache.http.impl.cookie.RFC2965SpecFactory;
89 import org.apache.http.params.HttpParams;
90 import org.apache.http.protocol.BasicHttpContext;
91 import org.apache.http.protocol.BasicHttpProcessor;
92 import org.apache.http.protocol.DefaultedHttpContext;
93 import org.apache.http.protocol.HttpContext;
94 import org.apache.http.protocol.HttpProcessor;
95 import org.apache.http.protocol.HttpRequestExecutor;
96 import org.apache.http.protocol.ImmutableHttpProcessor;
97 import org.apache.http.util.EntityUtils;
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 @SuppressWarnings("deprecation")
184 @ThreadSafe
185 public abstract class AbstractHttpClient implements HttpClient {
186
187 private final Log log = LogFactory.getLog(getClass());
188
189
190 @GuardedBy("this")
191 private HttpParams defaultParams;
192
193
194 @GuardedBy("this")
195 private HttpRequestExecutor requestExec;
196
197
198 @GuardedBy("this")
199 private ClientConnectionManager connManager;
200
201
202 @GuardedBy("this")
203 private ConnectionReuseStrategy reuseStrategy;
204
205
206 @GuardedBy("this")
207 private ConnectionKeepAliveStrategy keepAliveStrategy;
208
209
210 @GuardedBy("this")
211 private CookieSpecRegistry supportedCookieSpecs;
212
213
214 @GuardedBy("this")
215 private AuthSchemeRegistry supportedAuthSchemes;
216
217
218 @GuardedBy("this")
219 private BasicHttpProcessor mutableProcessor;
220
221 @GuardedBy("this")
222 private ImmutableHttpProcessor protocolProcessor;
223
224
225 @GuardedBy("this")
226 private HttpRequestRetryHandler retryHandler;
227
228
229 @GuardedBy("this")
230 private RedirectStrategy redirectStrategy;
231
232
233 @GuardedBy("this")
234 private AuthenticationStrategy targetAuthStrategy;
235
236
237 @GuardedBy("this")
238 private AuthenticationStrategy proxyAuthStrategy;
239
240
241 @GuardedBy("this")
242 private CookieStore cookieStore;
243
244
245 @GuardedBy("this")
246 private CredentialsProvider credsProvider;
247
248
249 @GuardedBy("this")
250 private HttpRoutePlanner routePlanner;
251
252
253 @GuardedBy("this")
254 private UserTokenHandler userTokenHandler;
255
256
257 @GuardedBy("this")
258 private ConnectionBackoffStrategy connectionBackoffStrategy;
259
260
261 @GuardedBy("this")
262 private BackoffManager backoffManager;
263
264
265
266
267
268
269
270 protected AbstractHttpClient(
271 final ClientConnectionManager conman,
272 final HttpParams params) {
273 defaultParams = params;
274 connManager = conman;
275 }
276
277
278 protected abstract HttpParams createHttpParams();
279
280
281 protected abstract BasicHttpProcessor createHttpProcessor();
282
283
284 protected HttpContext createHttpContext() {
285 final HttpContext context = new BasicHttpContext();
286 context.setAttribute(
287 ClientContext.SCHEME_REGISTRY,
288 getConnectionManager().getSchemeRegistry());
289 context.setAttribute(
290 ClientContext.AUTHSCHEME_REGISTRY,
291 getAuthSchemes());
292 context.setAttribute(
293 ClientContext.COOKIESPEC_REGISTRY,
294 getCookieSpecs());
295 context.setAttribute(
296 ClientContext.COOKIE_STORE,
297 getCookieStore());
298 context.setAttribute(
299 ClientContext.CREDS_PROVIDER,
300 getCredentialsProvider());
301 return context;
302 }
303
304
305 protected ClientConnectionManager createClientConnectionManager() {
306 final SchemeRegistry registry = SchemeRegistryFactory.createDefault();
307
308 ClientConnectionManager connManager = null;
309 final HttpParams params = getParams();
310
311 ClientConnectionManagerFactory factory = null;
312
313 final String className = (String) params.getParameter(
314 ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
315 if (className != null) {
316 try {
317 final Class<?> clazz = Class.forName(className);
318 factory = (ClientConnectionManagerFactory) clazz.newInstance();
319 } catch (final ClassNotFoundException ex) {
320 throw new IllegalStateException("Invalid class name: " + className);
321 } catch (final IllegalAccessException ex) {
322 throw new IllegalAccessError(ex.getMessage());
323 } catch (final InstantiationException ex) {
324 throw new InstantiationError(ex.getMessage());
325 }
326 }
327 if (factory != null) {
328 connManager = factory.newInstance(params, registry);
329 } else {
330 connManager = new BasicClientConnectionManager(registry);
331 }
332
333 return connManager;
334 }
335
336
337 protected AuthSchemeRegistry createAuthSchemeRegistry() {
338 final AuthSchemeRegistry registry = new AuthSchemeRegistry();
339 registry.register(
340 AuthPolicy.BASIC,
341 new BasicSchemeFactory());
342 registry.register(
343 AuthPolicy.DIGEST,
344 new DigestSchemeFactory());
345 registry.register(
346 AuthPolicy.NTLM,
347 new NTLMSchemeFactory());
348 registry.register(
349 AuthPolicy.SPNEGO,
350 new SPNegoSchemeFactory());
351 registry.register(
352 AuthPolicy.KERBEROS,
353 new KerberosSchemeFactory());
354 return registry;
355 }
356
357
358 protected CookieSpecRegistry createCookieSpecRegistry() {
359 final CookieSpecRegistry registry = new CookieSpecRegistry();
360 registry.register(
361 CookiePolicy.BEST_MATCH,
362 new BestMatchSpecFactory());
363 registry.register(
364 CookiePolicy.BROWSER_COMPATIBILITY,
365 new BrowserCompatSpecFactory());
366 registry.register(
367 CookiePolicy.NETSCAPE,
368 new NetscapeDraftSpecFactory());
369 registry.register(
370 CookiePolicy.RFC_2109,
371 new RFC2109SpecFactory());
372 registry.register(
373 CookiePolicy.RFC_2965,
374 new RFC2965SpecFactory());
375 registry.register(
376 CookiePolicy.IGNORE_COOKIES,
377 new IgnoreSpecFactory());
378 return registry;
379 }
380
381 protected HttpRequestExecutor createRequestExecutor() {
382 return new HttpRequestExecutor();
383 }
384
385 protected ConnectionReuseStrategy createConnectionReuseStrategy() {
386 return new DefaultConnectionReuseStrategy();
387 }
388
389 protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() {
390 return new DefaultConnectionKeepAliveStrategy();
391 }
392
393 protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
394 return new DefaultHttpRequestRetryHandler();
395 }
396
397
398
399
400 @Deprecated
401 protected RedirectHandler createRedirectHandler() {
402 return new DefaultRedirectHandler();
403 }
404
405 protected AuthenticationStrategy createTargetAuthenticationStrategy() {
406 return new TargetAuthenticationStrategy();
407 }
408
409
410
411
412 @Deprecated
413 protected AuthenticationHandler createTargetAuthenticationHandler() {
414 return new DefaultTargetAuthenticationHandler();
415 }
416
417 protected AuthenticationStrategy createProxyAuthenticationStrategy() {
418 return new ProxyAuthenticationStrategy();
419 }
420
421
422
423
424 @Deprecated
425 protected AuthenticationHandler createProxyAuthenticationHandler() {
426 return new DefaultProxyAuthenticationHandler();
427 }
428
429 protected CookieStore createCookieStore() {
430 return new BasicCookieStore();
431 }
432
433 protected CredentialsProvider createCredentialsProvider() {
434 return new BasicCredentialsProvider();
435 }
436
437 protected HttpRoutePlanner createHttpRoutePlanner() {
438 return new DefaultHttpRoutePlanner(getConnectionManager().getSchemeRegistry());
439 }
440
441 protected UserTokenHandler createUserTokenHandler() {
442 return new DefaultUserTokenHandler();
443 }
444
445
446 public synchronized final HttpParams getParams() {
447 if (defaultParams == null) {
448 defaultParams = createHttpParams();
449 }
450 return defaultParams;
451 }
452
453
454
455
456
457
458
459 public synchronized void setParams(final HttpParams params) {
460 defaultParams = params;
461 }
462
463
464 public synchronized final ClientConnectionManager getConnectionManager() {
465 if (connManager == null) {
466 connManager = createClientConnectionManager();
467 }
468 return connManager;
469 }
470
471
472 public synchronized final HttpRequestExecutor getRequestExecutor() {
473 if (requestExec == null) {
474 requestExec = createRequestExecutor();
475 }
476 return requestExec;
477 }
478
479
480 public synchronized final AuthSchemeRegistry getAuthSchemes() {
481 if (supportedAuthSchemes == null) {
482 supportedAuthSchemes = createAuthSchemeRegistry();
483 }
484 return supportedAuthSchemes;
485 }
486
487 public synchronized void setAuthSchemes(final AuthSchemeRegistry registry) {
488 supportedAuthSchemes = registry;
489 }
490
491 public synchronized final ConnectionBackoffStrategy getConnectionBackoffStrategy() {
492 return connectionBackoffStrategy;
493 }
494
495 public synchronized void setConnectionBackoffStrategy(final ConnectionBackoffStrategy strategy) {
496 connectionBackoffStrategy = strategy;
497 }
498
499 public synchronized final CookieSpecRegistry getCookieSpecs() {
500 if (supportedCookieSpecs == null) {
501 supportedCookieSpecs = createCookieSpecRegistry();
502 }
503 return supportedCookieSpecs;
504 }
505
506 public synchronized final BackoffManager getBackoffManager() {
507 return backoffManager;
508 }
509
510 public synchronized void setBackoffManager(final BackoffManager manager) {
511 backoffManager = manager;
512 }
513
514 public synchronized void setCookieSpecs(final CookieSpecRegistry registry) {
515 supportedCookieSpecs = registry;
516 }
517
518 public synchronized final ConnectionReuseStrategy getConnectionReuseStrategy() {
519 if (reuseStrategy == null) {
520 reuseStrategy = createConnectionReuseStrategy();
521 }
522 return reuseStrategy;
523 }
524
525
526 public synchronized void setReuseStrategy(final ConnectionReuseStrategy strategy) {
527 this.reuseStrategy = strategy;
528 }
529
530
531 public synchronized final ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy() {
532 if (keepAliveStrategy == null) {
533 keepAliveStrategy = createConnectionKeepAliveStrategy();
534 }
535 return keepAliveStrategy;
536 }
537
538
539 public synchronized void setKeepAliveStrategy(final ConnectionKeepAliveStrategy strategy) {
540 this.keepAliveStrategy = strategy;
541 }
542
543
544 public synchronized final HttpRequestRetryHandler getHttpRequestRetryHandler() {
545 if (retryHandler == null) {
546 retryHandler = createHttpRequestRetryHandler();
547 }
548 return retryHandler;
549 }
550
551 public synchronized void setHttpRequestRetryHandler(final HttpRequestRetryHandler handler) {
552 this.retryHandler = handler;
553 }
554
555
556
557
558 @Deprecated
559 public synchronized final RedirectHandler getRedirectHandler() {
560 return createRedirectHandler();
561 }
562
563
564
565
566 @Deprecated
567 public synchronized void setRedirectHandler(final RedirectHandler handler) {
568 this.redirectStrategy = new DefaultRedirectStrategyAdaptor(handler);
569 }
570
571
572
573
574 public synchronized final RedirectStrategy getRedirectStrategy() {
575 if (redirectStrategy == null) {
576 redirectStrategy = new DefaultRedirectStrategy();
577 }
578 return redirectStrategy;
579 }
580
581
582
583
584 public synchronized void setRedirectStrategy(final RedirectStrategy strategy) {
585 this.redirectStrategy = strategy;
586 }
587
588
589
590
591 @Deprecated
592 public synchronized final AuthenticationHandler getTargetAuthenticationHandler() {
593 return createTargetAuthenticationHandler();
594 }
595
596
597
598
599 @Deprecated
600 public synchronized void setTargetAuthenticationHandler(final AuthenticationHandler handler) {
601 this.targetAuthStrategy = new AuthenticationStrategyAdaptor(handler);
602 }
603
604
605
606
607 public synchronized final AuthenticationStrategy getTargetAuthenticationStrategy() {
608 if (targetAuthStrategy == null) {
609 targetAuthStrategy = createTargetAuthenticationStrategy();
610 }
611 return targetAuthStrategy;
612 }
613
614
615
616
617 public synchronized void setTargetAuthenticationStrategy(final AuthenticationStrategy strategy) {
618 this.targetAuthStrategy = strategy;
619 }
620
621
622
623
624 @Deprecated
625 public synchronized final AuthenticationHandler getProxyAuthenticationHandler() {
626 return createProxyAuthenticationHandler();
627 }
628
629
630
631
632 @Deprecated
633 public synchronized void setProxyAuthenticationHandler(final AuthenticationHandler handler) {
634 this.proxyAuthStrategy = new AuthenticationStrategyAdaptor(handler);
635 }
636
637
638
639
640 public synchronized final AuthenticationStrategy getProxyAuthenticationStrategy() {
641 if (proxyAuthStrategy == null) {
642 proxyAuthStrategy = createProxyAuthenticationStrategy();
643 }
644 return proxyAuthStrategy;
645 }
646
647
648
649
650 public synchronized void setProxyAuthenticationStrategy(final AuthenticationStrategy strategy) {
651 this.proxyAuthStrategy = strategy;
652 }
653
654 public synchronized final CookieStore getCookieStore() {
655 if (cookieStore == null) {
656 cookieStore = createCookieStore();
657 }
658 return cookieStore;
659 }
660
661 public synchronized void setCookieStore(final CookieStore cookieStore) {
662 this.cookieStore = cookieStore;
663 }
664
665 public synchronized final CredentialsProvider getCredentialsProvider() {
666 if (credsProvider == null) {
667 credsProvider = createCredentialsProvider();
668 }
669 return credsProvider;
670 }
671
672 public synchronized void setCredentialsProvider(final CredentialsProvider credsProvider) {
673 this.credsProvider = credsProvider;
674 }
675
676 public synchronized final HttpRoutePlanner getRoutePlanner() {
677 if (this.routePlanner == null) {
678 this.routePlanner = createHttpRoutePlanner();
679 }
680 return this.routePlanner;
681 }
682
683 public synchronized void setRoutePlanner(final HttpRoutePlanner routePlanner) {
684 this.routePlanner = routePlanner;
685 }
686
687 public synchronized final UserTokenHandler getUserTokenHandler() {
688 if (this.userTokenHandler == null) {
689 this.userTokenHandler = createUserTokenHandler();
690 }
691 return this.userTokenHandler;
692 }
693
694 public synchronized void setUserTokenHandler(final UserTokenHandler handler) {
695 this.userTokenHandler = handler;
696 }
697
698 protected synchronized final BasicHttpProcessor getHttpProcessor() {
699 if (mutableProcessor == null) {
700 mutableProcessor = createHttpProcessor();
701 }
702 return mutableProcessor;
703 }
704
705 private synchronized final HttpProcessor getProtocolProcessor() {
706 if (protocolProcessor == null) {
707
708 final BasicHttpProcessor proc = getHttpProcessor();
709
710 final int reqc = proc.getRequestInterceptorCount();
711 final HttpRequestInterceptor[] reqinterceptors = new HttpRequestInterceptor[reqc];
712 for (int i = 0; i < reqc; i++) {
713 reqinterceptors[i] = proc.getRequestInterceptor(i);
714 }
715 final int resc = proc.getResponseInterceptorCount();
716 final HttpResponseInterceptor[] resinterceptors = new HttpResponseInterceptor[resc];
717 for (int i = 0; i < resc; i++) {
718 resinterceptors[i] = proc.getResponseInterceptor(i);
719 }
720 protocolProcessor = new ImmutableHttpProcessor(reqinterceptors, resinterceptors);
721 }
722 return protocolProcessor;
723 }
724
725 public synchronized int getResponseInterceptorCount() {
726 return getHttpProcessor().getResponseInterceptorCount();
727 }
728
729 public synchronized HttpResponseInterceptor getResponseInterceptor(final int index) {
730 return getHttpProcessor().getResponseInterceptor(index);
731 }
732
733 public synchronized HttpRequestInterceptor getRequestInterceptor(final int index) {
734 return getHttpProcessor().getRequestInterceptor(index);
735 }
736
737 public synchronized int getRequestInterceptorCount() {
738 return getHttpProcessor().getRequestInterceptorCount();
739 }
740
741 public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp) {
742 getHttpProcessor().addInterceptor(itcp);
743 protocolProcessor = null;
744 }
745
746 public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp, final int index) {
747 getHttpProcessor().addInterceptor(itcp, index);
748 protocolProcessor = null;
749 }
750
751 public synchronized void clearResponseInterceptors() {
752 getHttpProcessor().clearResponseInterceptors();
753 protocolProcessor = null;
754 }
755
756 public synchronized void removeResponseInterceptorByClass(final Class<? extends HttpResponseInterceptor> clazz) {
757 getHttpProcessor().removeResponseInterceptorByClass(clazz);
758 protocolProcessor = null;
759 }
760
761 public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp) {
762 getHttpProcessor().addInterceptor(itcp);
763 protocolProcessor = null;
764 }
765
766 public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp, final int index) {
767 getHttpProcessor().addInterceptor(itcp, index);
768 protocolProcessor = null;
769 }
770
771 public synchronized void clearRequestInterceptors() {
772 getHttpProcessor().clearRequestInterceptors();
773 protocolProcessor = null;
774 }
775
776 public synchronized void removeRequestInterceptorByClass(final Class<? extends HttpRequestInterceptor> clazz) {
777 getHttpProcessor().removeRequestInterceptorByClass(clazz);
778 protocolProcessor = null;
779 }
780
781 public final HttpResponse execute(final HttpUriRequest request)
782 throws IOException, ClientProtocolException {
783
784 return execute(request, (HttpContext) null);
785 }
786
787
788
789
790
791
792
793
794
795
796 public final HttpResponse execute(final HttpUriRequest request,
797 final HttpContext context)
798 throws IOException, ClientProtocolException {
799
800 if (request == null) {
801 throw new IllegalArgumentException
802 ("Request must not be null.");
803 }
804
805 return execute(determineTarget(request), request, context);
806 }
807
808 private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException {
809
810
811 HttpHost target = null;
812
813 final URI requestURI = request.getURI();
814 if (requestURI.isAbsolute()) {
815 target = URIUtils.extractHost(requestURI);
816 if (target == null) {
817 throw new ClientProtocolException(
818 "URI does not specify a valid host name: " + requestURI);
819 }
820 }
821 return target;
822 }
823
824 public final HttpResponse execute(final HttpHost target, final HttpRequest request)
825 throws IOException, ClientProtocolException {
826
827 return execute(target, request, (HttpContext) null);
828 }
829
830 public final HttpResponse execute(final HttpHost target, final HttpRequest request,
831 final HttpContext context)
832 throws IOException, ClientProtocolException {
833
834 if (request == null) {
835 throw new IllegalArgumentException
836 ("Request must not be null.");
837 }
838
839
840
841 HttpContext execContext = null;
842 RequestDirector director = null;
843 HttpRoutePlanner routePlanner = null;
844 ConnectionBackoffStrategy connectionBackoffStrategy = null;
845 BackoffManager backoffManager = null;
846
847
848
849 synchronized (this) {
850
851 final HttpContext defaultContext = createHttpContext();
852 if (context == null) {
853 execContext = defaultContext;
854 } else {
855 execContext = new DefaultedHttpContext(context, defaultContext);
856 }
857
858 director = createClientRequestDirector(
859 getRequestExecutor(),
860 getConnectionManager(),
861 getConnectionReuseStrategy(),
862 getConnectionKeepAliveStrategy(),
863 getRoutePlanner(),
864 getProtocolProcessor(),
865 getHttpRequestRetryHandler(),
866 getRedirectStrategy(),
867 getTargetAuthenticationStrategy(),
868 getProxyAuthenticationStrategy(),
869 getUserTokenHandler(),
870 determineParams(request));
871 routePlanner = getRoutePlanner();
872 connectionBackoffStrategy = getConnectionBackoffStrategy();
873 backoffManager = getBackoffManager();
874 }
875
876 try {
877 if (connectionBackoffStrategy != null && backoffManager != null) {
878 final HttpHost targetForRoute = (target != null) ? target
879 : (HttpHost) determineParams(request).getParameter(
880 ClientPNames.DEFAULT_HOST);
881 final HttpRoute route = routePlanner.determineRoute(targetForRoute, request, execContext);
882
883 HttpResponse out;
884 try {
885 out = director.execute(target, request, execContext);
886 } catch (final RuntimeException re) {
887 if (connectionBackoffStrategy.shouldBackoff(re)) {
888 backoffManager.backOff(route);
889 }
890 throw re;
891 } catch (final Exception e) {
892 if (connectionBackoffStrategy.shouldBackoff(e)) {
893 backoffManager.backOff(route);
894 }
895 if (e instanceof HttpException) throw (HttpException)e;
896 if (e instanceof IOException) throw (IOException)e;
897 throw new UndeclaredThrowableException(e);
898 }
899 if (connectionBackoffStrategy.shouldBackoff(out)) {
900 backoffManager.backOff(route);
901 } else {
902 backoffManager.probe(route);
903 }
904 return out;
905 } else {
906 return director.execute(target, request, execContext);
907 }
908 } catch(final HttpException httpException) {
909 throw new ClientProtocolException(httpException);
910 }
911 }
912
913
914
915
916 @Deprecated
917 protected RequestDirector createClientRequestDirector(
918 final HttpRequestExecutor requestExec,
919 final ClientConnectionManager conman,
920 final ConnectionReuseStrategy reustrat,
921 final ConnectionKeepAliveStrategy kastrat,
922 final HttpRoutePlanner rouplan,
923 final HttpProcessor httpProcessor,
924 final HttpRequestRetryHandler retryHandler,
925 final RedirectHandler redirectHandler,
926 final AuthenticationHandler targetAuthHandler,
927 final AuthenticationHandler proxyAuthHandler,
928 final UserTokenHandler userTokenHandler,
929 final HttpParams params) {
930 return new DefaultRequestDirector(
931 requestExec,
932 conman,
933 reustrat,
934 kastrat,
935 rouplan,
936 httpProcessor,
937 retryHandler,
938 redirectHandler,
939 targetAuthHandler,
940 proxyAuthHandler,
941 userTokenHandler,
942 params);
943 }
944
945
946
947
948 @Deprecated
949 protected RequestDirector createClientRequestDirector(
950 final HttpRequestExecutor requestExec,
951 final ClientConnectionManager conman,
952 final ConnectionReuseStrategy reustrat,
953 final ConnectionKeepAliveStrategy kastrat,
954 final HttpRoutePlanner rouplan,
955 final HttpProcessor httpProcessor,
956 final HttpRequestRetryHandler retryHandler,
957 final RedirectStrategy redirectStrategy,
958 final AuthenticationHandler targetAuthHandler,
959 final AuthenticationHandler proxyAuthHandler,
960 final UserTokenHandler userTokenHandler,
961 final HttpParams params) {
962 return new DefaultRequestDirector(
963 log,
964 requestExec,
965 conman,
966 reustrat,
967 kastrat,
968 rouplan,
969 httpProcessor,
970 retryHandler,
971 redirectStrategy,
972 targetAuthHandler,
973 proxyAuthHandler,
974 userTokenHandler,
975 params);
976 }
977
978
979
980
981
982 protected RequestDirector createClientRequestDirector(
983 final HttpRequestExecutor requestExec,
984 final ClientConnectionManager conman,
985 final ConnectionReuseStrategy reustrat,
986 final ConnectionKeepAliveStrategy kastrat,
987 final HttpRoutePlanner rouplan,
988 final HttpProcessor httpProcessor,
989 final HttpRequestRetryHandler retryHandler,
990 final RedirectStrategy redirectStrategy,
991 final AuthenticationStrategy targetAuthStrategy,
992 final AuthenticationStrategy proxyAuthStrategy,
993 final UserTokenHandler userTokenHandler,
994 final HttpParams params) {
995 return new DefaultRequestDirector(
996 log,
997 requestExec,
998 conman,
999 reustrat,
1000 kastrat,
1001 rouplan,
1002 httpProcessor,
1003 retryHandler,
1004 redirectStrategy,
1005 targetAuthStrategy,
1006 proxyAuthStrategy,
1007 userTokenHandler,
1008 params);
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 protected HttpParams determineParams(final HttpRequest req) {
1027 return new ClientParamsStack
1028 (null, getParams(), req.getParams(), null);
1029 }
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 public <T> T execute(
1047 final HttpUriRequest request,
1048 final ResponseHandler<? extends T> responseHandler)
1049 throws IOException, ClientProtocolException {
1050 return execute(request, responseHandler, null);
1051 }
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 public <T> T execute(
1071 final HttpUriRequest request,
1072 final ResponseHandler<? extends T> responseHandler,
1073 final HttpContext context)
1074 throws IOException, ClientProtocolException {
1075 final HttpHost target = determineTarget(request);
1076 return execute(target, request, responseHandler, context);
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 public <T> T execute(
1099 final HttpHost target,
1100 final HttpRequest request,
1101 final ResponseHandler<? extends T> responseHandler)
1102 throws IOException, ClientProtocolException {
1103 return execute(target, request, responseHandler, null);
1104 }
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 public <T> T execute(
1128 final HttpHost target,
1129 final HttpRequest request,
1130 final ResponseHandler<? extends T> responseHandler,
1131 final HttpContext context)
1132 throws IOException, ClientProtocolException {
1133 if (responseHandler == null) {
1134 throw new IllegalArgumentException
1135 ("Response handler must not be null.");
1136 }
1137
1138 final HttpResponse response = execute(target, request, context);
1139
1140 T result;
1141 try {
1142 result = responseHandler.handleResponse(response);
1143 } catch (final Exception t) {
1144 final HttpEntity entity = response.getEntity();
1145 try {
1146 EntityUtils.consume(entity);
1147 } catch (final Exception t2) {
1148
1149
1150 this.log.warn("Error consuming content after an exception.", t2);
1151 }
1152 if (t instanceof RuntimeException) {
1153 throw (RuntimeException) t;
1154 }
1155 if (t instanceof IOException) {
1156 throw (IOException) t;
1157 }
1158 throw new UndeclaredThrowableException(t);
1159 }
1160
1161
1162
1163 final HttpEntity entity = response.getEntity();
1164 EntityUtils.consume(entity);
1165 return result;
1166 }
1167
1168 }