Package sunlabs.brazil.server
Class ChainHandler
- java.lang.Object
-
- sunlabs.brazil.server.ChainHandler
-
- All Implemented Interfaces:
Handler
- Direct Known Subclasses:
ChainSawHandler
public class ChainHandler extends java.lang.Object implements Handler
Allows multiple handlers to be invoked sequentially for a single HTTP request. A list of handlers is supplied when thisChainHandleris initialized. When an HTTP request is received by thisChainHandler, each of the handlers from the list is called in turn until one of them responds and returnstrue.A useful trick is that some handlers can be run by a
ChainHandlerfor their side effects. The handler can modify theRequestobject and then returnfalse; the next handler in the list will get a crack at the modified request.The following configuration parameters eare used to initialize this
Handler:-
handlers - A list of
Handlernames that will be invoked in the given order to handle the request. These are considered the "wrapped" handlers. These handlers will all be initialized at startup byinit(sunlabs.brazil.server.Server, java.lang.String). For each name in the list, the propertyname.classis examined to determine which class to use for this handler. Thennameis used as the prefix in the handler's init() method. -
report - If set, this property will be set to the name of the handler that handled the request (e.g. returned true).
-
exitOnError - If set, the server's
initFailurewill set any of the handlers fail to initialize. No handler prefix is required. prefix, suffix, glob, match- Specify the URL that triggers this handler.
- Version:
- 2.5
- Author:
- Stephen Uhler (stephen.uhler@sun.com), Colin Stevens (colin.stevens@sun.com)
- See Also:
Handler
-
-
Field Summary
Fields Modifier and Type Field Description booleanexitOnErrorA flag to require the successfull initialization of all handlers.Handler[]handlersThe array of handlers that will be invoked to handle the request.MatchStringisMineThe URL that must match for this handler to runjava.lang.String[]namesThe names of the abovehandlersas specified by the configuration parameters.java.lang.StringprefixThe prefix used to initialize thisChainHandler, used for logging.java.lang.StringreportThe name (if any) of the property to receive the name of the handler that handled the request.
-
Constructor Summary
Constructors Constructor Description ChainHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleaninit(Server server, java.lang.String prefix)Initializes thisChainHandlerby initializing all the "wrapped" handlers in the list of handlers.static HandlerinitHandler(Server server, java.lang.String prefix, java.lang.String name)Helper function that allocates and initializes a newHandler, given its name.booleanrespond(Request request)Calls each of theHandlers in turn until one of them returnstrue.
-
-
-
Field Detail
-
handlers
public Handler[] handlers
The array of handlers that will be invoked to handle the request.
-
names
public java.lang.String[] names
The names of the abovehandlersas specified by the configuration parameters. Used for logging the names of eachHandleras it is invoked.
-
prefix
public java.lang.String prefix
The prefix used to initialize thisChainHandler, used for logging.
-
isMine
public MatchString isMine
The URL that must match for this handler to run
-
report
public java.lang.String report
The name (if any) of the property to receive the name of the handler that handled the request.
-
exitOnError
public boolean exitOnError
A flag to require the successfull initialization of all handlers.
-
-
Method Detail
-
init
public boolean init(Server server, java.lang.String prefix)
Initializes thisChainHandlerby initializing all the "wrapped" handlers in the list of handlers. If a wrapped handler cannot be initialized, this method logs a message and skips it. If no handlers were specified, or no handlers were successfully initialized, then the initialization of thisChainHandleris considered to have failed.
-
initHandler
public static Handler initHandler(Server server, java.lang.String prefix, java.lang.String name)
Helper function that allocates and initializes a newHandler, given its name. In addition to theChainHandler, several other handlers contain embeddedHandlers -- this method can be used to initialize those embeddedHandlers.If there is an error initializing the specified
Handler, this method will log a dignostic message to the server and returnnull. This happens if the specified class cannot be found or instantiated, if the specified class is not actually aHandler, if theHandler.initmethod returnsfalse, or if there is any other exception.- Parameters:
server- The server that will own the newHandler. Mainly used for the server's properties, which contain the configuration parameters for the new handler.prefix- The prefix in the server's properties for the newHandler's configuration parameters. The prefix is prepended to the configuation parameters used by theHandler.name- The name of the newHandler. The name can be one of two forms:- The name of the Java class for the
Handler. ThisHandlerwill be initialized using theprefixspecified above. - A symbolic
name. The configuration parametername.classis the name of the Java class for theHandler. The aboveprefixwill be ignored and thisHandlerwill be initialized with the prefix "name." (the symbolic name followed by a ".").
- The name of the Java class for the
- Returns:
- The newly allocated
Handler, ornullif theHandlercould not be allocated.
-
respond
public boolean respond(Request request) throws java.io.IOException
Calls each of theHandlers in turn until one of them returnstrue.
-
-