HowTo hook into the lift's request processing cycle

From Lift

Jump to: navigation, search

Lift provides request hook points to invoke your code early before normal request processing. You can use these hook points to log before/after debug messages, capture request start/end times, open/close extra resources, etc.

Usually, you will register these hooks in your bootstrap.liftweb.Boot.boot method.

The S.addAround() API let's you provide your callback function. For example...

 package bootstrap.liftweb
 import net.liftweb.util._
 import net.liftweb.http._
 class Boot {
  def boot { 
    ...
    S.addAround(User.requestLoans) // Z
    S.addAround(List(new LoanWrapper { // Y
      def apply[T](f: => T): T = {
        ...your before code goes here...
        val result = f // Let lift do normal request processing.
        ...your after code goes here...
        result
      }
    }))
    S.addAround(List(new LoanWrapper { // X
      def apply[T](f: => T): T = {
        println("hello to the request!")
        val result = f // Let lift do normal request processing.
        println("goodbye request!")
        result
      }
    }))
    S.addAround(List(around1, around2, around3))
 }

The callbacks will be called in this order: around1, around2, around3, X, Y, Z. That is, logically like...

around1(around2(around3(X(Y(Z(normal request processing)))))
Personal tools