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 thisChainHandler
is 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
ChainHandler
for their side effects. The handler can modify theRequest
object 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
Handler
names 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.class
is examined to determine which class to use for this handler. Thenname
is 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
initFailure
will 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 boolean
exitOnError
A flag to require the successfull initialization of all handlers.Handler[]
handlers
The array of handlers that will be invoked to handle the request.MatchString
isMine
The URL that must match for this handler to runjava.lang.String[]
names
The names of the abovehandlers
as specified by the configuration parameters.java.lang.String
prefix
The prefix used to initialize thisChainHandler
, used for logging.java.lang.String
report
The 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 boolean
init(Server server, java.lang.String prefix)
Initializes thisChainHandler
by initializing all the "wrapped" handlers in the list of handlers.static Handler
initHandler(Server server, java.lang.String prefix, java.lang.String name)
Helper function that allocates and initializes a newHandler
, given its name.boolean
respond(Request request)
Calls each of theHandler
s 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 abovehandlers
as specified by the configuration parameters. Used for logging the names of eachHandler
as 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 thisChainHandler
by 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 thisChainHandler
is 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 embeddedHandler
s -- this method can be used to initialize those embeddedHandler
s.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.init
method 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
. ThisHandler
will be initialized using theprefix
specified above. - A symbolic
name
. The configuration parametername.class
is the name of the Java class for theHandler
. The aboveprefix
will be ignored and thisHandler
will 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
, ornull
if theHandler
could not be allocated.
-
respond
public boolean respond(Request request) throws java.io.IOException
Calls each of theHandler
s in turn until one of them returnstrue
.
-
-