public abstract class AbstractProxyServlet extends HttpServlet
Abstract base class for proxy servlets.
Forwards requests to another server either as a standard web reverse proxy or as a transparent reverse proxy (as defined by RFC 7230).
To facilitate JMX monitoring, the HttpClient
instance is set
as ServletContext attribute, prefixed with this servlet's name and
exposed by the mechanism provided by
ServletContext.setAttribute(String, Object)
.
The following init parameters may be used to configure the servlet:
In addition, see createHttpClient()
for init parameters
used to configure the HttpClient
instance.
NOTE: By default the Host header sent to the server by this proxy
servlet is the server's host name. However, this breaks redirects.
Set preserveHost
to true
to make redirects working,
although this may break server's virtual host selection.
The default behavior of not preserving the Host header mimics the default behavior of Apache httpd and Nginx, which both have a way to be configured to preserve the Host header.
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractProxyServlet.TransparentDelegate
Utility class that implement transparent proxy functionalities.
|
Modifier and Type | Field and Description |
---|---|
protected Logger |
_log |
protected static Set<String> |
HOP_HEADERS |
Constructor and Description |
---|
AbstractProxyServlet() |
Modifier and Type | Method and Description |
---|---|
protected void |
addProxyHeaders(HttpServletRequest clientRequest,
Request proxyRequest) |
protected void |
addViaHeader(Request proxyRequest) |
protected void |
addXForwardedHeaders(HttpServletRequest clientRequest,
Request proxyRequest) |
protected void |
copyRequestHeaders(HttpServletRequest clientRequest,
Request proxyRequest) |
protected HttpClient |
createHttpClient()
Creates a
HttpClient instance, configured with init parameters of this servlet. |
protected Logger |
createLogger() |
void |
destroy() |
protected String |
filterServerResponseHeader(HttpServletRequest clientRequest,
Response serverResponse,
String headerName,
String headerValue) |
protected Set<String> |
findConnectionHeaders(HttpServletRequest clientRequest) |
Set<String> |
getBlackListHosts() |
String |
getHostHeader() |
protected HttpClient |
getHttpClient() |
protected int |
getRequestId(HttpServletRequest clientRequest) |
long |
getTimeout() |
String |
getViaHost() |
Set<String> |
getWhiteListHosts() |
protected boolean |
hasContent(HttpServletRequest clientRequest) |
void |
init() |
protected HttpClient |
newHttpClient() |
protected abstract Response.CompleteListener |
newProxyResponseListener(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse) |
protected void |
onClientRequestFailure(HttpServletRequest clientRequest,
Request proxyRequest,
HttpServletResponse proxyResponse,
Throwable failure) |
protected void |
onProxyResponseFailure(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse,
Response serverResponse,
Throwable failure) |
protected void |
onProxyResponseSuccess(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse,
Response serverResponse) |
protected void |
onProxyRewriteFailed(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse)
Callback method invoked when the URI rewrite performed
in
rewriteTarget(HttpServletRequest) returns null
indicating that no rewrite can be performed. |
protected void |
onServerResponseHeaders(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse,
Response serverResponse) |
protected String |
rewriteTarget(HttpServletRequest clientRequest) |
protected void |
sendProxyRequest(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse,
Request proxyRequest) |
protected void |
sendProxyResponseError(HttpServletRequest clientRequest,
HttpServletResponse proxyResponse,
int status) |
void |
setTimeout(long timeout) |
boolean |
validateDestination(String host,
int port)
Checks the given
host and port against whitelist and blacklist. |
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
public void init() throws ServletException
init
in class GenericServlet
ServletException
public void destroy()
destroy
in interface Servlet
destroy
in class GenericServlet
public String getHostHeader()
public String getViaHost()
public long getTimeout()
public void setTimeout(long timeout)
protected Logger createLogger()
protected HttpClient createHttpClient() throws ServletException
Creates a HttpClient
instance, configured with init parameters of this servlet.
The init parameters used to configure the HttpClient
instance are:
init-param | default | description |
---|---|---|
maxThreads | 256 | The max number of threads of HttpClient's Executor. If not set, or set to the value of "-", then the Jetty server thread pool will be used. |
maxConnections | 32768 | The max number of connections per destination, see HttpClient.setMaxConnectionsPerDestination(int) |
idleTimeout | 30000 | The idle timeout in milliseconds, see HttpClient.setIdleTimeout(long) |
timeout | 60000 | The total timeout in milliseconds, see Request.timeout(long, java.util.concurrent.TimeUnit) |
requestBufferSize | HttpClient's default | The request buffer size, see HttpClient.setRequestBufferSize(int) |
responseBufferSize | HttpClient's default | The response buffer size, see HttpClient.setResponseBufferSize(int) |
HttpClient
configured from the servlet configuration
ServletException
- if the HttpClient
cannot be createdprotected HttpClient newHttpClient()
protected HttpClient getHttpClient()
public boolean validateDestination(String host, int port)
host
and port
against whitelist and blacklist.host
- the host to checkport
- the port to checkprotected String rewriteTarget(HttpServletRequest clientRequest)
protected void onProxyRewriteFailed(HttpServletRequest clientRequest, HttpServletResponse proxyResponse)
Callback method invoked when the URI rewrite performed
in rewriteTarget(HttpServletRequest)
returns null
indicating that no rewrite can be performed.
It is possible to use blocking API in this method,
like HttpServletResponse.sendError(int)
.
clientRequest
- the client requestproxyResponse
- the client responseprotected boolean hasContent(HttpServletRequest clientRequest)
protected void copyRequestHeaders(HttpServletRequest clientRequest, Request proxyRequest)
protected Set<String> findConnectionHeaders(HttpServletRequest clientRequest)
protected void addProxyHeaders(HttpServletRequest clientRequest, Request proxyRequest)
protected void addViaHeader(Request proxyRequest)
protected void addXForwardedHeaders(HttpServletRequest clientRequest, Request proxyRequest)
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest)
protected abstract Response.CompleteListener newProxyResponseListener(HttpServletRequest clientRequest, HttpServletResponse proxyResponse)
protected void onClientRequestFailure(HttpServletRequest clientRequest, Request proxyRequest, HttpServletResponse proxyResponse, Throwable failure)
protected void onServerResponseHeaders(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)
protected String filterServerResponseHeader(HttpServletRequest clientRequest, Response serverResponse, String headerName, String headerValue)
protected void onProxyResponseSuccess(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)
protected void onProxyResponseFailure(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse, Throwable failure)
protected int getRequestId(HttpServletRequest clientRequest)
protected void sendProxyResponseError(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, int status)
Copyright © 1995–2016 Webtide. All rights reserved.