Last active
August 29, 2015 14:08
-
-
Save vdebergue/c2a6d63ed2d396fe8a6f to your computer and use it in GitHub Desktop.
Play Action composition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package controllers | |
| import scala.concurrent.Future | |
| import play.api._ | |
| import play.api.mvc._ | |
| object Application extends Controller { | |
| def index = Action { | |
| Ok(views.html.index("Your new application is ready.")) | |
| } | |
| def myAuthAction = Auth(Action.async(parse.json) { request => | |
| Future.successful(Ok(request.body)) | |
| }) | |
| def sync = Auth(Action { request => | |
| Ok("toto") | |
| }) | |
| } | |
| case class Auth[A](action: Action[A]) extends Action[A] { | |
| def apply(request: Request[A]): Future[SimpleResult] = { | |
| val token = request.headers.get("Auth") | |
| val body = request.body.toString | |
| println("body:" + body) | |
| // validate the token | |
| if (true) { | |
| action(request) | |
| } else { | |
| Future.successful(Results.Forbidden) | |
| } | |
| } | |
| lazy val parser = action.parser | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problems: if I call
syncwith body “test”, I see “body:AnyContentAsFormUrlEncoded(Map())” in the logs (because it tries to parse “test” as a form fields then return the form data).The body used for the check is not the real one (a big string), it is the parsed one :-(
Converting the JSON body into string is easy, but a form… (changing a single character would make the signature invalid)