Skip to content

Commit

Permalink
Closes #1177. Adds 'allAround' to LiftRules for putting loans around …
Browse files Browse the repository at this point in the history
…the entire HTTP request
  • Loading branch information
dpp committed Jan 9, 2012
1 parent dff0b6a commit 67d2975
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
29 changes: 26 additions & 3 deletions web/webkit/src/main/scala/net/liftweb/http/LiftRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -956,9 +956,32 @@ class LiftRules() extends Factory with FormVendor with LazyLoggable {

/**
* Holds user's DispatchPF functions that will be executed in a stateless context. This means that
* S object is not availble yet.
* no session will be created and no JSESSIONID cookie will be presented to the user (unless
* the user has presented a JSESSIONID cookie).
*/
val statelessDispatchTable = RulesSeq[DispatchPF]
@scala.deprecated("Use statelessDispatch")
def statelessDispatchTable = statelessDispatch

/**
* Holds user's DispatchPF functions that will be executed in a stateless context. This means that
* no session will be created and no JSESSIONID cookie will be presented to the user (unless
* the user has presented a JSESSIONID cookie).
*
* This is the way to do stateless REST in Lift
*/
val statelessDispatch = RulesSeq[DispatchPF]

/**
* Add functionality around all of the HTTP request/response cycle.
* This is an optimal place to get a database connection. Note that whatever
* is loaned at the begining of the request will not be returned until the end
* of the request. It's super-important to (1) not do anything related
* to state or touch the request objects or anything else at the begining or
* end of the loan wrapper phase; (2) make sure that your code does not throw
* exceptions as exceptions can cause major problems.
*/
val allAround = RulesSeq[LoanWrapper]


private[http] def dispatchTable(req: HTTPRequest): List[DispatchPF] = {
req match {
Expand Down Expand Up @@ -1772,7 +1795,7 @@ class RulesSeq[T] {
})(f)
}

def toList = cur.value match {
def toList: List[T] = cur.value match {
case null => rules
case xs => xs
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class LiftServlet extends Loggable {
// if the request is matched is defined in the stateless table, dispatch
if (S.statelessInit(req) {
tmpStatelessHolder = NamedPF.applyBox(req,
LiftRules.statelessDispatchTable.toList).map(_.apply() match {
LiftRules.statelessDispatch.toList).map(_.apply() match {
case Full(a) => Full(LiftRules.convertResponse((a, Nil, S.responseCookies, req)))
case r => r
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ trait HTTPProvider {

private def preBoot() {
// do this stateless
LiftRules.statelessDispatchTable.prepend(NamedPF("Classpath service") {
LiftRules.statelessDispatch.prepend(NamedPF("Classpath service") {
case r@Req(mainPath :: subPath, suffx, _) if (mainPath == LiftRules.resourceServerPath) =>
ResourceServer.findResourceInClasspath(r, r.path.wholePath.drop(1))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ trait ServletFilterProvider extends Filter with HTTPProvider {

def context: HTTPContext = ctx

/**
* Wrap the loans around the incoming request
*/
private def handleLoanWrappers[T](f: => T): T = {
val wrappers = LiftRules.allAround.toList

def handleLoan(lst: List[LoanWrapper]): T = lst match {
case Nil => f
case x :: xs => x(handleLoan(xs))
}

handleLoan(wrappers)
}

/**
* Executes the Lift filter component.
*/
Expand All @@ -59,14 +73,15 @@ trait ServletFilterProvider extends Filter with HTTPProvider {
try {
TransientRequestVarHandler(Empty,
RequestVarHandler(Empty,

(req, res) match {
case (httpReq: HttpServletRequest, httpRes: HttpServletResponse) =>
val httpRequest = new HTTPRequestServlet(httpReq, this)
val httpResponse = new HTTPResponseServlet(httpRes)

service(httpRequest, httpResponse) {
handleLoanWrappers(service(httpRequest, httpResponse) {
chain.doFilter(req, res)
}
})
case _ => chain.doFilter(req, res)
}))
} finally {LiftRules.reqCnt.decrementAndGet()}
Expand Down

0 comments on commit 67d2975

Please sign in to comment.