S.addAround vs. LiftRules.earlyInStateful

38 views
Skip to first unread message

Andreas Joseph Krogh

unread,
Jun 17, 2017, 7:09:38 AM6/17/17
to Lift
Hi Lifters!
 
I experience that RequestVars are not yet populated when executing LiftRules.earlyInStateful and S.addAround;
 
 
S.addAround(new LoanWrapper {
      def apply[T](f: => T) : T =  {
         val isSet = UrlLocalizer.currentLocale.set_?
         println(s"UrlLocalizer.currentLocale.set_? == $isSet")
prints "== false"
 
LiftRules.earlyInStateful.append(req => {
   val isSet = UrlLocalizer.currentLocale.set_?
   println(s"UrlLocalizer.currentLocale.set_? == $isSet")
 
prints "== false".
 
UrlLocalizer is as follow:
 
 
object UrlLocalizer extends Loggable {

   /**
    * What are the available locales?
    */
   val locales: Map[String, Locale] =
      Map(Locale.getAvailableLocales.map(l => l.toString -> l) :_*)

   object currentLocale extends RequestVar[Box[Locale]](Empty)

   /**
    * Extract the locale
    */
   def unapply(in: String): Option[Locale] = {
      val locale = locales.get(in)
      locale.foreach{l =>
         currentLocale.set(Full(l))
      }
      locale
   }

   /**
     * Calculate the Locale
     */
   def calcLocale(in: Box[HTTPRequest]): Locale = {
      currentLocale.get match {
         case Full(locale) =>
            // trace("calcLocale: using currentLocale.get: " + locale)
            locale
         case _ =>
            val locale = in.flatMap(r => r.locale) match {
               case Full(reqLocale) =>
                  // trace("calcLocale: using Req.locale: " + reqLocale)
                  reqLocale
               case _ =>
                  val defaultLocale = Locale.getDefault
                  // trace("calcLocale: using Locale.getDefault: " + defaultLocale)
                  defaultLocale
            }
            currentLocale.set(Full(locale))
            locale
      }
   }
}
 
And I use it like this in a Loc's rewrite:
override val rewrite: LocRewrite = Full(NamedPF("Admin stuff") {
   case RewriteRequest(ParsePath(UrlLocalizer(locale) :: "admin" :: Nil,_,_,_),_,_)
   => (RewriteResponse(templatePath, Map.empty, stopRewriting = true), Empty)
})
 
So UrlLocalizer sets currentLocale in it's unapply but still the currentLocale requestVar is not populated (set_? returns false) in neither S.addAround nor LiftRules.earlyInStateful.
 
Is this by design or am I doing something wrong?
 
Thanks.
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963

Bhashit Parikh

unread,
Jun 17, 2017, 11:31:43 AM6/17/17
to Lift
Try using the lifecycle callbacks available in LiftSession. For ex.

LiftSession.onBeginServicing = ((session: LiftSession, req: Req) => {
  // Do something here
}) :: LiftSession.onBeginServicing

Andreas Joseph Krogh

unread,
Jun 17, 2017, 5:26:10 PM6/17/17
to lif...@googlegroups.com
På lørdag 17. juni 2017 kl. 17:31:43, skrev Bhashit Parikh <bhashit...@gmail.com>:
Try using the lifecycle callbacks available in LiftSession. For ex.
 
LiftSession.onBeginServicing = ((session: LiftSession, req: Req) => {
  // Do something here
}) :: LiftSession.onBeginServicing
 
Thanks!
 
At first I thought you ment LiftRules.onBeginServicing and just had the signature wrong, but then realized there exists an onBeginServicing in LiftSession also, and it serves my purpose.
 
Now, am I the only one who thinks we have too many "on-start-of-request"-methods?
Who can remember the difference between:
1. LiftRules.onBeginServicing
2. LiftRules.early
3. LiftRules.earlyInStateful
4. S.addAround
5. LiftSession.onBeginServicing
 
It isn't exactly obvious when to use which and when S and requestVars have been populated/restored state.
Message has been deleted

Bhashit Parikh

unread,
Jun 18, 2017, 3:37:36 AM6/18/17
to Lift
It is a bit confusing.  Looks like I'm not the only one :).  I recently had a chance to explore these in a bit more detail. I think I'll try and write a small blog post about it.

David Pollak

unread,
Jun 18, 2017, 6:46:26 PM6/18/17
to lif...@googlegroups.com
Y'all are hitting "Lift Archaeology". There were a lot of different approaches I took to request handling over time. Some are good and some are not-so-good. Sadly, there's all there... Maybe in Lift 4, we do a cleanup. :-)
--
--
Lift, the simply functional web framework: http://liftweb.net

---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Lift, the simply functional web framework http://liftweb.net

Andreas Joseph Krogh

unread,
Jun 18, 2017, 10:51:11 PM6/18/17
to lif...@googlegroups.com
På mandag 19. juni 2017 kl. 00:46:21, skrev David Pollak <feeder.of...@gmail.com>:
Y'all are hitting "Lift Archaeology". There were a lot of different approaches I took to request handling over time. Some are good and some are not-so-good. Sadly, there's all there... Maybe in Lift 4, we do a cleanup. :-)
 
Yep:-)

Antonio Salazar Cardozo

unread,
Jun 19, 2017, 1:56:28 PM6/19/17
to Lift
https://github.com/lift/framework/issues/1867 just added it so we've got an eye on it :)
Thanks,
Antonio
Reply all
Reply to author
Forward
0 new messages