Reputation: 3
It seems that there is no around interceptor for the filter in grails.We have the same in case of Spring.If we want to implement it as per grails format what will be the best way to do it.
Upvotes: 0
Views: 191
Reputation: 75671
You could mimic that somewhat. In the before
closure you can check the request, session, etc. and determine whether to proceed with the regular call. If you want you can redirect, or render a response in the filter, and return false
to indicate that the controller shouldn't be called.
If you want to allow the controller to proceed, you can then do work in the after
closure. You can store information in request scope in the before
closure (e.g. request.currentRequestNumber = counter.incrementAndGet()
) and retrieve it in the after
closure (e.g. int currentRequestNumber = request.currentRequestNumber
) taking advantage of using property access to store and retrieve request attributes.
You cannot however "call" the controller and inspect its result, and choose whether to send that response, or modify it, or send a different response. You could probably do that with a response wrapper though, where you have a custom writer that captures the rendered response.
Note that you have more flexibility (but less Grails/Groovy help) with a regular servlet filter, i.e. a class that implements javax.servlet.Filter
and is registered in web.xml.
Upvotes: 1