HttpdBase4J 0.2

HttpdBase4J is an embeddable Java web server framework that supports HTTP, HTTPS, templated content.
HttpdBase4J is an embeddable Java web server framework that supports HTTP, HTTPS, templated content and serving content from inside an archive.

The classes are easy to extend as they utilize the Hollywood 'don't call us, we'll call you' principle to allow the user to overide the framework behaviour during all the different phases of the HTTP transaction.

Note:HttpdBase4J uses the Java 6 com.sun.net.httpserver classes so it won't currently work under earlier versions of Java.

Usage Examples:

The simplest use case is to create an embedded web server on port 8088 with its home directory at htdocs in the local filesystem and a root url at / ie / maps onto htdocs:

homeDir = new java.io.File("./htdocs");
httpd = new FileHttpd(homeDir, 10); // Creates a server with a threadpool of 10
httpd.setLogger(System.err); // Log to console
httpd.start(8088, "/");

Creating a HTTPS server equivalent the the HTTP server above requires passing a keystore and password to start. Passing null will generate a certificate free keystore if you only need the encryption capabilities of HTTPS and not the certificate capability:

httpd = new FileHttpd(homeDir, 10);
m_httpd.start(8089, "/", keystore, password);

Serving content from inside an archive or from a jar or zip in the Java classpath requires creating an ArchiveHttpd instance instead of a FileHttpd. To serve content located in a jar or zip in the class path you need only specify the resource directory in the classpath where the content islocated, for example:

httpd = new ArchiveHttpd("/resources/htdocs", 10);
httpd.start(8088, "/");

To serve content from within a specific archive specify the archive as a Java File when constructing the ArchiveHttpd:

httpd = new ArchiveHttpd(new File("content.zip"), "/resources/htdocs", 10);
httpd.start(8088, "/");

Creating web content using templates is also supported. Currently only the StringTemplate library (http://www.stringtemplate.org) is is supported but it should be relatively easy to create user derived classes for other template implementations. To create an HTTP embedded web server on serving templated content from resources/htdocs in the classpath and having a template file handler (A Java class implenting the Templatable interface that is used to fill the templates) in net.homeip.donaldm.test.templates.

httpd = new ArchiveHttpd("resources/htdocs", 10);

StringTemplateHandler stHandler = new ArchiveStringTemplateHandler(httpd,
"net.homeip.donaldm.test.templates");

httpd.addHandler(".st", stHandler); // .st extension = template files
httpd.start(m_port, "/");

The HTTP transaction handling can be customized at any point by overiding any of the methods in the HttpHandleable interface or by overiding onCreateRequestHandler in Httpd in order to provide your own request handler. A simple example of overiding HttpHandleable methods:

httpd = new TestOverideHttpd(m_homeDir, 10);
httpd.start(m_port, "/");

class TestOverideHttpd extends FileHttpd
{
public HttpResponse onServeHeaders(long id, HttpExchange ex, Request request)
{
//Create or amend content
}
public InputStream onServeBody(long id, HttpExchange ex, Request request)
{
//Return amended or created content
}
}

last updated on:
July 16th, 2007, 14:36 GMT
price:
FREE!
developed by:
Donald Munro
homepage:
httpdbase4j.berlios.de
license type:
LGPL (GNU Lesser General Public License) 
category:
ROOT \ Internet \ HTTP (WWW)

FREE!

In a hurry? Add it to your Download Basket!

user rating

UNRATED
0.0/5
 

0/5

Add your review!

SUBMIT