org.eclipse.jetty.servlets
Class ProxyServlet

java.lang.Object
  extended by org.eclipse.jetty.servlets.ProxyServlet
All Implemented Interfaces:
javax.servlet.Servlet
Direct Known Subclasses:
ProxyServlet.Transparent

public class ProxyServlet
extends java.lang.Object
implements javax.servlet.Servlet

Asynchronous Proxy Servlet. Forward requests to another server either as a standard web proxy (as defined by RFC2616) or as a transparent proxy.

This servlet needs the jetty-util and jetty-client classes to be available to the web application.

To facilitate JMX monitoring, the "HttpClient" and "ThreadPool" are set as context attributes prefixed with the servlet name.

The following init parameters may be used to configure the servlet:

See Also:
ConnectHandler

Nested Class Summary
static class ProxyServlet.Transparent
          Transparent Proxy.
 
Field Summary
protected  HostMap<PathMap> _black
           
protected  HttpClient _client
           
protected  javax.servlet.ServletConfig _config
           
protected  javax.servlet.ServletContext _context
           
protected  java.util.HashSet<java.lang.String> _DontProxyHeaders
           
protected  java.lang.String _hostHeader
           
protected  Logger _log
           
protected  HostMap<PathMap> _white
           
 
Constructor Summary
ProxyServlet()
           
 
Method Summary
protected  HttpClient createHttpClient(javax.servlet.ServletConfig config)
          Create and return an HttpClient based on ServletConfig By default this implementation will create an instance of the HttpClient for use by this proxy servlet.
protected  Logger createLogger(javax.servlet.ServletConfig config)
          Create and return a logger based on the ServletConfig for use in the proxy servlet
protected  void customizeContinuation(Continuation continuation)
          Extension point for subclasses to customize the Continuation after it's initial creation in the service method.
protected  void customizeExchange(HttpExchange exchange, javax.servlet.http.HttpServletRequest request)
          Extension point for subclasses to customize an exchange.
 void destroy()
           
 java.lang.String getHostHeader()
          Get the hostHeader.
 javax.servlet.ServletConfig getServletConfig()
           
 java.lang.String getServletInfo()
           
 void handleConnect(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected  void handleOnConnectionFailed(java.lang.Throwable ex, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Extension point for custom handling of an HttpExchange's onConnectionFailed method.
protected  void handleOnException(java.lang.Throwable ex, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Extension point for custom handling of an HttpExchange's onException method.
protected  void handleOnExpire(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Extension point for custom handling of an HttpExchange's onExpire method.
 void init(javax.servlet.ServletConfig config)
           
protected  HttpURI proxyHttpURI(javax.servlet.http.HttpServletRequest request, java.lang.String uri)
           
protected  HttpURI proxyHttpURI(java.lang.String scheme, java.lang.String serverName, int serverPort, java.lang.String uri)
           
 void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
           
 void setHostHeader(java.lang.String hostHeader)
          Set the hostHeader.
 boolean validateDestination(java.lang.String host, java.lang.String path)
          Check the request hostname and path against white- and blacklist.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log

protected Logger _log

_client

protected HttpClient _client

_hostHeader

protected java.lang.String _hostHeader

_DontProxyHeaders

protected java.util.HashSet<java.lang.String> _DontProxyHeaders

_config

protected javax.servlet.ServletConfig _config

_context

protected javax.servlet.ServletContext _context

_white

protected HostMap<PathMap> _white

_black

protected HostMap<PathMap> _black
Constructor Detail

ProxyServlet

public ProxyServlet()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Servlet
Throws:
javax.servlet.ServletException

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Servlet

createLogger

protected Logger createLogger(javax.servlet.ServletConfig config)
Create and return a logger based on the ServletConfig for use in the proxy servlet

Parameters:
config -
Returns:
Logger

createHttpClient

protected HttpClient createHttpClient(javax.servlet.ServletConfig config)
                               throws java.lang.Exception
Create and return an HttpClient based on ServletConfig By default this implementation will create an instance of the HttpClient for use by this proxy servlet.

Parameters:
config -
Returns:
HttpClient
Throws:
java.lang.Exception

validateDestination

public boolean validateDestination(java.lang.String host,
                                   java.lang.String path)
Check the request hostname and path against white- and blacklist.

Parameters:
host - hostname to check
path - path to check
Returns:
true if request is allowed to be proxied

getServletConfig

public javax.servlet.ServletConfig getServletConfig()
Specified by:
getServletConfig in interface javax.servlet.Servlet

getHostHeader

public java.lang.String getHostHeader()
Get the hostHeader.

Returns:
the hostHeader

setHostHeader

public void setHostHeader(java.lang.String hostHeader)
Set the hostHeader.

Parameters:
hostHeader - the hostHeader to set

service

public void service(javax.servlet.ServletRequest req,
                    javax.servlet.ServletResponse res)
             throws javax.servlet.ServletException,
                    java.io.IOException
Specified by:
service in interface javax.servlet.Servlet
Throws:
javax.servlet.ServletException
java.io.IOException

handleConnect

public void handleConnect(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response)
                   throws java.io.IOException
Throws:
java.io.IOException

proxyHttpURI

protected HttpURI proxyHttpURI(javax.servlet.http.HttpServletRequest request,
                               java.lang.String uri)
                        throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

proxyHttpURI

protected HttpURI proxyHttpURI(java.lang.String scheme,
                               java.lang.String serverName,
                               int serverPort,
                               java.lang.String uri)
                        throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

getServletInfo

public java.lang.String getServletInfo()
Specified by:
getServletInfo in interface javax.servlet.Servlet

customizeExchange

protected void customizeExchange(HttpExchange exchange,
                                 javax.servlet.http.HttpServletRequest request)
Extension point for subclasses to customize an exchange. Useful for setting timeouts etc. The default implementation does nothing.

Parameters:
exchange -
request -

customizeContinuation

protected void customizeContinuation(Continuation continuation)
Extension point for subclasses to customize the Continuation after it's initial creation in the service method. Useful for setting timeouts etc. The default implementation does nothing.

Parameters:
continuation -

handleOnConnectionFailed

protected void handleOnConnectionFailed(java.lang.Throwable ex,
                                        javax.servlet.http.HttpServletRequest request,
                                        javax.servlet.http.HttpServletResponse response)
Extension point for custom handling of an HttpExchange's onConnectionFailed method. The default implementation delegates to handleOnException(Throwable, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Parameters:
ex -
request -
response -

handleOnException

protected void handleOnException(java.lang.Throwable ex,
                                 javax.servlet.http.HttpServletRequest request,
                                 javax.servlet.http.HttpServletResponse response)
Extension point for custom handling of an HttpExchange's onException method. The default implementation sets the response status to HttpServletResponse.SC_INTERNAL_SERVER_ERROR (503)

Parameters:
ex -
request -
response -

handleOnExpire

protected void handleOnExpire(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
Extension point for custom handling of an HttpExchange's onExpire method. The default implementation sets the response status to HttpServletResponse.SC_GATEWAY_TIMEOUT (504)

Parameters:
request -
response -


Copyright © 2013. All Rights Reserved.