scalaplayframeworkdeadbolt-2

Scala Play 2.5 Action composition with Deadbolt-2 actions


I'm working on a Scala Play application and need many Controller actions disabling caching of the browser by setting parameters in the HTTP headers of the Response. I decided to create a NoCache composite action and since I am also using Deadbolt-2 (and need a Deadbolt-2's AuthenticatedRequest[_]) it looks like this:

package action

import be.objectify.deadbolt.scala.AuthenticatedRequest
import play.api.http.HeaderNames
import play.api.mvc._

import scala.concurrent.Future
import scala.util.Success

case class NoCache[A](action: Action[A]) extends Action[A] with HeaderNames {
  def apply(request: AuthenticatedRequest[A]): Future[Result] = {
    action(request).andThen {
      case Success(result) => result.withHeaders(
        (CACHE_CONTROL -> "no-cache, no-store, must-revalidate"),
        (PRAGMA -> "no-cache"),
        (EXPIRES -> "0")
      )
    }
  }

  lazy val parser = action.parser
}

but then it won't compile trying to mix in this Action into my Controller action implementations e.g.

def link = deadbolt.SubjectPresent()() andThen NoCache() { implicit request =>

or

def link = NoCache(deadbolt.SubjectPresent()()) { implicit request =>

but can't see how to compose them ...


Solution

  • I found how to do it for a single action:

    def index = NoCache {
      deadbolt.WithAuthRequest()() { implicit request =>
        Future {
          Ok(views.html.index(userService))
        }
      }
    } 
    

    However, I still haven't found how to apply NoCache to the entire controller class.