public class ProxyServlet extends Object
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
| Modifier and Type | Field and Description |
|---|---|
protected static BitSet |
asciiQueryChars |
protected boolean |
doLog |
protected static org.apache.http.message.HeaderGroup |
hopByHopHeaders
These are the "hop-by-hop" headers that should not be copied.
|
static String |
P_LOG
A boolean parameter name to enable logging of input and target URLs to the servlet log.
|
protected org.apache.http.client.HttpClient |
proxyClient |
protected String |
targetUri |
protected URI |
targetUriObj |
| Constructor and Description |
|---|
ProxyServlet() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
closeQuietly(Closeable closeable) |
protected void |
copyRequestHeaders(HttpServletRequest servletRequest,
org.apache.http.HttpRequest proxyRequest)
Copy request headers from the servlet client to the proxy request.
|
protected void |
copyResponseEntity(org.apache.http.HttpResponse proxyResponse,
HttpServletResponse servletResponse)
Copy response body data (the entity) from the proxy to the servlet client.
|
protected void |
copyResponseHeaders(org.apache.http.HttpResponse proxyResponse,
HttpServletResponse servletResponse)
Copy proxied response headers back to the servlet client.
|
protected org.apache.http.client.HttpClient |
createHttpClient(org.apache.http.params.HttpParams hcParams)
Called from
init(javax.servlet.ServletConfig). |
void |
destroy() |
protected boolean |
doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest,
HttpServletResponse servletResponse,
org.apache.http.HttpResponse proxyResponse,
int statusCode) |
protected static CharSequence |
encodeUriQuery(CharSequence in)
Encodes characters in the query or fragment part of the URI.
|
ServletConfig |
getServletConfig() |
void |
init(ServletConfig servletConfig) |
protected void |
readConfigParam(org.apache.http.params.HttpParams hcParams,
String hcParamName,
Class type) |
protected String |
rewriteUrlFromResponse(HttpServletRequest servletRequest,
String theUrl)
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(HttpServletRequest servletRequest,
HttpServletResponse servletResponse) |
public static final String P_LOG
protected boolean doLog
protected URI targetUriObj
protected String targetUri
protected org.apache.http.client.HttpClient proxyClient
protected static final org.apache.http.message.HeaderGroup hopByHopHeaders
protected static final BitSet asciiQueryChars
public ServletConfig getServletConfig()
public void init(ServletConfig servletConfig) throws ServletException
ServletExceptionprotected org.apache.http.client.HttpClient createHttpClient(org.apache.http.params.HttpParams hcParams)
init(javax.servlet.ServletConfig). HttpClient offers many opportunities for customization.protected void readConfigParam(org.apache.http.params.HttpParams hcParams,
String hcParamName,
Class type)
public void destroy()
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException
ServletExceptionIOExceptionprotected boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest, HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode) throws ServletException, IOException
ServletExceptionIOExceptionprotected void closeQuietly(Closeable closeable)
protected void copyRequestHeaders(HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest)
protected void copyResponseHeaders(org.apache.http.HttpResponse proxyResponse,
HttpServletResponse servletResponse)
protected void copyResponseEntity(org.apache.http.HttpResponse proxyResponse,
HttpServletResponse servletResponse)
throws IOException
IOExceptionprotected String rewriteUrlFromResponse(HttpServletRequest servletRequest, String theUrl)
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 © 2016 OCPsoft. All rights reserved.