Skip to content
This repository
Browse code

Closes #1177. Adds 'allAround' to LiftRules for putting loans around …

…the entire HTTP request
  • Loading branch information...
commit 67d29753bcc1e05283627eb070bddef7a9d498bd 1 parent dff0b6a
David Pollak authored January 09, 2012
29  web/webkit/src/main/scala/net/liftweb/http/LiftRules.scala
@@ -956,9 +956,32 @@ class LiftRules() extends Factory with FormVendor with LazyLoggable {
956 956
 
957 957
   /**
958 958
    * Holds user's DispatchPF functions that will be executed in a stateless context. This means that
959  
-   * S object is not availble yet.
  959
+   * no session will be created and no JSESSIONID cookie will be presented to the user (unless
  960
+   * the user has presented a JSESSIONID cookie).
960 961
    */
961  
-  val statelessDispatchTable = RulesSeq[DispatchPF]
  962
+  @scala.deprecated("Use statelessDispatch")
  963
+  def statelessDispatchTable = statelessDispatch
  964
+
  965
+  /**
  966
+   * Holds user's DispatchPF functions that will be executed in a stateless context. This means that
  967
+   * no session will be created and no JSESSIONID cookie will be presented to the user (unless
  968
+   * the user has presented a JSESSIONID cookie).
  969
+   *
  970
+   * This is the way to do stateless REST in Lift
  971
+   */
  972
+  val statelessDispatch = RulesSeq[DispatchPF]
  973
+
  974
+  /**
  975
+   * Add functionality around all of the HTTP request/response cycle.
  976
+   * This is an optimal place to get a database connection.  Note that whatever
  977
+   * is loaned at the begining of the request will not be returned until the end
  978
+   * of the request.  It's super-important to (1) not do anything related
  979
+   * to state or touch the request objects or anything else at the begining or
  980
+   * end of the loan wrapper phase; (2) make sure that your code does not throw
  981
+   * exceptions as exceptions can cause major problems.
  982
+   */
  983
+  val allAround = RulesSeq[LoanWrapper]
  984
+
962 985
 
963 986
   private[http] def dispatchTable(req: HTTPRequest): List[DispatchPF] = {
964 987
     req match {
@@ -1772,7 +1795,7 @@ class RulesSeq[T] {
1772 1795
   })(f)
1773 1796
   }
1774 1797
 
1775  
-  def toList = cur.value match {
  1798
+  def toList: List[T] = cur.value match {
1776 1799
     case null => rules
1777 1800
     case xs => xs
1778 1801
   }
2  web/webkit/src/main/scala/net/liftweb/http/LiftServlet.scala
@@ -257,7 +257,7 @@ class LiftServlet extends Loggable {
257 257
       // if the request is matched is defined in the stateless table, dispatch
258 258
       if (S.statelessInit(req) {
259 259
         tmpStatelessHolder = NamedPF.applyBox(req,
260  
-          LiftRules.statelessDispatchTable.toList).map(_.apply() match {
  260
+          LiftRules.statelessDispatch.toList).map(_.apply() match {
261 261
           case Full(a) => Full(LiftRules.convertResponse((a, Nil, S.responseCookies, req)))
262 262
           case r => r
263 263
         });
2  web/webkit/src/main/scala/net/liftweb/http/provider/HTTPProvider.scala
@@ -99,7 +99,7 @@ trait HTTPProvider {
99 99
 
100 100
   private def preBoot() {
101 101
     // do this stateless
102  
-    LiftRules.statelessDispatchTable.prepend(NamedPF("Classpath service") {
  102
+    LiftRules.statelessDispatch.prepend(NamedPF("Classpath service") {
103 103
       case r@Req(mainPath :: subPath, suffx, _) if (mainPath == LiftRules.resourceServerPath) =>
104 104
         ResourceServer.findResourceInClasspath(r, r.path.wholePath.drop(1))
105 105
     })
19  web/webkit/src/main/scala/net/liftweb/http/provider/servlet/ServletFilterProvider.scala
@@ -50,6 +50,20 @@ trait ServletFilterProvider extends Filter with HTTPProvider {
50 50
   def context: HTTPContext = ctx
51 51
 
52 52
   /**
  53
+   * Wrap the loans around the incoming request
  54
+   */
  55
+  private def handleLoanWrappers[T](f: => T): T = {
  56
+    val wrappers = LiftRules.allAround.toList
  57
+
  58
+    def handleLoan(lst: List[LoanWrapper]): T = lst match {
  59
+      case Nil => f
  60
+      case x :: xs => x(handleLoan(xs))
  61
+    }
  62
+
  63
+    handleLoan(wrappers)
  64
+  }
  65
+
  66
+  /**
53 67
    * Executes the Lift filter component.
54 68
    */
55 69
   def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain) = {
@@ -59,14 +73,15 @@ trait ServletFilterProvider extends Filter with HTTPProvider {
59 73
       try {
60 74
         TransientRequestVarHandler(Empty,
61 75
                                    RequestVarHandler(Empty,
  76
+
62 77
                                                      (req, res) match {
63 78
               case (httpReq: HttpServletRequest, httpRes: HttpServletResponse) =>
64 79
                 val httpRequest = new HTTPRequestServlet(httpReq, this)
65 80
                 val httpResponse = new HTTPResponseServlet(httpRes)
66 81
 
67  
-                service(httpRequest, httpResponse) {
  82
+                handleLoanWrappers(service(httpRequest, httpResponse) {
68 83
                   chain.doFilter(req, res)
69  
-                }
  84
+                })
70 85
               case _ => chain.doFilter(req, res)
71 86
             }))
72 87
       } finally {LiftRules.reqCnt.decrementAndGet()}

0 notes on commit 67d2975

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