This repository was archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
gServ Filter API
Lee Collins edited this page May 22, 2015
·
16 revisions
<style ng-click="">
pre {
font-family: "Consolas", "Menlo", "Courier", monospace;border: groove;border-color: navy;
padding: 1em;min-width: 20%;max-width: 90%;
}
body{
padding: 2em;
}
</style>
In the GServ Config you can define filters. GServ defines 2 kinds of filters: After and Before
As you can probably guess, before-filters are called before the Action code is called.
Similarly, after-filters are called after the Action code is called and has a chance to modify the output produced by the Action.
Argument | Description | |||
---|---|---|---|---|
String name | Optional name of the filter. | |||
String url | The path for which this filter is defined. | |||
List<String> method | "GET" | "PUT" | "POST" | "DELETE" | |||
Map options |
|
|||
Integer order | An integer which specifies the relative order of filter execution. Low numbers are run first. The default is 5. | |||
Closure beforeFunction | The closure that is called whenever this filter is invoked.
The arguments passed to the closure vary depending on the filter's options.
This closure must return: a modified or wrapped RequestContext implementation, or null. |
The before-filter may choose to:
- call a different Action
- return a response or error
- pre-process the data sent in the request
- be passive and do nothing more than record activity rather than effect input
Argument | Description |
---|---|
String name | Optional name of the filter. |
String url | The path for which this filter is defined. |
List method | "GET" | "PUT" | "POST" | "DELETE" |
Map options |
The after-filter may choose to:
RequestContext beforeFn(RequestContext context, List actionVars) byte[] afterFn ( RequestContext requestContext, byte[] data ) // Add a filter to intercept // and reject letters to 'Lee' before "rejectLee", "/letter/:name", "GET", [:], { name -> if ( name == "Lee") error 403, "Lee not allowed" /// closure must return the requestContext requestContext; } // Add a filter to capture the response // and change its text to uppercase after "upperCaseFilter", "/letter/:name", "GET", [:], { requestContext, data -> def newData = new String(data).toUpperCase().bytes; return newData; }To create a filter, the ResourceActionFactory is used. To create After filter: ResourceActionFactory.createAfterFilter(name, url, options, order, closure)And to create Before filter: ResourceActionFactory.createBeforeFilter(name, url, options, order, closure) |
gServ © 2014-2017 Lee Collins. All rights reserved.