|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "Filters" |
| 4 | +category: doc |
| 5 | +date: 2015-06-18 17:31:52 |
| 6 | +order: 27 |
| 7 | +--- |
| 8 | + |
| 9 | +A filter can be any regular `Route` that doesn't commit the response. |
| 10 | + |
| 11 | +For example we can create an `AuditFilter` (a __before filter__) with this code: |
| 12 | + |
| 13 | +```java |
| 14 | +// audit filter |
| 15 | +ALL("/.*", (routeContext) -> { |
| 16 | + log.info("Request for {} '{}'", routeContext.getRequestMethod(), routeContext.getRequestUri()); |
| 17 | + routeContext.next(); |
| 18 | +}); |
| 19 | +``` |
| 20 | + |
| 21 | +In above snippet we added a before filter on all requests, this filter logs the request method and the request URI. |
| 22 | +At the end of the filter we __MUST__ call `routeContext.next()` to jump to the next route/filter from the chain. |
| 23 | + |
| 24 | +**Note:** If you want to add a filter for all routes you **MUST** use `/.*` and **NOT** `/*`: |
| 25 | + |
| 26 | +Another __before filter__ can be an `AuthenticationFilter`: |
| 27 | + |
| 28 | +```java |
| 29 | +// authentication filter |
| 30 | +GET("/contact.*", (routeContext) { |
| 31 | + if (routeContext.getSession("username") == null) { |
| 32 | + routeContext.redirect("/login"); |
| 33 | + } else { |
| 34 | + routeContext.next(); |
| 35 | + } |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +In above snippet we protected all contact pages. If the current session doesn't contain an attribute `username` |
| 40 | +then our filter redirects the request to the `login` page. |
| 41 | + |
| 42 | +Now we want to propose you a most complicated scenario where we are getting a Database instance to be shared |
| 43 | +throughout a request-response cycle: |
| 44 | + |
| 45 | +```java |
| 46 | +// before filter that create a database instance |
| 47 | +ALL("/.*", (routeContext) -> { |
| 48 | + routeContext.setLocal("database", getDatabase()); |
| 49 | + routeContext.next(); |
| 50 | +}); |
| 51 | + |
| 52 | +// insert data in database |
| 53 | +POST("/{id}", (routeContext) -> { |
| 54 | + Database database = routeContext.getLocal("database"); |
| 55 | + database.insert(something); // this line can throws an error |
| 56 | +}); |
| 57 | + |
| 58 | +// after filter that release the database instance |
| 59 | +ALL("/.*", (routeContext) -> { |
| 60 | + Database database = routeContextremoveLocal("database"); |
| 61 | + database.release(); |
| 62 | +}).runAsFinally(); |
| 63 | +``` |
| 64 | + |
| 65 | +Please notice that we must mark the last filter with __runAsFinally__ that are guaranteed to execute |
| 66 | +at the end of the `Request-Response` cycle despite exceptions within the chain. |
| 67 | + |
0 commit comments