com.bbn.openmap.util.http
Class HttpConnection

java.lang.Object
  extended by java.lang.Thread
      extended by com.bbn.openmap.util.http.HttpConnection
All Implemented Interfaces:
java.lang.Runnable

public class HttpConnection
extends java.lang.Thread

HttpConnection handles the communication with an HTTP client in its own thread. An instance of this class is created by the HttpServer each time a connection is made. The instance exists only long enough to fulfill the request, then dies.

Version:
1.1, 06/17/97
Author:
Tom Mitchell
See Also:
HttpServer

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  java.net.Socket client
           
static java.lang.String CONTENT_GEOTIFF
           
static java.lang.String CONTENT_GIF
           
static java.lang.String CONTENT_HTML
           
static java.lang.String CONTENT_JPEG
           
static java.lang.String CONTENT_JSON
           
static java.lang.String CONTENT_MOV
           
static java.lang.String CONTENT_PLAIN
           
static java.lang.String CONTENT_PNG
           
static java.lang.String CONTENT_PPM
           
static java.lang.String CONTENT_SVG
           
static java.lang.String CONTENT_TIFF
           
static java.lang.String CONTENT_WBMP
           
static java.lang.String CONTENT_XML
           
protected  java.io.BufferedReader in
           
protected  boolean isConnectionOpen
           
protected  java.io.OutputStream out
           
protected  HttpServer server
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
HttpConnection(java.net.Socket client_socket, HttpServer server)
          Initialize the input Reader and output Writer and start the connection thread.
 
Method Summary
 void close()
          Close the socket connection that we have opened
static java.lang.String[] getAllContentTypes()
           
protected  void handlePost()
           
protected  boolean isEnvarLine(java.lang.String line)
           
protected  void processGetCommand(java.lang.String cmd)
          Process a "GET" HTTP command.
protected  void processLine(java.lang.String line)
          Processes a line of an HTTP request.
protected  void readContent(int length)
           
protected  int readContentLength(java.lang.String line)
           
 void run()
          The running thread simply reads all the lines of input and hands each line off to be parsed.
static void writeHttpResponse(java.io.OutputStream out, java.lang.String contentType, byte[] response)
          Write a byte[] response to the OutputStream.
static void writeHttpResponse(java.io.OutputStream out, java.lang.String contentType, java.lang.String response)
          Write a String response to the OutputStream.
static void writeHttpResponseHeader(java.io.Writer out, java.lang.String contentType, int contentLength)
          Can be used to write the header to an HttpResponse.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

server

protected HttpServer server

client

protected java.net.Socket client

in

protected java.io.BufferedReader in

out

protected java.io.OutputStream out

isConnectionOpen

protected boolean isConnectionOpen

CONTENT_JPEG

public static final java.lang.String CONTENT_JPEG
See Also:
Constant Field Values

CONTENT_GIF

public static final java.lang.String CONTENT_GIF
See Also:
Constant Field Values

CONTENT_PPM

public static final java.lang.String CONTENT_PPM
See Also:
Constant Field Values

CONTENT_PNG

public static final java.lang.String CONTENT_PNG
See Also:
Constant Field Values

CONTENT_TIFF

public static final java.lang.String CONTENT_TIFF
See Also:
Constant Field Values

CONTENT_GEOTIFF

public static final java.lang.String CONTENT_GEOTIFF
See Also:
Constant Field Values

CONTENT_WBMP

public static final java.lang.String CONTENT_WBMP
See Also:
Constant Field Values

CONTENT_SVG

public static final java.lang.String CONTENT_SVG
See Also:
Constant Field Values

CONTENT_HTML

public static final java.lang.String CONTENT_HTML
See Also:
Constant Field Values

CONTENT_MOV

public static final java.lang.String CONTENT_MOV
See Also:
Constant Field Values

CONTENT_PLAIN

public static final java.lang.String CONTENT_PLAIN
See Also:
Constant Field Values

CONTENT_XML

public static final java.lang.String CONTENT_XML
See Also:
Constant Field Values

CONTENT_JSON

public static final java.lang.String CONTENT_JSON
See Also:
Constant Field Values
Constructor Detail

HttpConnection

public HttpConnection(java.net.Socket client_socket,
                      HttpServer server)
Initialize the input Reader and output Writer and start the connection thread.

Parameters:
client_socket - the client's socket
server - the server object
Method Detail

getAllContentTypes

public static java.lang.String[] getAllContentTypes()

run

public void run()
The running thread simply reads all the lines of input and hands each line off to be parsed.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

processLine

protected void processLine(java.lang.String line)
                    throws java.io.IOException
Processes a line of an HTTP request. The only interesting line we really look at is a GET command, which starts with "GET".

Parameters:
line - one line of an HTTP request
Throws:
java.io.IOException

handlePost

protected void handlePost()
                   throws java.io.IOException
Throws:
java.io.IOException

readContentLength

protected int readContentLength(java.lang.String line)

isEnvarLine

protected boolean isEnvarLine(java.lang.String line)

readContent

protected void readContent(int length)
                    throws java.io.IOException
Throws:
java.io.IOException

processGetCommand

protected void processGetCommand(java.lang.String cmd)
                          throws java.io.IOException
Process a "GET" HTTP command. The leading "GET " and the trailing HTTP version information are stripped off and a HttpRequestEvent is fired via the HttpServer.

Parameters:
cmd - a "GET" HTTP command
Throws:
java.io.IOException

writeHttpResponseHeader

public static void writeHttpResponseHeader(java.io.Writer out,
                                           java.lang.String contentType,
                                           int contentLength)
                                    throws java.io.IOException
Can be used to write the header to an HttpResponse. You need to create a Writer tied to the OutputStream in order to write this text.

Parameters:
out - Writer to place text on the OutputStream.
contentType - the mime type for your response.
contentLength - the byte length of your response.
Throws:
java.io.IOException

writeHttpResponse

public static void writeHttpResponse(java.io.OutputStream out,
                                     java.lang.String contentType,
                                     java.lang.String response)
                              throws java.io.IOException
Write a String response to the OutputStream.

Parameters:
out - the OutputStream of the response.
contentType - the content type of the response.
response - the string containing the response.
Throws:
java.io.IOException

writeHttpResponse

public static void writeHttpResponse(java.io.OutputStream out,
                                     java.lang.String contentType,
                                     byte[] response)
                              throws java.io.IOException
Write a byte[] response to the OutputStream.

Parameters:
out - the OutputStream of the response.
contentType - the content type of the response.
response - the byte array containing the response.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close the socket connection that we have opened

Throws:
java.io.IOException


Copyright (C) BBNT Solutions LLC; See http://openmap.bbn.com/ for details