public static final class H.Session extends H.KV<H.Session>
Defines a data structure to encapsulate a stateless session which accept only String
type value, and will be persisted at client side as a cookie. This means the entire size of the information stored in session including names and values shall not exceed 4096 bytes.
To store typed value or big value, use the cache methods of the session class. However it is subject to the implementation to decide whether cache methods are provided and how it is implemented
Modifier and Type | Field and Description |
---|---|
static String |
KEY_AUTHENTICITY_TOKEN
Stores the authenticity token in the session
|
static String |
KEY_EXPIRATION
Stores the expiration date in the session
|
static String |
KEY_EXPIRE_INDICATOR
Used to mark if a session has just expired
|
static String |
KEY_FINGER_PRINT
Stores the fingerprint to the session
|
static String |
KEY_ID
Session identifier
|
Constructor and Description |
---|
H.Session() |
Modifier and Type | Method and Description |
---|---|
H.Session |
cache(String key,
Object obj)
Store an object into cache using key specified.
|
H.Session |
cache(String key,
Object obj,
int expiration)
Store an object into cache with expiration specified
|
<T> T |
cached(String key)
Retrieve an object from cache by key.
|
<T> T |
cached(String key,
Class<T> clz)
Retrieve an object from cache by key.
|
H.Session |
cacheFor10Min(String key,
Object obj)
Store an object into cache for 10 minutes
|
H.Session |
cacheFor1Hr(String key,
Object obj)
Store an object into cache for 1 hour
|
H.Session |
cacheFor1Min(String key,
Object obj)
Store an object into cache for 1 minutes
|
H.Session |
cacheFor30Min(String key,
Object obj)
Store an object into cache for 30 minutes
|
static H.Session |
current()
Return a session instance of the current execution context, For example from a
ThreadLocal |
static void |
current(H.Session session)
Set a session instance into the current execution context, for example into a
ThreadLocal |
H.Session |
decr(String key)
Decrement the value associated with
key by one. |
H.Session |
decr(String key,
int n)
Decrement the value associated with
key by n . |
boolean |
empty()
Returns
true if the session is empty. |
boolean |
equals(Object obj) |
H.Session |
evict(String key)
Evict an object from cache
|
boolean |
expired()
Check if the session is expired.
|
H.Session |
expireOn(long expiry)
Set session expiry in milliseconds
|
long |
expiry()
Returns the expiration time in milliseconds of this session.
|
String |
id()
Returns the session identifier
|
H.Session |
incr(String key)
Increment the value associated with
key by one. |
H.Session |
incr(String key,
int n)
Increment the value associated with
key by n . |
boolean |
isEmpty()
Returns
true if internal data map is empty |
static H.Session |
resolve(H.Cookie sessionCookie,
int ttl)
Resolve a Session instance from a session cookie
|
H.Cookie |
serialize(String sessionKey)
Serialize this session into a cookie.
|
public static final String KEY_ID
Session identifier
public static final String KEY_EXPIRATION
Stores the expiration date in the session
public static final String KEY_AUTHENTICITY_TOKEN
Stores the authenticity token in the session
public static final String KEY_EXPIRE_INDICATOR
Used to mark if a session has just expired
public static final String KEY_FINGER_PRINT
Stores the fingerprint to the session
public String id()
Returns the session identifier
public H.Session decr(String key)
Decrement the value associated with key
by one.
If there is no value associated with key
then put number -1
for key
in the session.
If the existing value associated with key
is not an integer then raise IllegalStateExeption
.
key
- the key to get the existing value and associate the new valuepublic H.Session decr(String key, int n)
Decrement the value associated with key
by n
.
If there is no value associated with key
then put number -n
for key
in the session.
If the existing value associated with key
is not an integer then raise IllegalStateExeption
.
key
- the key to get the existing value and associate the new valuepublic H.Session incr(String key)
Increment the value associated with key
by one.
If there is no value associated with key
then put number 1
for key
in the session.
If the existing value associated with key
is not an integer then raise IllegalStateExeption
.
key
- the key to get the existing value and associate the new valuepublic H.Session incr(String key, int n)
Increment the value associated with key
by n
.
If there is no value associated with key
then put number n
for key
in the session.
If the existing value associated with key
is not an integer then raise IllegalStateExeption
.
key
- the key to get the existing value and associate the new valuepublic boolean empty()
Returns true
if the session is empty. e.g. does not contain anything else than the timestamp
public boolean isEmpty()
H.KV
Returns true
if internal data map is empty
public boolean expired()
Check if the session is expired. A session is considered to be expired if it has a timestamp and the timestamp is non negative number and is less than System.currentTimeMillis()
true
if the session is expiredpublic long expiry()
Returns the expiration time in milliseconds of this session. If there is no expiration set up, then this method return -1
-1
if the session has no expirypublic H.Session expireOn(long expiry)
Set session expiry in milliseconds
expiry
- the difference, measured in milliseconds, between the expiry and midnight, January 1, 1970 UTC.public H.Session cache(String key, Object obj)
Store an object into cache using key specified. The key will be appended with session id, so that it distinct between caching using the same key but in different user sessions.
The object is cached for default
ttl
key
- the key to cache the objectobj
- the object to be cachedpublic H.Session cache(String key, Object obj, int expiration)
Store an object into cache with expiration specified
key
- the key to cache the objectobj
- the object to be cachedexpiration
- specify the cache expiration in secondscache(String, Object)
public H.Session cacheFor1Hr(String key, Object obj)
Store an object into cache for 1 hour
key
- the key to cache the objectobj
- the object to be cachedpublic H.Session cacheFor30Min(String key, Object obj)
Store an object into cache for 30 minutes
key
- the key to cache the objectobj
- the object to be cachedpublic H.Session cacheFor10Min(String key, Object obj)
Store an object into cache for 10 minutes
key
- the key to cache the objectobj
- the object to be cachedpublic H.Session cacheFor1Min(String key, Object obj)
Store an object into cache for 1 minutes
key
- the key to cache the objectobj
- the object to be cachedpublic H.Session evict(String key)
Evict an object from cache
key
- the key to cache the objectpublic <T> T cached(String key)
Retrieve an object from cache by key. The key will be attached with session id
T
- the object typekey
- the key to get the cached objectnull
if it cannot find the object by key specifiedcache(String, Object)
public <T> T cached(String key, Class<T> clz)
Retrieve an object from cache by key. The key will be attached with session id
T
- the object typekey
- the key to get the cached objectclz
- the class to specify the return typenull
if it cannot find the object by key specifiedcache(String, Object)
public static H.Session current()
Return a session instance of the current execution context, For example from a ThreadLocal
public static void current(H.Session session)
Set a session instance into the current execution context, for example into a ThreadLocal
session
- the session to be set to current execution contextpublic static H.Session resolve(H.Cookie sessionCookie, int ttl)
Resolve a Session instance from a session cookie
sessionCookie
- the cookie corresponding to a sessionttl
- session time to live in secondsserialize(String)
public H.Cookie serialize(String sessionKey)
Serialize this session into a cookie. Note the cookie returned has only name, value maxAge been set. It’s up to the caller to set the secure, httpOnly and path attributes
sessionKey
- the cookie name for the session cookienull
if this session is empty or this session hasn’t been changed and there is no expiryresolve(org.osgl.http.H.Cookie, int)
Copyright © 2017–2020 OSGL (Open Source General Library). All rights reserved.