All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.web.ProxySession Maven / Gradle / Ivy

package com.alachisoft.ncache.web;

import com.alachisoft.ncache.web.session.SessionMonitor;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import org.apache.log4j.Logger;

public class ProxySession implements HttpSession
{
    private HttpSession session = null;
    private boolean invalidated = false;
    private CacheSession csession = null;
    private ProxyRequest request = null;
    private Logger logger = Logger.getLogger(ProxySession.class);

    //--------------------------------------------------------------------------------------------------------
    public ProxySession(HttpSession session, ProxyRequest request, CacheSession csession)
    {
        this.session = session;
        this.csession = csession;
        this.request = request;
        if (csession != null && session != null)
        {
            //--- this should never happen
            logger.debug("Initializing proxy session with attributes from remote session.");
            fillLocalSession(csession, session);
        }
    }
    //--------------------------------------------------------------------------------------------------------
    private void fillLocalSession(CacheSession csession, HttpSession session)
    {
        Set keys = csession.getKeys();
        Iterator it = keys.iterator();
        while (it.hasNext())
        {
            String key = (String) it.next();
            logger.debug("Adding attribute [" + key + "] to local session.");
            session.setAttribute(key, csession.getAttribute(key));
        }
        /**
         * Clear the CacheSession attributes locally. We'll again fill it
         * with the attributes of the local session before saving it to JvCache
         * once we are done with execution of the request.
         */
        csession.clearAttributes();
    }
    //--------------------------------------------------------------------------------------------------------
    public long getCreationTime()
    {
        return session.getCreationTime();
    }
    //--------------------------------------------------------------------------------------------------------
    public String getId()
    {
        //return session.getId();
        return csession.getSessionId();
    }
    //--------------------------------------------------------------------------------------------------------
    public long getLastAccessedTime()
    {
        return session.getLastAccessedTime();
    }
    //--------------------------------------------------------------------------------------------------------
    public ServletContext getServletContext()
    {
        return session.getServletContext();
    }
    //--------------------------------------------------------------------------------------------------------
    public void setMaxInactiveInterval(int interval)
    {
        session.setMaxInactiveInterval(interval);
    }
    //--------------------------------------------------------------------------------------------------------
    public int getMaxInactiveInterval()
    {
        return session.getMaxInactiveInterval();
    }
    //--------------------------------------------------------------------------------------------------------
    public HttpSessionContext getSessionContext()
    {
        return session.getSessionContext();
    }
    //--------------------------------------------------------------------------------------------------------
    public Object getAttribute(String name)
    {
        return session.getAttribute(name);
    }
    //--------------------------------------------------------------------------------------------------------
    /**
     * @deprecated @param name
     * @return
     */
    public Object getValue(String name)
    {
        return getAttribute(name);
    }
    //--------------------------------------------------------------------------------------------------------
    public Enumeration getAttributeNames()
    {
        return session.getAttributeNames();
    }
    //--------------------------------------------------------------------------------------------------------
    public String[] getValueNames()
    {
        return session.getValueNames();
    }
    //--------------------------------------------------------------------------------------------------------
    public void setAttribute(String name, Object value)
    {
        ensureSerializable(value);
        session.setAttribute(name, value);
    }
    //--------------------------------------------------------------------------------------------------------
    public void putValue(String name, Object value)
    {
        ensureSerializable(value);
        setAttribute(name, value);
    }
    //--------------------------------------------------------------------------------------------------------
    public void removeAttribute(String name)
    {
        session.removeAttribute(name);
    }
    //--------------------------------------------------------------------------------------------------------
    public void removeValue(String name)
    {
        removeAttribute(name);
    }
    //--------------------------------------------------------------------------------------------------------
    public void invalidate()
    {
        logger.debug("Session [" + getId() + "] is invalidated.");
        this.invalidated = true;
        SessionMonitor.resetRequestCount(getId());
        session.invalidate();
        request.sessionInvalidated(); //--- notify request to take appropriate measures
    }
    //--------------------------------------------------------------------------------------------------------
    public boolean isNew()
    {
        return csession.isIsNew();
    }
    //--------------------------------------------------------------------------------------------------------
    public boolean isInvalid()
    {
        return invalidated;
    }
    //--------------------------------------------------------------------------------------------------------
    private void ensureSerializable(Object value)
    {
        if (!(value instanceof Serializable))
        {
            //throw new IllegalArgumentException("The session does not support non-serializable attributes."); - sajid
            throw new IllegalArgumentException("non-serializable attributes can not be added/saved in session.");
        }
    }
    //--------------------------------------------------------------------------------------------------------
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy