Class ProxyServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
io.hawt.web.proxy.ProxyServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

public class ProxyServlet extends jakarta.servlet.http.HttpServlet
An HTTP reverse proxy/gateway servlet. It is designed to be extended for customization if desired. Most of the work is handled by Apache HttpClient.

There are alternatives to a servlet based proxy such as Apache mod_proxy if that is available to you. However this servlet is easily customizable by Java, secure-able by your web application's security (e.g. spring-security), portable across servlet engines, and is embeddable into another web application.

Inspiration: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html

Original implementation at https://github.com/mitre/HTTP-Proxy-Servlet, released under ASL 2.0.

Author:
David Smiley dsmiley@mitre.org
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected ProxyAllowlist
     
    protected static final BitSet
     
    static final String
     
    protected boolean
     
    protected boolean
     
    static final String
     
    static final String
     
    static final String
     
    protected static final org.apache.http.message.HeaderGroup
    These are the "hop-by-hop" headers that should not be copied.
    static final String
     
    static final String
    A boolean parameter name to enable forwarding of the client IP
    static final String
     
    protected org.apache.http.impl.client.CloseableHttpClient
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    copyRequestHeaders(jakarta.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, URI targetUriObj)
    Copy request headers from the servlet client to the proxy request.
    protected void
    copyResponseEntity(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse)
    Copy response body data (the entity) from the proxy to the servlet client.
    protected void
    copyResponseHeaders(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse)
    Copy proxied response headers back to the servlet client.
    void
     
    protected boolean
    doResponseRedirectOrNotModifiedLogic(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, URI targetUriObj)
     
     
    void
    init(jakarta.servlet.ServletConfig servletConfig)
     
    protected ProxyAddress
    parseProxyAddress(jakarta.servlet.http.HttpServletRequest servletRequest)
     
    protected String
    rewriteUrlFromResponse(jakarta.servlet.http.HttpServletRequest servletRequest, String theUrl, String targetUri)
    For a redirect response from the target server, this translates theUrl to redirect to and translates it to one the original client can use.
    protected void
    service(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse)
     

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service

    Methods inherited from class jakarta.servlet.GenericServlet

    getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • P_FORWARDEDFOR

      public static final String P_FORWARDEDFOR
      A boolean parameter name to enable forwarding of the client IP
      See Also:
    • PROXY_ALLOWLIST

      public static final String PROXY_ALLOWLIST
      See Also:
    • LOCAL_ADDRESS_PROBING

      public static final String LOCAL_ADDRESS_PROBING
      See Also:
    • DISABLE_PROXY

      public static final String DISABLE_PROXY
      See Also:
    • HAWTIO_PROXY_ALLOWLIST

      public static final String HAWTIO_PROXY_ALLOWLIST
      See Also:
    • HAWTIO_LOCAL_ADDRESS_PROBING

      public static final String HAWTIO_LOCAL_ADDRESS_PROBING
      See Also:
    • HAWTIO_DISABLE_PROXY

      public static final String HAWTIO_DISABLE_PROXY
      See Also:
    • enabled

      protected boolean enabled
    • doForwardIP

      protected boolean doForwardIP
    • acceptSelfSignedCerts

      protected boolean acceptSelfSignedCerts
    • allowlist

      protected ProxyAllowlist allowlist
    • proxyClient

      protected org.apache.http.impl.client.CloseableHttpClient proxyClient
    • hopByHopHeaders

      protected static final org.apache.http.message.HeaderGroup hopByHopHeaders
      These are the "hop-by-hop" headers that should not be copied. http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html I use an HttpClient HeaderGroup class instead of Set because this approach does case-insensitive lookup faster.
    • asciiQueryChars

      protected static final BitSet asciiQueryChars
  • Constructor Details

    • ProxyServlet

      public ProxyServlet()
  • Method Details

    • getServletInfo

      public String getServletInfo()
      Specified by:
      getServletInfo in interface jakarta.servlet.Servlet
      Overrides:
      getServletInfo in class jakarta.servlet.GenericServlet
    • init

      public void init(jakarta.servlet.ServletConfig servletConfig) throws jakarta.servlet.ServletException
      Specified by:
      init in interface jakarta.servlet.Servlet
      Overrides:
      init in class jakarta.servlet.GenericServlet
      Throws:
      jakarta.servlet.ServletException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface jakarta.servlet.Servlet
      Overrides:
      destroy in class jakarta.servlet.GenericServlet
    • service

      protected void service(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse) throws IOException
      Overrides:
      service in class jakarta.servlet.http.HttpServlet
      Throws:
      IOException
    • parseProxyAddress

      protected ProxyAddress parseProxyAddress(jakarta.servlet.http.HttpServletRequest servletRequest)
    • doResponseRedirectOrNotModifiedLogic

      protected boolean doResponseRedirectOrNotModifiedLogic(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, URI targetUriObj) throws jakarta.servlet.ServletException, IOException
      Throws:
      jakarta.servlet.ServletException
      IOException
    • copyRequestHeaders

      protected void copyRequestHeaders(jakarta.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, URI targetUriObj)
      Copy request headers from the servlet client to the proxy request.
    • copyResponseHeaders

      protected void copyResponseHeaders(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse)
      Copy proxied response headers back to the servlet client.
    • copyResponseEntity

      protected void copyResponseEntity(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) throws IOException
      Copy response body data (the entity) from the proxy to the servlet client.
      Throws:
      IOException
    • rewriteUrlFromResponse

      protected String rewriteUrlFromResponse(jakarta.servlet.http.HttpServletRequest servletRequest, String theUrl, String targetUri)
      For a redirect response from the target server, this translates theUrl to redirect to and translates it to one the original client can use.