Lift framework lacks the ability to fetch a session creation timestamp by default. This post will provide a short example of how one can do this while working with Lift.
Note: This example uses
org.joda.time.DateTimeclass for working with date.
Lift provides an ability to customize a session creation flow by injecting a function into the
LiftRules.sessionCreator configuration value during the application bootstrapping. It is a function which accepts
HTTPSession and a
String with an application context path wrapped in a
LiftSession. And that's where a session
creation timestamp value can be added to the session:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
The following code can be used to extract a creation timestamp from the Lift session:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Here we are extracting the
LiftSession. Then retrieving an underlying instance of the
HTTPSession to be able
to extract a value of the session scope attribute by its name. The latter is done by calling
attribute(key: String) method of the
The value of an attribute will be returned as an instance of scala's
Any. Then it can be casted to the actual type
asInstanceOf[T] method. The
T type parameter should be set with the desired result type. In case the
specified type mismatches the actual type of a value, a
java.lang.ClassCastException will be thrown.
Hope you find this tip helpful.