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

class ContextHandlingHttpClient extends AbstractHttpClient
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:

  1. Static handlers supplied via the constructor, in original list order
  2. Dynamic handlers from the context attribute HttpClientSupport.CONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERS, in original list order
  3. the wrapped client's corresponding execute(...) method
  4. Dynamic handlers from the context attribute HttpClientSupport.CONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERS, in reverse list order
  5. Static handlers supplied via the constructor, in reverse list order
  • Field Details

    • log

      @Nonnull private org.slf4j.Logger log
      Logger.
    • httpClient

      @Nonnull private org.apache.hc.client5.http.classic.HttpClient httpClient
      The wrapped client instance.
    • handlers

      @Nonnull private List<HttpClientContextHandler> 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 instance
      staticHandlers - the list of static handlers
  • Method Details

    • close

      public void close() throws IOException
      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:
      doExecute in class AbstractHttpClient
      Parameters:
      target - the target host for the request, may be null
      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
      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
      Parameters:
      request - the HTTP request
      context - 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
      Parameters:
      request - the HTTP request
      context - the HTTP context
      priorError - an error thrown by by either invokeBefore(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

      @Nullable private IOException processHandlerErrors(String stage, @Nonnull List<Throwable> errors)
      Process the error(s) seen during invokeBefore(ClassicHttpRequest, HttpClientContext) or invokeAfter(ClassicHttpRequest, HttpClientContext, Throwable) into a single IOException that will be propagated out of that method.
      Parameters:
      stage - the name of the stage, for reporting purposes
      errors - 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
      Parameters:
      invokeAfterException - the exception thrown by invokeAfter handlers, if any
      priorError - an error thrown by by either invokeBefore(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.