infoservice
Class InfoServiceConnection

java.lang.Object
  extended by infoservice.InfoServiceConnection
All Implemented Interfaces:
java.lang.Runnable

public final class InfoServiceConnection
extends java.lang.Object
implements java.lang.Runnable

This is a simple implementation of an HTTP server. This implementation doesn't support the most HTTP/1.1 stuff (like persistent connections or special encodings). But parsing an HTTP/1.1 request is working.


Field Summary
private  int m_byteLimit
          This stores the currently number of bytes which can be read from the InputStream until the data limit is exhausted.
private  int m_connectionId
          Stores the ID of the connection which is used in the log-output for identifying the current connection.
private  java.io.InputStream m_inputStream
           
private  JWSInternalCommands m_serverImplementation
          Stores the implementation which is used for processing the received HTTP request and creating the HTTP response which is sent back to the client.
private  java.net.Socket m_socket
          Stores the socket which is connected to the client we got the request from.
private  java.io.ByteArrayOutputStream m_tmpByteArrayOut
           
 
Constructor Summary
InfoServiceConnection(java.net.Socket a_socket, int a_connectionId, JWSInternalCommands a_serverImplementation)
          Creates a new instance of InfoServiceConnection for handling the received data as an HTTP request.
 
Method Summary
private  void closeSockets()
           
private  void initReader(int limit)
           
private  int read()
          Reads one byte from the underlying InputStream.
private  java.util.Vector readHeader()
          Reads the whole header of an HTTP request (including the last CRLF signalizing the end of the header, so the next byte read from the stream would be the first of the HTTP content).
private  java.lang.String readRequestLine()
          Reads the first line of an HTTP request (including the CRLF at the end of the line, so the next byte read from the underlying stream, is the first byte of the request header).
 void run()
          This is the Thread implementation for reading, parsing, processing the request and sending the response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_socket

private java.net.Socket m_socket
Stores the socket which is connected to the client we got the request from.


m_inputStream

private java.io.InputStream m_inputStream

m_connectionId

private int m_connectionId
Stores the ID of the connection which is used in the log-output for identifying the current connection.


m_serverImplementation

private JWSInternalCommands m_serverImplementation
Stores the implementation which is used for processing the received HTTP request and creating the HTTP response which is sent back to the client.


m_byteLimit

private int m_byteLimit
This stores the currently number of bytes which can be read from the InputStream until the data limit is exhausted.


m_tmpByteArrayOut

private java.io.ByteArrayOutputStream m_tmpByteArrayOut
Constructor Detail

InfoServiceConnection

public InfoServiceConnection(java.net.Socket a_socket,
                             int a_connectionId,
                             JWSInternalCommands a_serverImplementation)
Creates a new instance of InfoServiceConnection for handling the received data as an HTTP request.

Parameters:
a_socket - The socket which is connected to the client. We read the data from this socket, parse it as an HTTP request, process the request and send back the HTTP response to this socket.
a_connectionId - The connection ID which is used for identifying the log outputs of this new instance of InfoServiceConnection.
a_serverImplementation - The implementation which is used for processing the HTTP request and creating the HTTP response which is sent back to the client.
Method Detail

run

public void run()
This is the Thread implementation for reading, parsing, processing the request and sending the response. In every case, the socket is closed after finishing this method.

Specified by:
run in interface java.lang.Runnable

closeSockets

private void closeSockets()

readRequestLine

private java.lang.String readRequestLine()
                                  throws java.lang.Exception
Reads the first line of an HTTP request (including the CRLF at the end of the line, so the next byte read from the underlying stream, is the first byte of the request header). The line is also parsed for illegal characters. If there are illegal characters found or the read limit is exhausted or there is an unexpected end of the stream, an exception is thrown. Also if there occured an exception while reading from the stream, this exception is thrown. The syntax of the request line (whether it is a valid HTTP request line) is not checked here.

Parameters:
a_inputData - The InfoServiceConnectionReader for reading the line (with a limit of maximally read bytes).
Returns:
The line read from the stream without the trailing CRLF.
Throws:
java.lang.Exception

initReader

private void initReader(int limit)

read

private int read()
          throws java.lang.Exception
Reads one byte from the underlying InputStream. If the call is successful, the limit of bytes able to read from the stream is also decremented by 1. If the end of the stream is reached or there was an exception while reading from the stream, the byte limit is not decremented. In the case of a read exception, this exception is thrown. If the byte limit is exhausted, also an exception is thrown.

Returns:
The byte read from the stream or -1, if the end of the stream was reached.
Throws:
java.lang.Exception

readHeader

private java.util.Vector readHeader()
                             throws java.lang.Exception
Reads the whole header of an HTTP request (including the last CRLF signalizing the end of the header, so the next byte read from the stream would be the first of the HTTP content). The request line of the HTTP request should already be read from the stream. Folded header lines are concatenated to one header line (by removing the CRLF used for folding). Also all lines are parsed for illegal characters. If illegal characters are found or the read limit is exhausted or there is an unexpected end of the stream, an exception is thrown. Also if there occured an exception while reading from the stream, this exception is thrown. The syntax of the header lines (whether they are valid HTTP header lines) returned by this method is not checked here.

Parameters:
a_inputData - The InfoServiceConnectionReader where the HTTP header shall be read from (with a limit of maximally read bytes). The initially request line of the HTTP request should already be read from the underlying stream.
Returns:
A Vector of strings with the header lines (maybe empty, if there were no header fields). The trailing CRLF is removed at every line. If a header line was folded, the folding CRLF is removed (but not the SPACEs or TABs at the begin of the next line) and the whole line is within one String stored. The empty line which signals the end of the HTTP header is not included within the Vector.
Throws:
java.lang.Exception