Class Connection

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--RestartableThread
              |
              +--Connection
All Implemented Interfaces:
java.lang.Runnable

public class Connection
extends RestartableThread

This is the class that handles one connection And makes sure the content is delivered to the client. It reads the request and get the data, and filters it and sends the data to the client. If Keepalive is suitable it reads the next request.


Field Summary
static int READ_BUFFER_SIZE
           
 java.net.Socket socket
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Connection()
          Create a new Connection.
 
Method Summary
protected  void closeDown()
          Close down nicely.
 void doRun()
          Does run the thread: called by RestartableThread.run()
static HTTPHeader getHeader(java.lang.String statusLine)
          Get a new HTTPHeader initialized with some data.
 boolean getKeepalive()
          Get the keepalive value.
 void handleRequest(HTTPHeader header)
          Handle a request by getting the datastream (from the cache or the web).
protected  boolean openStreams()
          Set up the streams used for reading request and sending data to and from the client.
protected  boolean readBody(HTTPHeader header)
          If a content-length header field has been found, we have to read content-length 's value bytes and forward them to the server
protected  void send(HTTPHeader header)
          Send a header to the client.
 void send400(java.lang.String message)
          Send a 400 Bad Request error to the client.
 void send403(java.lang.String message)
          Send a 403 Forbidden error to the client.
 void send500(java.lang.Throwable exception)
          Send a 500 Internal Server Error header for the given exception.
 void setKeepalive(boolean keepalive)
          Set keepalive to a new value.
 void setSocket(java.net.Socket socket)
          Give this connection a socket to handle, this makes call to start() method
 
Methods inherited from class RestartableThread
run, start
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

READ_BUFFER_SIZE

public static int READ_BUFFER_SIZE

socket

public java.net.Socket socket
Constructor Detail

Connection

public Connection()
Create a new Connection. You need to call setSocket on this connection to make it do stuff.
Method Detail

setSocket

public void setSocket(java.net.Socket socket)
Give this connection a socket to handle, this makes call to start() method
Parameters:
socket - the Socket that is requesting service.

closeDown

protected void closeDown()
Close down nicely.

doRun

public void doRun()
Does run the thread: called by RestartableThread.run()
Overrides:
doRun in class RestartableThread

openStreams

protected boolean openStreams()
Set up the streams used for reading request and sending data to and from the client.
Returns:
true if all goes well.

readBody

protected boolean readBody(HTTPHeader header)
If a content-length header field has been found, we have to read content-length 's value bytes and forward them to the server

send

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

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.

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.

getHeader

public static HTTPHeader getHeader(java.lang.String statusLine)
Get a new HTTPHeader initialized with some data.
Parameters:
statusline - the statusline of the response.
Returns:
a new HTTPHeader.

send403

public void send403(java.lang.String message)
Send a 403 Forbidden error to the client.
Parameters:
message - additional message to tell the client.

send400

public void send400(java.lang.String message)
Send a 400 Bad Request error to the client.
Parameters:
message - additional message to tell the client.

send500

public void send500(java.lang.Throwable exception)
Send a 500 Internal Server Error header for the given exception.
Parameters:
exception - the Exception made.
Returns:
a suitable HTTPHeader.