Class ChainHandler
java.lang.Object
sunlabs.brazil.server.ChainHandler
- All Implemented Interfaces:
Handler
- Direct Known Subclasses:
ChainSawHandler
Allows multiple handlers to be invoked sequentially for a single HTTP
request. A list of handlers is supplied when this
ChainHandler
is initialized. When an HTTP request is
received by this ChainHandler
, each of the handlers from the
list is called in turn until one of them responds and returns
true
.
A useful trick is that some handlers can be run by a
ChainHandler
for their side effects. The handler can modify
the Request
object and then return false
; 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(Server, 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionboolean
A flag to require the successfull initialization of all handlers.Handler[]
The array of handlers that will be invoked to handle the request.The URL that must match for this handler to runString[]
The names of the abovehandlers
as specified by the configuration parameters.The prefix used to initialize thisChainHandler
, used for logging.The name (if any) of the property to receive the name of the handler that handled the request. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Initializes thisChainHandler
by initializing all the "wrapped" handlers in the list of handlers.static Handler
initHandler
(Server server, String prefix, String name) Helper function that allocates and initializes a newHandler
, given its name.boolean
Calls each of theHandler
s in turn until one of them returnstrue
.
-
Field Details
-
handlers
The array of handlers that will be invoked to handle the request. -
names
The names of the abovehandlers
as specified by the configuration parameters. Used for logging the names of eachHandler
as it is invoked. -
prefix
The prefix used to initialize thisChainHandler
, used for logging. -
isMine
The URL that must match for this handler to run -
report
The name (if any) of the property to receive the name of the handler that handled the request. -
exitOnError
public boolean exitOnErrorA flag to require the successfull initialization of all handlers.
-
-
Constructor Details
-
ChainHandler
public ChainHandler()
-
-
Method Details
-
init
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
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
Calls each of theHandler
s in turn until one of them returnstrue
.- Specified by:
respond
in interfaceHandler
- Parameters:
request
- The HTTP request.- Returns:
true
if one of theHandler
s returnstrue
,false
otherwise.- Throws:
IOException
- if one of theHandler
s throws anIOException
while responding.
-