Tuesday 21 October 2008

Accessing Session from a Grails Service

Grails makes the session object (instance of the Servlet API's HttpSession class) directly available in the Controllers and Tag Libraries.

Furthermore, the Grails implementation of HttpSession allows you to access the session as a Groovy map.

session.user = "Robert"

will store a user name in the session's attribute list.

I wanted to encapsulate session data in a service and avoid defining session properties many places.

However, the session is not directly available in a Grails Service. So if you want to access the session from a Service, you do it via the RequestContextHolder class (part of the Spring framework).

Here is simple Service that does exactly this:

import javax.servlet.http.HttpSession
import org.springframework.web.context.request.RequestContextHolder

class SessionStorageService {

static transactional = false
static scope = "singleton"

def setUser(User user) {
getSession().user = user
}

def getUser() {
getSession().user
}

private HttpSession getSession() {
return RequestContextHolder.currentRequestAttributes().getSession()
}
}

This service is made available in Controllers by automatic dependency injection by placing a property in the controller with the name sessionStorageService:

class BaseController {
def sessionStorageService
...
sessionStorageService.setUser("Robert")
...
}
Any comments or suggestions are appreciated .

11 comments:

Unknown said...

Appart from the undefined method RequestAttributes.getSession()?

Robert Bjærum said...

The Grails implementation of RequestAttributes inherits the getSession() method from Spring's ServletWebRequest. Note casting is not necessary since Grails is dynamically loading the methods.

Ho Minh Chuc said...

Sometime I need to use some available variable in Controller such as "request", "response", "session", etc. I don't know how to get these variables but I always define a method called initService, its params can be the request or session itself. Everytime I use my service, I call this method and everything is ok! Is it right?

Varnoobius said...
This comment has been removed by the author.
Varnoobius said...

Thank you, the RequestContextHolder
worked from the src/groovy subdirectory.

hv said...

Thanks, very helpful posting

Anonymous said...

Thank you very nice posting... web application

Robert Bjærum said...

You should avoid using "id" as name of property stored in the session object as it probably is reserved for internal used by Grails.

Unknown said...

Thanks, very helpful.

Lead Visionary said...

Why not use a GrailsWebRequest?

http://grails.org/doc/1.0.x/api/org/codehaus/groovy/grails/web/servlet/mvc/GrailsWebRequest.html

Anonymous said...

But the Session object we get from this doesn't contain 'attribute HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY'.

I actually want to get the loggedIn user details in src/groovy classes.

And SpringSecurityUtils.getSecurityContext(session) returns me null.

P.S. I have checked the HTTPSession Object, It contains all other attributes.