org.eclipse.jetty.server.handler
Class ProxyHandler

java.lang.Object
  extended by org.eclipse.jetty.util.component.AbstractLifeCycle
      extended by org.eclipse.jetty.server.handler.AbstractHandler
          extended by org.eclipse.jetty.server.handler.AbstractHandlerContainer
              extended by org.eclipse.jetty.server.handler.HandlerWrapper
                  extended by org.eclipse.jetty.server.handler.ProxyHandler
All Implemented Interfaces:
Handler, HandlerContainer, LifeCycle

public class ProxyHandler
extends HandlerWrapper

Implementation of a tunneling proxy that supports HTTP CONNECT and transparent proxy.

To work as CONNECT proxy, objects of this class must be instantiated using the no-arguments constructor, since the remote server information will be present in the CONNECT URI.

To work as transparent proxy, objects of this class must be instantiated using the string argument constructor, passing the remote host address and port in the form host:port.

Version:
$Revision: 1626 $ $Date: 2010-04-29 18:03:04 -0500 (Thu, 29 Apr 2010) $

Nested Class Summary
 class ProxyHandler.ClientToProxyConnection
           
 class ProxyHandler.ProxyToServerConnection
           
 
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
 
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
 
Field Summary
 
Fields inherited from class org.eclipse.jetty.server.handler.HandlerWrapper
_handler
 
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
_listeners, FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
 
Constructor Summary
ProxyHandler()
           
ProxyHandler(Handler handler)
           
 
Method Summary
protected  SocketChannel connect(HttpServletRequest request, String host, int port)
          Establishes a connection to the remote server.
protected  void doStart()
           
protected  void doStop()
           
 int getConnectTimeout()
           
 ThreadPool getThreadPool()
           
 int getWriteTimeout()
           
 void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
          Handle a request.
protected  boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address)
          Handles the authentication before setting up the tunnel to the remote server.
protected  void handleConnect(Request baseRequest, HttpServletRequest request, HttpServletResponse response, String serverAddress)
          Handles a CONNECT request.
protected  ProxyHandler.ClientToProxyConnection newClientToProxyConnection(ConcurrentMap<String,Object> context, SocketChannel channel, EndPoint endPoint, long timeStamp)
           
protected  ProxyHandler.ProxyToServerConnection newProxyToServerConnection(ConcurrentMap<String,Object> context, Buffer buffer)
           
protected  void prepareContext(HttpServletRequest request, ConcurrentMap<String,Object> context)
           
protected  int read(EndPoint endPoint, Buffer buffer, ConcurrentMap<String,Object> context)
          Reads (with non-blocking semantic) into the given buffer from the given endPoint.
 void setConnectTimeout(int connectTimeout)
           
 void setServer(Server server)
           
 void setThreadPool(ThreadPool threadPool)
           
 void setWriteTimeout(int writeTimeout)
           
protected  int write(EndPoint endPoint, Buffer buffer, ConcurrentMap<String,Object> context)
          Writes (with blocking semantic) the given buffer of data onto the given endPoint.
 
Methods inherited from class org.eclipse.jetty.server.handler.HandlerWrapper
expandChildren, getHandler, getHandlers, setHandler
 
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandlerContainer
dump, expandHandler, getChildHandlerByClass, getChildHandlers, getChildHandlersByClass
 
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandler
destroy, dump, getServer, toString
 
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
 

Constructor Detail

ProxyHandler

public ProxyHandler()

ProxyHandler

public ProxyHandler(Handler handler)
Method Detail

getConnectTimeout

public int getConnectTimeout()
Returns:
the timeout, in milliseconds, to connect to the remote server

setConnectTimeout

public void setConnectTimeout(int connectTimeout)
Parameters:
connectTimeout - the timeout, in milliseconds, to connect to the remote server

getWriteTimeout

public int getWriteTimeout()
Returns:
the timeout, in milliseconds, to write data to a peer

setWriteTimeout

public void setWriteTimeout(int writeTimeout)
Parameters:
writeTimeout - the timeout, in milliseconds, to write data to a peer

setServer

public void setServer(Server server)
Specified by:
setServer in interface Handler
Overrides:
setServer in class HandlerWrapper

getThreadPool

public ThreadPool getThreadPool()
Returns:
the thread pool

setThreadPool

public void setThreadPool(ThreadPool threadPool)
Parameters:
threadPool - the thread pool

doStart

protected void doStart()
                throws Exception
Overrides:
doStart in class HandlerWrapper
Throws:
Exception

doStop

protected void doStop()
               throws Exception
Overrides:
doStop in class HandlerWrapper
Throws:
Exception

handle

public void handle(String target,
                   Request baseRequest,
                   HttpServletRequest request,
                   HttpServletResponse response)
            throws ServletException,
                   IOException
Description copied from interface: Handler
Handle a request.

Specified by:
handle in interface Handler
Overrides:
handle in class HandlerWrapper
Parameters:
target - The target of the request - either a URI or a name.
baseRequest - The original unwrapped request object.
request - The request either as the Request object or a wrapper of that request. The HttpConnection.getCurrentConnection() method can be used access the Request object if required.
response - The response as the Response object or a wrapper of that request. The HttpConnection.getCurrentConnection() method can be used access the Response object if required.
Throws:
ServletException
IOException

handleConnect

protected void handleConnect(Request baseRequest,
                             HttpServletRequest request,
                             HttpServletResponse response,
                             String serverAddress)
                      throws ServletException,
                             IOException

Handles a CONNECT request.

CONNECT requests may have authentication headers such as Proxy-Authorization that authenticate the client with the proxy.

Parameters:
baseRequest - Jetty-specific http request
request - the http request
response - the http response
serverAddress - the remote server address in the form host:port
Throws:
ServletException - if an application error occurs
IOException - if an I/O error occurs

handleAuthentication

protected boolean handleAuthentication(HttpServletRequest request,
                                       HttpServletResponse response,
                                       String address)
                                throws ServletException,
                                       IOException

Handles the authentication before setting up the tunnel to the remote server.

The default implementation returns true.

Parameters:
request - the HTTP request
response - the HTTP response
address - the address of the remote server in the form host:port.
Returns:
true to allow to connect to the remote host, false otherwise
Throws:
ServletException - to report a server error to the caller
IOException - to report a server error to the caller

newClientToProxyConnection

protected ProxyHandler.ClientToProxyConnection newClientToProxyConnection(ConcurrentMap<String,Object> context,
                                                                          SocketChannel channel,
                                                                          EndPoint endPoint,
                                                                          long timeStamp)

newProxyToServerConnection

protected ProxyHandler.ProxyToServerConnection newProxyToServerConnection(ConcurrentMap<String,Object> context,
                                                                          Buffer buffer)

connect

protected SocketChannel connect(HttpServletRequest request,
                                String host,
                                int port)
                         throws IOException

Establishes a connection to the remote server.

Parameters:
request - the HTTP request that initiated the tunnel
host - the host to connect to
port - the port to connect to
Returns:
a SocketChannel connected to the remote server
Throws:
IOException - if the connection cannot be established

prepareContext

protected void prepareContext(HttpServletRequest request,
                              ConcurrentMap<String,Object> context)

read

protected int read(EndPoint endPoint,
                   Buffer buffer,
                   ConcurrentMap<String,Object> context)
            throws IOException

Reads (with non-blocking semantic) into the given buffer from the given endPoint.

Parameters:
endPoint - the endPoint to read from
buffer - the buffer to read data into
context - the context information related to the connection
Returns:
the number of bytes read (possibly 0 since the read is non-blocking) or -1 if the channel has been closed remotely
Throws:
IOException - if the endPoint cannot be read

write

protected int write(EndPoint endPoint,
                    Buffer buffer,
                    ConcurrentMap<String,Object> context)
             throws IOException

Writes (with blocking semantic) the given buffer of data onto the given endPoint.

Parameters:
endPoint - the endPoint to write to
buffer - the buffer to write
context - the context information related to the connection
Returns:
the number of bytes written
Throws:
IOException - if the buffer cannot be written


Copyright © 1995-2010 Mort Bay Consulting. All Rights Reserved.