Class SimpleSession
- java.lang.Object
-
- org.zkoss.zk.ui.http.SimpleSession
-
- All Implemented Interfaces:
Scope,Session,SessionCtrl
- Direct Known Subclasses:
SerializableSession
public class SimpleSession extends java.lang.Object implements Session, SessionCtrl
A non-serializable implementation ofSession.Note:
Though this class is serializable, it is meaningless to serialize it directly. Reason: it doesn't serialize any session attributes. Rather, it is serialized when Web server serialized HttpSession. Also, notice thatSimpleSessionis stored as an attribute HttpSession.- Author:
- tomyeh
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleSession()Used by the deriving class, only if the deriving class implements java.io.Serializable.SimpleSession(WebApp wapp, java.lang.Object navsess, java.lang.Object request)Constructs a ZK session with either a HTTP session or a Portlet session.SimpleSession(WebApp wapp, jakarta.servlet.http.HttpSession hsess, java.lang.Object request)Constructs a ZK session with a HTTP session.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddScopeListener(ScopeListener listener)Adds a listener to listen whether this scope is changed.java.lang.ObjectgetAttribute(java.lang.String name)Returns the value of the specified custom attribute.java.lang.ObjectgetAttribute(java.lang.String name, boolean recurse)Returns the custom attribute associated with this object.java.util.Map<java.lang.String,java.lang.Object>getAttributes()Returns a map of custom attributes associated with this session.DesktopCachegetDesktopCache()Returns the desktop cache, or null if not available.java.lang.StringgetDeviceType()Returns the device type that this session belongs to.intgetMaxInactiveInterval()Return the time, in seconds, between client requests before the servlet container will invalidate this session.java.lang.ObjectgetNativeSession()Returns the native session, or null if not available.WebAppgetWebApp()Returns the Web application that this session belongs to.booleanhasAttribute(java.lang.String name)Returns if a custom attribute is associated with this object (scope).booleanhasAttribute(java.lang.String name, boolean recurse)Returns if a custom attribute is associated with this object.voidinvalidate()Invalidates this session then unbinds any objects bound to it.voidinvalidateNow()Really invalidates the session.booleanisInvalidated()Returns whether this session is invalidated (i.e.,Session.invalidate()was called).booleannotifyClientRequest(boolean keepAlive)Notifies the session that a client request is received.voidonDestroyed()Called when the session is destroyed.protected voidreadThis(java.io.ObjectInputStream s)Used by the deriving class to read back this object, only if the deriving class implements java.io.Serializable.voidrecover(java.lang.Object nativeSession)Called when ZK detectsSession.getNativeSession()is not initialized properly.java.lang.ObjectremoveAttribute(java.lang.String name)Removes the specified custom attribute.java.lang.ObjectremoveAttribute(java.lang.String name, boolean recurse)Removes the custom attribute associated with this scope.booleanremoveScopeListener(ScopeListener listener)Removes a change listener from this scope.protected voidsessionDidActivate(jakarta.servlet.http.HttpSession hsess)Used by the deriving class to post-process a session after it is read back.protected voidsessionWillPassivate()Used by the deriving class to pre-process a session before writing the sessionjava.lang.ObjectsetAttribute(java.lang.String name, java.lang.Object value)Sets the value of the specified custom attribute.java.lang.ObjectsetAttribute(java.lang.String name, java.lang.Object value, boolean recurse)Sets the custom attribute associated with this scope, or the parent scope.voidsetDesktopCache(DesktopCache cache)Sets the desktop cache.voidsetDeviceType(java.lang.String deviceType)Sets the device type that this session belongs to.voidsetMaxInactiveInterval(int interval)Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.protected voidwriteThis(java.io.ObjectOutputStream s)Used by the deriving class to write this object, only if the deriving class implements java.io.Serializable.
-
-
-
Constructor Detail
-
SimpleSession
public SimpleSession(WebApp wapp, jakarta.servlet.http.HttpSession hsess, java.lang.Object request)
Constructs a ZK session with a HTTP session.- Parameters:
hsess- the original HTTP session.request- the original request causing this session to be created. If HTTP and servlet, it is jakarta.servlet.http.HttpServletRequest. If portlet, it is javax.portlet.RenderRequest.- Since:
- 3.0.1
-
SimpleSession
public SimpleSession(WebApp wapp, java.lang.Object navsess, java.lang.Object request)
Constructs a ZK session with either a HTTP session or a Portlet session.Note: it assumes the scope of attributes is PortletSession.APPLICATION_SCOPE.
- Parameters:
navsess- the original session, either an instance of HttpSession or PortletSession. Notice: we don't declare PortletSession in API to avoid this class failed to be loaded in some system (without portlet-api.jar)request- the original request causing this session to be created. If portlet, it is javax.portlet.RenderRequest.- Since:
- 3.6.2
-
SimpleSession
protected SimpleSession()
Used by the deriving class, only if the deriving class implements java.io.Serializable.
-
-
Method Detail
-
getDeviceType
public java.lang.String getDeviceType()
Description copied from interface:SessionReturns the device type that this session belongs to.A device type identifies the type of a client. For example, "ajax" represents the Web browsers with Ajax support, while "mil" represents clients that supports Mobile Interactive markup Language (on Limited Connected Device, such as mobile phones).
All desktops of the same session must belong to the same device type.
The session's device type is determined by the first desktop's device type.
- Specified by:
getDeviceTypein interfaceSession
-
setDeviceType
public void setDeviceType(java.lang.String deviceType)
Description copied from interface:SessionCtrlSets the device type that this session belongs to.It is called by the desktop when
Desktop.setDeviceType(java.lang.String)is called. Don't call this method directly. Otherwise, the result is unpredictable.- Specified by:
setDeviceTypein interfaceSessionCtrl
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Description copied from interface:SessionReturns the value of the specified custom attribute.- Specified by:
getAttributein interfaceScope- Specified by:
getAttributein interfaceSession
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name, boolean recurse)Description copied from interface:ScopeReturns the custom attribute associated with this object.- Specified by:
getAttributein interfaceScoperecurse- whether to search its ancestor scope. If true and the current scope doen't define the attribute, it searches up its ancestor to see any of them has defined the specified attribute.
-
hasAttribute
public boolean hasAttribute(java.lang.String name)
Description copied from interface:ScopeReturns if a custom attribute is associated with this object (scope).Notice that
nullis a valid value, so you can tell if an attribute is associated by examining the return value ofScope.getAttribute(java.lang.String).- Specified by:
hasAttributein interfaceScope
-
hasAttribute
public boolean hasAttribute(java.lang.String name, boolean recurse)Description copied from interface:ScopeReturns if a custom attribute is associated with this object.Notice that
nullis a valid value, so you can tell if an attribute is associated by examining the return value ofScope.getAttribute(java.lang.String).- Specified by:
hasAttributein interfaceScoperecurse- whether to search its ancestor scope. If true and the current scope doen't define the attribute, it searches up its ancestor to see any of them has defined the specified attribute.
-
setAttribute
public java.lang.Object setAttribute(java.lang.String name, java.lang.Object value)Description copied from interface:SessionSets the value of the specified custom attribute.- Specified by:
setAttributein interfaceScope- Specified by:
setAttributein interfaceSession- Returns:
- the previous value if any (since ZK 5)
-
setAttribute
public java.lang.Object setAttribute(java.lang.String name, java.lang.Object value, boolean recurse)Description copied from interface:ScopeSets the custom attribute associated with this scope, or the parent scope.- Specified by:
setAttributein interfaceScoperecurse- whether to look up the parent scope for the existence of the attribute.
If recurse is true and the attribute is defined in one of its ancestor (including page), the attribute is replaced. Otherwise, it is the same asScope.setAttribute(String,Object).
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name, boolean recurse)Description copied from interface:ScopeRemoves the custom attribute associated with this scope.- Specified by:
removeAttributein interfaceScoperecurse- whether to look up the parent scope for the existence of the attribute.
If recurse is true and the attribute is defined in one of its ancestor (including page), the attribute is removed. Otherwise, it is the same asScope.removeAttribute(String).
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name)
Description copied from interface:SessionRemoves the specified custom attribute.- Specified by:
removeAttributein interfaceScope- Specified by:
removeAttributein interfaceSession- Returns:
- the previous value if any (since ZK 5)
-
addScopeListener
public boolean addScopeListener(ScopeListener listener)
Description copied from interface:ScopeAdds a listener to listen whether this scope is changed. The listener is called when a custom attribute is added, removed, or the parent is changed.- Specified by:
addScopeListenerin interfaceScope- Returns:
- weather the listener is added successfully. Note: if the resolver was added before, it won't be added again and this method returns false.
-
removeScopeListener
public boolean removeScopeListener(ScopeListener listener)
Description copied from interface:ScopeRemoves a change listener from this scope.- Specified by:
removeScopeListenerin interfaceScope- Returns:
- false if listener is not added before.
-
getAttributes
public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
Description copied from interface:SessionReturns a map of custom attributes associated with this session.- Specified by:
getAttributesin interfaceScope- Specified by:
getAttributesin interfaceSession
-
invalidateNow
public void invalidateNow()
Description copied from interface:SessionCtrlReally invalidates the session.Application shall never access this method.
- Specified by:
invalidateNowin interfaceSessionCtrl
-
setMaxInactiveInterval
public void setMaxInactiveInterval(int interval)
Description copied from interface:SessionSpecifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.- Specified by:
setMaxInactiveIntervalin interfaceSession- See Also:
Configuration.setTimerKeepAlive(boolean),Configuration.setSessionMaxInactiveInterval(int)
-
getMaxInactiveInterval
public int getMaxInactiveInterval()
Description copied from interface:SessionReturn the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.- Specified by:
getMaxInactiveIntervalin interfaceSession- See Also:
Configuration.isTimerKeepAlive(),Configuration.getSessionMaxInactiveInterval()
-
getNativeSession
public java.lang.Object getNativeSession()
Description copied from interface:SessionReturns the native session, or null if not available. The returned object depends on the type of clients. If HTTP, the object is an instance of jakarta.servlet.http.HttpSession. If portlet, the object is an instance of javax.portlet.PortletSession.- Specified by:
getNativeSessionin interfaceSession
-
notifyClientRequest
public boolean notifyClientRequest(boolean keepAlive)
Description copied from interface:SessionCtrlNotifies the session that a client request is received.- Specified by:
notifyClientRequestin interfaceSessionCtrl- Parameters:
keepAlive- if the request will keep the session alive, or the request shall be ignored. If the request shall be ignored and the session is timeout, it will cause the session to expire (i.e., be invalidated).- Returns:
- whether the session is invalidated (since 6.0.0)
-
getWebApp
public final WebApp getWebApp()
Description copied from interface:SessionReturns the Web application that this session belongs to.
-
invalidate
public final void invalidate()
Description copied from interface:SessionInvalidates this session then unbinds any objects bound to it.Note: you usually have to ask the client to redirect to another page (or reload the same page) by use of
Executions.sendRedirect(java.lang.String).The session is not invalidated immediately. Rather, it is invalidated after processing the current request.
- Specified by:
invalidatein interfaceSession
-
isInvalidated
public final boolean isInvalidated()
Description copied from interface:SessionCtrlReturns whether this session is invalidated (i.e.,Session.invalidate()was called).Note: ZK doesn't invalidate it immediately (until the event has been processed), so the session might be still alive.
- Specified by:
isInvalidatedin interfaceSessionCtrl
-
getDesktopCache
public DesktopCache getDesktopCache()
Description copied from interface:SessionCtrlReturns the desktop cache, or null if not available.Note: this is an utility to implement
DesktopCacheProvider, which might or might not use this method. Other part of ZK shall not access this method.- Specified by:
getDesktopCachein interfaceSessionCtrl
-
setDesktopCache
public void setDesktopCache(DesktopCache cache)
Description copied from interface:SessionCtrlSets the desktop cache.Note: this is an utility to implement
DesktopCacheProvider, which might or might not use this method. Other part of ZK shall not access this method.- Specified by:
setDesktopCachein interfaceSessionCtrl- Parameters:
cache- the desktop cache; null to denote no cache available.
-
recover
public void recover(java.lang.Object nativeSession)
Description copied from interface:SessionCtrlCalled when ZK detectsSession.getNativeSession()is not initialized properly.It is actually a workaround to solve the issue that some Web container fails to call HttpSessionActivationListener.sessionDidActivate() upon failover.
It can be used to solve the issue that the mapping between the native session and the ZK session is changed. For example, you might manually invalidate the http session and recreate one. Then, you have to call this method to associate the ZK session with the new created session.
- Specified by:
recoverin interfaceSessionCtrl
-
onDestroyed
public void onDestroyed()
Description copied from interface:SessionCtrlCalled when the session is destroyed.Application shall never access this method.
- Specified by:
onDestroyedin interfaceSessionCtrl
-
writeThis
protected void writeThis(java.io.ObjectOutputStream s) throws java.io.IOExceptionUsed by the deriving class to write this object, only if the deriving class implements java.io.Serializable.- Throws:
java.io.IOException
-
readThis
protected void readThis(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundExceptionUsed by the deriving class to read back this object, only if the deriving class implements java.io.Serializable.- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
sessionWillPassivate
protected void sessionWillPassivate()
Used by the deriving class to pre-process a session before writing the session
-
sessionDidActivate
protected void sessionDidActivate(jakarta.servlet.http.HttpSession hsess)
Used by the deriving class to post-process a session after it is read back.Application shall not call this method directly.
-
-