scalaplayframeworkactionbuilder

Play 2.6 ActionBuilder


I upgraded my Play application from 2.5 to 2.6 today and i got a problem with ActionBuilder. The docs state:

The Scala ActionBuilder trait has been modified to specify the type of the body as a type parameter, and add an abstract parser member as the default body parsers. You will need to modify your ActionBuilders and pass the body parser directly.

documentation

Sadly I haven't found any example and i don't know how to fix that:

class AuthenticatedRequest[A](val token: ProfileTokenData, request: Request[A]) extends WrappedRequest[A](request)

trait Secured {

  object SetExtractor {
    def unapplySeq[T](s: Set[T]): Option[Seq[T]] = Some(s.toSeq)
  }

  def Authenticated = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = {
      request.jwtSession.claimData.asOpt[JWTToken] match {
        case Some(token) => block(new AuthenticatedRequest(ProfileTokenData(null, token.sub, AuthRole.None), request)).map(_.refreshJwtSession(request))
        case _ => Future.successful(Unauthorized)
      }
    }
  }

  def Registered = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin, AuthRole.Customer, AuthRole.Registered))
  }

  def Customer = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin, AuthRole.Customer))
  }

  def Admin = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin))
  }

}

Does anyone know whic BodyParser i have to pass as second argument?


Solution

  • Had a similar problem. Play 2.6 injects a ControllerComponents, which has a default body parser. Maybe this helps:

    class CheckApiKey(apiKeyToCheck: String, cc: ControllerComponents)
      extends ActionBuilder[Request, AnyContent] with ActionFilter[Request] {
      ...
      override protected def executionContext: ExecutionContext = cc.executionContext
      override def parser: BodyParser[AnyContent] = cc.parsers.defaultBodyParser
    }