org.astrogrid.samp.xmlrpc.internal
Class HttpServer

java.lang.Object
  extended byorg.astrogrid.samp.xmlrpc.internal.HttpServer

public class HttpServer
extends Object

Simple modular HTTP server. One thread is started per request. Connections are not kept open between requests. Suitable for very large response bodies, but not for very large request bodies. Concrete subclasses need to implement the serve(org.astrogrid.samp.xmlrpc.internal.HttpServer.Request) method. The protocol version served is HTTP/1.0.

Since:
21 Aug 2008
Author:
Mark Taylor

Nested Class Summary
static interface HttpServer.Handler
          Implemented to serve data for some URLs.
static class HttpServer.Request
          Represents a parsed HTTP client request.
static class HttpServer.Response
          Represents a response to an HTTP request.
 
Field Summary
static String HDR_CONTENT_TYPE
          Header string for MIME content type.
static int STATUS_OK
          Status code for OK (200).
 
Constructor Summary
HttpServer()
          Constructs a server based on a default socket, on any free port.
HttpServer(ServerSocket socket)
          Constructs a server based on a given socket.
 
Method Summary
 void addHandler(HttpServer.Handler handler)
          Adds a handler which can serve some requests going through this server.
static HttpServer.Response createErrorResponse(int code, String phrase)
          Utility method to create an error response.
static HttpServer.Response createErrorResponse(int code, String phrase, Throwable e)
          Utility method to create an error response given an exception.
 URL getBaseUrl()
          Returns the base URL for this server.
 ServerSocket getSocket()
          Returns the socket on which this server listens.
 boolean isRunning()
          Indicates whether this server is currently running.
 void removeHandler(HttpServer.Handler handler)
          Removes a handler previously added by addHandler(org.astrogrid.samp.xmlrpc.internal.HttpServer.Handler).
 HttpServer.Response serve(HttpServer.Request request)
          Does the work for providing output corresponding to a given HTTP request.
 void setDaemon(boolean isDaemon)
          Determines whether the server thread will be a daemon thread or not.
 void start()
          Starts the server if it is not already started.
 void stop()
          Stops the server if it is currently running.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HDR_CONTENT_TYPE

public static final String HDR_CONTENT_TYPE
Header string for MIME content type.

See Also:
Constant Field Values

STATUS_OK

public static final int STATUS_OK
Status code for OK (200).

See Also:
Constant Field Values
Constructor Detail

HttpServer

public HttpServer(ServerSocket socket)
Constructs a server based on a given socket.

Parameters:
socket - listening socket

HttpServer

public HttpServer()
           throws IOException
Constructs a server based on a default socket, on any free port.

Method Detail

addHandler

public void addHandler(HttpServer.Handler handler)
Adds a handler which can serve some requests going through this server.

Parameters:
handler - handler to add

removeHandler

public void removeHandler(HttpServer.Handler handler)
Removes a handler previously added by addHandler(org.astrogrid.samp.xmlrpc.internal.HttpServer.Handler).

Parameters:
handler - handler to remove

getSocket

public ServerSocket getSocket()
Returns the socket on which this server listens.

Returns:
server socket

getBaseUrl

public URL getBaseUrl()
Returns the base URL for this server.

Returns:
base URL

serve

public HttpServer.Response serve(HttpServer.Request request)
Does the work for providing output corresponding to a given HTTP request. This implementation calls each Handler in turn and the first one to provide a non-null response is used.

Parameters:
request - represents an HTTP request that has been received
Returns:
represents the content of an HTTP response that should be sent

setDaemon

public void setDaemon(boolean isDaemon)
Determines whether the server thread will be a daemon thread or not. Must be called before start() to have an effect. The default is true.

Parameters:
isDaemon - whether server thread will be daemon
See Also:
Thread.setDaemon(boolean)

start

public void start()
Starts the server if it is not already started.


stop

public void stop()
Stops the server if it is currently running. Processing of any requests which have already been received is completed.


isRunning

public boolean isRunning()
Indicates whether this server is currently running.

Returns:
true if running

createErrorResponse

public static HttpServer.Response createErrorResponse(int code,
                                                      String phrase)
Utility method to create an error response.

Parameters:
code - status code
phrase - status phrase
Returns:
new response object

createErrorResponse

public static HttpServer.Response createErrorResponse(int code,
                                                      String phrase,
                                                      Throwable e)
Utility method to create an error response given an exception.

Parameters:
code - status code
phrase - status phrase
e - exception which caused the trouble
Returns:
new response object


Copyright © 2008 AstroGrid. All Rights Reserved.