Wadjet-HTTP supplies a multi-threaded HTTP daemon that can be run within a process. It is used by the JMX Http Adaptor in Wadjet-JMX . The daemon supports:
Request processors are usually tied to a request path to handle the requests for a specific path in the server.
Authentication can be tied to the server as a whole and/or to individual processors. This allows you to fine tune the access to different directories.
The following sections describe how to use the IHttpd implementation in your programs:
To run an httpd within your process you will need to have entries in the DynamicPool.conf file so that the the httpd can configure the thread pool for handling incoming requests, this file will need the following entries:
| Key | Value | Meaning |
| RequestProcessor.type | com.addc.httpd.impl.HttpClientThread | The class name of the pool theads |
| RequestProcessor.maxObjects | 5 | Maximum number of concurrent requests threads |
| RequestProcessor.minObjects | 2 | The minimum number of request threads in the pool |
For the httpd itself, you will need to either set up a service configuration file named Httpd.con or explicitly set values on the IHttpd implementation see Starting The IHttpd . The Httpd.conf file takes the properties shown in the table below.
| Key | Description | Default |
| hostname | The host name to accept connections on e.g. localhost | The local host name |
| port | The port to listen for connections on | 8082 |
| rootPath | The path to the files to serve | / |
The IHttpd is, like most of the services in Wadjet Libraries obtained using a locator:
IHttpd httpd = HttpdFactory.getHttpd();
This will return an initialised IHttpd instance. you can now set up any authentication for the server, set a chain of processors, a path translator and any error page handler you may wish. By default, no authentication is used, the simple default processor, the simple default path translator and the simple default error page handler are installed.
IHttpAuthentica auth = new DefaultHttpAuthenticator(myPasswordFilePath);
httpd.setAuthenticator(auth);
httpd.setErrorPageHandler(myErrorPageHandler);
httpd.setPathTranslator(myPathTranslator);
httpd.setProcessors(myHttpdProcessorChain);
You can also set the port, root path and host name here if you didn't set up a configuration file
httpd.setPort(8080);
httpd.setHost("localhosr");
httpd.setRootPath("/path/to/files/");
The httpd is now ready to start.
httpd.start();
NOTE on startup the main object is registered with the ShutdownHookThread so that the service is closed down cleanly on exit.
Authentication is defined per request processor and the authenticator class can either be global (attached to the IHttpd instance) or attached to a processor.
To use the authentication you need to tell each processor what kind of authentication to use by setting the authentication type with one of the constants:
myProcessor1.setAuthenticationType(IHttpd.AUTH_NONE);
myProcessor2.setAuthenticationType(IHttpd.AUTH_BASIC);
myProcessor3.setAuthenticationType(IHttpd.AUTH_DIGEST);
The above will set up request processors that have different authentication types and use the global IHttpAuthenticator set on the IHttpd instance.
To set a specific authenticator on a processor, you can use the supplied implementation and a different password file or implement your own authentication
IHttpAuthentica auth = new DefaultHttpAuthenticator(processorPasswordFilePath);
myProcessor.setAuthenticator(auth);
IHttpAuthentica auth = new MyHttpAuthenticator();
myProcessor.setAuthenticator(auth);
WARNING - Older versions of Microsoft Internet Explorer do not respond correctly to RFC2617 Digest headers (no surprise given their abominable security record). I assume that Billy's boys have, in fact, invented their own version of the standard to render their users MS dependent but I'm not willing to spend my valuable time trying to work out what strange and wonderful method they have invented. This means that having Digest authentication activated will make any paths using this authentication type invisible to Older versions of MSIE before version 7.
At the heart of the httpd is the IHttpProcessor chain which is called for every request received by the daemon. If you want to deliver content that is generated by your progrtam rather than just static content, you need to develop a chain of one or more processors (see the code for Wadjet-JMX ).
we supply the AbstractProcessor class whith all the base methods for setting up authentication, the path the processor is responsible for etc.. In fact, everything you need except the actual process() method.
To make a chain, you need to do something like this:
IHttpProcessor head = new HeadOfChaionProcessor();
head.setPath("/");
head.setAuthenticationType(IHttpd.AUTH_BASIC);
IHttpProcessor current = head;
IHttpProcessor next = new processor1();
next.setPath("/somewhere");
next.setAuthenticationType(IHttpd.AUTH_BASIC);
current.setNext(next);
current = next;
IHttpProcessor next = new processor2();
next.setPath("/somewhere/else");
next.setAuthenticationType(IHttpd.AUTH_BASIC);
current.setNext(next);
current = next;
...
httpd.setProcessors(head);
You could also have your HeadOfChainProcessor create the next one and add it to the chain and each other one do the same.
The process method is where you will make the decision that the processor should actually process the request or pass it on:
public void process(String path,
HttpInputStream httpIn,
HttpOutputStream httpOut,
String host) throws ServiceException,
IOException,
EndOfChainException {
if (!m_path.equals(path)) {
if (m_next == null) {
throw new EndOfChainException();
} else {
m_next.process(path, httpIn, httpOut, host);
}
}
checkAthentication(httpIn, httpOut);
...
// Process the page here
...
// Create the header
httpOut.setCode(HttpURLConnection.HTTP_OK);
httpOut.setHeader("Content-Type", "text/html");
httpOut.setHeader("Cache-Control", "cache");
// httpOut.setHeader("expires", "now");
httpOut.setHeader("pragma", "cache");
httpOut.sendHeaders();
...
// Send the body of the page
....
}
As you saw above, you can use the following classes/interfaces to extend the httpd >:
You could event write your own implementation of IHttp interface.