Class ProxyHandler.Reverse

All Implemented Interfaces:
Handler, Request.Handler, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable
Direct Known Subclasses:
FastCGIProxyHandler
Enclosing class:
ProxyHandler

public static class ProxyHandler.Reverse extends ProxyHandler

A ProxyHandler that can be used to implement a reverse proxy.

A reverse proxy must rewrite the client-to-proxy request URI into the proxy-to-server request URI. This can be done by providing a rewrite function to the constructor, and/or override rewriteHttpURI(Request).

See Also:
  • Constructor Details

    • Reverse

      public Reverse(String uriPattern, String uriReplacement)

      Convenience constructor that provides a rewrite function using String.replaceAll(String, String).

      As a simple example, given the URI pattern of:

      (https?)://([a-z]+):([0-9]+)/([^/]+)/(.*)

      and given a replacement string of:

      $1://$2:9000/proxy/$5

      an incoming HttpURI of:

      http://host:8080/ctx/path

      will be rewritten as:

      http://host:9000/proxy/path

      Parameters:
      uriPattern - the regex pattern to use to match the incoming URI
      uriReplacement - the replacement string to use to rewrite the incoming URI
    • Reverse

      public Reverse(Function<Request,HttpURI> httpURIRewriter)

      Creates a new instance with the given HttpURI rewrite function.

      The rewrite functions rewrites the client-to-proxy request URI into the proxy-to-server request URI.

      Parameters:
      httpURIRewriter - a function that returns the URI of the server
  • Method Details

    • getHttpURIRewriter

      public Function<Request,HttpURI> getHttpURIRewriter()
    • rewriteHttpURI

      protected HttpURI rewriteHttpURI(Request clientToProxyRequest)

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

      Applications that use this class typically provide a rewrite function to the constructor.

      The rewrite function rewrites the client-to-proxy request URI, for example http://example.com/app/path, to the proxy-to-server request URI, for example http://backend1:8080/legacy/path.

      Specified by:
      rewriteHttpURI in class ProxyHandler
      Parameters:
      clientToProxyRequest - the client-to-proxy request
      Returns:
      the proxy-to-server request URI.