如何获取 Default BodyParser?

How to get hold of the Default BodyParser?

我要问的是如何在 2.6 中获得 Default Scala Play BodyParser 实现。但是,如果您知道解决此用例的更简洁方法,我将非常高兴听到它。

一些序言...我在顶部重复使用了 Java Play 框架 Play-Authenticate (PA) to build a Scala Play application。是的,为此钉死我!我想要所有的社交身份验证功能,但我的应用程序是在 Scala 中使用的,例如光滑。是的是的,我可以尝试在 Scala 中重写 PA,但现在没时间了……虽然在考虑。

现在这已经不存在了,这是我的用例。我有 Scala 控制器,需要将 Java 上下文传递给 PA 框架才能使用它。我还需要检查用户是否可以通过cookie验证,即用户在第一次登录时是否勾选了"Remember Me"框。 "solution" 在迁移到 Play 2.6 之前可以正常工作,但现在由于 BodyParser.

而无法正常工作

这就是 "burger" 的样子。我想吃它所以...我需要通过检查传入的 cookie 来验证用户身份,但同时创建了 Java 上下文(与 PA 互操作)。但是因为我有可怕的 Java 上下文,我可以在一个请求的范围内使它在整个 Scala 应用程序中可用:

def index =
  TryCookieAuthAction { implicit jContext =>  // <==== this is the burger
    deadbolt.WithAuthRequest()() { implicit request =>
     Future {
        Ok(indexView(userService))
     }
  }
}

现在让我们看看牛。 2.6 之前它曾经工作,因为我没有被迫覆盖 parser 但现在在 2.6 中我被迫:

case class TryCookieAuthAction[A](block: Http.Context => Action[A])(implicit auth: PlayAuthenticate, config: Configuration, env: Environment, mat: Materializer, ec: ExecutionContext) extends Action[A] {
  def apply(request: Request[A]): Future[Result] = {
    val contextComponents = JavaHelpers.createContextComponents(config, env)
    val jContext = JavaHelpers.createJavaContext(request, contextComponents)

    TryCookieAuthAction.jContextDv += (request.id -> jContext)

    if(!auth.isLoggedIn(jContext)) {
      // calling Java here so need a Java Context
      auth.tryAuthenticateWithCookie(jContext)
    }

    val scalaResult: Future[Result] = Await.ready(block(jContext)(request), 60 seconds)

    val session : Seq[(String, String)] = jContext.session().keySet().toArray.map(key => (key.toString, jContext.session().get(key)))
    val cookies : Seq[Cookie] = jContext.response().cookies().asScala.toSeq.map(cookie =>
      Cookie(cookie.name(), cookie.value(), maxAge = Option(cookie.maxAge()), path = cookie.path(), domain = Option(cookie.domain()),
        secure = cookie.secure(), httpOnly = cookie.httpOnly())
    )

    TryCookieAuthAction.jContextDv -= request.id

    scalaResult.map(_.withSession(session : _*).withCookies(cookies : _*))
  }

  override def executionContext = ec

  override val parser: BodyParser[A] = ???
}

object TryCookieAuthAction {
  private lazy val jContextDv = TrieMap[Long, play.mvc.Http.Context]()

  /**
    * Extracts the Java context given a request
    * @param request The request
    * @tparam A The request body type
    */
  implicit class RequestToContext[A](request: Request[A]) {
    def jContextOption : Option[Http.Context] = jContextDv.get(request.id)
    def jContext : Http.Context = jContextDv(request.id)
  }

  def apply[A](action: Action[A])(implicit auth: PlayAuthenticate, config: Configuration, env: Environment, mat: Materializer, ec: ExecutionContext): TryCookieAuthAction[A] = TryCookieAuthAction(_ => action)
}

我的问题是如何获得默认值 BodyParser 或者是否有更聪明的方法来做到这一点?例如,我尝试创建一个 ActionBuilder,但后来我无法使 jContext 隐式可用于随附的 Action.

我找到了解决方案或可能解决方案?

首先通过更改 object WithJContextSupportActionapply 方法以包含 play.api.mvc.PlayBodyParsers 作为隐式参数:

def apply[A](action: Action[A])(implicit config: Configuration, env: Environment, bodyParsers: PlayBodyParsers, ec: ExecutionContext): WithJContextSupportAction[A] = WithJContextSupportAction(_ => action)

然后将新的隐式更改传播到操作 class:

case class WithJContextSupportAction[A](block: JContext => Action[A])(implicit config: Configuration, env: Environment,
                                                                      bodyParsers: PlayBodyParsers, ec: ExecutionContext) extends Action[A] {

最终像这样覆盖 parser

override def parser= bodyParsers.default.asInstanceOf[BodyParser[A]]