garbagetown/playdocja — pull request #607 by wm3 (2014-03-22 08:40:59+00:00)
Title: manual/scalaGuide/main/global/ScalaInterceptors.md

- https://github.com/garbagetown/playdocja/blob/2.2.0/documentation/2.2.0/manual/scalaGuide/main/global/ScalaInterceptors.md
```
--- /Users/garbagetown/Desktop/2.1.5/manual/scalaGuide/main/global/ScalaInterceptors.md	2013-09-19 22:53:02.000000000 +0900
+++ /Users/garbagetown/Desktop/2.2.0/manual/scalaGuide/main/global/ScalaInterceptors.md	2013-09-19 10:11:22.000000000 +0900
@@ -4,48 +4,17 @@

The filter component allows you to intercept requests coming to the application, transform request and responses. Filters provide a nice ability to implement cross-cutting concerns of your application. You can create a filter by extending the `Filter` trait and then add the filter to `Global` object. The following example creates an access log filter that logs the result of all the actions:

-'''scala
-import play.api.mvc._
+@[filter-log](code/ScalaInterceptors.scala)

-object Global extends WithFilters(AccessLog)

-object AccessLog extends Filter {
- override def apply(next: RequestHeader => Result)(request: RequestHeader): Result = {
- val result = next(request)
- play.Logger.info(request + "\n\t => " + result)
- result
- }
-}
-'''
The `Global` object extends the `WithFilters` class that allows you to pass one more filters to form a filter chain.

> **Note** `WithFilters` now extends the `GlobalSettings` trait

Here is another example where filter is very useful, check authorization before invoking certain actions:

-'''scala
-object Global extends WithFilters(AuthorizedFilter("editProfile", "buy", "sell")) with GlobalSettings {}
-
-object AuthorizedFilter {
- def apply(actionNames: String*) = new AuthorizedFilter(actionNames)
-}
-
-class AuthorizedFilter(actionNames: Seq[String]) extends Filter {
- override def apply(next: RequestHeader => Result)(request: RequestHeader): Result = {
- if(authorizationRequired(request)) {
- /* do the auth stuff here */
- println("auth required")
- next(request)
- }
- else next(request)
- }
-
- private def authorizationRequired(request: RequestHeader) = {
- val actionInvoked: String = request.tags.getOrElse(play.api.Routes.ROUTE_ACTION_METHOD, "")
- actionNames.contains(actionInvoked)
- }
-}
-'''
+@[filter-authorize](code/ScalaInterceptors.scala)
+

> **Tip** `RequestHeader.tags` provides lots of useful information about the route used to invoke the action.

@@ -57,20 +26,8 @@

Let’s see how this works in practice:

-'''scala
-import play.api._
-import play.api.mvc._
-
-// Note: this is in the default package.
-object Global extends GlobalSettings {
-
- override def onRouteRequest(request: RequestHeader): Option[Handler] = {
- println("executed before every request:" + request.toString)
- super.onRouteRequest(request)
- }
+@[onroute-request](code/ScalaInterceptors.scala)

-}
-'''

It’s also possible to intercept a specific Action method, using [[Action composition | ScalaActionsComposition]].

```