org.apache.http.impl.client
Class AbstractHttpClient

java.lang.Object
  extended by org.apache.http.impl.client.AbstractHttpClient
All Implemented Interfaces:
HttpClient
Direct Known Subclasses:
DefaultHttpClient

@ThreadSafe
public abstract class AbstractHttpClient
extends Object
implements HttpClient

Base class for HttpClient implementations. This class acts as a facade to a number of special purpose handler or strategy implementations responsible for handling of a particular aspect of the HTTP protocol such as redirect or authentication handling or making decision about connection persistence and keep alive duration. This enables the users to selectively replace default implementation of those aspects with custom, application specific ones. This class also provides factory methods to instantiate those objects:

This class also maintains a list of protocol interceptors intended for processing outgoing requests and incoming responses and provides methods for managing those interceptors. New protocol interceptors can be introduced to the protocol processor chain or removed from it if needed. Internally protocol interceptors are stored in a simple ArrayList. They are executed in the same natural order as they are added to the list.

AbstractHttpClient is thread safe. It is recommended that the same instance of this class is reused for multiple request executions. When an instance of DefaultHttpClient is no longer needed and is about to go out of scope the connection manager associated with it must be shut down by calling ClientConnectionManager.shutdown()!

Since:
4.0

Constructor Summary
protected AbstractHttpClient(ClientConnectionManager conman, HttpParams params)
          Creates a new HTTP client.
 
Method Summary
 void addRequestInterceptor(HttpRequestInterceptor itcp)
           
 void addRequestInterceptor(HttpRequestInterceptor itcp, int index)
           
 void addResponseInterceptor(HttpResponseInterceptor itcp)
           
 void addResponseInterceptor(HttpResponseInterceptor itcp, int index)
           
 void clearRequestInterceptors()
           
 void clearResponseInterceptors()
           
protected  AuthSchemeRegistry createAuthSchemeRegistry()
           
protected  ClientConnectionManager createClientConnectionManager()
           
protected  RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec, ClientConnectionManager conman, ConnectionReuseStrategy reustrat, ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor, HttpRequestRetryHandler retryHandler, RedirectHandler redirectHandler, AuthenticationHandler targetAuthHandler, AuthenticationHandler proxyAuthHandler, UserTokenHandler userTokenHandler, HttpParams params)
          Deprecated. (4.1) do not use
protected  RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec, ClientConnectionManager conman, ConnectionReuseStrategy reustrat, ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor, HttpRequestRetryHandler retryHandler, RedirectStrategy redirectStrategy, AuthenticationHandler targetAuthHandler, AuthenticationHandler proxyAuthHandler, UserTokenHandler userTokenHandler, HttpParams params)
          Deprecated. (4.2) do not use
protected  RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec, ClientConnectionManager conman, ConnectionReuseStrategy reustrat, ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor, HttpRequestRetryHandler retryHandler, RedirectStrategy redirectStrategy, AuthenticationStrategy targetAuthStrategy, AuthenticationStrategy proxyAuthStrategy, UserTokenHandler userTokenHandler, HttpParams params)
           
protected  ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy()
           
protected  ConnectionReuseStrategy createConnectionReuseStrategy()
           
protected  CookieSpecRegistry createCookieSpecRegistry()
           
protected  CookieStore createCookieStore()
           
protected  CredentialsProvider createCredentialsProvider()
           
protected  HttpContext createHttpContext()
           
protected abstract  HttpParams createHttpParams()
           
protected abstract  BasicHttpProcessor createHttpProcessor()
           
protected  HttpRequestRetryHandler createHttpRequestRetryHandler()
           
protected  HttpRoutePlanner createHttpRoutePlanner()
           
protected  AuthenticationHandler createProxyAuthenticationHandler()
          Deprecated. (4.2) do not use
protected  AuthenticationStrategy createProxyAuthenticationStrategy()
           
protected  RedirectHandler createRedirectHandler()
          Deprecated. (4.1) do not use
protected  HttpRequestExecutor createRequestExecutor()
           
protected  AuthenticationHandler createTargetAuthenticationHandler()
          Deprecated. (4.2) do not use
protected  AuthenticationStrategy createTargetAuthenticationStrategy()
           
protected  UserTokenHandler createUserTokenHandler()
           
protected  HttpParams determineParams(HttpRequest req)
          Obtains parameters for executing a request.
 HttpResponse execute(HttpHost target, HttpRequest request)
          Executes a request to the target using the default context.
 HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
          Executes a request to the target using the given context.
<T> T
execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)
          Executes a request using the default context and processes the response using the given response handler.
<T> T
execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
          Executes a request using the default context and processes the response using the given response handler.
 HttpResponse execute(HttpUriRequest request)
          Executes a request using the default context.
 HttpResponse execute(HttpUriRequest request, HttpContext context)
          Maps to execute(target, request, context).
<T> T
execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
          Executes a request using the default context and processes the response using the given response handler.
<T> T
execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
          Executes a request using the default context and processes the response using the given response handler.
 AuthSchemeRegistry getAuthSchemes()
           
 BackoffManager getBackoffManager()
           
 ConnectionBackoffStrategy getConnectionBackoffStrategy()
           
 ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy()
           
 ClientConnectionManager getConnectionManager()
          Obtains the connection manager used by this client.
 ConnectionReuseStrategy getConnectionReuseStrategy()
           
 CookieSpecRegistry getCookieSpecs()
           
 CookieStore getCookieStore()
           
 CredentialsProvider getCredentialsProvider()
           
protected  BasicHttpProcessor getHttpProcessor()
           
 HttpRequestRetryHandler getHttpRequestRetryHandler()
           
 HttpParams getParams()
          Obtains the parameters for this client.
 AuthenticationHandler getProxyAuthenticationHandler()
          Deprecated. (4.2) do not use
 AuthenticationStrategy getProxyAuthenticationStrategy()
           
 RedirectHandler getRedirectHandler()
          Deprecated. (4.1) do not use
 RedirectStrategy getRedirectStrategy()
           
 HttpRequestExecutor getRequestExecutor()
           
 HttpRequestInterceptor getRequestInterceptor(int index)
           
 int getRequestInterceptorCount()
           
 HttpResponseInterceptor getResponseInterceptor(int index)
           
 int getResponseInterceptorCount()
           
 HttpRoutePlanner getRoutePlanner()
           
 AuthenticationHandler getTargetAuthenticationHandler()
          Deprecated. (4.2) do not use
 AuthenticationStrategy getTargetAuthenticationStrategy()
           
 UserTokenHandler getUserTokenHandler()
           
 void removeRequestInterceptorByClass(Class<? extends HttpRequestInterceptor> clazz)
           
 void removeResponseInterceptorByClass(Class<? extends HttpResponseInterceptor> clazz)
           
 void setAuthSchemes(AuthSchemeRegistry registry)
           
 void setBackoffManager(BackoffManager manager)
           
 void setConnectionBackoffStrategy(ConnectionBackoffStrategy strategy)
           
 void setCookieSpecs(CookieSpecRegistry registry)
           
 void setCookieStore(CookieStore cookieStore)
           
 void setCredentialsProvider(CredentialsProvider credsProvider)
           
 void setHttpRequestRetryHandler(HttpRequestRetryHandler handler)
           
 void setKeepAliveStrategy(ConnectionKeepAliveStrategy strategy)
           
 void setParams(HttpParams params)
          Replaces the parameters.
 void setProxyAuthenticationHandler(AuthenticationHandler handler)
          Deprecated. (4.2) do not use
 void setProxyAuthenticationStrategy(AuthenticationStrategy strategy)
           
 void setRedirectHandler(RedirectHandler handler)
          Deprecated. (4.1) do not use
 void setRedirectStrategy(RedirectStrategy strategy)
           
 void setReuseStrategy(ConnectionReuseStrategy strategy)
           
 void setRoutePlanner(HttpRoutePlanner routePlanner)
           
 void setTargetAuthenticationHandler(AuthenticationHandler handler)
          Deprecated. (4.2) do not use
 void setTargetAuthenticationStrategy(AuthenticationStrategy strategy)
           
 void setUserTokenHandler(UserTokenHandler handler)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractHttpClient

protected AbstractHttpClient(ClientConnectionManager conman,
                             HttpParams params)
Creates a new HTTP client.

Parameters:
conman - the connection manager
params - the parameters
Method Detail

createHttpParams

protected abstract HttpParams createHttpParams()

createHttpProcessor

protected abstract BasicHttpProcessor createHttpProcessor()

createHttpContext

protected HttpContext createHttpContext()

createClientConnectionManager

protected ClientConnectionManager createClientConnectionManager()

createAuthSchemeRegistry

protected AuthSchemeRegistry createAuthSchemeRegistry()

createCookieSpecRegistry

protected CookieSpecRegistry createCookieSpecRegistry()

createRequestExecutor

protected HttpRequestExecutor createRequestExecutor()

createConnectionReuseStrategy

protected ConnectionReuseStrategy createConnectionReuseStrategy()

createConnectionKeepAliveStrategy

protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy()

createHttpRequestRetryHandler

protected HttpRequestRetryHandler createHttpRequestRetryHandler()

createRedirectHandler

@Deprecated
protected RedirectHandler createRedirectHandler()
Deprecated. (4.1) do not use


createTargetAuthenticationStrategy

protected AuthenticationStrategy createTargetAuthenticationStrategy()

createTargetAuthenticationHandler

@Deprecated
protected AuthenticationHandler createTargetAuthenticationHandler()
Deprecated. (4.2) do not use


createProxyAuthenticationStrategy

protected AuthenticationStrategy createProxyAuthenticationStrategy()

createProxyAuthenticationHandler

@Deprecated
protected AuthenticationHandler createProxyAuthenticationHandler()
Deprecated. (4.2) do not use


createCookieStore

protected CookieStore createCookieStore()

createCredentialsProvider

protected CredentialsProvider createCredentialsProvider()

createHttpRoutePlanner

protected HttpRoutePlanner createHttpRoutePlanner()

createUserTokenHandler

protected UserTokenHandler createUserTokenHandler()

getParams

public final HttpParams getParams()
Description copied from interface: HttpClient
Obtains the parameters for this client. These parameters will become defaults for all requests being executed with this client, and for the parameters of dependent objects in this client.

Specified by:
getParams in interface HttpClient
Returns:
the default parameters

setParams

public void setParams(HttpParams params)
Replaces the parameters. The implementation here does not update parameters of dependent objects.

Parameters:
params - the new default parameters

getConnectionManager

public final ClientConnectionManager getConnectionManager()
Description copied from interface: HttpClient
Obtains the connection manager used by this client.

Specified by:
getConnectionManager in interface HttpClient
Returns:
the connection manager

getRequestExecutor

public final HttpRequestExecutor getRequestExecutor()

getAuthSchemes

public final AuthSchemeRegistry getAuthSchemes()

setAuthSchemes

public void setAuthSchemes(AuthSchemeRegistry registry)

getConnectionBackoffStrategy

public final ConnectionBackoffStrategy getConnectionBackoffStrategy()

setConnectionBackoffStrategy

public void setConnectionBackoffStrategy(ConnectionBackoffStrategy strategy)

getCookieSpecs

public final CookieSpecRegistry getCookieSpecs()

getBackoffManager

public final BackoffManager getBackoffManager()

setBackoffManager

public void setBackoffManager(BackoffManager manager)

setCookieSpecs

public void setCookieSpecs(CookieSpecRegistry registry)

getConnectionReuseStrategy

public final ConnectionReuseStrategy getConnectionReuseStrategy()

setReuseStrategy

public void setReuseStrategy(ConnectionReuseStrategy strategy)

getConnectionKeepAliveStrategy

public final ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy()

setKeepAliveStrategy

public void setKeepAliveStrategy(ConnectionKeepAliveStrategy strategy)

getHttpRequestRetryHandler

public final HttpRequestRetryHandler getHttpRequestRetryHandler()

setHttpRequestRetryHandler

public void setHttpRequestRetryHandler(HttpRequestRetryHandler handler)

getRedirectHandler

@Deprecated
public final RedirectHandler getRedirectHandler()
Deprecated. (4.1) do not use


setRedirectHandler

@Deprecated
public void setRedirectHandler(RedirectHandler handler)
Deprecated. (4.1) do not use


getRedirectStrategy

public final RedirectStrategy getRedirectStrategy()
Since:
4.1

setRedirectStrategy

public void setRedirectStrategy(RedirectStrategy strategy)
Since:
4.1

getTargetAuthenticationHandler

@Deprecated
public final AuthenticationHandler getTargetAuthenticationHandler()
Deprecated. (4.2) do not use


setTargetAuthenticationHandler

@Deprecated
public void setTargetAuthenticationHandler(AuthenticationHandler handler)
Deprecated. (4.2) do not use


getTargetAuthenticationStrategy

public final AuthenticationStrategy getTargetAuthenticationStrategy()
Since:
4.2

setTargetAuthenticationStrategy

public void setTargetAuthenticationStrategy(AuthenticationStrategy strategy)
Since:
4.2

getProxyAuthenticationHandler

@Deprecated
public final AuthenticationHandler getProxyAuthenticationHandler()
Deprecated. (4.2) do not use


setProxyAuthenticationHandler

@Deprecated
public void setProxyAuthenticationHandler(AuthenticationHandler handler)
Deprecated. (4.2) do not use


getProxyAuthenticationStrategy

public final AuthenticationStrategy getProxyAuthenticationStrategy()
Since:
4.2

setProxyAuthenticationStrategy

public void setProxyAuthenticationStrategy(AuthenticationStrategy strategy)
Since:
4.2

getCookieStore

public final CookieStore getCookieStore()

setCookieStore

public void setCookieStore(CookieStore cookieStore)

getCredentialsProvider

public final CredentialsProvider getCredentialsProvider()

setCredentialsProvider

public void setCredentialsProvider(CredentialsProvider credsProvider)

getRoutePlanner

public final HttpRoutePlanner getRoutePlanner()

setRoutePlanner

public void setRoutePlanner(HttpRoutePlanner routePlanner)

getUserTokenHandler

public final UserTokenHandler getUserTokenHandler()

setUserTokenHandler

public void setUserTokenHandler(UserTokenHandler handler)

getHttpProcessor

protected final BasicHttpProcessor getHttpProcessor()

getResponseInterceptorCount

public int getResponseInterceptorCount()

getResponseInterceptor

public HttpResponseInterceptor getResponseInterceptor(int index)

getRequestInterceptor

public HttpRequestInterceptor getRequestInterceptor(int index)

getRequestInterceptorCount

public int getRequestInterceptorCount()

addResponseInterceptor

public void addResponseInterceptor(HttpResponseInterceptor itcp)

addResponseInterceptor

public void addResponseInterceptor(HttpResponseInterceptor itcp,
                                   int index)

clearResponseInterceptors

public void clearResponseInterceptors()

removeResponseInterceptorByClass

public void removeResponseInterceptorByClass(Class<? extends HttpResponseInterceptor> clazz)

addRequestInterceptor

public void addRequestInterceptor(HttpRequestInterceptor itcp)

addRequestInterceptor

public void addRequestInterceptor(HttpRequestInterceptor itcp,
                                  int index)

clearRequestInterceptors

public void clearRequestInterceptors()

removeRequestInterceptorByClass

public void removeRequestInterceptorByClass(Class<? extends HttpRequestInterceptor> clazz)

execute

public final HttpResponse execute(HttpUriRequest request)
                           throws IOException,
                                  ClientProtocolException
Description copied from interface: HttpClient
Executes a request using the default context.

Specified by:
execute in interface HttpClient
Parameters:
request - the request to execute
Returns:
the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

execute

public final HttpResponse execute(HttpUriRequest request,
                                  HttpContext context)
                           throws IOException,
                                  ClientProtocolException
Maps to execute(target, request, context). The target is determined from the URI of the request.

Specified by:
execute in interface HttpClient
Parameters:
request - the request to execute
context - the request-specific execution context, or null to use a default context
Returns:
the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

execute

public final HttpResponse execute(HttpHost target,
                                  HttpRequest request)
                           throws IOException,
                                  ClientProtocolException
Description copied from interface: HttpClient
Executes a request to the target using the default context.

Specified by:
execute in interface HttpClient
Parameters:
target - the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request - the request to execute
Returns:
the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

execute

public final HttpResponse execute(HttpHost target,
                                  HttpRequest request,
                                  HttpContext context)
                           throws IOException,
                                  ClientProtocolException
Description copied from interface: HttpClient
Executes a request to the target using the given context.

Specified by:
execute in interface HttpClient
Parameters:
target - the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request - the request to execute
context - the context to use for the execution, or null to use the default context
Returns:
the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

createClientRequestDirector

@Deprecated
protected RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec,
                                                                 ClientConnectionManager conman,
                                                                 ConnectionReuseStrategy reustrat,
                                                                 ConnectionKeepAliveStrategy kastrat,
                                                                 HttpRoutePlanner rouplan,
                                                                 HttpProcessor httpProcessor,
                                                                 HttpRequestRetryHandler retryHandler,
                                                                 RedirectHandler redirectHandler,
                                                                 AuthenticationHandler targetAuthHandler,
                                                                 AuthenticationHandler proxyAuthHandler,
                                                                 UserTokenHandler userTokenHandler,
                                                                 HttpParams params)
Deprecated. (4.1) do not use


createClientRequestDirector

@Deprecated
protected RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec,
                                                                 ClientConnectionManager conman,
                                                                 ConnectionReuseStrategy reustrat,
                                                                 ConnectionKeepAliveStrategy kastrat,
                                                                 HttpRoutePlanner rouplan,
                                                                 HttpProcessor httpProcessor,
                                                                 HttpRequestRetryHandler retryHandler,
                                                                 RedirectStrategy redirectStrategy,
                                                                 AuthenticationHandler targetAuthHandler,
                                                                 AuthenticationHandler proxyAuthHandler,
                                                                 UserTokenHandler userTokenHandler,
                                                                 HttpParams params)
Deprecated. (4.2) do not use


createClientRequestDirector

protected RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec,
                                                      ClientConnectionManager conman,
                                                      ConnectionReuseStrategy reustrat,
                                                      ConnectionKeepAliveStrategy kastrat,
                                                      HttpRoutePlanner rouplan,
                                                      HttpProcessor httpProcessor,
                                                      HttpRequestRetryHandler retryHandler,
                                                      RedirectStrategy redirectStrategy,
                                                      AuthenticationStrategy targetAuthStrategy,
                                                      AuthenticationStrategy proxyAuthStrategy,
                                                      UserTokenHandler userTokenHandler,
                                                      HttpParams params)
Since:
4.2

determineParams

protected HttpParams determineParams(HttpRequest req)
Obtains parameters for executing a request. The default implementation in this class creates a new ClientParamsStack from the request parameters and the client parameters.
This method is called by the default implementation of execute(HttpHost,HttpRequest,HttpContext) to obtain the parameters for the DefaultRequestDirector.

Parameters:
req - the request that will be executed
Returns:
the parameters to use

execute

public <T> T execute(HttpUriRequest request,
                     ResponseHandler<? extends T> responseHandler)
          throws IOException,
                 ClientProtocolException
Executes a request using the default context and processes the response using the given response handler. The content entity associated with the response is fully consumed and the underlying connection is released back to the connection manager automatically in all cases relieving individual ResponseHandlers from having to manage resource deallocation internally.

Specified by:
execute in interface HttpClient
Parameters:
request - the request to execute
responseHandler - the response handler
Returns:
the response object as generated by the response handler.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

execute

public <T> T execute(HttpUriRequest request,
                     ResponseHandler<? extends T> responseHandler,
                     HttpContext context)
          throws IOException,
                 ClientProtocolException
Executes a request using the default context and processes the response using the given response handler. The content entity associated with the response is fully consumed and the underlying connection is released back to the connection manager automatically in all cases relieving individual ResponseHandlers from having to manage resource deallocation internally.

Specified by:
execute in interface HttpClient
Parameters:
request - the request to execute
responseHandler - the response handler
context - the context to use for the execution, or null to use the default context
Returns:
the response object as generated by the response handler.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

execute

public <T> T execute(HttpHost target,
                     HttpRequest request,
                     ResponseHandler<? extends T> responseHandler)
          throws IOException,
                 ClientProtocolException
Executes a request using the default context and processes the response using the given response handler. The content entity associated with the response is fully consumed and the underlying connection is released back to the connection manager automatically in all cases relieving individual ResponseHandlers from having to manage resource deallocation internally.

Specified by:
execute in interface HttpClient
Parameters:
target - the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request - the request to execute
responseHandler - the response handler
Returns:
the response object as generated by the response handler.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error

execute

public <T> T execute(HttpHost target,
                     HttpRequest request,
                     ResponseHandler<? extends T> responseHandler,
                     HttpContext context)
          throws IOException,
                 ClientProtocolException
Executes a request using the default context and processes the response using the given response handler. The content entity associated with the response is fully consumed and the underlying connection is released back to the connection manager automatically in all cases relieving individual ResponseHandlers from having to manage resource deallocation internally.

Specified by:
execute in interface HttpClient
Parameters:
target - the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request - the request to execute
responseHandler - the response handler
context - the context to use for the execution, or null to use the default context
Returns:
the response object as generated by the response handler.
Throws:
IOException - in case of a problem or the connection was aborted
ClientProtocolException - in case of an http protocol error


Copyright © 1999-2013 The Apache Software Foundation. All Rights Reserved.