Package net.shibboleth.shared.httpclient
Class ContextHandlingHttpClient
java.lang.Object
net.shibboleth.shared.httpclient.AbstractHttpClient
net.shibboleth.shared.httpclient.ContextHandlingHttpClient
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.hc.client5.http.classic.HttpClient,org.apache.hc.core5.io.ModalCloseable
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
FieldsModifier and TypeFieldDescriptionprivate List<HttpClientContextHandler>Optional list of static handlers supplied to this class instance.private org.apache.hc.client5.http.classic.HttpClientThe wrapped client instance.private org.slf4j.LoggerLogger. -
Constructor Summary
ConstructorsConstructorDescriptionContextHandlingHttpClient(org.apache.hc.client5.http.classic.HttpClient client) Constructor.ContextHandlingHttpClient(org.apache.hc.client5.http.classic.HttpClient client, List<HttpClientContextHandler> staticHandlers) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidclose(org.apache.hc.core5.io.CloseMode closeMode) protected org.apache.hc.core5.http.ClassicHttpResponsedoExecute(org.apache.hc.core5.http.HttpHost target, org.apache.hc.core5.http.ClassicHttpRequest request, org.apache.hc.core5.http.protocol.HttpContext context) Execute the HTTP request.private voidinvokeAfter(org.apache.hc.core5.http.ClassicHttpRequest request, org.apache.hc.client5.http.protocol.HttpClientContext context, Throwable priorError) InvokeHttpClientContextHandler.invokeAfter(HttpClientContext, ClassicHttpRequest)for all supplied handlers.private voidinvokeBefore(org.apache.hc.core5.http.ClassicHttpRequest request, org.apache.hc.client5.http.protocol.HttpClientContext context) InvokeHttpClientContextHandler.invokeBefore(HttpClientContext, ClassicHttpRequest)for supplied handlers.private voidprocessErrorsForInvokeAfter(IOException invokeAfterException, Throwable priorError) Process errors forinvokeAfter(ClassicHttpRequest, HttpClientContext, Throwable).private IOExceptionprocessHandlerErrors(String stage, List<Throwable> errors) Process the error(s) seen duringinvokeBefore(ClassicHttpRequest, HttpClientContext)orinvokeAfter(ClassicHttpRequest, HttpClientContext, Throwable)into a singleIOExceptionthat will be propagated out of that method.Methods inherited from class net.shibboleth.shared.httpclient.AbstractHttpClient
execute, execute, execute, execute, execute, execute, execute, executeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.hc.client5.http.classic.HttpClient
executeOpen
-
Field Details
-
log
@Nonnull private org.slf4j.Logger logLogger. -
httpClient
@Nonnull private org.apache.hc.client5.http.classic.HttpClient httpClientThe wrapped client instance. -
handlers
Optional list of static handlers supplied to this class instance.
-
-
Constructor Details
-
ContextHandlingHttpClient
public ContextHandlingHttpClient(@Nonnull org.apache.hc.client5.http.classic.HttpClient client) Constructor.- Parameters:
client- the wrapped client instance
-
ContextHandlingHttpClient
public ContextHandlingHttpClient(@Nonnull org.apache.hc.client5.http.classic.HttpClient client, @Nonnull List<HttpClientContextHandler> staticHandlers) Constructor.- Parameters:
client- the wrapped client instancestaticHandlers- the list of static handlers
-
-
Method Details
-
close
- Throws:
IOException
-
close
public void close(org.apache.hc.core5.io.CloseMode closeMode) -
doExecute
protected org.apache.hc.core5.http.ClassicHttpResponse doExecute(@Nullable org.apache.hc.core5.http.HttpHost target, @Nonnull org.apache.hc.core5.http.ClassicHttpRequest request, @Nullable org.apache.hc.core5.http.protocol.HttpContext context) throws IOException Execute the HTTP request.- Specified by:
doExecutein classAbstractHttpClient- Parameters:
target- the target host for the request, may benullrequest- the request to executecontext- the context to use for the execution, ornullto use the default context- Returns:
- the response to the request
- Throws:
IOException- in case of a problem or the connection was aborted
-
invokeBefore
private void invokeBefore(@Nonnull org.apache.hc.core5.http.ClassicHttpRequest request, @Nonnull org.apache.hc.client5.http.protocol.HttpClientContext context) throws IOException InvokeHttpClientContextHandler.invokeBefore(HttpClientContext, ClassicHttpRequest)for supplied handlers.- Parameters:
request- the HTTP requestcontext- the HTTP context- Throws:
IOException- if any handler throws an error
-
invokeAfter
private void invokeAfter(@Nonnull org.apache.hc.core5.http.ClassicHttpRequest request, @Nonnull org.apache.hc.client5.http.protocol.HttpClientContext context, @Nullable Throwable priorError) throws IOException InvokeHttpClientContextHandler.invokeAfter(HttpClientContext, ClassicHttpRequest)for all supplied handlers.- Parameters:
request- the HTTP requestcontext- the HTTP contextpriorError- an error thrown by by eitherinvokeBefore(ClassicHttpRequest, 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(ClassicHttpRequest, HttpClientContext)orinvokeAfter(ClassicHttpRequest, 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(@Nullable IOException invokeAfterException, @Nullable Throwable priorError) throws IOException Process errors forinvokeAfter(ClassicHttpRequest, HttpClientContext, Throwable).- Parameters:
invokeAfterException- the exception thrown by invokeAfter handlers, if anypriorError- an error thrown by by eitherinvokeBefore(ClassicHttpRequest, 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.
-