FileDocCategorySizeDatePackage
AbstractHttpClient.javaAPI DocAndroid 1.5 API22488Wed May 06 22:41:10 BST 2009org.apache.http.impl.client

AbstractHttpClient

public abstract class AbstractHttpClient extends Object implements HttpClient
Convenience base class for HTTP client implementations.
author
Roland Weber
author
Oleg Kalnichevski
version
$Revision: 677250 $
since
4.0

Fields Summary
private final Log
log
private HttpParams
defaultParams
The parameters.
private HttpRequestExecutor
requestExec
The request executor.
private ClientConnectionManager
connManager
The connection manager.
private ConnectionReuseStrategy
reuseStrategy
The connection re-use strategy.
private ConnectionKeepAliveStrategy
keepAliveStrategy
The connection keep-alive strategy.
private CookieSpecRegistry
supportedCookieSpecs
The cookie spec registry.
private AuthSchemeRegistry
supportedAuthSchemes
The authentication scheme registry.
private BasicHttpProcessor
httpProcessor
The HTTP processor.
private HttpRequestRetryHandler
retryHandler
The request retry handler.
private RedirectHandler
redirectHandler
The redirect handler.
private AuthenticationHandler
targetAuthHandler
The target authentication handler.
private AuthenticationHandler
proxyAuthHandler
The proxy authentication handler.
private CookieStore
cookieStore
The cookie store.
private CredentialsProvider
credsProvider
The credentials provider.
private HttpRoutePlanner
routePlanner
The route planner.
private UserTokenHandler
userTokenHandler
The user token handler.
Constructors Summary
protected AbstractHttpClient(ClientConnectionManager conman, HttpParams params)
Creates a new HTTP client.

param
conman the connection manager
param
params the parameters



                             
     
              
               
        defaultParams        = params;
        connManager          = conman;
    
Methods Summary
public synchronized voidaddRequestInterceptor(org.apache.http.HttpRequestInterceptor itcp)

        getHttpProcessor().addInterceptor(itcp);
    
public synchronized voidaddRequestInterceptor(org.apache.http.HttpRequestInterceptor itcp, int index)

        getHttpProcessor().addInterceptor(itcp, index);
    
public synchronized voidaddResponseInterceptor(org.apache.http.HttpResponseInterceptor itcp)

        getHttpProcessor().addInterceptor(itcp);
    
public synchronized voidaddResponseInterceptor(org.apache.http.HttpResponseInterceptor itcp, int index)

        getHttpProcessor().addInterceptor(itcp, index);
    
public synchronized voidclearRequestInterceptors()

        getHttpProcessor().clearRequestInterceptors();
    
public synchronized voidclearResponseInterceptors()

        getHttpProcessor().clearResponseInterceptors();
    
protected abstract org.apache.http.auth.AuthSchemeRegistrycreateAuthSchemeRegistry()

protected abstract org.apache.http.conn.ClientConnectionManagercreateClientConnectionManager()

protected org.apache.http.client.RequestDirectorcreateClientRequestDirector(org.apache.http.protocol.HttpRequestExecutor requestExec, org.apache.http.conn.ClientConnectionManager conman, org.apache.http.ConnectionReuseStrategy reustrat, org.apache.http.conn.ConnectionKeepAliveStrategy kastrat, org.apache.http.conn.routing.HttpRoutePlanner rouplan, org.apache.http.protocol.HttpProcessor httpProcessor, org.apache.http.client.HttpRequestRetryHandler retryHandler, org.apache.http.client.RedirectHandler redirectHandler, org.apache.http.client.AuthenticationHandler targetAuthHandler, org.apache.http.client.AuthenticationHandler proxyAuthHandler, org.apache.http.client.UserTokenHandler stateHandler, org.apache.http.params.HttpParams params)

        return new DefaultRequestDirector(
                requestExec,
                conman,
                reustrat,
                kastrat,
                rouplan,
                httpProcessor,
                retryHandler,
                redirectHandler,
                targetAuthHandler,
                proxyAuthHandler,
                stateHandler,
                params);
    
protected abstract org.apache.http.conn.ConnectionKeepAliveStrategycreateConnectionKeepAliveStrategy()

protected abstract org.apache.http.ConnectionReuseStrategycreateConnectionReuseStrategy()

protected abstract org.apache.http.cookie.CookieSpecRegistrycreateCookieSpecRegistry()

protected abstract org.apache.http.client.CookieStorecreateCookieStore()

protected abstract org.apache.http.client.CredentialsProvidercreateCredentialsProvider()

protected abstract org.apache.http.protocol.HttpContextcreateHttpContext()

protected abstract org.apache.http.params.HttpParamscreateHttpParams()

protected abstract org.apache.http.protocol.BasicHttpProcessorcreateHttpProcessor()

protected abstract org.apache.http.client.HttpRequestRetryHandlercreateHttpRequestRetryHandler()

protected abstract org.apache.http.conn.routing.HttpRoutePlannercreateHttpRoutePlanner()

protected abstract org.apache.http.client.AuthenticationHandlercreateProxyAuthenticationHandler()

protected abstract org.apache.http.client.RedirectHandlercreateRedirectHandler()

protected abstract org.apache.http.protocol.HttpRequestExecutorcreateRequestExecutor()

protected abstract org.apache.http.client.AuthenticationHandlercreateTargetAuthenticationHandler()

protected abstract org.apache.http.client.UserTokenHandlercreateUserTokenHandler()

protected org.apache.http.params.HttpParamsdetermineParams(org.apache.http.HttpRequest req)
Obtains parameters for executing a request. The default implementation in this class creates a new {@link ClientParamsStack} from the request parameters and the client parameters.
This method is called by the default implementation of {@link #execute(HttpHost,HttpRequest,HttpContext)} to obtain the parameters for the {@link DefaultRequestDirector}.

param
req the request that will be executed
return
the parameters to use

        return new ClientParamsStack
            (null, getParams(), req.getParams(), null);
    
private org.apache.http.HttpHostdetermineTarget(org.apache.http.client.methods.HttpUriRequest request)

        // A null target may be acceptable if there is a default target.
        // Otherwise, the null target is detected in the director.
        HttpHost target = null;

        URI requestURI = request.getURI();
        if (requestURI.isAbsolute()) {
            target = new HttpHost(
                    requestURI.getHost(),
                    requestURI.getPort(),
                    requestURI.getScheme());
        }
        return target;
    
public final org.apache.http.HttpResponseexecute(org.apache.http.client.methods.HttpUriRequest request)


        return execute(request, (HttpContext) null);
    
public final org.apache.http.HttpResponseexecute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.protocol.HttpContext context)
Maps to {@link HttpClient#execute(HttpHost,HttpRequest,HttpContext) execute(target, request, context)}. The target is determined from the URI of the request.

param
request the request to execute
param
context the request-specific execution context, or null to use a default context


        if (request == null) {
            throw new IllegalArgumentException
                ("Request must not be null.");
        }

        return execute(determineTarget(request), request, context);
    
public final org.apache.http.HttpResponseexecute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request)


        return execute(target, request, (HttpContext) null);
    
public final org.apache.http.HttpResponseexecute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)


        if (request == null) {
            throw new IllegalArgumentException
                ("Request must not be null.");
        }
        // a null target may be acceptable, this depends on the route planner
        // a null context is acceptable, default context created below

        HttpContext execContext = null;
        RequestDirector director = null;
        
        // Initialize the request execution context making copies of 
        // all shared objects that are potentially threading unsafe.
        synchronized (this) {

            HttpContext defaultContext = createHttpContext();
            if (context == null) {
                execContext = defaultContext;
            } else {
                execContext = new DefaultedHttpContext(context, defaultContext);
            }
            // Create a director for this request
            director = createClientRequestDirector(
                    getRequestExecutor(),
                    getConnectionManager(),
                    getConnectionReuseStrategy(),
                    getConnectionKeepAliveStrategy(),
                    getRoutePlanner(),
                    getHttpProcessor().copy(),
                    getHttpRequestRetryHandler(),
                    getRedirectHandler(),
                    getTargetAuthenticationHandler(),
                    getProxyAuthenticationHandler(),
                    getUserTokenHandler(),
                    determineParams(request));
        }

        try {
            return director.execute(target, request, execContext);
        } catch(HttpException httpException) {
            throw new ClientProtocolException(httpException);
        }
    
public Texecute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler responseHandler)

        return execute(request, responseHandler, null);
    
public Texecute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler responseHandler, org.apache.http.protocol.HttpContext context)

        HttpHost target = determineTarget(request);
        return execute(target, request, responseHandler, context);
    
public Texecute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler responseHandler)

        return execute(target, request, responseHandler, null);
    
public Texecute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler responseHandler, org.apache.http.protocol.HttpContext context)

        if (responseHandler == null) {
            throw new IllegalArgumentException
                ("Response handler must not be null.");
        }

        HttpResponse response = execute(target, request, context);

        T result;
        try {
            result = responseHandler.handleResponse(response);
        } catch (Throwable t) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (Throwable t2) {
                    // Log this exception. The original exception is more
                    // important and will be thrown to the caller.
                    this.log.warn("Error consuming content after an exception.", t2);
                }
            }

            if (t instanceof Error) {
                throw (Error) t;
            }

            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            }

            if (t instanceof IOException) {
                throw (IOException) t;
            }
            
            throw new UndeclaredThrowableException(t);
        }

        // Handling the response was successful. Ensure that the content has
        // been fully consumed.
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            // Let this exception go to the caller.
            entity.consumeContent();
        }

        return result;
    
public final synchronized org.apache.http.auth.AuthSchemeRegistrygetAuthSchemes()

        if (supportedAuthSchemes == null) {
            supportedAuthSchemes = createAuthSchemeRegistry();
        }
        return supportedAuthSchemes;
    
public final synchronized org.apache.http.conn.ConnectionKeepAliveStrategygetConnectionKeepAliveStrategy()

        if (keepAliveStrategy == null) {
            keepAliveStrategy = createConnectionKeepAliveStrategy();
        }
        return keepAliveStrategy;
    
public final synchronized org.apache.http.conn.ClientConnectionManagergetConnectionManager()

        if (connManager == null) {
            connManager = createClientConnectionManager();
        }
        return connManager;
    
public final synchronized org.apache.http.ConnectionReuseStrategygetConnectionReuseStrategy()

        if (reuseStrategy == null) {
            reuseStrategy = createConnectionReuseStrategy();
        }
        return reuseStrategy;
    
public final synchronized org.apache.http.cookie.CookieSpecRegistrygetCookieSpecs()

        if (supportedCookieSpecs == null) {
            supportedCookieSpecs = createCookieSpecRegistry();
        }
        return supportedCookieSpecs;
    
public final synchronized org.apache.http.client.CookieStoregetCookieStore()

        if (cookieStore == null) {
            cookieStore = createCookieStore();
        }
        return cookieStore;
    
public final synchronized org.apache.http.client.CredentialsProvidergetCredentialsProvider()

        if (credsProvider == null) {
            credsProvider = createCredentialsProvider();
        }
        return credsProvider;
    
protected final synchronized org.apache.http.protocol.BasicHttpProcessorgetHttpProcessor()

        if (httpProcessor == null) {
            httpProcessor = createHttpProcessor();
        }
        return httpProcessor;
    
public final synchronized org.apache.http.client.HttpRequestRetryHandlergetHttpRequestRetryHandler()

        if (retryHandler == null) {
            retryHandler = createHttpRequestRetryHandler();
        }
        return retryHandler;
    
public final synchronized org.apache.http.params.HttpParamsgetParams()

        if (defaultParams == null) {
            defaultParams = createHttpParams();
        }
        return defaultParams;
    
public final synchronized org.apache.http.client.AuthenticationHandlergetProxyAuthenticationHandler()

        if (proxyAuthHandler == null) {
            proxyAuthHandler = createProxyAuthenticationHandler();
        }
        return proxyAuthHandler;
    
public final synchronized org.apache.http.client.RedirectHandlergetRedirectHandler()

        if (redirectHandler == null) {
            redirectHandler = createRedirectHandler();
        }
        return redirectHandler;
    
public final synchronized org.apache.http.protocol.HttpRequestExecutorgetRequestExecutor()

        if (requestExec == null) {
            requestExec = createRequestExecutor();
        }
        return requestExec;
    
public synchronized org.apache.http.HttpRequestInterceptorgetRequestInterceptor(int index)

        return getHttpProcessor().getRequestInterceptor(index);
    
public synchronized intgetRequestInterceptorCount()

        return getHttpProcessor().getRequestInterceptorCount();
    
public synchronized org.apache.http.HttpResponseInterceptorgetResponseInterceptor(int index)

        return getHttpProcessor().getResponseInterceptor(index);
    
public synchronized intgetResponseInterceptorCount()

        return getHttpProcessor().getResponseInterceptorCount();
    
public final synchronized org.apache.http.conn.routing.HttpRoutePlannergetRoutePlanner()

        if (this.routePlanner == null) {
            this.routePlanner = createHttpRoutePlanner();
        }
        return this.routePlanner;
    
public final synchronized org.apache.http.client.AuthenticationHandlergetTargetAuthenticationHandler()

        if (targetAuthHandler == null) {
            targetAuthHandler = createTargetAuthenticationHandler();
        }
        return targetAuthHandler;
    
public final synchronized org.apache.http.client.UserTokenHandlergetUserTokenHandler()

        if (this.userTokenHandler == null) {
            this.userTokenHandler = createUserTokenHandler();
        }
        return this.userTokenHandler;
    
public voidremoveRequestInterceptorByClass(java.lang.Class clazz)

        getHttpProcessor().removeRequestInterceptorByClass(clazz);
    
public voidremoveResponseInterceptorByClass(java.lang.Class clazz)

        getHttpProcessor().removeResponseInterceptorByClass(clazz);
    
public synchronized voidsetAuthSchemes(org.apache.http.auth.AuthSchemeRegistry authSchemeRegistry)

        supportedAuthSchemes = authSchemeRegistry;
    
public synchronized voidsetCookieSpecs(org.apache.http.cookie.CookieSpecRegistry cookieSpecRegistry)

        supportedCookieSpecs = cookieSpecRegistry;
    
public synchronized voidsetCookieStore(org.apache.http.client.CookieStore cookieStore)

        this.cookieStore = cookieStore;
    
public synchronized voidsetCredentialsProvider(org.apache.http.client.CredentialsProvider credsProvider)

        this.credsProvider = credsProvider;
    
public synchronized voidsetHttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler retryHandler)

        this.retryHandler = retryHandler;
    
public synchronized voidsetKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy keepAliveStrategy)

        this.keepAliveStrategy = keepAliveStrategy;
    
public synchronized voidsetParams(org.apache.http.params.HttpParams params)
Replaces the parameters. The implementation here does not update parameters of dependent objects.

param
params the new default parameters

        defaultParams = params;
    
public synchronized voidsetProxyAuthenticationHandler(org.apache.http.client.AuthenticationHandler proxyAuthHandler)

        this.proxyAuthHandler = proxyAuthHandler;
    
public synchronized voidsetRedirectHandler(org.apache.http.client.RedirectHandler redirectHandler)

        this.redirectHandler = redirectHandler;
    
public synchronized voidsetReuseStrategy(org.apache.http.ConnectionReuseStrategy reuseStrategy)

        this.reuseStrategy = reuseStrategy;
    
public synchronized voidsetRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner routePlanner)

        this.routePlanner = routePlanner;
    
public synchronized voidsetTargetAuthenticationHandler(org.apache.http.client.AuthenticationHandler targetAuthHandler)

        this.targetAuthHandler = targetAuthHandler;
    
public synchronized voidsetUserTokenHandler(org.apache.http.client.UserTokenHandler userTokenHandler)

        this.userTokenHandler = userTokenHandler;