[2.0] [Scala] How to wrap all the incoming requests in a WrappedRequest

918 views
Skip to first unread message

Drew Kutcharian

unread,
Feb 6, 2012, 11:56:44 PM2/6/12
to play-framework
How can I wrap all the incoming requests in a WrappedRequest?

I want to have a custom Request object just like the one explained in https://github.com/playframework/Play20/wiki/ScalaActionsComposition section "Another way to create the Authenticated action"

I looked at GlobalSettings.onRouteRequest but that takes in a RequestHeader, not a Request. I'm going to need to access the Session and Cookies inside my custom Request object.

Thanks,

Drew

Pascal Voitot Dev

unread,
Feb 7, 2012, 2:39:13 AM2/7/12
to play-fr...@googlegroups.com
I don't know exactly what you try to do but WrappedRequest inherits Request which inherits RequestHeader.
So a WrappedRequest is a RequestHeader and sessions and cookies are defined in RequestHeader apparently.

regards
Pascal

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Drew Kutcharian

unread,
Feb 7, 2012, 3:43:16 AM2/7/12
to play-fr...@googlegroups.com, play-fr...@googlegroups.com
Pascal,

What I'm trying to do is to have something like RichRequest that adds some methods/attributes (i.e current user, current role, etc. which are extracted from the original request) on top of what's available in Request and pass that down the stack. That way those extra methods/attributes are available in all the actions as part of the request.  Does that make sense?

Thanks,

Drew

Drew Kutcharian

unread,
Feb 7, 2012, 3:48:57 AM2/7/12
to play-fr...@googlegroups.com, play-fr...@googlegroups.com
My original comment regarding GlobalSettings.onRouteRequest was that if that's the right place to do something like:

router.handleFor(new RichRequest(request))


On Feb 6, 2012, at 11:39 PM, Pascal Voitot Dev <pascal.v...@gmail.com> wrote:

Guillaume Bort

unread,
Feb 7, 2012, 4:31:33 AM2/7/12
to play-fr...@googlegroups.com
When onRouteRequest is called you don't have access to the request
yet, because it has not been parsed.

I'm not sure what you are trying to do but:

case class CustomRequest(token: String, request: Request[AnyContent])
extends WrappedRequest(request)
case class CustomAction(f: CustomRequest => Result) extends Action[AnyContent] {

lazy val parser = BodyParsers.parse.anyContent

def apply(req: Request[AnyContent]) = req match {
case r: CustomRequest => f(r)
case _ => sys.error("Invalid usage")
}

}

object Application extends Controller {

def index = CustomAction { request =>
Ok("Token: " + request.token)
}

}

With onRouteRequest:

override def onRouteRequest(req: RequestHeader) = {
super.onRouteRequest(req).map { _ match {
case a: CustomAction => Action { (request: Request[AnyContent]) =>
a(CustomRequest("XXX", request))
}
case o => o
}
}
}

--
Guillaume Bort

Drew Kutcharian

unread,
Feb 7, 2012, 4:42:24 AM2/7/12
to play-fr...@googlegroups.com, play-fr...@googlegroups.com
Thanks Guillaume, This is what I was looking for.

Drew Kutcharian

unread,
Feb 8, 2012, 3:21:24 AM2/8/12
to play-fr...@googlegroups.com, Guillaume Bort
BTW, this sets the BodyParser to AnyContent. I tried making CustomRequest generic but I got into trouble in onRouteRequest. Any suggestion?

case class CustomRequest[A](token: String, request: Request[A])
extends WrappedRequest(request)
case class CustomAction[A](f: CustomRequest[A] => Result)(implicit bp: BodyParser[A]) extends Action[A] {

lazy val parser = bp

def apply(req: Request[A]) = req match {
  case r: CustomRequest[_] => f(r.asInstanceOf[CustomRequest[A]])
  case _ => sys.error("Invalid usage")
}

}

object Application extends Controller {

def index = CustomAction[AnyContent] { request =>
  Ok("Token: " + request.token)
}

}

With onRouteRequest:

override def onRouteRequest(req: RequestHeader) = {
  super.onRouteRequest(req).map { _ match {
      case a: CustomAction[_] => Action { (request: Request[_]) =>

John Ridout

unread,
Apr 11, 2012, 8:50:37 AM4/11/12
to play-fr...@googlegroups.com, Guillaume Bort


On Wednesday, 8 February 2012 08:21:24 UTC, Drew Kutcharian wrote:
BTW, this sets the BodyParser to AnyContent. I tried making CustomRequest generic but I got into trouble in onRouteRequest. Any suggestion?

I'm also trying to implement a parameterised custom request. I'm sure a solution would be useful to a lot of people.

ftc

unread,
Oct 12, 2012, 7:23:05 PM10/12/12
to play-fr...@googlegroups.com, Guillaume Bort, jo...@weylin.co.uk
Reply all
Reply to author
Forward
0 new messages