somedev
somedev

Reputation: 488

Converting a Scala Session Object to a Java Session Object in Play Framework

Is there a way to convert the session object in scala to the session object in java in Play?

I have a Model method written in java like :

public void DoSomething(Request request, Session session)
{
       String fancyValue = request.getQueryString("userInput");
       session.put("Some Fancy Stuff",fancyValue);
}

and a Controller method written in scala like:

def showHomePage = Action { implicit request =>

     val JRequest = play.core.j.JavaHelpers.createJavaRequest(request)
     val JSession // conversion needed from request.session to play.mvc.Http.Session

     new SomeModel().DoSomething(JRequest,JSession)

     // would this include the updates done to the session in the java model?
     Ok("Testing Stuff").withSession(session) 
}

Upvotes: 2

Views: 586

Answers (1)

Carsten
Carsten

Reputation: 18446

If you look at the docs of the JavaHelpers package, you'll find a function createJavaContext there. You can use that to obtain a play.mvc.Http.Context, from which you can extract a play.mvc.Http.Request and play.mvc.Http.Session.

val java_ctx = play.core.j.JavaHelpers.createJavaContext(request)
val java_request = java_ctx.request()
val java_session = java_ctx.session()

Upvotes: 2

Related Questions