I intensely need my user object so I thought I would store it in the Cache. Since I do not know when it is destroyed from the Cache, I though to put it as an interceptor
. So I defined the following code:
@Override
public Action<?> onRequest(Request request, Method actionMethod) {
// find the user based on the userId that is stored in the session
// scope.
String userId = session.get("user"); // does not work
User loggedInUser = (User) Cache.get(userId);
if (loggedInUser == null) {
loggedInUser = User.getUerById(userSyscode);
}
return super.onRequest(request, actionMethod);
}
I thought that I can use:
session.get("user");
But it seems to me, like the session
is not accessible from the Global class
, am I doing something wrong?
I had a similar problem and solved it with "action composition".
http://www.playframework.org/documentation/2.0.1/JavaActionsComposition
Alternatively, this is the code for onRequest
that you are overriding, so I would think you can do the same thing and just put your code in the call
method.
public Action onRequest(Request request, Method actionMethod) {
return new Action.Simple() {
public Result call(Context ctx) throws Throwable {
/* your code here */
return delegate.call(ctx);
}
};
}
I don't know if session
will be available directly, but at that point you can get to it from the ctx
variable. I think it'll be something like ctx.session().get("user")
.
In scala works this code:
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
if(!request.session.get("email").isEmpty){
//I have session with email
request.session.apply("email") //this is for getting session
}
}
request.session.get return Option[String]
request.session.apply return String
play framework 2 scala Play.api.mvc.Session