Search in sources :

Example 1 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project tomcat 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) && manager.getContext().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.getContext().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.getContext().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
        }
    }
    if (!notify)
        return;
    // Notify interested application event listeners
    Context context = manager.getContext();
    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.getContext().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException)

Example 2 with HttpSessionBindingListener

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

the class StandardSessionContext method removeAttributeInternal.

/**
     * Remove the object bound with the specified name from this session.  If
     * the session does not have an object bound with this name, this method
     * does nothing.
     * <p>
     * After this method executes, and if the object implements
     * <code>HttpSessionBindingListener</code>, the container calls
     * <code>valueUnbound()</code> on the object.
     *
     * @param name Name of the object to remove from this session.
     * @param notify Should we notify interested listeners that this
     *  attribute is being removed?
     */
protected void removeAttributeInternal(String name, boolean notify) {
    // Avoid NPE
    if (name == null)
        return;
    // Remove this attribute from our collection
    Object value = attributes.remove(name);
    // Do we need to do valueUnbound() and attributeRemoved() notification?
    if (!notify || (value == null)) {
        return;
    }
    // Call the valueUnbound() method if necessary
    HttpSessionBindingEvent event = null;
    if (value instanceof HttpSessionBindingListener) {
        event = new HttpSessionBindingEvent(getSession(), name, value);
        ((HttpSessionBindingListener) value).valueUnbound(event);
    }
    // Notify interested application event listeners
    Context context = manager.getContext();
    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 {
            context.fireContainerEvent("beforeSessionAttributeRemoved", listener);
            if (event == null) {
                event = new HttpSessionBindingEvent(getSession(), name, value);
            }
            listener.attributeRemoved(event);
            context.fireContainerEvent("afterSessionAttributeRemoved", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            try {
                context.fireContainerEvent("afterSessionAttributeRemoved", listener);
            } catch (Exception e) {
            // Ignore
            }
            manager.getContext().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException)

Example 3 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project spring-framework by spring-projects.

the class MockHttpSession method removeAttribute.

@Override
public void removeAttribute(String name) {
    assertIsValid();
    Assert.notNull(name, "Attribute name must not be null");
    Object value = this.attributes.remove(name);
    if (value instanceof HttpSessionBindingListener) {
        ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener)

Example 4 with HttpSessionBindingListener

use of javax.servlet.http.HttpSessionBindingListener in project gocd by gocd.

the class MockHttpSession method serializeState.

/**
 * Serialize the attributes of this session into an object that can be
 * turned into a byte array with standard Java serialization.
 * @return a representation of this session's serialized state
 */
public Serializable serializeState() {
    HashMap<String, Serializable> state = new HashMap<>();
    for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<String, Object> entry = it.next();
        String name = entry.getKey();
        Object value = entry.getValue();
        it.remove();
        if (value instanceof Serializable) {
            state.put(name, (Serializable) value);
        } else {
            // unbind the attribute in this case.
            if (value instanceof HttpSessionBindingListener) {
                ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
            }
        }
    }
    return state;
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) Serializable(java.io.Serializable) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener)

Example 5 with HttpSessionBindingListener

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

the class HttpSessionWrapper method setAttribute.

@Override
public void setAttribute(final String name, final Object value) {
    this.checkInvalid();
    if (value == null) {
        this.removeAttribute(name);
        return;
    }
    final Object oldValue = this.getAttribute(name);
    // wrap http session binding listener to avoid container calling it!
    if (value instanceof HttpSessionBindingListener) {
        this.delegate.setAttribute(this.getKey(name), new SessionBindingValueListenerWrapper((HttpSessionBindingListener) value));
    } else {
        this.delegate.setAttribute(this.getKey(name), value);
    }
    if (value instanceof HttpSessionBindingListener) {
        ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name));
    }
    if (this.context.getHttpSessionAttributeListener() != null) {
        if (oldValue != null) {
            this.context.getHttpSessionAttributeListener().attributeReplaced(new HttpSessionBindingEvent(this, name, oldValue));
        } else {
            this.context.getHttpSessionAttributeListener().attributeAdded(new HttpSessionBindingEvent(this, name, value));
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener)

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