Class UrlMapperHandler
java.lang.Object
sunlabs.brazil.handler.UrlMapperHandler
- All Implemented Interfaces:
Handler
Handler for mapping URL's or HTTP headers, or redirecting URLs
based on the contents of the current HTTP request.
Matches URL's (or arbitrary request properties) against a regexp pattern.
If there is a match, the URL
(or specified HTTP header)
is rewritten or the URL is redirected.
Properties:
- match
- The regexp to match a url. May contain constructs
of the form ${xxx}, which are replaced by the value of
request.propsfor the key xxx - replace
- The url to replace it with. This may contain both regular expression sub-patterns, such as "\1", or variables of the form ${..} which are replaced with the equivalent request properties.
- export
- If set, use this as a properties prefix, and set request properties for each sub-expression in "match". (E.g. [export]1 [export]2 ...).
- redirect
- If set, the request is redirected instead of being rewritten
- ignoreCase
- If set, the case of the expression is ignored.
- source
-
If set, then this string is used instead of the
url as the source of the match. Variable substitution
using ${xxx} is performed on source, which, if unset,
defaults to "${url}". If set, ${} substitutions "method", "url", "protocol",
"query", and "serverUrl" are taken from the current Request object. Then
names in the Http Request headers are used, then names from the
Request.props.
The source property
is obtained at init time, but evaluated (for ${...}) at every request.
As an example, the configuration:
prefix.source=${user-agent}!${url}could cause all browsers with "Lynx" in their user agent header to the "text" sub-directory.
prefix.match=Lynx.*!(.*)
prefix.replace=/text\\1 - target
- By default, this handler modifies the request URL. If target is specified, it names an HTTP header to be replaced instead of the URL. The "target" is ignored if "redirect" is specified, and a new header is created if the "target" header doesn't already exist.
- Version:
- 2.6, 07/03/26
- Author:
- Stephen Uhler
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassLook in a dictionary first, then the provided properties. -
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
UrlMapperHandler
public UrlMapperHandler()
-
-
Method Details
-
init
Description copied from interface:HandlerInitializes the handler.- Specified by:
initin interfaceHandler- Parameters:
server- The HTTP server that created thisHandler. TypicalHandlers will useServer.propsto obtain run-time configuration information.prefix- The handlers name. The string thisHandlermay prepend to all of the keys that it uses to extract configuration information fromServer.props. This is set (by theServerandChainHandler) to help avoid configuration parameter namespace collisions.- Returns:
trueif thisHandlerinitialized successfully,falseotherwise. Iffalseis returned, thisHandlershould not be used.
-
respond
If this request matches the expression, rewrite it.- Specified by:
respondin interfaceHandler- Parameters:
request- TheRequestobject that represents the HTTP request.- Returns:
trueif the request was handled. A request was handled if a response was supplied to the client, typically by callingRequest.sendResponse()orRequest.sendError.- Throws:
IOException- if there was an I/O error while sending the response to the client. Typically, in that case, theServerwill (try to) send an error message to the client and then close the client's connection.The
IOExceptionshould not be used to silently ignore problems such as being unable to access some server-side resource (for example getting aFileNotFoundExceptiondue to not being able to open a file). In that case, theHandler's duty is to turn thatIOExceptioninto a HTTP response indicating, in this case, that a file could not be found.
-