Search in sources :

Example 1 with ISessionListener

use of org.polymap.core.runtime.session.ISessionListener in project polymap4-core by Polymap4.

the class WebDavServer method createNewSession.

/**
 * Initializes a new session for the given user.
 * <p/>
 * This method is called by {@link SecurityManagerAdapter}
 *
 * @param user
 * @return The specified user.
 */
public static Principal createNewSession(final Principal user) {
    HttpServletRequest req = io.milton.servlet.ServletRequest.getRequest();
    final HttpSession session = req.getSession();
    // HTTP session timeout: 30min
    session.setMaxInactiveInterval(30 * 60);
    FsPlugin.getDefault().sessionContextProvider.mapContext(user.getName(), true);
    final SessionContext sessionContext = SessionContext.current();
    // ContentManager
    Locale locale = req.getLocale();
    sessionContext.setAttribute("contentManager", ContentManager.forUser(user.getName(), locale, sessionContext));
    // invalidate HTTP session when context is destroyed
    sessionContext.addSessionListener(new ISessionListener() {

        public void beforeDestroy() {
            log.info("SessionContext is destroyed -> invalidating HTTP session");
            try {
                // sessionContext.removeSessionListener( this );
                session.invalidate();
            } catch (Exception e) {
                log.warn("HTTP session already invalidated: " + e);
            }
        }
    });
    // session destroy listener
    session.setAttribute("sessionListener", new HttpSessionBindingListener() {

        public void valueBound(HttpSessionBindingEvent ev) {
        }

        public void valueUnbound(HttpSessionBindingEvent ev) {
            // 
            sessionContext.execute(new Runnable() {

                public void run() {
                    ContentManager.releaseSession(user.getName());
                }
            });
            // prevent life-lock
            if (!sessionContext.isDestroyed() && sessionContext.getAttribute("destroying") == null) {
                sessionContext.setAttribute("destroying", true);
                FsPlugin.getDefault().sessionContextProvider.destroyContext(sessionContext.getSessionKey());
                log.info("HTTP Session destroyed: " + session.getId() + ", user: " + user);
            }
        }
    });
    log.info("New HTTP session: " + session.getId() + ", user: " + user);
    return user;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSession(javax.servlet.http.HttpSession) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) ISessionListener(org.polymap.core.runtime.session.ISessionListener) SessionContext(org.polymap.core.runtime.session.SessionContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Locale (java.util.Locale)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 HttpSessionBindingEvent (javax.servlet.http.HttpSessionBindingEvent)1 HttpSessionBindingListener (javax.servlet.http.HttpSessionBindingListener)1 ISessionListener (org.polymap.core.runtime.session.ISessionListener)1 SessionContext (org.polymap.core.runtime.session.SessionContext)1