diff --git a/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java b/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java index ddd485070..5069e25b3 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -28,41 +28,61 @@ package org.apache.http.impl.client; import java.io.IOException; -import java.net.URI; import java.lang.reflect.UndeclaredThrowableException; - -import org.apache.http.annotation.ThreadSafe; -import org.apache.http.annotation.GuardedBy; +import java.net.URI; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.ConnectionReuseStrategy; +import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; -import org.apache.http.HttpEntity; +import org.apache.http.annotation.GuardedBy; +import org.apache.http.annotation.ThreadSafe; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.RedirectHandler; -import org.apache.http.client.RedirectStrategy; -import org.apache.http.client.RequestDirector; -import org.apache.http.client.ResponseHandler; import org.apache.http.client.CookieStore; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.HttpRequestRetryHandler; +import org.apache.http.client.RedirectHandler; +import org.apache.http.client.RedirectStrategy; +import org.apache.http.client.RequestDirector; +import org.apache.http.client.ResponseHandler; import org.apache.http.client.UserTokenHandler; import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.params.AuthPolicy; +import org.apache.http.client.params.ClientPNames; +import org.apache.http.client.params.CookiePolicy; +import org.apache.http.client.protocol.ClientContext; import org.apache.http.client.utils.URIUtils; import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.ClientConnectionManagerFactory; import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.conn.routing.HttpRoutePlanner; +import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.cookie.CookieSpecRegistry; +import org.apache.http.impl.DefaultConnectionReuseStrategy; +import org.apache.http.impl.auth.BasicSchemeFactory; +import org.apache.http.impl.auth.DigestSchemeFactory; +import org.apache.http.impl.auth.NTLMSchemeFactory; +import org.apache.http.impl.auth.NegotiateSchemeFactory; +import org.apache.http.impl.conn.DefaultHttpRoutePlanner; +import org.apache.http.impl.conn.SchemeRegistryFactory; +import org.apache.http.impl.conn.SingleClientConnManager; +import org.apache.http.impl.cookie.BestMatchSpecFactory; +import org.apache.http.impl.cookie.BrowserCompatSpecFactory; +import org.apache.http.impl.cookie.IgnoreSpecFactory; +import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; +import org.apache.http.impl.cookie.RFC2109SpecFactory; +import org.apache.http.impl.cookie.RFC2965SpecFactory; import org.apache.http.params.HttpParams; +import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.DefaultedHttpContext; import org.apache.http.protocol.HttpContext; @@ -242,56 +262,163 @@ public abstract class AbstractHttpClient implements HttpClient { connManager = conman; } // constructor + protected abstract HttpParams createHttpParams(); - protected abstract HttpContext createHttpContext(); - - - protected abstract HttpRequestExecutor createRequestExecutor(); - - - protected abstract ClientConnectionManager createClientConnectionManager(); - - - protected abstract AuthSchemeRegistry createAuthSchemeRegistry(); - - - protected abstract CookieSpecRegistry createCookieSpecRegistry(); - - - protected abstract ConnectionReuseStrategy createConnectionReuseStrategy(); - - - protected abstract ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy(); - - protected abstract BasicHttpProcessor createHttpProcessor(); - protected abstract HttpRequestRetryHandler createHttpRequestRetryHandler(); + protected HttpContext createHttpContext() { + HttpContext context = new BasicHttpContext(); + context.setAttribute( + ClientContext.SCHEME_REGISTRY, + getConnectionManager().getSchemeRegistry()); + context.setAttribute( + ClientContext.AUTHSCHEME_REGISTRY, + getAuthSchemes()); + context.setAttribute( + ClientContext.COOKIESPEC_REGISTRY, + getCookieSpecs()); + context.setAttribute( + ClientContext.COOKIE_STORE, + getCookieStore()); + context.setAttribute( + ClientContext.CREDS_PROVIDER, + getCredentialsProvider()); + return context; + } + + + protected ClientConnectionManager createClientConnectionManager() { + SchemeRegistry registry = SchemeRegistryFactory.createDefault(); + + ClientConnectionManager connManager = null; + HttpParams params = getParams(); + + ClientConnectionManagerFactory factory = null; + + String className = (String) params.getParameter( + ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME); + if (className != null) { + try { + Class> clazz = Class.forName(className); + factory = (ClientConnectionManagerFactory) clazz.newInstance(); + } catch (ClassNotFoundException ex) { + throw new IllegalStateException("Invalid class name: " + className); + } catch (IllegalAccessException ex) { + throw new IllegalAccessError(ex.getMessage()); + } catch (InstantiationException ex) { + throw new InstantiationError(ex.getMessage()); + } + } + if (factory != null) { + connManager = factory.newInstance(params, registry); + } else { + connManager = new SingleClientConnManager(registry); + } + + return connManager; + } + + + protected AuthSchemeRegistry createAuthSchemeRegistry() { + AuthSchemeRegistry registry = new AuthSchemeRegistry(); + registry.register( + AuthPolicy.BASIC, + new BasicSchemeFactory()); + registry.register( + AuthPolicy.DIGEST, + new DigestSchemeFactory()); + registry.register( + AuthPolicy.NTLM, + new NTLMSchemeFactory()); + registry.register( + AuthPolicy.SPNEGO, + new NegotiateSchemeFactory()); + return registry; + } + + + protected CookieSpecRegistry createCookieSpecRegistry() { + CookieSpecRegistry registry = new CookieSpecRegistry(); + registry.register( + CookiePolicy.BEST_MATCH, + new BestMatchSpecFactory()); + registry.register( + CookiePolicy.BROWSER_COMPATIBILITY, + new BrowserCompatSpecFactory()); + registry.register( + CookiePolicy.NETSCAPE, + new NetscapeDraftSpecFactory()); + registry.register( + CookiePolicy.RFC_2109, + new RFC2109SpecFactory()); + registry.register( + CookiePolicy.RFC_2965, + new RFC2965SpecFactory()); + registry.register( + CookiePolicy.IGNORE_COOKIES, + new IgnoreSpecFactory()); + return registry; + } + + + protected HttpRequestExecutor createRequestExecutor() { + return new HttpRequestExecutor(); + } + + + protected ConnectionReuseStrategy createConnectionReuseStrategy() { + return new DefaultConnectionReuseStrategy(); + } + + + protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { + return new DefaultConnectionKeepAliveStrategy(); + } + + + protected HttpRequestRetryHandler createHttpRequestRetryHandler() { + return new DefaultHttpRequestRetryHandler(); + } @Deprecated - protected abstract RedirectHandler createRedirectHandler(); + protected RedirectHandler createRedirectHandler() { + return new DefaultRedirectHandler(); + } - protected abstract AuthenticationHandler createTargetAuthenticationHandler(); + protected AuthenticationHandler createTargetAuthenticationHandler() { + return new DefaultTargetAuthenticationHandler(); + } - protected abstract AuthenticationHandler createProxyAuthenticationHandler(); + protected AuthenticationHandler createProxyAuthenticationHandler() { + return new DefaultProxyAuthenticationHandler(); + } - protected abstract CookieStore createCookieStore(); + protected CookieStore createCookieStore() { + return new BasicCookieStore(); + } - protected abstract CredentialsProvider createCredentialsProvider(); + protected CredentialsProvider createCredentialsProvider() { + return new BasicCredentialsProvider(); + } - protected abstract HttpRoutePlanner createHttpRoutePlanner(); + protected HttpRoutePlanner createHttpRoutePlanner() { + return new DefaultHttpRoutePlanner(getConnectionManager().getSchemeRegistry()); + } - protected abstract UserTokenHandler createUserTokenHandler(); + protected UserTokenHandler createUserTokenHandler() { + return new DefaultUserTokenHandler(); + } + // non-javadoc, see interface HttpClient public synchronized final HttpParams getParams() { @@ -401,7 +528,7 @@ public abstract class AbstractHttpClient implements HttpClient { @Deprecated - public synchronized void setRedirectHandler(final org.apache.http.client.RedirectHandler redirectHandler) { + public synchronized void setRedirectHandler(final RedirectHandler redirectHandler) { this.redirectStrategy = new DefaultRedirectStrategyAdaptor(redirectHandler); } diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index 622261179..872eac87a 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -27,20 +27,9 @@ package org.apache.http.impl.client; -import org.apache.http.annotation.ThreadSafe; - -import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpVersion; -import org.apache.http.auth.AuthSchemeRegistry; -import org.apache.http.client.AuthenticationHandler; -import org.apache.http.client.CookieStore; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.HttpRequestRetryHandler; -import org.apache.http.client.UserTokenHandler; -import org.apache.http.client.params.AuthPolicy; -import org.apache.http.client.params.ClientPNames; -import org.apache.http.client.params.CookiePolicy; -import org.apache.http.client.protocol.ClientContext; +import org.apache.http.annotation.ThreadSafe; +import org.apache.http.client.HttpClient; import org.apache.http.client.protocol.RequestAddCookies; import org.apache.http.client.protocol.RequestAuthCache; import org.apache.http.client.protocol.RequestClientConnControl; @@ -50,36 +39,14 @@ import org.apache.http.client.protocol.RequestTargetAuthentication; import org.apache.http.client.protocol.ResponseAuthCache; import org.apache.http.client.protocol.ResponseProcessCookies; import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.ClientConnectionManagerFactory; -import org.apache.http.conn.ConnectionKeepAliveStrategy; -import org.apache.http.conn.routing.HttpRoutePlanner; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.cookie.CookieSpecRegistry; -import org.apache.http.impl.DefaultConnectionReuseStrategy; -import org.apache.http.impl.auth.BasicSchemeFactory; -import org.apache.http.impl.auth.DigestSchemeFactory; -import org.apache.http.impl.auth.NTLMSchemeFactory; -import org.apache.http.impl.auth.NegotiateSchemeFactory; -import org.apache.http.impl.conn.DefaultHttpRoutePlanner; -import org.apache.http.impl.conn.SchemeRegistryFactory; -import org.apache.http.impl.conn.SingleClientConnManager; -import org.apache.http.impl.cookie.BestMatchSpecFactory; -import org.apache.http.impl.cookie.BrowserCompatSpecFactory; -import org.apache.http.impl.cookie.IgnoreSpecFactory; -import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; -import org.apache.http.impl.cookie.RFC2109SpecFactory; -import org.apache.http.impl.cookie.RFC2965SpecFactory; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.params.SyncBasicHttpParams; -import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.HTTP; -import org.apache.http.protocol.HttpContext; -import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.protocol.RequestContent; import org.apache.http.protocol.RequestExpectContinue; import org.apache.http.protocol.RequestTargetHost; @@ -87,13 +54,9 @@ import org.apache.http.protocol.RequestUserAgent; import org.apache.http.util.VersionInfo; /** - * Default implementation of {@link AbstractHttpClient}. + * Default implementation of {@link HttpClient} pre-configured for most common use scenarios. *
- * This class creates an instance of {@link SingleClientConnManager} - * for connection management if not explicitly set. - *
- * This class creates the following chain of protocol interceptors per - * default: + * This class creates the following chain of protocol interceptors per default: *