Class ContextHandlingHttpClient
- java.lang.Object
-
- org.apache.http.impl.client.CloseableHttpClient
-
- net.shibboleth.utilities.java.support.httpclient.ContextHandlingHttpClient
-
- All Implemented Interfaces:
Closeable,AutoCloseable,HttpClient
class ContextHandlingHttpClient extends CloseableHttpClient
A wrapper implementation ofHttpClientwhich invokes supplied instances ofHttpClientContextHandlerbefore and after request execution.By definition the handlers will only be invoked for the
HttpClientexecute(...) method variants which take anHttpContextargument.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>handlersOptional list of static handlers supplied to this class instance.private CloseableHttpClienthttpClientThe wrapped client instance.private LoggerlogLogger.
-
Constructor Summary
Constructors Constructor Description ContextHandlingHttpClient(CloseableHttpClient client)Constructor.ContextHandlingHttpClient(CloseableHttpClient client, List<HttpClientContextHandler> staticHandlers)Constructor.
-
Method Summary
-
-
-
Field Detail
-
log
private Logger log
Logger.
-
httpClient
@Nonnull private CloseableHttpClient httpClient
The wrapped client instance.
-
handlers
@Nonnull private List<HttpClientContextHandler> handlers
Optional list of static handlers supplied to this class instance.
-
-
Constructor Detail
-
ContextHandlingHttpClient
public ContextHandlingHttpClient(@Nonnull CloseableHttpClient client)Constructor.- Parameters:
client- the wrapped client instance
-
ContextHandlingHttpClient
public ContextHandlingHttpClient(@Nonnull CloseableHttpClient client, @Nonnull List<HttpClientContextHandler> staticHandlers)Constructor.- Parameters:
client- the wrapped client instancestaticHandlers- the list of static handlers
-
-
Method Detail
-
getParams
public HttpParams getParams()
-
getConnectionManager
public ClientConnectionManager getConnectionManager()
-
close
public void close() throws IOException- Throws:
IOException
-
doExecute
protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException
- Specified by:
doExecutein classCloseableHttpClient- Throws:
IOExceptionClientProtocolException
-
invokeBefore
private void invokeBefore(HttpUriRequest request, HttpClientContext context) throws IOException
InvokeHttpClientContextHandler.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(HttpUriRequest request, HttpClientContext context, Throwable priorError) throws IOException
InvokeHttpClientContextHandler.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
private IOException processHandlerErrors(String stage, List<Throwable> errors)
Process the error(s) seen duringinvokeBefore(HttpUriRequest, HttpClientContext)orinvokeAfter(HttpUriRequest, HttpClientContext, Throwable)into a singleIOExceptionthat 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 IOException
Process 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.
-
-