Skip to content
This repository was archived by the owner on Aug 24, 2023. It is now read-only.

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>

Filter API

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.

before( name, url, method, options, order, beforeFunction )

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
MatchedActionsOnly Boolean If true, the beforeFunction will ONLY be called when the request path actually matches an action or static path. Default is false.
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

after(name, uri, method, options, order, afterFunction)

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
    <tr><th>MatchedActionsOnly</th><td>Boolean</td><td>If true, the afterFunction will ONLY be called when the request path actually matches a resource action or static path. Default is false.</td></tr>
</table>
</td></tr>
<tr> <td>Integer order</td><td>An integer which specifies the relative order of filter execution.  Lowest numbers are run first. The default is 5. </td></tr>
<tr> <td>Closure afterFunction</td><td>The closure that is called whenever this filter is invoked. Returns null if no change to the data is needed else returns the modified data</td></tr>

The after-filter may choose to:

  • transform the data produced by actions and up-stream filters
  • pre-process the data sent in the request
  • be passive and do nothing more than record activity rather than effect output

Filter Function Signatures

Before

RequestContext beforeFn(RequestContext context, List actionVars)

After

byte[] afterFn ( RequestContext requestContext, byte[] data )

Filter Examples

Before Filter

    // 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; 
    }

After Filter

    // 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;
    } 

Creating Filters Outside a Server Configuration

ResourceActionFactory

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)
Clone this wiki locally