Search in sources :

Example 6 with HttpSessionBindingEvent

use of jakarta.servlet.http.HttpSessionBindingEvent in project spring-framework by spring-projects.

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(jakarta.servlet.http.HttpSessionBindingEvent) Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpSessionBindingListener(jakarta.servlet.http.HttpSessionBindingListener) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with HttpSessionBindingEvent

use of jakarta.servlet.http.HttpSessionBindingEvent in project spring-framework by spring-projects.

the class MockHttpSession method setAttribute.

@Override
public void setAttribute(String name, @Nullable Object value) {
    assertIsValid();
    Assert.notNull(name, "Attribute name must not be null");
    if (value != null) {
        Object oldValue = this.attributes.put(name, value);
        if (value != oldValue) {
            if (oldValue instanceof HttpSessionBindingListener) {
                ((HttpSessionBindingListener) oldValue).valueUnbound(new HttpSessionBindingEvent(this, name, oldValue));
            }
            if (value instanceof HttpSessionBindingListener) {
                ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value));
            }
        }
    } else {
        removeAttribute(name);
    }
}
Also used : HttpSessionBindingEvent(jakarta.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(jakarta.servlet.http.HttpSessionBindingListener)

Example 8 with HttpSessionBindingEvent

use of jakarta.servlet.http.HttpSessionBindingEvent in project spring-framework by spring-projects.

the class MockHttpSession method clearAttributes.

/**
 * Clear all of this session's attributes.
 */
public void clearAttributes() {
    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 HttpSessionBindingListener) {
            ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
        }
    }
}
Also used : HttpSessionBindingEvent(jakarta.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(jakarta.servlet.http.HttpSessionBindingListener) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with HttpSessionBindingEvent

use of jakarta.servlet.http.HttpSessionBindingEvent 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(jakarta.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(jakarta.servlet.http.HttpSessionBindingListener)

Example 10 with HttpSessionBindingEvent

use of jakarta.servlet.http.HttpSessionBindingEvent in project spring-framework by spring-projects.

the class MockHttpSession method setAttribute.

@Override
public void setAttribute(String name, @Nullable Object value) {
    assertIsValid();
    Assert.notNull(name, "Attribute name must not be null");
    if (value != null) {
        Object oldValue = this.attributes.put(name, value);
        if (value != oldValue) {
            if (oldValue instanceof HttpSessionBindingListener) {
                ((HttpSessionBindingListener) oldValue).valueUnbound(new HttpSessionBindingEvent(this, name, oldValue));
            }
            if (value instanceof HttpSessionBindingListener) {
                ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value));
            }
        }
    } else {
        removeAttribute(name);
    }
}
Also used : HttpSessionBindingEvent(jakarta.servlet.http.HttpSessionBindingEvent) HttpSessionBindingListener(jakarta.servlet.http.HttpSessionBindingListener)

Aggregations

HttpSessionBindingEvent (jakarta.servlet.http.HttpSessionBindingEvent)10 HttpSessionBindingListener (jakarta.servlet.http.HttpSessionBindingListener)10 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 ServletContext (jakarta.servlet.ServletContext)2 HttpSessionAttributeListener (jakarta.servlet.http.HttpSessionAttributeListener)2 IOException (java.io.IOException)2 NotSerializableException (java.io.NotSerializableException)2 ObjectStreamException (java.io.ObjectStreamException)2 Serializable (java.io.Serializable)2 WriteAbortedException (java.io.WriteAbortedException)2 Context (org.apache.catalina.Context)2