Class ProxyHandler

All Implemented Interfaces:
Handler, Request.Handler, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable
Direct Known Subclasses:
ProxyHandler.Forward, ProxyHandler.Reverse

public abstract class ProxyHandler extends Handler.Abstract

A Handler that can be used to implement a forward proxy ("proxy") or a reverse proxy ("gateway") as defined by RFC 7230.

This class uses HttpClient to send requests from the proxy to the server.

The HttpClient instance is either set explicitly, or created implicitly. To customize the implicit HttpClient instance, applications can override newHttpClient() and configureHttpClient(HttpClient).

See Also:
  • Constructor Details

    • ProxyHandler

      public ProxyHandler()
  • Method Details

    • getHttpClient

      public HttpClient getHttpClient()
    • setHttpClient

      public void setHttpClient(HttpClient httpClient)
    • getProxyToServerHost

      public String getProxyToServerHost()
      Get the proxy-to-server Host header value.
      Returns:
      the proxy-to-server Host header value
    • setProxyToServerHost

      public void setProxyToServerHost(String host)

      Sets the value to use for the Host header in proxy-to-server requests.

      If null, the client-to-proxy value is used.

      Parameters:
      host - the proxy-to-server Host header value
    • getViaHost

      public String getViaHost()
      Get the value to use for the Via header.
      Returns:
      the value to use for the Via header
    • setViaHost

      public void setViaHost(String viaHost)

      Sets the value to use for the Via header in proxy-to-server requests.

      If null, the local host name is used.

      Parameters:
      viaHost - the value to use for the Via header
    • doStart

      protected void doStart() throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the managed lifecycle beans in the order they were added.
      Overrides:
      doStart in class Handler.Abstract
      Throws:
      AbstractLifeCycle.StopException - If thrown, the lifecycle will immediately be stopped.
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • newHttpClient

      protected HttpClient newHttpClient()

      Creates a new HttpClient instance, by default with a thread pool named proxy-client and with the dynamic transport configured only with HTTP/1.1.

      Returns:
      a new HttpClient instance
    • configureHttpClient

      protected void configureHttpClient(HttpClient httpClient)

      Configures the HttpClient instance before it is started.

      Parameters:
      httpClient - the HttpClient instance to configure
    • requestId

      protected static String requestId(Request clientToProxyRequest)
    • handle

      public boolean handle(Request clientToProxyRequest, Response proxyToClientResponse, Callback proxyToClientCallback)
      Description copied from interface: Request.Handler

      Invoked to handle the passed HTTP request and response.

      The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

      Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

      The simplest implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           callback.succeeded();
           return true;
       }
       

      A HelloWorld implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
           return true;
       }
       
      Parameters:
      clientToProxyRequest - the HTTP request to handle
      proxyToClientResponse - the HTTP response to handle
      proxyToClientCallback - the callback to complete when the handling is complete
      Returns:
      True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
      See Also:
    • rewriteHttpURI

      protected abstract HttpURI rewriteHttpURI(Request clientToProxyRequest)

      Rewrites the client-to-proxy request URI to the proxy-to-server request URI.

      Parameters:
      clientToProxyRequest - the client-to-proxy request
      Returns:
      an HttpURI for the proxy-to-server request
    • newProxyToServerRequest

      protected Request newProxyToServerRequest(Request clientToProxyRequest, HttpURI newHttpURI)
    • copyRequestHeaders

      protected void copyRequestHeaders(Request clientToProxyRequest, Request proxyToServerRequest)
    • addProxyHeaders

      protected void addProxyHeaders(Request clientToProxyRequest, Request proxyToServerRequest)
    • addViaHeader

      protected void addViaHeader(Request clientToProxyRequest, Request proxyToServerRequest)
    • addForwardedHeader

      protected void addForwardedHeader(Request clientToProxyRequest, Request proxyToServerRequest)
    • newProxyToServerRequestContent

      protected Request.Content newProxyToServerRequestContent(Request clientToProxyRequest, Response proxyToClientResponse, Request proxyToServerRequest)
    • sendProxyToServerRequest

      protected void sendProxyToServerRequest(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback)
    • newServerToProxyResponseListener

      protected Response.CompleteListener newServerToProxyResponseListener(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback)
    • filterServerToProxyResponseField

      protected HttpField filterServerToProxyResponseField(HttpField serverToProxyResponseField)
    • onServerToProxyResponseFailure

      protected void onServerToProxyResponseFailure(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback, Throwable failure)
    • onServerToProxyResponse100Continue

      protected Runnable onServerToProxyResponse100Continue(Request clientToProxyRequest, Request proxyToServerRequest)
    • onServerToProxyResponse102Processing

      protected void onServerToProxyResponse102Processing(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse)
    • onServerToProxyResponse103EarlyHints

      protected void onServerToProxyResponse103EarlyHints(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse)
    • onProxyToClientResponseComplete

      protected void onProxyToClientResponseComplete(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback)
    • onProxyToClientResponseFailure

      protected void onProxyToClientResponseFailure(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback, Throwable failure)