com.bigdata.util.httpd
Class NanoHTTPD

java.lang.Object
  extended by com.bigdata.util.httpd.NanoHTTPD
All Implemented Interfaces:
IServiceShutdown
Direct Known Subclasses:
AbstractHTTPD

public class NanoHTTPD
extends Object
implements IServiceShutdown

A simple, tiny, nicely embeddable HTTP 1.0 server in Java

NanoHTTPD version 1.1, Copyright © 2001,2005-2007 Jarno Elonen (elonen@iki.fi, http://iki.fi/elonen/)

Various modifications since supporting integration within bigdata services © 2008, SYSTAP, LLC.

Features + limitations:

Ways to use:

See the end of the source file for distribution license (Modified BSD licence)

Version:
$Id: NanoHTTPD.java 2265 2009-10-26 12:51:06Z thompsonbry $

Nested Class Summary
 class NanoHTTPD.Response
          HTTP response.
 
Nested classes/interfaces inherited from interface com.bigdata.service.IServiceShutdown
IServiceShutdown.Options
 
Field Summary
static String HTTP_BADREQUEST
          Some HTTP response status codes
static String HTTP_FORBIDDEN
          Some HTTP response status codes
static String HTTP_INTERNALERROR
          Some HTTP response status codes
static String HTTP_METHOD_NOT_ALLOWED
          Some HTTP response status codes
static String HTTP_NOTFOUND
          Some HTTP response status codes
static String HTTP_NOTIMPLEMENTED
          Some HTTP response status codes
static String HTTP_OK
          Some HTTP response status codes
static String HTTP_REDIRECT
          Some HTTP response status codes
static org.apache.log4j.Logger log
           
static String MIME_APPLICATION_XML
          Common mime types for dynamic content
static String MIME_DEFAULT_BINARY
          Common mime types for dynamic content
static String MIME_TEXT_HTML
          Common mime types for dynamic content
static String MIME_TEXT_JAVASCRIPT
          Common mime types for dynamic content
static String MIME_TEXT_PLAIN
          Common mime types for dynamic content
 
Constructor Summary
NanoHTTPD(int port)
          Starts a HTTP server to given port.
 
Method Summary
static LinkedHashMap<String,Vector<String>> decodeParms(String parms)
          Decodes parameters in percent-encoded URI-format ( e.g.
 int getPort()
          The port on which the service was started.
 boolean isOpen()
          Return true iff the service is running.
static void main(String[] args)
          Starts as a standalone file server and waits for Enter.
 NanoHTTPD.Response serve(String uri, String method, Properties header, LinkedHashMap<String,Vector<String>> parms)
          Override this to customize the server.
 NanoHTTPD.Response serveFile(String uri, Properties header, File homeDir, boolean allowDirectoryListing)
          Serves file from homeDir and its' subdirectories (only).
 void shutdown()
          The service will no longer accept new requests, but existing requests will be processed (sychronous).
 void shutdownNow()
          The service will no longer accept new requests and will make a best effort attempt to terminate all existing requests and return ASAP.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

public static final org.apache.log4j.Logger log

HTTP_OK

public static final String HTTP_OK
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_REDIRECT

public static final String HTTP_REDIRECT
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_FORBIDDEN

public static final String HTTP_FORBIDDEN
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_NOTFOUND

public static final String HTTP_NOTFOUND
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_BADREQUEST

public static final String HTTP_BADREQUEST
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_METHOD_NOT_ALLOWED

public static final String HTTP_METHOD_NOT_ALLOWED
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_INTERNALERROR

public static final String HTTP_INTERNALERROR
Some HTTP response status codes

See Also:
Constant Field Values

HTTP_NOTIMPLEMENTED

public static final String HTTP_NOTIMPLEMENTED
Some HTTP response status codes

See Also:
Constant Field Values

MIME_TEXT_PLAIN

public static final String MIME_TEXT_PLAIN
Common mime types for dynamic content

See Also:
Constant Field Values

MIME_TEXT_HTML

public static final String MIME_TEXT_HTML
Common mime types for dynamic content

See Also:
Constant Field Values

MIME_DEFAULT_BINARY

public static final String MIME_DEFAULT_BINARY
Common mime types for dynamic content

See Also:
Constant Field Values

MIME_APPLICATION_XML

public static final String MIME_APPLICATION_XML
Common mime types for dynamic content

See Also:
Constant Field Values

MIME_TEXT_JAVASCRIPT

public static final String MIME_TEXT_JAVASCRIPT
Common mime types for dynamic content

See Also:
Constant Field Values
Constructor Detail

NanoHTTPD

public NanoHTTPD(int port)
          throws IOException
Starts a HTTP server to given port.

Throws an IOException if the socket is already in use

Parameters:
port - The port. If 0 the the server will start on a random port. The actual port is available from #getPort().
Throws:
IOException
Method Detail

serve

public NanoHTTPD.Response serve(String uri,
                                String method,
                                Properties header,
                                LinkedHashMap<String,Vector<String>> parms)
Override this to customize the server.

(By default, this delegates to serveFile() and allows directory listing.)

Parameters:
uri - Percent-decoded URI without parameters, for example "/index.cgi"
method - "GET", "POST" etc.
parms - Parsed, percent decoded parameters from URI and, in case of POST, data. The keys are the parameter names. Each value is a Collection of Strings containing the bindings for the named parameter. The order of the URL parameters is preserved.
header - Header entries, percent decoded
Returns:
HTTP response, see class Response for details

isOpen

public boolean isOpen()
Description copied from interface: IServiceShutdown
Return true iff the service is running.

Specified by:
isOpen in interface IServiceShutdown

shutdown

public void shutdown()
Description copied from interface: IServiceShutdown
The service will no longer accept new requests, but existing requests will be processed (sychronous). This method should await the termination of pending requests, but no longer than the timeout specified by IServiceShutdown.Options.SHUTDOWN_TIMEOUT. Implementations SHOULD be synchronized. If the service is aleady shutdown, then this method should be a NOP.

Specified by:
shutdown in interface IServiceShutdown

shutdownNow

public void shutdownNow()
Description copied from interface: IServiceShutdown
The service will no longer accept new requests and will make a best effort attempt to terminate all existing requests and return ASAP. This method should terminate any asynchronous processing, release all resources and return immediately. Implementations SHOULD be synchronized. If the service is aleady shutdown, then this method should be a NOP.

Specified by:
shutdownNow in interface IServiceShutdown

main

public static void main(String[] args)
Starts as a standalone file server and waits for Enter.


getPort

public int getPort()
The port on which the service was started.


serveFile

public NanoHTTPD.Response serveFile(String uri,
                                    Properties header,
                                    File homeDir,
                                    boolean allowDirectoryListing)
Serves file from homeDir and its' subdirectories (only). Uses only URI, ignores all headers and HTTP parameters.


decodeParms

public static LinkedHashMap<String,Vector<String>> decodeParms(String parms)
                                                        throws UnsupportedEncodingException
Decodes parameters in percent-encoded URI-format ( e.g. "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to a Map .

Returns:
A map of the parsed, percent decoded parameters from URI and. The keys are the parameter names. Each value is a Vector of Strings containing the bindings for the named parameter. The order of the URL parameters is preserved by the insertion order of the LinkedHashMap and the elements of the Vector values.
Throws:
UnsupportedEncodingException


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.