Search in sources :

Example 6 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project felix by apache.

the class HttpSessionWrapper method removeAttribute.

@Override
public void removeAttribute(final String name) {
    this.checkInvalid();
    final Object oldValue = this.getAttribute(name);
    if (oldValue != null) {
        this.delegate.removeAttribute(this.getKey(name));
        if (oldValue instanceof HttpSessionBindingListener) {
            ((HttpSessionBindingListener) oldValue).valueUnbound(new HttpSessionBindingEvent(this, name));
        }
        if (this.context.getHttpSessionAttributeListener() != null) {
            this.context.getHttpSessionAttributeListener().attributeRemoved(new HttpSessionBindingEvent(this, name, oldValue));
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener)

Example 7 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project shiro by apache.

the class ShiroHttpSession method afterUnbound.

protected void afterUnbound(String s, Object o) {
    if (o instanceof HttpSessionBindingListener) {
        HttpSessionBindingListener listener = (HttpSessionBindingListener) o;
        HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, s, o);
        listener.valueUnbound(event);
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener)

Example 8 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project shiro by apache.

the class ShiroHttpSession method afterBound.

protected void afterBound(String s, Object o) {
    if (o instanceof HttpSessionBindingListener) {
        HttpSessionBindingListener listener = (HttpSessionBindingListener) o;
        HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, s, o);
        listener.valueBound(event);
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener)

Example 9 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener 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)

Example 10 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project tomcat70 by apache.

the class StandardSessionContext method setAttribute.

/**
 * Bind an object to this session, using the specified name.  If an object
 * of the same name is already bound to this session, the object is
 * replaced.
 * <p>
 * After this method executes, and if the object implements
 * <code>HttpSessionBindingListener</code>, the container calls
 * <code>valueBound()</code> on the object.
 *
 * @param name Name to which the object is bound, cannot be null
 * @param value Object to be bound, cannot be null
 * @param notify whether to notify session listeners
 * @exception IllegalArgumentException if an attempt is made to add a
 *  non-serializable object in an environment marked distributable.
 * @exception IllegalStateException if this method is called on an
 *  invalidated session
 */
public void setAttribute(String name, Object value, boolean notify) {
    // Name cannot be null
    if (name == null)
        throw new IllegalArgumentException(sm.getString("standardSession.setAttribute.namenull"));
    // Null value is the same as removeAttribute()
    if (value == null) {
        removeAttribute(name);
        return;
    }
    // Validate our current state
    if (!isValidInternal()) {
        throw new IllegalStateException(sm.getString("standardSession.setAttribute.ise", getIdInternal()));
    }
    if ((manager != null) && ((Context) manager.getContainer()).getDistributable() && !isAttributeDistributable(name, value) && !exclude(name, value)) {
        throw new IllegalArgumentException(sm.getString("standardSession.setAttribute.iae", name));
    }
    // Construct an event with the new value
    HttpSessionBindingEvent event = null;
    // Call the valueBound() method if necessary
    if (notify && value instanceof HttpSessionBindingListener) {
        // Don't call any notification if replacing with the same value
        Object oldValue = attributes.get(name);
        if (value != oldValue) {
            event = new HttpSessionBindingEvent(getSession(), name, value);
            try {
                ((HttpSessionBindingListener) value).valueBound(event);
            } catch (Throwable t) {
                manager.getContainer().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
            }
        }
    }
    // Replace or add this attribute
    Object unbound = attributes.put(name, value);
    // Call the valueUnbound() method if necessary
    if (notify && (unbound != null) && (unbound != value) && (unbound instanceof HttpSessionBindingListener)) {
        try {
            ((HttpSessionBindingListener) unbound).valueUnbound(new HttpSessionBindingEvent(getSession(), name));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            manager.getContainer().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
        }
    }
    if (!notify)
        return;
    // Notify interested application event listeners
    Context context = (Context) manager.getContainer();
    Object[] listeners = context.getApplicationEventListeners();
    if (listeners == null)
        return;
    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof HttpSessionAttributeListener))
            continue;
        HttpSessionAttributeListener listener = (HttpSessionAttributeListener) listeners[i];
        try {
            if (unbound != null) {
                context.fireContainerEvent("beforeSessionAttributeReplaced", listener);
                if (event == null) {
                    event = new HttpSessionBindingEvent(getSession(), name, unbound);
                }
                listener.attributeReplaced(event);
                context.fireContainerEvent("afterSessionAttributeReplaced", listener);
            } else {
                context.fireContainerEvent("beforeSessionAttributeAdded", listener);
                if (event == null) {
                    event = new HttpSessionBindingEvent(getSession(), name, value);
                }
                listener.attributeAdded(event);
                context.fireContainerEvent("afterSessionAttributeAdded", listener);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            try {
                if (unbound != null) {
                    context.fireContainerEvent("afterSessionAttributeReplaced", listener);
                } else {
                    context.fireContainerEvent("afterSessionAttributeAdded", listener);
                }
            } catch (Exception e) {
            // Ignore
            }
            manager.getContainer().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StandardContext(org.apache.catalina.core.StandardContext) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException)

Aggregations

HttpSessionBindingEvent (javax.servlet.http.HttpSessionBindingEvent)24 HttpSessionBindingListener (javax.servlet.http.HttpSessionBindingListener)24 IOException (java.io.IOException)5 NotSerializableException (java.io.NotSerializableException)4 WriteAbortedException (java.io.WriteAbortedException)4 ServletContext (javax.servlet.ServletContext)4 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)4 Context (org.apache.catalina.Context)4 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 StandardContext (org.apache.catalina.core.StandardContext)2 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)1 Locale (java.util.Locale)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 ISessionListener (org.polymap.core.runtime.session.ISessionListener)1 SessionContext (org.polymap.core.runtime.session.SessionContext)1