Class ProxyServlet
java.lang.Object
org.ocpsoft.rewrite.servlet.config.proxy.ProxyServlet
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
- Author:
- David Smiley dsmiley@mitre.org
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final BitSetprotected booleanprotected static final org.apache.http.message.HeaderGroupThese are the "hop-by-hop" headers that should not be copied.static final StringA boolean parameter name to enable logging of input and target URLs to the servlet log.protected org.apache.http.impl.client.CloseableHttpClientprotected Stringprotected URI -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcloseQuietly(Closeable closeable) protected voidcopyRequestHeaders(jakarta.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest) Copy request headers from the servlet client to the proxy request.protected voidcopyResponseEntity(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) Copy response body data (the entity) from the proxy to the servlet client.protected voidcopyResponseHeaders(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) Copy proxied response headers back to the servlet client.voiddestroy()protected booleandoResponseRedirectOrNotModifiedLogic(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode) protected static CharSequenceEncodes characters in the query or fragment part of the URI.jakarta.servlet.ServletConfigvoidinit(jakarta.servlet.ServletConfig servletConfig) protected StringrewriteUrlFromResponse(jakarta.servlet.http.HttpServletRequest servletRequest, String theUrl) For a redirect response from the target server, this translatestheUrlto redirect to and translates it to one the original client can use.protected voidservice(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse)
-
Field Details
-
P_LOG
A boolean parameter name to enable logging of input and target URLs to the servlet log.- See Also:
-
doLog
protected boolean doLog -
targetUriObj
-
targetUri
-
proxyClient
protected org.apache.http.impl.client.CloseableHttpClient proxyClient -
hopByHopHeaders
protected static final org.apache.http.message.HeaderGroup hopByHopHeadersThese 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 Setbecause this approach does case insensitive lookup faster. -
asciiQueryChars
-
-
Constructor Details
-
ProxyServlet
public ProxyServlet()
-
-
Method Details
-
getServletConfig
public jakarta.servlet.ServletConfig getServletConfig() -
init
public void init(jakarta.servlet.ServletConfig servletConfig) throws jakarta.servlet.ServletException - Throws:
jakarta.servlet.ServletException
-
destroy
public void destroy() -
service
protected void service(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse) throws jakarta.servlet.ServletException, IOException - Throws:
jakarta.servlet.ServletExceptionIOException
-
doResponseRedirectOrNotModifiedLogic
protected boolean doResponseRedirectOrNotModifiedLogic(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode) throws jakarta.servlet.ServletException, IOException - Throws:
jakarta.servlet.ServletExceptionIOException
-
closeQuietly
-
copyRequestHeaders
protected void copyRequestHeaders(jakarta.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest) 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) For a redirect response from the target server, this translatestheUrlto redirect to and translates it to one the original client can use. -
encodeUriQuery
Encodes characters in the query or fragment part of the URI.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.- Parameters:
in- example: name=value&foo=bar#fragment
-