sunlabs.brazil.handler
Class VirtualHostHandler

java.lang.Object
  extended by sunlabs.brazil.handler.VirtualHostHandler (view source)
All Implemented Interfaces:
Handler

public class VirtualHostHandler
extends Object
implements Handler

Handler for managing virtual hosts using the same server configuration. This prefixes the host name (from the http "host" header, with the port portion removed) onto the url and passes the request along. If no host is provided, the host "default" is used instead.
If hosts require their own server configurations, use the MultiHostHandler instead.

Configuration parameters:

maproot
If set upon server startup, this handler changes the "root" property instead of the "url" property, by appending the "host" onto the document root, instead of prepending the "host" to the url.
[prefix].[host].
If the "mapping" property exists that matches the incoming "host" name, then that value is used instead of [host] to rewrite the "url" or "root".
default
If set, then all hosts for which no mappings are defined are mapped to the value of this property.
levels
If defined, then for the purpose of host matching, only "levels" of hostnames are considered. If levels=3, then for host: a.b.c.d.e, the host is considered to be "c.d.e". This enables support for wildcard-host matching within a virtual domain.
addlevel=true|false
If "true", "levels" is specified, and the number of tokens (levels) in the hostname exceeds "levels", then all the extra tokens in the hostname are prepended to the URL as initial directories: If "levels" is 3, and "addlevel=true" then: host http://a.b.c.d.e/foo.html will be mapped to http://c.d.e/b/a/foo.html, and the file "foo.html" should be at [docroot]/c.d.e/b/a/foo.html].

If "addlevel=false", then http://a.b.c.d.e/foo.html will be mapped to http://c.d.e/foo.html, and the file "foo.html" should be at [docroot]/c.d.e/foo.html. In this case, the "a.b" part of the host is available as part of the host property, which retains its original value.

With no configuration options, each virtual host document root is in a subdirectory whose name matches the host (e.g. www.foo.com). The "maproot" property changes how virtual roots are distingished: by URL or by document root. The "mapping" properties are used to choose a name for the subdirectory that differs from the virtual hostname. Finally, if "default" is set, then virtual hosts with no subdirectory are all shunted into the subdirectory specified.


Constructor Summary
VirtualHostHandler()
           
 
Method Summary
 boolean init(Server server, String prefix)
          Initializes the handler.
 boolean respond(Request request)
          Either look for host header, tack on front of url, or modify the "root" property
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VirtualHostHandler

public VirtualHostHandler()
Method Detail

init

public boolean init(Server server,
                    String prefix)
Description copied from interface: Handler
Initializes the handler.

Specified by:
init in interface Handler
Parameters:
server - The HTTP server that created this Handler. Typical Handlers will use Server.props to obtain run-time configuration information.
prefix - The handlers name. The string this Handler may prepend to all of the keys that it uses to extract configuration information from Server.props. This is set (by the Server and ChainHandler) to help avoid configuration parameter namespace collisions.
Returns:
true if this Handler initialized successfully, false otherwise. If false is returned, this Handler should not be used.

respond

public boolean respond(Request request)
                throws IOException
Either look for host header, tack on front of url, or modify the "root" property

Specified by:
respond in interface Handler
Parameters:
request - The Request object that represents the HTTP request.
Returns:
true if the request was handled. A request was handled if a response was supplied to the client, typically by calling Request.sendResponse() or Request.sendError.
Throws:
IOException - if there was an I/O error while sending the response to the client. Typically, in that case, the Server will (try to) send an error message to the client and then close the client's connection.

The IOException should not be used to silently ignore problems such as being unable to access some server-side resource (for example getting a FileNotFoundException due to not being able to open a file). In that case, the Handler's duty is to turn that IOException into a HTTP response indicating, in this case, that a file could not be found.


Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.