Play for Scala: In an ActionBuilder, is it possible to modify the session of a wrapped action's result?

StackOverflow https://stackoverflow.com/questions/21917304

Question

My problem is that SimpleResult doesn't seem to allow read access to its session, only write access (withSession).

object MyAction extends ActionBuilder[MyRequest] {

    def invokeBlock[A](
        request: Request[A],
        block: (MyRequest[A]) => Future[SimpleResult]
    ): Future[SimpleResult] = {
        // do stuff, create x, y
        val resultFuture = block(MyRequest(x, y, request)
        // Now I want to modify resultFuture's session,
        // keeping any changes block might have done to request.session.
        // And I'd rather not parse result's headers by hand to do that.
    }
}

What am I missing?

Was it helpful?

Solution

This seems to be impossible in 2.2.x, but in 2.3.x play.api.mvc.Result has 2 new methods addingToSession and removingFromSession which allow to add and remove session values from a Result.

So the above problem could be solved by:

...
resultFuture map (_.addingToSession(myKey -> myValue)(request))
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top