Thursday, October 10, 2013

Forward Requests From Grails Spring Security Filter

I was trying to get a request to be send to a specific controller action in case of session expiry. I was trying to use redirect and later forward (to avoid additional requests from the client) method from grails. But I keep getting this error even though the params to the methods are all correct.
Message: No signature of method: com.rwi.springsecurity.filters.CustomSessionExpiryFilter.forward() is applicable
for argument types: (java.util.LinkedHashMap) values: [[controller:exampleApi, action:init, params:[...]]]
Then it struck, maybe by default we cannot use those grails methods in the spring security classes as they are under src/groovy and also the doc mentions that the method is to forward from one action to another. So in this case, to forward a request we can use methods from the servlet classes.

Forward a request to a controller action, where api is the controller and init is the action which includes query string as well.
request.getRequestDispatcher("/api/init").forward(request, response)
For a redirect request we can use response.sendRedirect() method with uri containing the necessary params like query strings appended manually. An example code snippet follows.
// Construct the redirect uri
String responseUri = {
String uri = request.forwardURI
String qs = request.getQueryString()
return (qs?.size() > 0) ? "${uri}?${qs}" : uri