How to Global onRouteRequest directly to onBadRequest?

Posted by virtualeyes on Stack Overflow See other posts from Stack Overflow or by virtualeyes
Published on 2012-11-17T15:59:12Z Indexed on 2012/11/17 17:00 UTC
Read the original article Hit count: 606

EDIT
Came up with this to sanitize URI date params prior to passing off to Play router

val ymdMatcher = "\\d{8}".r // matcher for yyyyMMdd URI param
val ymdFormat = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd")
def ymd2Date(ymd: String) = ymdFormat.parseDateTime(ymd)

override def onRouteRequest(r: RequestHeader): Option[Handler] = {
  import play.api.i18n.Messages
  ymdMatcher.findFirstIn(r.uri) map{ ymd=>
    try { ymd2Date( ymd); super.onRouteRequest(r) }
    catch { case e:Exception => // kick to "bad" action handler on invalid date 
      Some(controllers.Application.bad(Messages("bad.date.format"))) 
    }
  } getOrElse(super.onRouteRequest(r))
}

ORIGINAL
Let's say I want to return a BadRequest result type for all /foo URIs:

override def onBadRequest(r: RequestHeader, error: String) = {
  BadRequest("Bad Request: " + error)
}

override def onRouteRequest(r: RequestHeader): Option[Handler] = {
  if(r.uri.startsWith("/foo") onBadRequest("go away")
  else super.onRouteRequest(r)
}

Of course does not work, since the expected return type is Option[play.api.mvc.Handler]

What's the idiomatic way to deal with this, create a default Application controller method to handle filtered bad requests? Ideally, since I know in onRouteRequest that /foo is in fact a BadRequest, I'd like to call onBadRequest directly.

Should note that this is a contrived example, am actually verifying a URI yyyyMMdd date param, and BadRequest-ing if it does not parse to a JodaTime instance -- basically a catch-all filter to sanitize a given date param rather than handling on every single controller method call, not to mention, avoiding cluttering up application log with useless stack traces re: invalid date parse conversions (have several MBs of these junk trace entries accruing daily due to users pointlessly manipulating the uri date in attempts to get at paid subscriber content)

© Stack Overflow or respective owner

Related posts about scala

Related posts about playframework