1
votes

In an answer to "How to run filter on demand scala play framework", the following code is suggested:

// in your filter
val handlerDef: Option[HandlerDef] = request.attrs.get(Router.Attrs.HandlerDef)

I'm not sure what's happening here - is it safe to .get on this val (to get it out of the Option)? In what scenarios would this code result in a None (ie, when would Router.Attrs.HandlerDef not be present)?

I'm working with Scala and PlayFramework 2.6.

1

1 Answers

2
votes

According to Route modifier tags

Please be aware that the HandlerDef request attribute exists only when using a router generated by Play from a routes file. This attribute is not added when the routes are defined in code, for example using the Scala SIRD or Java RoutingDsl. In this case request.attrs.get(HandlerDef) will return None in Scala or null in Java. Keep this in mind when creating filters.

Hence if you are using routes file then Router.Attrs.HandlerDef should always be available. As a shorthand instead of

val handlerDef: HandlerDef = request.attrs.get(Router.Attrs.HandlerDef).get

your can use apply sugar like so

val handlerDef: HandlerDef = request.attrs(Router.Attrs.HandlerDef)