simple.http.serve
Class DomainHandler

java.lang.Object
  extended by simple.http.serve.DomainHandler
All Implemented Interfaces:
ProtocolHandler

public class DomainHandler
extends java.lang.Object
implements ProtocolHandler

This class is used to route requests to the proper host. This allows the virtual host facility to be achieved with a set of ProtocolHandler's. This will ensure that if requests are issued, the host header is checked to see what domain it has and dispatches it to the correct ProtocolHandler.

Also if there is a Host header with an unknown domain name then the default ProtocolHandler is given the request. In this case the default protocol handler chosen may be an error handler. However to avoid this the domain multiplexer may be wrapped in another ProtocolHandler implementation to handle these requests before the DomainHandler sees them.

Author:
Niall Gallagher

Constructor Summary
DomainHandler(ProtocolHandler primary)
          Constructor for the DomainHandler.
 
Method Summary
 boolean contains(java.lang.String pattern)
          This is used to lookup a certain pattern match to see if the DomainHandler has it.
 void handle(Request req, Response resp)
          This is used to route the request to the correct handler.
 void insert(ProtocolHandler handler, java.lang.String pattern)
          This will insert a new ProtocolHandler for the specified pattern.
 ProtocolHandler lookup(java.lang.String domain)
          Used to lookup a specific ProtocolHandler for a specific domain.
 void remove(java.lang.String pattern)
          This will remove any ProtocolHandler for the specified pattern match.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DomainHandler

public DomainHandler(ProtocolHandler primary)
Constructor for the DomainHandler. This takes a default ProtocolHandler. Which enables this to accommodate HTTP/1.0 clients to interact with the server without using the Host header. This default ProtocolHandler can also be an error generator that will respond with a 400, Bad Request, status code as suggested by RFC 2616 section 14.23 for HTTP/1.1 clients without the Host header.

This ensures that there will be very little performance loss when there are several ProtocolHandler's being multiplexed using this ProtocolHandler implementation.

Parameters:
primary - the default ProtocolHandler
Method Detail

handle

public void handle(Request req,
                   Response resp)
This is used to route the request to the correct handler. This will use the Host header to choose a handler for the request. The host header is a mandatory client request header for HTTP/1.1 clients. The header contains the domain of the intended host and an optional port.

For compatibility with HTTP/1.0 clients that do not support the Host header this delegates the processing of requests without the Host header to a default ProtocolHandler.

Specified by:
handle in interface ProtocolHandler
Parameters:
req - this is the Request that was made from the http client
resp - this is the Response that will be given to the client

insert

public void insert(ProtocolHandler handler,
                   java.lang.String pattern)
This will insert a new ProtocolHandler for the specified pattern. The new ProtocolHandler will manage the requests that have the host header with the domain name part equal to the domain given.

Parameters:
pattern - the pattern of the virtual host to be added
handler - the ProtocolHandler that will process requests

remove

public void remove(java.lang.String pattern)
This will remove any ProtocolHandler for the specified pattern match. Further Request's to this domain will not be processed.

Parameters:
pattern - this is the name of the handler to remove

contains

public boolean contains(java.lang.String pattern)
This is used to lookup a certain pattern match to see if the DomainHandler has it. If this returns true then the pattern exists.

Parameters:
pattern - the pattern being checked for in this
Returns:
true if there is a handler with this pattern

lookup

public ProtocolHandler lookup(java.lang.String domain)
Used to lookup a specific ProtocolHandler for a specific domain. This allows the user to take the ProtocolHandler objects from this for some modification, or to use it.

Parameters:
domain - the host domain name for this handler
Returns:
the ProtocolHandler for the domain