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;
}
Aggregations