public class ProxyServlet
extends javax.servlet.http.HttpServlet
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.
Modifier and Type | Field and Description |
---|---|
protected boolean |
acceptSelfSignedCerts |
protected static BitSet |
asciiQueryChars |
static String |
DISABLE_PROXY |
protected boolean |
doForwardIP |
protected boolean |
doLog |
protected boolean |
enabled |
static String |
HAWTIO_DISABLE_PROXY |
static String |
HAWTIO_LOCAL_ADDRESS_PROBING |
static String |
HAWTIO_PROXY_WHITELIST |
protected static org.apache.http.message.HeaderGroup |
hopByHopHeaders
These are the "hop-by-hop" headers that should not be copied.
|
static String |
LOCAL_ADDRESS_PROBING |
static String |
P_FORWARDEDFOR
A boolean parameter name to enable forwarding of the client IP
|
static String |
P_LOG
Deprecated.
Use SLF4J
Logger |
static String |
PROXY_WHITELIST |
protected org.apache.http.impl.client.CloseableHttpClient |
proxyClient |
protected ProxyWhitelist |
whitelist |
Constructor and Description |
---|
ProxyServlet() |
Modifier and Type | Method and Description |
---|---|
protected void |
copyRequestHeaders(javax.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,
javax.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,
javax.servlet.http.HttpServletResponse servletResponse)
Copy proxied response headers back to the servlet client.
|
void |
destroy() |
protected boolean |
doResponseRedirectOrNotModifiedLogic(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.HttpResponse proxyResponse,
int statusCode,
URI targetUriObj) |
protected static CharSequence |
encodeUriQuery(CharSequence in)
Encodes characters in the query or fragment part of the URI.
|
String |
getServletInfo() |
void |
init(javax.servlet.ServletConfig servletConfig) |
protected ProxyAddress |
parseProxyAddress(javax.servlet.http.HttpServletRequest servletRequest) |
protected String |
rewriteUrlFromResponse(javax.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(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse) |
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
@Deprecated public static final String P_LOG
Logger
public static final String P_FORWARDEDFOR
public static final String PROXY_WHITELIST
public static final String LOCAL_ADDRESS_PROBING
public static final String DISABLE_PROXY
public static final String HAWTIO_PROXY_WHITELIST
public static final String HAWTIO_LOCAL_ADDRESS_PROBING
public static final String HAWTIO_DISABLE_PROXY
protected boolean enabled
protected boolean doLog
protected boolean doForwardIP
protected boolean acceptSelfSignedCerts
protected ProxyWhitelist whitelist
protected org.apache.http.impl.client.CloseableHttpClient proxyClient
protected static final org.apache.http.message.HeaderGroup hopByHopHeaders
protected static final BitSet asciiQueryChars
public String getServletInfo()
getServletInfo
in interface javax.servlet.Servlet
getServletInfo
in class javax.servlet.GenericServlet
public void init(javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
javax.servlet.ServletException
public void destroy()
destroy
in interface javax.servlet.Servlet
destroy
in class javax.servlet.GenericServlet
protected void service(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse) throws javax.servlet.ServletException, IOException
service
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
IOException
protected ProxyAddress parseProxyAddress(javax.servlet.http.HttpServletRequest servletRequest)
protected boolean doResponseRedirectOrNotModifiedLogic(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, URI targetUriObj) throws javax.servlet.ServletException, IOException
javax.servlet.ServletException
IOException
protected void copyRequestHeaders(javax.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, URI targetUriObj)
protected void copyResponseHeaders(org.apache.http.HttpResponse proxyResponse, javax.servlet.http.HttpServletResponse servletResponse)
protected void copyResponseEntity(org.apache.http.HttpResponse proxyResponse, javax.servlet.http.HttpServletResponse servletResponse) throws IOException
IOException
protected String rewriteUrlFromResponse(javax.servlet.http.HttpServletRequest servletRequest, String theUrl, String targetUri)
theUrl
to redirect to
and translates it to one the original client can use.protected static CharSequence encodeUriQuery(CharSequence in)
Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient
insists that the outgoing proxied request has a valid URI because it uses Java's URI
.
To be more forgiving, we must escape the problematic characters. See the URI class for the
spec.
in
- example: name=value&foo=bar#fragmentCopyright © 2021 JBoss by Red Hat. All rights reserved.