SerializableURITemplateProxyServletpublic 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
| Modifier and Type | Field | Description |
|---|---|---|
protected static BitSet |
asciiQueryChars |
|
protected static String |
ATTR_TARGET_HOST |
|
protected static String |
ATTR_TARGET_URI |
|
protected int |
connectTimeout |
|
protected boolean |
doForwardIP |
|
protected boolean |
doHandleRedirects |
|
protected boolean |
doLog |
|
protected boolean |
doPreserveCookies |
|
protected boolean |
doPreserveHost |
|
protected boolean |
doSendUrlFragment |
User agents shouldn't send the url fragment but what if it does?
|
protected static org.apache.http.message.HeaderGroup |
hopByHopHeaders |
These are the "hop-by-hop" headers that should not be copied.
|
static String |
P_CONNECTTIMEOUT |
A integer parameter name to set the socket connection timeout (millis)
|
static String |
P_FORWARDEDFOR |
A boolean parameter name to enable forwarding of the client IP
|
static String |
P_HANDLEREDIRECTS |
A boolean parameter name to have auto-handle redirects
|
static String |
P_LOG |
A boolean parameter name to enable logging of input and target URLs to the servlet log.
|
static String |
P_PRESERVECOOKIES |
A boolean parameter name to keep COOKIES as-is
|
static String |
P_PRESERVEHOST |
A boolean parameter name to keep HOST parameter as-is
|
static String |
P_READTIMEOUT |
A integer parameter name to set the socket read timeout (millis)
|
protected static String |
P_TARGET_URI |
The parameter name for the target (destination) URI to proxy to.
|
protected int |
readTimeout |
|
protected org.apache.http.HttpHost |
targetHost |
|
protected String |
targetUri |
From the configured parameter "targetUri".
|
protected URI |
targetUriObj |
| Constructor | Description |
|---|---|
ProxyServlet() |
| Modifier and Type | Method | Description |
|---|---|---|
protected org.apache.http.client.config.RequestConfig |
buildRequestConfig() |
Sub-classes can override specific behaviour of
RequestConfig. |
protected void |
closeQuietly(Closeable closeable) |
|
protected void |
consumeQuietly(org.apache.http.HttpEntity entity) |
HttpClient v4.1 doesn't have the
EntityUtils.consumeQuietly(org.apache.http.HttpEntity) method. |
protected void |
copyProxyCookie(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
String headerValue) |
Copy cookie from the proxy to the servlet client.
|
protected void |
copyRequestHeader(javax.servlet.http.HttpServletRequest servletRequest,
org.apache.http.HttpRequest proxyRequest,
String headerName) |
Copy a request header from the servlet client to the proxy request.
|
protected void |
copyRequestHeaders(javax.servlet.http.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,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.HttpRequest proxyRequest,
javax.servlet.http.HttpServletRequest servletRequest) |
Copy response body data (the entity) from the proxy to the servlet client.
|
protected void |
copyResponseHeader(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.Header header) |
Copy a proxied response header back to the servlet client.
|
protected void |
copyResponseHeaders(org.apache.http.HttpResponse proxyResponse,
javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse) |
Copy proxied response headers back to the servlet client.
|
protected org.apache.http.client.HttpClient |
createHttpClient(org.apache.http.client.config.RequestConfig requestConfig) |
Called from
GenericServlet.init(javax.servlet.ServletConfig). |
void |
destroy() |
|
protected org.apache.http.HttpResponse |
doExecute(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.HttpRequest proxyRequest) |
|
protected static CharSequence |
encodeUriQuery(CharSequence in,
boolean encodePercent) |
Encodes characters in the query or fragment part of the URI.
|
protected String |
getConfigParam(String key) |
Reads a configuration parameter.
|
protected String |
getCookieNamePrefix(String name) |
The string prefixing rewritten cookies.
|
protected org.apache.http.client.HttpClient |
getProxyClient() |
The http client used.
|
protected String |
getRealCookie(String cookieValue) |
Take any client cookies that were originally from the proxy and prepare them to send to the
proxy.
|
String |
getServletInfo() |
|
protected org.apache.http.HttpHost |
getTargetHost(javax.servlet.http.HttpServletRequest servletRequest) |
|
String |
getTargetUri() |
The target URI as configured.
|
protected String |
getTargetUri(javax.servlet.http.HttpServletRequest servletRequest) |
|
protected void |
handleRequestException(org.apache.http.HttpRequest proxyRequest,
Exception e) |
|
void |
init() |
|
protected void |
initTarget() |
|
protected org.apache.http.HttpRequest |
newProxyRequestWithEntity(String method,
String proxyRequestUri,
javax.servlet.http.HttpServletRequest servletRequest) |
|
protected String |
rewriteQueryStringFromRequest(javax.servlet.http.HttpServletRequest servletRequest,
String queryString) |
|
protected String |
rewriteUrlFromRequest(javax.servlet.http.HttpServletRequest servletRequest) |
Reads the request URI from
servletRequest and rewrites it, considering targetUri. |
protected String |
rewriteUrlFromResponse(javax.servlet.http.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(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse) |
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, logpublic static final String P_LOG
public static final String P_FORWARDEDFOR
public static final String P_PRESERVEHOST
public static final String P_PRESERVECOOKIES
public static final String P_HANDLEREDIRECTS
public static final String P_CONNECTTIMEOUT
public static final String P_READTIMEOUT
protected static final String P_TARGET_URI
protected static final String ATTR_TARGET_URI
protected static final String ATTR_TARGET_HOST
protected boolean doLog
protected boolean doForwardIP
protected boolean doSendUrlFragment
protected boolean doPreserveHost
protected boolean doPreserveCookies
protected boolean doHandleRedirects
protected int connectTimeout
protected int readTimeout
protected String targetUri
protected URI targetUriObj
protected org.apache.http.HttpHost targetHost
protected static final org.apache.http.message.HeaderGroup hopByHopHeaders
protected static final BitSet asciiQueryChars
public String getServletInfo()
getServletInfo in class javax.servlet.GenericServletprotected String getTargetUri(javax.servlet.http.HttpServletRequest servletRequest)
protected org.apache.http.HttpHost getTargetHost(javax.servlet.http.HttpServletRequest servletRequest)
protected String getConfigParam(String key)
public void init()
throws javax.servlet.ServletException
init in class javax.servlet.GenericServletjavax.servlet.ServletExceptionprotected org.apache.http.client.config.RequestConfig buildRequestConfig()
RequestConfig.protected void initTarget()
throws javax.servlet.ServletException
javax.servlet.ServletExceptionprotected org.apache.http.client.HttpClient createHttpClient(org.apache.http.client.config.RequestConfig requestConfig)
GenericServlet.init(javax.servlet.ServletConfig).
HttpClient offers many opportunities for customization.
In any case, it should be thread-safe.protected org.apache.http.client.HttpClient getProxyClient()
createHttpClient(RequestConfig)public void destroy()
destroy in class javax.servlet.GenericServletprotected void service(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse)
throws javax.servlet.ServletException,
IOException
service in class javax.servlet.http.HttpServletjavax.servlet.ServletExceptionIOExceptionprotected void handleRequestException(org.apache.http.HttpRequest proxyRequest,
Exception e)
throws javax.servlet.ServletException,
IOException
javax.servlet.ServletExceptionIOExceptionprotected org.apache.http.HttpResponse doExecute(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.HttpRequest proxyRequest)
throws IOException
IOExceptionprotected org.apache.http.HttpRequest newProxyRequestWithEntity(String method, String proxyRequestUri, javax.servlet.http.HttpServletRequest servletRequest) throws IOException
IOExceptionprotected void closeQuietly(Closeable closeable)
protected void consumeQuietly(org.apache.http.HttpEntity entity)
EntityUtils.consumeQuietly(org.apache.http.HttpEntity) method.protected void copyRequestHeaders(javax.servlet.http.HttpServletRequest servletRequest,
org.apache.http.HttpRequest proxyRequest)
protected void copyRequestHeader(javax.servlet.http.HttpServletRequest servletRequest,
org.apache.http.HttpRequest proxyRequest,
String headerName)
protected void copyResponseHeaders(org.apache.http.HttpResponse proxyResponse,
javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse)
protected void copyResponseHeader(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.Header header)
protected void copyProxyCookie(javax.servlet.http.HttpServletRequest servletRequest,
javax.servlet.http.HttpServletResponse servletResponse,
String headerValue)
protected String getRealCookie(String cookieValue)
protected String getCookieNamePrefix(String name)
protected void copyResponseEntity(org.apache.http.HttpResponse proxyResponse,
javax.servlet.http.HttpServletResponse servletResponse,
org.apache.http.HttpRequest proxyRequest,
javax.servlet.http.HttpServletRequest servletRequest)
throws IOException
IOExceptionprotected String rewriteUrlFromRequest(javax.servlet.http.HttpServletRequest servletRequest)
servletRequest and rewrites it, considering targetUri.
It's used to make the new request.protected String rewriteQueryStringFromRequest(javax.servlet.http.HttpServletRequest servletRequest, String queryString)
protected String rewriteUrlFromResponse(javax.servlet.http.HttpServletRequest servletRequest, String theUrl)
theUrl to redirect to
and translates it to one the original client can use.public String getTargetUri()
protected static CharSequence encodeUriQuery(CharSequence in, boolean encodePercent)
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#fragmentencodePercent - determine whether percent characters need to be encodedCopyright © 2017. All rights reserved.