Reputation: 45104
Is there a way to chain several filters in a grails application (as in Java filters)? Maybe something with spring?
I've written a couple of filters, and would like to get them to execute serially (order is not particularly important). The reason behind this? I need to write about 20, 30 filters, and don't want them all in the same file.
I've read about Spring's DelegatingFilterProxy but can't figure out on how to configure it to chain all my grails filters.
Upvotes: 0
Views: 3171
Reputation: 2587
Since Grails 1.3.1 you can chain filters by using the "dependsOn" keyword:
def dependsOn = [MyOtherFilters.class]
http://jira.codehaus.org/browse/GRAILS-6229
Upvotes: 4
Reputation: 5687
I may not be understanding the core issue here, but the simple answer might be "they're already chained". Filters are executed based on the selector you put in the filter closure (e.g. myPreProcessorFilter(controller:'', action:'') {}). All selectors that match your controller/action will execute. I do this all the time with logging and performance measurement filters.
Here's an example. Both the logAction and measureMethodTime filters will be applied to all controllers and actions (since I left the selector wide open).
import org.springframework.web.context.request.RequestContextHolder as RCH
import com.x.y.*
class PerformanceFilters {
def filters = {
logAction(controller:'*', action:'*'){
before = {
log.debug("${controllerName}.${actionName}: entering; params=${params}")
}
}
measureMethodTime(controller:'*', action:'*'){
before = {
def session = RCH.currentRequestAttributes().getSession(false)
if (session)
{
Q.startTimer("${session.id}-${controllerName}-${actionName}", "method.${controllerName}.${actionName}")
}
}
afterView = {
def session = RCH.currentRequestAttributes().getSession(false)
if (session)
{
Q.stopTimer("${session.id}-${controllerName}-${actionName}", "method.${controllerName}.${actionName}")
}
}
}
}
}
Upvotes: 2
Reputation: 1275
http://grails.org/doc/latest/guide/single.html#6.6.4%20Filter%20Dependencies
Upvotes: 2