Class JWSWorker

java.lang.Object
  extended by JWSWorker
All Implemented Interfaces:
java.lang.Runnable

 class JWSWorker
extends java.lang.Object
implements java.lang.Runnable

A worker thread's data and run() method. JWSWorker is not an actual thread in and of itself. The thread pool is what maintains the threads for us. JWSWorker is just a Runnable class that the thread pool will assign a thread to. That thread will execute the JWSWorker object's run() method. Which will then process the HTTP request coming in on the socket. When the JWSWorker's run() method finishes, the thread pool will assign the thread to another JWSWorker object. Which will process another HTTP request. And so on.


Field Summary
private  java.net.Socket clientSock
           
static java.net.FileNameMap mimeData
           
 
Constructor Summary
private JWSWorker()
          Don't use this constructor.
  JWSWorker(java.net.Socket s)
          Create a JWSWorker object with a socket to operate on.
 
Method Summary
private  void handleConnection(java.net.Socket clientSock)
          Reads in an HTTP request from the given Socket and sends the appropriate reply.
 void run()
          run() is just a wrapper for handleConnection().
static void sendErrorAndClose(java.net.Socket sock, java.io.OutputStream sockOut, java.lang.String error)
          Sends an error message and closes the connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mimeData

public static java.net.FileNameMap mimeData

clientSock

private java.net.Socket clientSock
Constructor Detail

JWSWorker

private JWSWorker()
Don't use this constructor. When you create a JWSWorker object with a null socket and put it into the task queue, at best you're wasting memory and CPU cycles. At worst you may cause a null pointer exception when a thread gets around to running the object.


JWSWorker

public JWSWorker(java.net.Socket s)
Create a JWSWorker object with a socket to operate on.

Parameters:
s - A connected socket to read the HTTP request from, and send the resulting data/error message back to.
See Also:
Socket
Method Detail

run

public void run()
run() is just a wrapper for handleConnection(). run() is executed by code inside the thread pool object. The thread pool assigns a thread to run this object when such a thread is available.

Specified by:
run in interface java.lang.Runnable

handleConnection

private void handleConnection(java.net.Socket clientSock)
Reads in an HTTP request from the given Socket and sends the appropriate reply. The request method and request URI are parsed out of the HTTP request and the corresponding file contents are sent, or an appropriate error code. Finally, the socket is closed.

BUGS: No security checking of the URI is performed, i.e. a malicious user could request "/../../../etc/passwd" and get it.

Parameters:
clientSock - A connected socket to read the HTTP request from, and send the resulting data/error message back to.
See Also:
Socket

sendErrorAndClose

public static void sendErrorAndClose(java.net.Socket sock,
                                     java.io.OutputStream sockOut,
                                     java.lang.String error)
Sends an error message and closes the connection. Catches IOException on write(), flush() and close() but ignores them.

Parameters:
sock - The socket to send the error to and close.
sockOut - The socket's output stream. Method creates if null.
error - A string that contains the error message.