如何使用EssentialAction [英] How To Use EssentialAction

查看:64
本文介绍了如何使用EssentialAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义主体解析器,可将请求主体流式传输到Amazon S3实例,并且我想在上传文件之前进行验证.

I have a custom body parser that streams the request body to an Amazon S3 instance and I want to do validation prior to uploading the file.

在请求的标头中,我可以访问内容大小和用户的身份验证令牌.通过这两件事,我可以验证用户是否具有上传文件的权限.

In the headers of the request I have access to the content size and the user's auth token. With these two things, I can validate that the user has permission to upload the file.

在阅读Play!的文档后,似乎 EssentialAction Action Composition 是解决之道.

After reading Play!'s documentation, it seems that EssentialAction or Action Composition is the way to go.

这是我的控制器方法,没有任何修饰:

Here is my controller method without any decoration:

def upload = Action(streamingBodyParser(streamConstructor)) { request =>
   ...
}

我尝试创建自定义基本操作,并在执行正文解析操作之前使用它,但是我不太正确地使用语法:

I've tried creating a custom essential action and using that before my body parsing action but I couldn't quite get the syntax right:

class HasEnoughSpace(action: EssentialAction) extends EssentialAction {
  override def apply(request: RequestHeader): EssentialAction = {
    val maybeToken = request.headers.get("X-Auth-Token")
    maybeToken match {
      case Some(t) => {

      }
      case _ => 
    }
  }
}

def upload = HasEnoughSpace { Action(streamingBodyParser(streamConstructor)) { request => 
  ...
}

我还尝试使用新的ActionBuilder方法,但是在允许我进行验证之前,该方法仍会解析正文.

I also tried using the new ActionBuilder method but that still parsed the body prior to allowing me to do validation.

是否有一种方法可以使用EssentialAction完成此操作?有没有更好的方法可以实现我在解析正文之前验证标头的目标?

Is there a way to accomplish this with EssentialAction? Is there a better way to accomplish my goal of validating the headers prior to parsing the body?

推荐答案

是的,这正是EssentialAction的用途.它使您可以检查请求标头,但忽略正文,对您的情况而言应该足够了.
本质上讲,EssentialActions只是接受RequestHeader并返回Iteratee [Array [Byte],Result]的函数:

Yes, that's exactly what EssentialAction is meant to be used for. It enables you to inspect the request header, but ignoring the body, what should be sufficient for your case.
EssentialActions are bascially just functions taking a RequestHeader and returning an Iteratee[Array[Byte], Result]:

trait EssentialAction extends (RequestHeader => Iteratee[Array[Byte], Result]) with Handler

因此,如果将HasEnoughSpace操作重写为创建EssentialAction而不是尝试扩展EssentialAction特性的函数,将会更容易:

Therefore it would be easier if you rewrite your HasEnoughSpace action to be a function that creates an EssentialAction instead of trying to extend the EssentialAction trait:

 def HasEnoughSpace(action: EssentialAction): EssentialAction =  EssentialAction { request =>
      val token = request.headers.get("X-Auth-Token")
      val contentSize = request.headers.get(play.api.http.HeaderNames.CONTENT_LENGTH)
      (token, contentSize) match {
        // perform validation if both headers are present
        case (Some(_), Some(len)) if validateContentLength(len) => {
          action(request)
        }
        case _ =>
          // replace with your client error
          Done(Unauthorized("401 No Token\n"))
      }
    }

在上面的代码中,您仍然需要编写validateContentLength函数.

In the above code you'd still need to write the validateContentLength function.

这篇关于如何使用EssentialAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆