rabbit.proxy
Class Connection

java.lang.Object
  extended by rabbit.proxy.Connection

public class Connection
extends Object

The base connection class for rabbit. This is the class that handle the http protocoll for proxies. For the technical overview of how connections and threads works see the file htdocs/technical_documentation/thread_handling_overview.txt

Author:
Robert Olofsson

Constructor Summary
Connection(ConnectionId id, SocketChannel channel, HttpProxy proxy)
          Create a new Connection
 
Method Summary
 void doError(int status, String message)
          Send an error (400 Bad Request) to the client.
 SocketChannel getChannel()
          Get the SocketChannel to the client
 boolean getChunking()
          Get the chunking option.
 ClientResourceHandler getClientResourceHandler()
          Get the client resource handler, that is the handler of any content the client is submitting (POSTED data, file uploads etc.)
 String getContentLength()
           
 Counter getCounter()
           
 String getDebugInfo()
          Get debug info for use in 500 error response
 String getExtraInfo()
          Get the extra information associated with the current request.
 HttpGenerator getHttpGenerator()
          Get the HttpGenerator that this connection uses when it needs to generate a custom respons header and resource.
 ConnectionId getId()
           
 boolean getMayFilter()
          Get the state of the request.
 boolean getMeta()
          Get the state of this request.
 NioHandler getNioHandler()
           
 String getPassword()
          Get the name of the user that is currently authorized.
 HttpProxy getProxy()
           
 String getRequestLine()
          Get the request line of the request currently being handled
 String getRequestURI()
          Get the current request uri.
 String getRequestVersion()
          Get the http version that the client used.
 long getStarted()
          Get the time the current request was started.
 String getStatus()
          Get the current status of this request
 String getUserName()
          Get the name of the user that is currently authorized.
 void logAndClose()
          Log the current request and close/end this connection
 void logAndTryRestart()
          Log the current request and start to listen for a new request if possible.
protected  boolean mustTunnel()
          Check if we must tunnel a request.
 void readRequest()
          Read a request.
 void sendAndClose(HttpHeader header)
          Send a request and then close this connection.
 void setChunking(boolean b)
          Set the chunking option.
 void setContentLength(String contentLength)
          Set the content length of the response.
 void setExtraInfo(String info)
          Set the extra info.
 void setFilteringNotAllowed()
          Get the state of this request.
 void setKeepalive(boolean keepalive)
          Set keepalive to a new value.
 void setMayCache(boolean cacheAllowed)
          Specify if we may cache the response resource.
 void setMayUseCache(boolean useCache)
          Specify if the current resource may be served from our cache.
 void setMeta()
          Flag this request as a meta-request, that is a request that the proxy should to handle.
 void setMustRevalidate()
          Tell this connection that the current request must be revalidated.
 void setPassword(String password)
          Set the password of the currently authenticated user (for basic proxy auth)
 void setStatusCode(String statusCode)
          Set the status code for the current request
 void setUserName(String userName)
          Set the name of the currently authenticated user (for basic proxy auth)
 void webConnectionEstablished(rabbit.proxy.RequestHandler rh)
          Fired when a web connection has been established.
 void webConnectionSetupFailed(rabbit.proxy.RequestHandler rh, Exception cause)
          Fired when setting up a web connection failed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Connection

public Connection(ConnectionId id,
                  SocketChannel channel,
                  HttpProxy proxy)
Create a new Connection

Parameters:
id - the ConnectionId of this connection.
channel - the SocketChannel to the client.
proxy - the HttpProxy that this connection belongs to.
Method Detail

getId

public ConnectionId getId()
Returns:
the ConnectionId of this connection

readRequest

public void readRequest()
Read a request.


webConnectionSetupFailed

public void webConnectionSetupFailed(rabbit.proxy.RequestHandler rh,
                                     Exception cause)
Fired when setting up a web connection failed.

Parameters:
rh - the RequestHandler
cause - the Exception that signaled the problem

mustTunnel

protected boolean mustTunnel()
Check if we must tunnel a request. Currently will only check if the Authorization starts with NTLM or Negotiate.

Returns:
true if the current request needs to be handled by a tunnel

webConnectionEstablished

public void webConnectionEstablished(rabbit.proxy.RequestHandler rh)
Fired when a web connection has been established. The web connection may be to the origin server or to an upstream proxy.

Parameters:
rh - the RequestHandler for the current request

doError

public void doError(int status,
                    String message)
Send an error (400 Bad Request) to the client.

Parameters:
status - the status code of the error.
message - the error message to tell the client.

getChannel

public SocketChannel getChannel()
Get the SocketChannel to the client

Returns:
the SocketChannel connected to the client

getNioHandler

public NioHandler getNioHandler()
Returns:
the NioHandler that this connection is using

getProxy

public HttpProxy getProxy()
Returns:
the HttProxy that this connection is serving

getCounter

public Counter getCounter()
Returns:
the Counter that keeps count of operations for this connection.

setKeepalive

public void setKeepalive(boolean keepalive)
Set keepalive to a new value. Note that keepalive can only be promoted down.

Parameters:
keepalive - the new keepalive value.

getUserName

public String getUserName()
Get the name of the user that is currently authorized.

Returns:
a username, may be null if the user is not know/authorized

setUserName

public void setUserName(String userName)
Set the name of the currently authenticated user (for basic proxy auth)

Parameters:
userName - the name of the current user

getPassword

public String getPassword()
Get the name of the user that is currently authorized.

Returns:
a username, may be null if the user is not know/authorized

setPassword

public void setPassword(String password)
Set the password of the currently authenticated user (for basic proxy auth)

Parameters:
password - the password that was used for authentication

getRequestLine

public String getRequestLine()
Get the request line of the request currently being handled

Returns:
the request line for the current request

getRequestURI

public String getRequestURI()
Get the current request uri. This will get the uri from the request header.

Returns:
the uri of the current request

getDebugInfo

public String getDebugInfo()
Get debug info for use in 500 error response

Returns:
a string with internal state of this connection

getRequestVersion

public String getRequestVersion()
Get the http version that the client used. We modify the request header to hold HTTP/1.1 since that is what rabbit uses, but the real client may have sent a 1.0 header.

Returns:
the request http version

getStatus

public String getStatus()
Get the current status of this request

Returns:
the current status

getContentLength

public String getContentLength()
Returns:
the content length of the current request

getClientResourceHandler

public ClientResourceHandler getClientResourceHandler()
Get the client resource handler, that is the handler of any content the client is submitting (POSTED data, file uploads etc.)

Returns:
the ClientResourceHandler for the current request

getExtraInfo

public String getExtraInfo()
Get the extra information associated with the current request.

Returns:
the currently set extra info or null if no such info is set.

setExtraInfo

public void setExtraInfo(String info)
Set the extra info.

Parameters:
info - the new info.

getStarted

public long getStarted()
Get the time the current request was started.

Returns:
the start time for the current request

setChunking

public void setChunking(boolean b)
Set the chunking option.

Parameters:
b - if true this connection should use chunking.

getChunking

public boolean getChunking()
Get the chunking option.

Returns:
if this connection is using chunking.

getMeta

public boolean getMeta()
Get the state of this request.

Returns:
true if this is a metapage request, false otherwise.

setMeta

public void setMeta()
Flag this request as a meta-request, that is a request that the proxy should to handle.


setMayUseCache

public void setMayUseCache(boolean useCache)
Specify if the current resource may be served from our cache. This can only be promoted down..

Parameters:
useCache - true if we may use the cache for serving this request, false otherwise.

setMayCache

public void setMayCache(boolean cacheAllowed)
Specify if we may cache the response resource. This can only be promoted down.

Parameters:
cacheAllowed - true if we may cache the response, false otherwise.

setFilteringNotAllowed

public void setFilteringNotAllowed()
Get the state of this request. This can only be promoted down.


getMayFilter

public boolean getMayFilter()
Get the state of the request.

Returns:
true if we may filter the response, false otherwise.

setMustRevalidate

public void setMustRevalidate()
Tell this connection that the current request must be revalidated.


setContentLength

public void setContentLength(String contentLength)
Set the content length of the response.

Parameters:
contentLength - the new content length.

setStatusCode

public void setStatusCode(String statusCode)
Set the status code for the current request

Parameters:
statusCode - the new status code

sendAndClose

public void sendAndClose(HttpHeader header)
Send a request and then close this connection.

Parameters:
header - the HttpHeader to send before closing down.

logAndClose

public void logAndClose()
Log the current request and close/end this connection


logAndTryRestart

public void logAndTryRestart()
Log the current request and start to listen for a new request if possible.


getHttpGenerator

public HttpGenerator getHttpGenerator()
Get the HttpGenerator that this connection uses when it needs to generate a custom respons header and resource.

Returns:
the current HttpGenerator