com.bbn.openmap.util.http
Class HttpServer

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

public class HttpServer
extends java.lang.Thread

A simple HTTP Server implementing HTTP/0.9 protocols. Cobbled together from a server originally written by David Flanagan for the book Java in a Nutshell , Copyright(c) 1996 O'Reilly & Associates. Modified to use JDK 1.1 Readers, and Writers. Further modified to use the JDK 1.1 Event model.

Version:
1.0, 06/13/97
Author:
Tom Mitchell

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static int DEFAULT_PORT
          The default port.
protected  java.net.ServerSocket listen_socket
           
protected  java.util.Vector listeners
           
protected  int port
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
HttpServer()
          Creates an Http Server on any free port, and then starts a thread that listens to that port.
HttpServer(int port)
          Creates an Http Server on the indicated port, and then starts a thread that listens to that port.
HttpServer(int port, boolean asDaemon)
          Creates an Http Server on the indicated port, and then starts a thread that listens to that port.
 
Method Summary
 void addHttpRequestListener(HttpRequestListener l)
          Adds a new http request listener.
 HttpRequestEvent fireHttpRequestEvent(java.lang.String request, java.io.OutputStream output)
          Creates a HttpRequestEvent and sends it to all registered listeners.
 int getPort()
          Gets the port associate with this server.
static void main(java.lang.String[] args)
          A main routine for unit testing.
 void removeHttpRequestListener(HttpRequestListener l)
          Removes an http request listener.
 void run()
          The body of the server thread.
 
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

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port. A port of 0 (zero) causes the system to allocate any unused port. With any other number the system will attempt to open that port, and throw an exception if it is in use.

See Also:
Constant Field Values

port

protected int port

listen_socket

protected java.net.ServerSocket listen_socket

listeners

protected java.util.Vector listeners
Constructor Detail

HttpServer

public HttpServer(int port)
           throws java.io.IOException
Creates an Http Server on the indicated port, and then starts a thread that listens to that port. The thread will not be a daemon thread.

Parameters:
port - the port to open
Throws:
java.io.IOException
See Also:
ServerSocket

HttpServer

public HttpServer(int port,
                  boolean asDaemon)
           throws java.io.IOException
Creates an Http Server on the indicated port, and then starts a thread that listens to that port. The thread will be a daemon thread of asDaemon is true.

Parameters:
port - the port to open
asDaemon - whether to make thread a daemon
Throws:
java.io.IOException
See Also:
ServerSocket

HttpServer

public HttpServer()
           throws java.io.IOException
Creates an Http Server on any free port, and then starts a thread that listens to that port.

Throws:
java.io.IOException
See Also:
ServerSocket
Method Detail

run

public void run()
The body of the server thread. Loop forever, listening for and accepting connections from clients. For each connection, create a HttpConnection object to handle communication through the new Socket.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread
See Also:
HttpConnection, Socket

getPort

public int getPort()
Gets the port associate with this server.

Returns:
the server's port

fireHttpRequestEvent

public HttpRequestEvent fireHttpRequestEvent(java.lang.String request,
                                             java.io.OutputStream output)
                                      throws java.io.IOException
Creates a HttpRequestEvent and sends it to all registered listeners.

Parameters:
request - the parsed http request
output - OutputStream associated with the request's client connection.
Throws:
java.io.IOException
See Also:
DataOutputStream, HttpRequestListener, HttpRequestEvent

addHttpRequestListener

public void addHttpRequestListener(HttpRequestListener l)
Adds a new http request listener. Don't add multiple listeners when binary content responses are required! One Listener should handle binary responses, because the result length needs to be calculated. You can add multiple Listeners that use the Writer inside the HttpRequestEvent to concatenate a complete text response.

Parameters:
l - the listener
See Also:
HttpRequestListener

removeHttpRequestListener

public void removeHttpRequestListener(HttpRequestListener l)
Removes an http request listener.

Parameters:
l - a listener
See Also:
HttpRequestListener

main

public static void main(java.lang.String[] args)
A main routine for unit testing. Starts a HttpServer, adds several HttpRequestListeners, and waits for connections.

Usage: java com.bbn.openmap.layer.util.http.HttpServer [port]

If no port is specified, the default port is used.

If port zero is specified, the system chooses the port.

If a port other than zero is specified, the http server will attempt to open that port, or fail if it is in use.

Examples:

java com.bbn.openmap.layer.util.http.HttpServer

java com.bbn.openmap.layer.util.http.HttpServer 8000

java com.bbn.openmap.layer.util.http.HttpServer 0

Parameters:
args - command line args


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