Grails filter syntax or how to call a Grails filter outside of Grails

Grails provides filters that run in front of your controllers. They are defined in classes that look like this:

class SecurityFilters { def filters = { myFilter(controller:'*', action:'*') { // What are those weird colons?? print "I'm filtering!" // Code that does the filtering goes here } } } 

They work fine, but I would like to better understand the syntax, as it doesn't look like the Groovy code I saw before. In particular, the line above starting with myFilter seems very strange. Is this a method definition for a method called myFilter ? If so, what does it mean :'*' after each parameter? I thought this might be the default parameter value, but it would be ='*' . I have seen named parameters using colons in method calls before, but this could not be a method call because I have not defined myFilter() anywhere else.

I think I would understand much better if someone could just tell me how to execute filtering code from a regular Groovy class. In other words, if I have a file MyFilters.groovy that contains the lines above, how can I finish this Groovy code so that it prints "I'm filtering"?

 import MyFilters def mf = new MyFilters() mf.filters.somethingGoesHere // Help! How do I finish this line so it calls my filtering code? 
+7
filter grails groovy
source share
2 answers

The following Groovy code will print β€œI'm filtering!”:

 class SecurityFilters { def filters = { myFilter(controller:'*', action:'*') { // What are those weird colons?? print "I'm filtering!" // Code that does the filtering goes here } } } class FilterDelegate { def methodMissing(String methodName, args) { // methodName == myFilter // args[0] == [controller:*, action:*] // args[1] == {print "I'm filtering!"} args[1].call() } } def sf = new SecurityFilters() def filtersClosure = sf.filters filtersClosure.delegate = new FilterDelegate() filtersClosure.call() 

In this example, filters are a closure that calls a method called myFilter and passes the map and closure. You can think of myFilter as:

 myFilter([controller:'*', action:'*'], closure) 

A map may contain keys, such as a controller, action, or uri. The wildcard character (*) is used when Grails tries to match a URI with an HTTP request, when it tries to determine which closure to call.

My understanding of how Grails handles filters is that it uses the delegate loader class. The loader class provides a methodMissing method and creates a FilterConfig for each method call inside filter closures. When the HTTP request is executed, Grails scans all FilterConfig objects and tries to find the appropriate area (looks on the map for the controller, action or uri and uses regular expressions to match wildcards). If it finds a match, it causes a closure that was passed to the method in the Filter class.

+5
source share

You cannot easily call filters outside the grail, because they depend on a lot of plumbing that will not be configured. Line
myFilter (controller: '', action: '')
is a method definition that takes two arguments, a controller and an action template. * means apply this filter to any object of an existing type. For example, if we want users to not be able to create anything if they were not authorized, we will use the following filter.
controller: '*', action: 'create'
This would mean that at any time when the action to create was created, the code stored in this function body will be executed, but for the list, display or editing, no filter will be executed. If you really want to detail, you can always download the source Grails code.

:
The code compiles because it is a function defined in the filter.

0
source share

All Articles