public class Session extends StorableObject
Modifier and Type | Field and Description |
---|---|
static long |
ACTIVE_MILLIS
The default active session time (10 minutes).
|
static java.lang.ThreadLocal |
activeSession
The currently active session (for the current thread).
|
static java.lang.String |
KEY_ACCESS_TIME
The dictionary key for the last access timestamp.
|
static java.lang.String |
KEY_CLIENT
The dictionary key for the user agent string of the web browser.
|
static java.lang.String |
KEY_CREATE_TIME
The dictionary key for the creation timestamp.
|
static java.lang.String |
KEY_DESTROY_TIME
The dictionary key for the destruction timestamp.
|
static java.lang.String |
KEY_FILES
The dictionary key for the temporary session files.
|
static java.lang.String |
KEY_IP
The dictionary key for the source IP address.
|
static java.lang.String |
KEY_USER
The dictionary key for the user id.
|
static long |
MAX_AGE_MILLIS
The default maximum session age (30 days).
|
static Path |
PATH
The session object storage path.
|
dict, KEY_CLASSNAME, KEY_ID, KEY_TYPE
Constructor and Description |
---|
Session(java.lang.String id,
java.lang.String type,
Dict dict)
Creates a new session from a serialized representation.
|
Session(java.lang.String user,
java.lang.String ip,
java.lang.String client)
Creates a new session for the specified user.
|
Modifier and Type | Method and Description |
---|---|
java.util.Date |
accessTime()
Returns the session last access timestamp.
|
void |
addFile(java.lang.String id,
java.io.File file)
Adds a file to the session.
|
java.lang.String |
client()
Returns the session user agent string of the web browser.
|
java.util.Date |
createTime()
Returns the session creation timestamp.
|
protected void |
destroy()
Destroys this session.
|
java.util.Date |
destroyTime()
Returns the scheduled session destruction timestamp.
|
java.io.File |
file(java.lang.String id)
Returns a session file with the specified unique id.
|
Dict |
files()
Returns a dictionary of all session files.
|
static Session |
find(Storage storage,
java.lang.String id)
Searches for a specific session in the storage.
|
void |
invalidate()
Invalidates this session by marking it as expired.
|
java.lang.String |
ip()
Returns the session source IP address.
|
protected boolean |
isActive()
Checks if this object is in active use.
|
protected boolean |
isModified()
Checks if this object has been modified since initialized from
storage.
|
boolean |
isNew()
Checks if this session is new (hasn't been stored).
|
boolean |
isValid()
Checks if this session is valid (hasn't expired).
|
protected void |
passivate()
Discards the modified flag for this object.
|
static void |
remove(Storage storage,
java.lang.String id)
Removes the specified session id from the provided storage.
|
void |
removeAllFiles()
Removes and deletes all session files.
|
static void |
removeExpired(Storage storage)
Removes all expired sessions from the provided storage.
|
void |
removeFile(java.lang.String id)
Removes and deletes a session file.
|
Dict |
serialize()
Returns a serialized representation of this object.
|
void |
setClient(java.lang.String client)
Sets the session user agent string of the web browser.
|
void |
setIp(java.lang.String ip)
Sets the session source IP address.
|
void |
setUserId(java.lang.String userId)
Sets the session user identifier if it was previously blank.
|
static void |
store(Storage storage,
Session session)
Stores the specified session in the provided storage.
|
void |
updateAccessTime()
Updates the session last access timestamp to the current system time.
|
java.lang.String |
userId()
Returns the session user identifier.
|
void |
validate()
Validates this session.
|
public static final Path PATH
public static final java.lang.String KEY_USER
public static final java.lang.String KEY_CREATE_TIME
public static final java.lang.String KEY_DESTROY_TIME
public static final java.lang.String KEY_ACCESS_TIME
public static final java.lang.String KEY_IP
public static final java.lang.String KEY_CLIENT
public static final java.lang.String KEY_FILES
public static final long MAX_AGE_MILLIS
public static final long ACTIVE_MILLIS
public static java.lang.ThreadLocal activeSession
public Session(java.lang.String id, java.lang.String type, Dict dict)
id
- the object identifiertype
- the object type namedict
- the serialized representationpublic Session(java.lang.String user, java.lang.String ip, java.lang.String client)
user
- the user idip
- the source IP addressclient
- the browser user agent stringpublic static Session find(Storage storage, java.lang.String id)
storage
- the storage to search inid
- the session identifierpublic static void store(Storage storage, Session session) throws StorageException
storage
- the storage to usesession
- the session to storeStorageException
- if the session couldn't be storedpublic static void remove(Storage storage, java.lang.String id)
storage
- the storage to useid
- the session id to removepublic static void removeExpired(Storage storage)
storage
- the storage to useprotected boolean isActive()
isActive
in class StorableObject
protected boolean isModified()
isModified
in class StorableObject
protected void destroy()
destroy
in class StorableObject
protected void passivate()
passivate
in class StorableObject
public Dict serialize()
serialize
in class StorableObject
public boolean isNew()
public boolean isValid()
public java.lang.String userId()
public void setUserId(java.lang.String userId)
userId
- the new session user identifierjava.lang.SecurityException
- if the session couldn't be bound to
the specified user identifierpublic java.util.Date createTime()
public java.util.Date destroyTime()
public java.util.Date accessTime()
public void updateAccessTime()
public java.lang.String ip()
public void setIp(java.lang.String ip)
ip
- the new session source IP address.public java.lang.String client()
public void setClient(java.lang.String client)
client
- the session user agent string.public Dict files()
public java.io.File file(java.lang.String id)
id
- the file idpublic void addFile(java.lang.String id, java.io.File file)
id
- the file idfile
- the file to addpublic void removeFile(java.lang.String id)
id
- the file idpublic void removeAllFiles()
public void validate() throws java.lang.SecurityException
java.lang.SecurityException
- if the session didn't match the
the provided valuespublic void invalidate()