rabbit.proxy
Class Connection

java.lang.Object
  extended by java.lang.Thread
      extended by rabbit.util.RestartableThread
          extended by rabbit.proxy.Connection
All Implemented Interfaces:
java.lang.Runnable

public class Connection
extends RestartableThread

This is the class that handles one connection And make sure the content is delivered to the client. It reads the request and get the data (from the cache or the web), and filters it and sends the data to the client. If Keepalive is suitable it reads the next request.


Nested Class Summary
protected  class Connection.RequestHandler
          A container to send around less parameters.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Connection(Proxy proxy)
          Create a new Connection.
 
Method Summary
 HTTPHeader checkIfMatch(HTTPHeader header, Connection.RequestHandler rh)
           
 boolean checkIPAccess(java.net.Socket sock)
          Check if this socket is in the possible range of ip:s being served.
 void doError(int statuscode, java.lang.Exception e)
          Send an error (400 Bad Request) to the client.
 void doError(int statuscode, java.lang.String message)
          Send an error (400 Bad Request) to the client.
 void doWork()
          Handle the incomming requests for as long as we can keep the connection alive.
 HTTPHeader filterHTTPIn(java.util.List<HTTPFilter> filters, HTTPHeader in)
          Filter the headers using the methods in the vector.
 HTTPHeader filterHTTPOut(java.util.List<HTTPFilter> filters, HTTPHeader in)
          Filter the headers using the methods in the vector.
 boolean getChunking()
          Get the chunking option.
 java.lang.String getContentLength()
          Get the content length of the response.
 java.lang.String getExtraInfo()
          Get the extra information for this request.
 boolean getKeepalive()
          Get the keepalive value.
 boolean getMayCache()
          Get the state of this request.
 boolean getMayFilter()
          Get the state of the request.
 boolean getMayUseCache()
          Get the state of this request.
 boolean getMeta()
          Get the state of this request.
 java.lang.String getPassWord()
          Get the password of the client.
 Proxy getProxy()
          Get the proxy that this connection is working for.
 java.lang.String getRequestLine()
          Get the current request line
 StandardResponseHeaders getResponseHandler()
           
 java.net.Socket getSocket()
          Get the Socket that is being served.
 java.util.Date getStarted()
          Get the time this Connection was started.
 java.lang.String getStatus()
          Get the status of this Connection
 java.lang.String getStatusCode()
          Get the status code of the request.
 java.lang.String getUserName()
          Get the username of the client.
 void handleMeta(HTTPHeader header)
          Handle a meta page.
 void handleRequest(HTTPHeader header)
          Handle a request by getting the datastream (from the cache or the web).
 HTTPHeader is304(HTTPHeader in, Connection.RequestHandler rh)
          Check if the request allows us to use a "304 Not modified" response.
protected  boolean readChunkedContent(HTTPHeader header)
           
protected  boolean readContent(HTTPHeader header)
          If this request has a body (due to some post or so) read it in and append it to the request.
protected  boolean readMultipartContent(HTTPHeader header, java.lang.String ct)
          If this request has a multipart body read it and append it to the request.
protected  void send(HTTPHeader header)
          Send a header to the client.
 void setAddedIMS(boolean b)
           
 void setAddedINM(boolean b)
           
 void setChunking(boolean b)
          Set the chunking option.
 void setContentLength(java.lang.String cl)
          Set the content length of the response.
 void setExtraInfo(java.lang.String exinfo)
          Set the extra information for this request.
 void setKeepalive(boolean keepalive)
          Set keepalive to a new value.
 void setMayCache(boolean cacheAllowed)
          Set the state of this request.
 void setMayFilter(boolean filterAllowed)
          Get the state of this request.
 void setMayUseCache(boolean usecache)
          Set the state of this request.
 void setMeta(boolean meta)
          Set the state of this request.
 void setMustRevalidate(boolean b)
           
 void setPassWord(java.lang.String password)
          Set the password of the client.
 void setSocket(java.nio.channels.SocketChannel socket)
          Give this connection a socket to handle
protected  boolean setupStreams()
          Set up the streams used for reading request and sending data to and from the client.
 void setUserName(java.lang.String username)
          Set the user name of the client.
 java.util.Properties splitArgs(java.lang.String params)
          splits the CGI-paramsstring into variables and values.
 
Methods inherited from class rabbit.util.RestartableThread
run, start
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Connection

public Connection(Proxy proxy)
Create a new Connection. You need to call setsocket on this connection to make it do stuff.

Parameters:
proxy - the Proxy to handle connections for.
Method Detail

setSocket

public void setSocket(java.nio.channels.SocketChannel socket)
Give this connection a socket to handle

Parameters:
socket - the Socket that is requesting service.

checkIPAccess

public boolean checkIPAccess(java.net.Socket sock)
Check if this socket is in the possible range of ip:s being served.

Parameters:
sock - the Socket to check.
Returns:
true if the socket is allowed, false if it should be rejected.

doWork

public void doWork()
Handle the incomming requests for as long as we can keep the connection alive.

Specified by:
doWork in class RestartableThread

setupStreams

protected boolean setupStreams()
Set up the streams used for reading request and sending data to and from the client.

Returns:
true if all goes well.

readContent

protected boolean readContent(HTTPHeader header)
If this request has a body (due to some post or so) read it in and append it to the request.


readChunkedContent

protected boolean readChunkedContent(HTTPHeader header)

readMultipartContent

protected boolean readMultipartContent(HTTPHeader header,
                                       java.lang.String ct)
If this request has a multipart body read it and append it to the request.


filterHTTPIn

public HTTPHeader filterHTTPIn(java.util.List<HTTPFilter> filters,
                               HTTPHeader in)
Filter the headers using the methods in the vector.

Parameters:
filters - a List with Methods.
in - the request or response header.
Returns:
null if all is ok, a HTTPHeader if this request is blocked.

filterHTTPOut

public HTTPHeader filterHTTPOut(java.util.List<HTTPFilter> filters,
                                HTTPHeader in)
Filter the headers using the methods in the vector.

Parameters:
filters - a List with Methods.
in - the request or response header.
Returns:
null if all is ok, a HTTPHeader if this request is blocked.

send

protected void send(HTTPHeader header)
Send a header to the client.

Parameters:
header - the HTTPHeader to send.

checkIfMatch

public HTTPHeader checkIfMatch(HTTPHeader header,
                               Connection.RequestHandler rh)

is304

public HTTPHeader is304(HTTPHeader in,
                        Connection.RequestHandler rh)
Check if the request allows us to use a "304 Not modified" response.

Parameters:
in - the request being made.
rh - the RequestHandler for this request

handleRequest

public void handleRequest(HTTPHeader header)
Handle a request by getting the datastream (from the cache or the web). After getting the handler for the mimetype, send it.

Parameters:
header - the request made.

handleMeta

public void handleMeta(HTTPHeader header)
Handle a meta page.

Parameters:
header - the request being made.

splitArgs

public java.util.Properties splitArgs(java.lang.String params)
splits the CGI-paramsstring into variables and values. put these values into a hashtable for easy retrival

Parameters:
params - the CGI-querystring.
Returns:
a hastable with type->value maps for the CGI-querystring

doError

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

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

doError

public void doError(int statuscode,
                    java.lang.Exception e)
Send an error (400 Bad Request) to the client.

Parameters:
statuscode - the status code of the error.
e - the exception to tell the client.

getProxy

public Proxy getProxy()
Get the proxy that this connection is working for.


getStatus

public java.lang.String getStatus()
Get the status of this Connection

Returns:
the current status.

getStarted

public java.util.Date getStarted()
Get the time this Connection was started.

Returns:
the time this Connection started serving requests.

setUserName

public void setUserName(java.lang.String username)
Set the user name of the client.

Parameters:
username - the username of the client.

getUserName

public java.lang.String getUserName()
Get the username of the client.

Returns:
the current username of the client.

setPassWord

public void setPassWord(java.lang.String password)
Set the password of the client.

Parameters:
password - the password of the client.

getPassWord

public java.lang.String getPassWord()
Get the password of the client.

Returns:
the current password of the client.

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.

getKeepalive

public boolean getKeepalive()
Get the keepalive value.

Returns:
true if keepalive should be done, false otherwise.

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.

setMeta

public void setMeta(boolean meta)
Set the state of this request.

Parameters:
meta - true if this request is a metapage request, false otherwise.

getMeta

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

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

setMayUseCache

public void setMayUseCache(boolean usecache)
Set the state of this request. This can only be promoted down..

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

getMayUseCache

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

Returns:
true if we may use the cache for this request, false otherwise.

setMayCache

public void setMayCache(boolean cacheAllowed)
Set the state of this request. This can only be promoted down.

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

getMayCache

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

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

setMayFilter

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

Parameters:
filterAllowed - true if we may filter the response, false otherwise.

getMayFilter

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

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

setAddedINM

public void setAddedINM(boolean b)

setAddedIMS

public void setAddedIMS(boolean b)

setMustRevalidate

public void setMustRevalidate(boolean b)

getSocket

public java.net.Socket getSocket()
Get the Socket that is being served.

Returns:
the requesting Socket.

getRequestLine

public java.lang.String getRequestLine()
Get the current request line

Returns:
the request being handled.

getStatusCode

public java.lang.String getStatusCode()
Get the status code of the request.

Returns:
the current status code.

getExtraInfo

public java.lang.String getExtraInfo()
Get the extra information for this request.

Returns:
the extra information if set.

setExtraInfo

public void setExtraInfo(java.lang.String exinfo)
Set the extra information for this request.

Parameters:
exinfo - the new extra information to set.

setContentLength

public void setContentLength(java.lang.String cl)
Set the content length of the response.

Parameters:
cl - the new content length.

getContentLength

public java.lang.String getContentLength()
Get the content length of the response.

Returns:
the content length of the response if set.

getResponseHandler

public StandardResponseHeaders getResponseHandler()