Redirect filter supports the following groovy-based DSL for "filter.groovy":
/**
* Adds new filter to filter chain.
* Must be called in the top scope of "filter.groovy" script.
* options - contains one or more filter criteria.
* closure - represents filter handler, which is invoked only when all criteria are fulfilled.
*/
void filter(Map options, Closure closure)
/**
* Redirects current HTTP request to the specified destination.
* Must be called only within closure of filter function.
* destination - can be URI, URIBuilder or String.
*/
void redirect(destination)
/**
* Forwards current HTTP request to the specified destination.
* Must be called only within closure of filter function.
* destination - can be URI, URIBuilder or String.
*/
void forward(destination)
"options" represents filter criteria, that are implicitly combined with AND operator. For example, if you put the following into options:
filter relPath: '/path', scheme: 'http', {
// ...
}
then program will check for relative path being equal to '/path' AND for scheme being equal to 'http'. Only when both comparisons are successful, then the closure will be called.
"options" supports two types of values: strings and regular expressions. Strings are compared as they are, while regular expressions are matched via =~
operator. The result of match is passed as map parameter to the closure. Example:
filter relPath: ~'/path/(*.)', query: ~'x=(.*)', { matches ->
// if we get here, then matches.relPath[0][1] will contain first capture group for relPath regex,
// while matches.query[0][1] will contain first capture group for query regex.
}
Redirect filter recognizes the following keys within "options":
key |
type |
meaning |
httpPort |
Integer |
port configured for (unencrypted) HTTP connections within servlet container config |
httpsPort |
Integer |
port configured for HTTPS connections within servlet container config |
contextPath |
String |
context path of the given web-app (where "filter.groovy" is located) |
authority |
String |
authority of request URI |
fragment |
String |
fragment of request URI |
host |
String |
host of request URI |
path |
String |
path of request URI |
port |
String |
port of request URI |
query |
String |
query of request URI |
scheme |
String |
scheme of request URI |
userInfo |
String |
userInfo of request URI |
relPath |
String |
path of the current HTTP request, relative to the current context path. For example, if context path is "/myWebApp" and request path is "/myWebApp/hello", then relPath is "/hello". |
Redirect filter provides the following variables to filter closure:
key |
type |
meaning |
requestURI |
URI |
the complete URI of the current HTTP request |
request |
HttpServletRequest |
the request object |
response |
HttpServletResponse |
the response object |
webappDir |
java.io.File |
the directory of the current web-app, as returned by ServletContext().getRealPath('') |
log |
org.slf4j.Logger |
Logger interface. Note that Gretty redirect filter does not depend on particular implementation of slf4j logger. It only depends on slf4j-api, so it’s up to you to initialize/configure slf4j properly. |
httpPort |
Integer |
port configured within servlet container for (unencrypted) HTTP connections |
httpsPort |
Integer |
port configured within servlet container for HTTPS connections |
contextPath |
String |
context path of the current web-app |
authority |
String |
authority of request URI |
fragment |
String |
fragment of request URI |
host |
String |
host of request URI |
path |
String |
path of request URI |
port |
String |
port of request URI |
query |
String |
query of request URI |
scheme |
String |
scheme of request URI |
userInfo |
String |
userInfo of request URI |
relPath |
String |
path of the current HTTP request, relative to the current context path. For example, if context path is "/myWebApp" and request path is "/myWebApp/hello", then relPath is "/hello". |
Redirect filter automatically imports groovyx.net.http.URIBuilder class into "filter.groovy", so that you can instantiate it with a simple call new URIBuilder(uri)
.
You can import arbitrary java classes into "filter.groovy" with usual "import" instruction. Don’t forget to add corresponding dependencies within your "build.gradle" script.