如何获得默认的BodyParser?

时间:2019-05-08 18:58:58

标签: scala playframework playframework-2.6

我要问的是如何掌握2.6中的默认 Scala Play BodyParser实现。但是,如果您知道解决此用例的更简洁的方法,我将很高兴听到它。

一些前言...我重用了Java Play框架Play-Authenticate(PA)在顶部构建Scala Play application。是的,把我钉在十字架上!我想要所有社交身份验证功能,但将我的应用程序放在Scala中并使用例如光滑是的,我可以尝试在Scala中重写PA,但现在没有时间...尽管考虑一下。

现在这已经不合时宜了,这是我的用例。我有Scala控制器,需要将Java Context传递到PA框架才能使用它。我还需要检查用户是否可以通过Cookie进行身份验证,即用户在首次登录时是否选中了“记住我”框。在迁移到Play 2.6之前,该“解决方案”曾经可以使用,但是由于BodyParser而无法使用。

这就是“汉堡”的样子。我想吃掉它……我需要通过检查传入的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

1 个答案:

答案 0 :(得分:-1)

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

首先,通过更改object WithJContextSupportAction的{​​{1}}方法以将apply包含为隐式参数:

play.api.mvc.PlayBodyParsers

然后将新的隐式更改传播到动作类:

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

最终像这样覆盖case class WithJContextSupportAction[A](block: JContext => Action[A])(implicit config: Configuration, env: Environment, bodyParsers: PlayBodyParsers, ec: ExecutionContext) extends Action[A] {

parser