Skip to content
Browse files

Rewrite is now a special route.

  • Loading branch information...
1 parent 9886ae9 commit cd3d27ae484690d92c8c49c1ffeb962b31a603ba @inca committed Jul 4, 2011
Showing with 28 additions and 7 deletions.
  1. +1 −1 CHANGELOG.md
  2. +0 −6 circumflex-web/src/main/scala/package.scala
  3. +27 −0 circumflex-web/src/main/scala/router.scala
View
2 CHANGELOG.md
@@ -40,7 +40,7 @@
* The `any` route now does not perform method matching at all instead of just accepting
standard HTTP methods.
-* Added `rewrite` method as a quick replacement for heavy `forward`.
+* Added `rewrite` route as a quick replacement for heavy `forward`.
## 2.0.1
View
6 circumflex-web/src/main/scala/package.scala
@@ -154,9 +154,6 @@ package object web {
if you want to forward the request to another Circumflex route, you must make
sure that `CircumflexFilter` is mapped with `<dispatcher>FORWARD</dispatcher>`
in `web.xml`;
- * `rewrite` changes URI for this request; this affects the `request.uri`
- method which is used in standard routes; use `request.originalUri` to access
- original URI after it has been rewritten;
* `pass_!` sends request and response down the filter chain and then immediately
flushes response.
@@ -206,9 +203,6 @@ package object web {
request.forward(url)
response.flush_!
}
- def rewrite(newUri: String): Unit = {
- ctx.update("cx.web.uri", newUri)
- }
def pass_!(): Nothing = {
filterChain.doFilter(request.raw, response.raw)
response.flush_!
View
27 circumflex-web/src/main/scala/router.scala
@@ -94,6 +94,25 @@ class RequestRouter(var prefix: String = "") {
case None => new MatchResult("uri", "splat" -> request.uri)
}
+ /*!## URI rewriting
+
+ Special `rewrite` route allows you to change URI for currently processed request.
+ If route has matched successfully, the URI will be set to the value returned by
+ the attached block. After that the matching will be continued.
+
+ This affects `request.uri` method which is used in standard routes.
+ Use `request.originalUri` to access original URI after it has been rewritten.
+
+ Here's the example of using URI rewriting:
+
+ rewrite("/a") = "/a/info"
+
+ get("/a") = { ... } // will never match
+ get("/a/info") = { ... } // will match for both `/a` and `/a/info` requests
+ */
+
+ val rewrite = new RewriteRoute
+
/*!## Subroutes
Subroutes represent an easy and powerful concept which allows nesting
@@ -158,6 +177,14 @@ class FilterRoute extends RoutingContext[Unit] {
protected def dispatch(block: => Unit) = block
}
+class RewriteRoute extends RoutingContext[String] {
+ def matches = true
+ protected def dispatch(block: => String): Unit = {
+ val newUri = block
+ ctx.update("cx.web.uri", newUri)
+ }
+}
+
object NopRoute extends RoutingContext[Any] {
protected def dispatch(block: => Any): Unit = {}
def matches = false

0 comments on commit cd3d27a

Please sign in to comment.
Something went wrong with that request. Please try again.