Search in sources :

Example 1 with SessionListener

use of org.apache.catalina.SessionListener in project tomcat70 by apache.

the class StandardSessionContext method fireSessionEvent.

/**
 * Notify all session event listeners that a particular event has
 * occurred for this Session.  The default implementation performs
 * this notification synchronously using the calling thread.
 *
 * @param type Event type
 * @param data Event data
 */
public void fireSessionEvent(String type, Object data) {
    if (listeners.size() < 1)
        return;
    SessionEvent event = new SessionEvent(this, type, data);
    SessionListener[] list = new SessionListener[0];
    synchronized (listeners) {
        list = listeners.toArray(list);
    }
    for (int i = 0; i < list.length; i++) {
        (list[i]).sessionEvent(event);
    }
}
Also used : HttpSessionEvent(javax.servlet.http.HttpSessionEvent) SessionEvent(org.apache.catalina.SessionEvent) HttpSessionListener(javax.servlet.http.HttpSessionListener) SessionListener(org.apache.catalina.SessionListener)

Example 2 with SessionListener

use of org.apache.catalina.SessionListener in project tomcat70 by apache.

the class DeltaRequest method execute.

public void execute(DeltaSession session, boolean notifyListeners) {
    if (!this.sessionId.equals(session.getId()))
        throw new java.lang.IllegalArgumentException("Session id mismatch, not executing the delta request");
    session.access();
    for (int i = 0; i < actions.size(); i++) {
        AttributeInfo info = actions.get(i);
        switch(info.getType()) {
            case TYPE_ATTRIBUTE:
                if (info.getAction() == ACTION_SET) {
                    if (log.isTraceEnabled())
                        log.trace("Session.setAttribute('" + info.getName() + "', '" + info.getValue() + "')");
                    session.setAttribute(info.getName(), info.getValue(), notifyListeners, false);
                } else {
                    if (log.isTraceEnabled())
                        log.trace("Session.removeAttribute('" + info.getName() + "')");
                    session.removeAttribute(info.getName(), notifyListeners, false);
                }
                break;
            case TYPE_ISNEW:
                if (log.isTraceEnabled())
                    log.trace("Session.setNew('" + info.getValue() + "')");
                session.setNew(((Boolean) info.getValue()).booleanValue(), false);
                break;
            case TYPE_MAXINTERVAL:
                if (log.isTraceEnabled())
                    log.trace("Session.setMaxInactiveInterval('" + info.getValue() + "')");
                session.setMaxInactiveInterval(((Integer) info.getValue()).intValue(), false);
                break;
            case TYPE_PRINCIPAL:
                Principal p = null;
                if (info.getAction() == ACTION_SET) {
                    SerializablePrincipal sp = (SerializablePrincipal) info.getValue();
                    p = sp.getPrincipal();
                }
                session.setPrincipal(p, false);
                break;
            case TYPE_AUTHTYPE:
                String authType = null;
                if (info.getAction() == ACTION_SET) {
                    authType = (String) info.getValue();
                }
                session.setAuthType(authType, false);
                break;
            case TYPE_LISTENER:
                SessionListener listener = (SessionListener) info.getValue();
                if (info.getAction() == ACTION_SET) {
                    session.addSessionListener(listener, false);
                } else {
                    session.removeSessionListener(listener, false);
                }
                break;
            default:
                throw new java.lang.IllegalArgumentException("Invalid attribute info type=" + info);
        }
    // switch
    }
    // for
    session.endAccess();
    reset();
}
Also used : SessionListener(org.apache.catalina.SessionListener) Principal(java.security.Principal) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal)

Example 3 with SessionListener

use of org.apache.catalina.SessionListener in project tomcat by apache.

the class DeltaSession method doReadObject.

private void doReadObject(ObjectInput stream) throws ClassNotFoundException, IOException {
    // Deserialize the scalar instance variables (except Manager)
    // Transient only
    authType = null;
    creationTime = ((Long) stream.readObject()).longValue();
    lastAccessedTime = ((Long) stream.readObject()).longValue();
    maxInactiveInterval = ((Integer) stream.readObject()).intValue();
    isNew = ((Boolean) stream.readObject()).booleanValue();
    isValid = ((Boolean) stream.readObject()).booleanValue();
    thisAccessedTime = ((Long) stream.readObject()).longValue();
    version = ((Long) stream.readObject()).longValue();
    boolean hasPrincipal = stream.readBoolean();
    principal = null;
    if (hasPrincipal) {
        principal = (Principal) stream.readObject();
    }
    // setId((String) stream.readObject());
    id = (String) stream.readObject();
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("deltaSession.readSession", id));
    }
    // Deserialize the attribute count and attribute values
    if (attributes == null) {
        attributes = new ConcurrentHashMap<>();
    }
    int n = ((Integer) stream.readObject()).intValue();
    boolean isValidSave = isValid;
    isValid = true;
    for (int i = 0; i < n; i++) {
        String name = (String) stream.readObject();
        final Object value;
        try {
            value = stream.readObject();
        } catch (WriteAbortedException wae) {
            if (wae.getCause() instanceof NotSerializableException) {
                // Skip non serializable attributes
                continue;
            }
            throw wae;
        }
        // the web application was stopped.
        if (exclude(name, value)) {
            continue;
        }
        // ConcurrentHashMap does not allow null keys or values
        if (null != value) {
            attributes.put(name, value);
        }
    }
    isValid = isValidSave;
    // Session listeners
    n = ((Integer) stream.readObject()).intValue();
    if (listeners == null || n > 0) {
        listeners = new ArrayList<>();
    }
    for (int i = 0; i < n; i++) {
        SessionListener listener = (SessionListener) stream.readObject();
        listeners.add(listener);
    }
    if (notes == null) {
        notes = new Hashtable<>();
    }
    activate();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException) SessionListener(org.apache.catalina.SessionListener)

Example 4 with SessionListener

use of org.apache.catalina.SessionListener in project tomcat by apache.

the class DeltaSession method doWriteObject.

private void doWriteObject(ObjectOutput stream) throws IOException {
    // Write the scalar instance variables (except Manager)
    stream.writeObject(Long.valueOf(creationTime));
    stream.writeObject(Long.valueOf(lastAccessedTime));
    stream.writeObject(Integer.valueOf(maxInactiveInterval));
    stream.writeObject(Boolean.valueOf(isNew));
    stream.writeObject(Boolean.valueOf(isValid));
    stream.writeObject(Long.valueOf(thisAccessedTime));
    stream.writeObject(Long.valueOf(version));
    stream.writeBoolean(getPrincipal() instanceof Serializable);
    if (getPrincipal() instanceof Serializable) {
        stream.writeObject(getPrincipal());
    }
    stream.writeObject(id);
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("deltaSession.writeSession", id));
    }
    // Accumulate the names of serializable and non-serializable attributes
    String[] keys = keys();
    List<String> saveNames = new ArrayList<>();
    List<Object> saveValues = new ArrayList<>();
    for (String key : keys) {
        Object value = null;
        value = attributes.get(key);
        if (value != null && !exclude(key, value) && isAttributeDistributable(key, value)) {
            saveNames.add(key);
            saveValues.add(value);
        }
    }
    // Serialize the attribute count and the Serializable attributes
    int n = saveNames.size();
    stream.writeObject(Integer.valueOf(n));
    for (int i = 0; i < n; i++) {
        stream.writeObject(saveNames.get(i));
        try {
            stream.writeObject(saveValues.get(i));
        } catch (NotSerializableException e) {
            log.error(sm.getString("standardSession.notSerializable", saveNames.get(i), id), e);
        }
    }
    // Serializable listeners
    ArrayList<SessionListener> saveListeners = new ArrayList<>();
    for (SessionListener listener : listeners) {
        if (listener instanceof ReplicatedSessionListener) {
            saveListeners.add(listener);
        }
    }
    stream.writeObject(Integer.valueOf(saveListeners.size()));
    for (SessionListener listener : saveListeners) {
        stream.writeObject(listener);
    }
}
Also used : Serializable(java.io.Serializable) NotSerializableException(java.io.NotSerializableException) ArrayList(java.util.ArrayList) SessionListener(org.apache.catalina.SessionListener)

Example 5 with SessionListener

use of org.apache.catalina.SessionListener in project tomcat by apache.

the class DeltaRequest method execute.

public void execute(DeltaSession session, boolean notifyListeners) {
    if (!this.sessionId.equals(session.getId())) {
        throw new java.lang.IllegalArgumentException(sm.getString("deltaRequest.ssid.mismatch"));
    }
    session.access();
    for (AttributeInfo info : actions) {
        switch(info.getType()) {
            case TYPE_ATTRIBUTE:
                if (info.getAction() == ACTION_SET) {
                    if (log.isTraceEnabled()) {
                        log.trace("Session.setAttribute('" + info.getName() + "', '" + info.getValue() + "')");
                    }
                    session.setAttribute(info.getName(), info.getValue(), notifyListeners, false);
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace("Session.removeAttribute('" + info.getName() + "')");
                    }
                    session.removeAttribute(info.getName(), notifyListeners, false);
                }
                break;
            case TYPE_ISNEW:
                if (log.isTraceEnabled()) {
                    log.trace("Session.setNew('" + info.getValue() + "')");
                }
                session.setNew(((Boolean) info.getValue()).booleanValue(), false);
                break;
            case TYPE_MAXINTERVAL:
                if (log.isTraceEnabled()) {
                    log.trace("Session.setMaxInactiveInterval('" + info.getValue() + "')");
                }
                session.setMaxInactiveInterval(((Integer) info.getValue()).intValue(), false);
                break;
            case TYPE_PRINCIPAL:
                Principal p = null;
                if (info.getAction() == ACTION_SET) {
                    p = (Principal) info.getValue();
                }
                session.setPrincipal(p, false);
                break;
            case TYPE_AUTHTYPE:
                String authType = null;
                if (info.getAction() == ACTION_SET) {
                    authType = (String) info.getValue();
                }
                session.setAuthType(authType, false);
                break;
            case TYPE_LISTENER:
                SessionListener listener = (SessionListener) info.getValue();
                if (info.getAction() == ACTION_SET) {
                    session.addSessionListener(listener, false);
                } else {
                    session.removeSessionListener(listener, false);
                }
                break;
            default:
                throw new IllegalArgumentException(sm.getString("deltaRequest.invalidAttributeInfoType", info));
        }
    // switch
    }
    // for
    session.endAccess();
    reset();
}
Also used : SessionListener(org.apache.catalina.SessionListener) Principal(java.security.Principal) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal)

Aggregations

SessionListener (org.apache.catalina.SessionListener)9 NotSerializableException (java.io.NotSerializableException)4 SessionEvent (org.apache.catalina.SessionEvent)3 WriteAbortedException (java.io.WriteAbortedException)2 Principal (java.security.Principal)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HttpSessionEvent (javax.servlet.http.HttpSessionEvent)2 HttpSessionListener (javax.servlet.http.HttpSessionListener)2 GenericPrincipal (org.apache.catalina.realm.GenericPrincipal)2 HttpSessionEvent (jakarta.servlet.http.HttpSessionEvent)1 HttpSessionListener (jakarta.servlet.http.HttpSessionListener)1 Serializable (java.io.Serializable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1