Browse files

merge Fix route params inside async result

  • Loading branch information...
2 parents eba5223 + 8f61521 commit 758c1bc3ba23fef47d642d47453fe9e46baff076 @casualjim casualjim committed Feb 25, 2013
View
13 core/src/main/scala/org/scalatra/FutureSupport.scala
@@ -43,17 +43,8 @@ trait FutureSupport extends AsyncSupport {
override protected def renderResponse(actionResult: Any) {
actionResult match {
- case r: AsyncResult
- val req = request
- setMultiparams(matchedRoute(req), multiParams(req))(req)
- val prelude = new AsyncResult {
- val is: Future[_] = Future { setMultiparams(matchedRoute, multiParams) }
- }
- handleFuture(prelude.is flatMap (_ => r.is) , r.timeout)
- case f: Future[_]
- val req = request
- setMultiparams(matchedRoute(req), multiParams(req))(req)
- handleFuture(Future { setMultiparams(matchedRoute(req), multiParams(req))(req) } flatMap (_ => f), asyncTimeout)
+ case r: AsyncResult handleFuture(r.is , r.timeout)
+ case f: Future[_] handleFuture(f, asyncTimeout)
case a super.renderResponse(a)
}
}
View
19 core/src/main/scala/org/scalatra/ScalatraBase.scala
@@ -213,17 +213,18 @@ trait ScalatraBase extends ScalatraContext with CoreDsl with DynamicScope with I
* are returned as a stream.
*/
protected def runRoutes(routes: Traversable[Route]) = {
- val result = for {
+ for {
route <- routes.toStream // toStream makes it lazy so we stop after match
matchedRoute <- route.apply(requestPath)
- actionResult <- invoke(matchedRoute)
- } yield (actionResult, matchedRoute)
- result map {
- case (actionResult, matchedRoute) =>
- request("org.scalatra.MatchedRoute") = matchedRoute
- setMultiparams(Some(matchedRoute), multiParams)
- actionResult
- }
+ saved = saveMatchedRoute(matchedRoute)
+ actionResult <- invoke(saved)
+ } yield actionResult
+ }
+
+ private[scalatra] def saveMatchedRoute(matchedRoute: MatchedRoute) = {
+ request("org.scalatra.MatchedRoute") = matchedRoute
+ setMultiparams(Some(matchedRoute), multiParams)
+ matchedRoute
}
private[scalatra] def matchedRoute(implicit request: HttpServletRequest) =
View
39 scalate/src/main/scala/org/scalatra/scalate/ScalatraRenderContext.scala
@@ -83,8 +83,7 @@ class ScalatraRenderContext(
* @throws IllegalStateException if the route's base path cannot be
* determined. This may occur outside of an HTTP request's lifecycle.
*/
- def url(route: Route, params: Pair[String, String]*): String =
- url(route, params.toMap, Seq.empty)
+ def url(route: Route, params: (String, String)*): String = UrlGenerator.url(route, params:_*)(request)
/**
* Calculate a URL for a reversible route and some splats.
@@ -97,8 +96,7 @@ class ScalatraRenderContext(
* @throws IllegalStateException if the route's base path cannot be
* determined. This may occur outside of an HTTP request's lifecycle.
*/
- def url(route: Route, splat: String, moreSplats: String*): String =
- url(route, Map[String, String](), splat +: moreSplats)
+ def url(route: Route, splat: String, moreSplats: String*): String = UrlGenerator.url(route, splat, moreSplats:_*)(request)
/**
* Calculate a URL for a reversible route, some params, and some splats.
@@ -114,36 +112,5 @@ class ScalatraRenderContext(
def url(
route: Route,
params: Map[String, String],
- splats: Iterable[String]
- ): String = {
-
- route.reversibleMatcher match {
- case Some(matcher: ReversibleRouteMatcher) =>
- withRouteMultiParams(MatchedRoute(route.action, multiParams)) {
- println("multiparams " + multiParams)
- route.contextPath(request) + matcher.reverse(params, splats.toList)
- }
- case _ =>
- throw new Exception("Route \"%s\" is not reversible" format (route))
- }
- }
-
-
- private[this] def withRouteMultiParams[S](matchedRoute: MatchedRoute)(thunk: => S): S = {
- val originalParams = multiParams
- setMultiparams(matchedRoute, originalParams)
- try {
- thunk
- } finally {
- request(MultiParamsKey) = originalParams
- }
- }
-
- def setMultiparams[S](matchedRoute: MatchedRoute, originalParams: MultiParams) {
- val routeParams = matchedRoute.multiParams map {
- case (key, values) =>
- key -> values.map(UriDecoder.secondStep(_))
- }
- request(MultiParamsKey) = originalParams ++ routeParams
- }
+ splats: Iterable[String]): String = UrlGenerator.url(route, params, splats)(request)
}

0 comments on commit 758c1bc

Please sign in to comment.