Class ContextHandlingHttpClient
java.lang.Object
org.apache.http.impl.client.CloseableHttpClient
net.shibboleth.utilities.java.support.httpclient.ContextHandlingHttpClient
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.apache.http.client.HttpClient
class ContextHandlingHttpClient
extends org.apache.http.impl.client.CloseableHttpClient
A wrapper implementation of
HttpClient
which invokes supplied instances of HttpClientContextHandler
before and after request execution.
By definition the handlers will only be invoked for the HttpClient
execute(...) method variants
which take an HttpContext
argument.
The order of execution is:
- Static handlers supplied via the constructor, in original list order
- Dynamic handlers from the context attribute
HttpClientSupport.CONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERS
, in original list order - the wrapped client's corresponding execute(...) method
- Dynamic handlers from the context attribute
HttpClientSupport.CONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERS
, in reverse list order - Static handlers supplied via the constructor, in reverse list order
-
Field Summary
Fields Modifier and Type Field Description private List<HttpClientContextHandler>
handlers
Optional list of static handlers supplied to this class instance.private org.apache.http.impl.client.CloseableHttpClient
httpClient
The wrapped client instance.private org.slf4j.Logger
log
Logger. -
Constructor Summary
Constructors Constructor Description ContextHandlingHttpClient(org.apache.http.impl.client.CloseableHttpClient client)
Constructor.ContextHandlingHttpClient(org.apache.http.impl.client.CloseableHttpClient client, List<HttpClientContextHandler> staticHandlers)
Constructor. -
Method Summary
Modifier and Type Method Description void
close()
protected org.apache.http.client.methods.CloseableHttpResponse
doExecute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)
org.apache.http.conn.ClientConnectionManager
getConnectionManager()
Deprecated.org.apache.http.params.HttpParams
getParams()
Deprecated.private void
invokeAfter(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.protocol.HttpClientContext context, Throwable priorError)
InvokeHttpClientContextHandler.invokeAfter(HttpClientContext, HttpUriRequest)
for all supplied handlers.private void
invokeBefore(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.protocol.HttpClientContext context)
InvokeHttpClientContextHandler.invokeBefore(HttpClientContext, HttpUriRequest)
for supplied handlers.private void
processErrorsForInvokeAfter(IOException invokeAfterException, Throwable priorError)
Process errors forinvokeAfter(HttpUriRequest, HttpClientContext, Throwable)
.private IOException
processHandlerErrors(String stage, List<Throwable> errors)
Process the error(s) seen duringinvokeBefore(HttpUriRequest, HttpClientContext)
orinvokeAfter(HttpUriRequest, HttpClientContext, Throwable)
into a singleIOException
that will be propagated out of that method.Methods inherited from class org.apache.http.impl.client.CloseableHttpClient
execute, execute, execute, execute, execute, execute, execute, execute
-
Field Details
-
log
private org.slf4j.Logger logLogger. -
httpClient
@Nonnull private org.apache.http.impl.client.CloseableHttpClient httpClientThe wrapped client instance. -
handlers
Optional list of static handlers supplied to this class instance.
-
-
Constructor Details
-
ContextHandlingHttpClient
public ContextHandlingHttpClient(@Nonnull org.apache.http.impl.client.CloseableHttpClient client)Constructor.- Parameters:
client
- the wrapped client instance
-
ContextHandlingHttpClient
public ContextHandlingHttpClient(@Nonnull org.apache.http.impl.client.CloseableHttpClient client, @Nonnull List<HttpClientContextHandler> staticHandlers)Constructor.- Parameters:
client
- the wrapped client instancestaticHandlers
- the list of static handlers
-
-
Method Details
-
getParams
Deprecated. -
getConnectionManager
Deprecated. -
close
- Throws:
IOException
-
doExecute
protected org.apache.http.client.methods.CloseableHttpResponse doExecute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context) throws IOException, org.apache.http.client.ClientProtocolException- Specified by:
doExecute
in classorg.apache.http.impl.client.CloseableHttpClient
- Throws:
IOException
org.apache.http.client.ClientProtocolException
-
invokeBefore
private void invokeBefore(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.protocol.HttpClientContext context) throws IOExceptionInvokeHttpClientContextHandler.invokeBefore(HttpClientContext, HttpUriRequest)
for supplied handlers.- Parameters:
request
- the HTTP requestcontext
- the HTTP context- Throws:
IOException
- if any handler throws an error
-
invokeAfter
private void invokeAfter(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.protocol.HttpClientContext context, Throwable priorError) throws IOExceptionInvokeHttpClientContextHandler.invokeAfter(HttpClientContext, HttpUriRequest)
for all supplied handlers.- Parameters:
request
- the HTTP requestcontext
- the HTTP contextpriorError
- an error thrown by by eitherinvokeBefore(HttpUriRequest, HttpClientContext)
or by HttpClient execute(...).- Throws:
IOException
- if any handler throws an error, or if priorError is an IOException. If priorError is a type of unchecked error (RuntimeException or Error) that will be propagated out here as well.
-
processHandlerErrors
Process the error(s) seen duringinvokeBefore(HttpUriRequest, HttpClientContext)
orinvokeAfter(HttpUriRequest, HttpClientContext, Throwable)
into a singleIOException
that will be propagated out of that method.- Parameters:
stage
- the name of the stage, for reporting purposeserrors
- all errors seen during the method execution- Returns:
- the single exception to be propagated out, will be null if no errors present
-
processErrorsForInvokeAfter
private void processErrorsForInvokeAfter(IOException invokeAfterException, Throwable priorError) throws IOExceptionProcess errors forinvokeAfter(HttpUriRequest, HttpClientContext, Throwable)
.- Parameters:
invokeAfterException
- the exception thrown by invokeAfter handlers, if anypriorError
- an error thrown by by eitherinvokeBefore(HttpUriRequest, HttpClientContext)
or by HttpClient execute(...), if any.- Throws:
IOException
- if invokeAfterException is non-null, or if priorError is an IOException. If priorError is a type of unchecked error (RuntimeException or Error) that will be propagated out here as well.
-