--- /dev/null
+// JigsawHttpSession.java\r
+// $Id: JigsawHttpSession.java,v 1.1 2010/06/15 12:24:14 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1998.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.servlet;\r
+\r
+import javax.servlet.ServletContext;\r
+\r
+import javax.servlet.http.Cookie;\r
+import javax.servlet.http.HttpSession;\r
+import javax.servlet.http.HttpSessionBindingEvent;\r
+import javax.servlet.http.HttpSessionBindingListener;\r
+import javax.servlet.http.HttpSessionContext;\r
+\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+\r
+import org.w3c.util.ArrayEnumeration;\r
+\r
+/**\r
+ * @version $Revision: 1.1 $\r
+ * @author Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class JigsawHttpSession implements HttpSession {\r
+\r
+ private JigsawHttpSessionContext sc = null;\r
+ \r
+ private JigsawServletContext servletContext = null;\r
+\r
+ private String id = null;\r
+\r
+ private long creationTime = -1;\r
+ private long lastAccessedTime = -1;\r
+\r
+ private boolean isValid = false;\r
+ private boolean isNew = false;\r
+\r
+ private Cookie cookie = null;\r
+\r
+ private Hashtable values = null;\r
+\r
+ private int maxidle = -1;\r
+\r
+ /**\r
+ * Returns the identifier assigned to this session. An HttpSession's \r
+ * identifier is a unique string that is created and maintained by\r
+ * HttpSessionContext. \r
+ * @return the identifier assigned to this session \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public String getId() {\r
+ return id;\r
+ }\r
+\r
+ /**\r
+ * Returns the context in which this session is bound. \r
+ * @return the context in which this session is bound.\r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ * @deprecated since jsdk2.1\r
+ */\r
+ public HttpSessionContext getSessionContext() {\r
+ return sc;\r
+ }\r
+\r
+ /**\r
+ * Returns the object bound with the specified name in this session, or\r
+ * <code>null</code> if no object is bound under the name.\r
+ * @param name a string specifying the name of the object\r
+ * @return the object with the specified name\r
+ * @exception IllegalStateException if this method is called on an\r
+ * invalidated session\r
+ */\r
+ public Object getAttribute(String name) {\r
+ return getValue(name);\r
+ }\r
+\r
+ /**\r
+ * Returns an <code>Enumeration</code> of <code>String</code> objects\r
+ * containing the names of all the objects bound to this session. \r
+ * @return an <code>Enumeration</code> of <code>String</code> objects \r
+ * specifying the names of all the objects bound to this session\r
+ * @exception IllegalStateException if this method is called on an\r
+ * invalidated session\r
+ */\r
+ public Enumeration getAttributeNames() {\r
+ if (!isValid)\r
+ throw new IllegalStateException("Invalid session");\r
+ return values.keys();\r
+ }\r
+\r
+ /**\r
+ * Binds an object to this session, using the name specified.\r
+ * If an object of the same name is already bound to the session,\r
+ * the object is replaced.\r
+ *\r
+ * <p>After this method executes, and if the object\r
+ * implements <code>HttpSessionBindingListener</code>,\r
+ * the container calls \r
+ * <code>HttpSessionBindingListener.valueBound</code>.\r
+ *\r
+ * @param name the name to which the object is bound; cannot be null\r
+ * @param value the object to be bound; cannot be null\r
+ * @exception IllegalStateException if this method is called on an\r
+ * invalidated session\r
+ */\r
+\r
+ public void setAttribute(String name, Object value) {\r
+ putValue(name, value);\r
+ }\r
+\r
+ /**\r
+ * Removes the object bound with the specified name from\r
+ * this session. If the session does not have an object\r
+ * bound with the specified name, this method does nothing.\r
+ *\r
+ * <p>After this method executes, and if the object\r
+ * implements <code>HttpSessionBindingListener</code>,\r
+ * the container calls \r
+ * <code>HttpSessionBindingListener.valueUnbound</code>.\r
+ * \r
+ * @param name the name of the object to remove from this session\r
+ * @exception IllegalStateException if this method is called on an\r
+ * invalidated session\r
+ */\r
+ public void removeAttribute(String name) {\r
+ removeValue(name);\r
+ }\r
+\r
+ /**\r
+ * Returns the time at which this session representation was created, \r
+ * in milliseconds since midnight, January 1, 1970 UTC. \r
+ * @return the time when the session was created \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public long getCreationTime() {\r
+ return creationTime;\r
+ }\r
+\r
+ /**\r
+ * Returns the last time the client sent a request carrying the identifier\r
+ * assigned to the session. Time is expressed as milliseconds\r
+ * since midnight, January 1, 1970 UTC. Application level operations, \r
+ * such as getting or setting a value associated with the session,\r
+ * does not affect the access time. \r
+ * @return the last time the client sent a request carrying the identifier\r
+ * assigned to the session \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public long getLastAccessedTime() {\r
+ return lastAccessedTime;\r
+ }\r
+\r
+ protected void setLastAccessedTime() {\r
+ lastAccessedTime = System.currentTimeMillis();\r
+ }\r
+\r
+ /**\r
+ * Causes this representation of the session to be invalidated and removed\r
+ * from its context. \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public void invalidate() {\r
+ // adding unbinding events during invalidate() in accordance \r
+ // with servlet api, tk, 23.10.2001\r
+ // start of modification \r
+ Enumeration names = values.keys();\r
+ while (names.hasMoreElements()) {\r
+ String name = (String)(names.nextElement());\r
+ removeValue(name);\r
+ }\r
+ // end of modification\r
+ \r
+ isValid = false;\r
+ sc.removeSession(id);\r
+ }\r
+\r
+ /**\r
+ * Returns the object bound to the given name in the session's application\r
+ * layer data. Returns null if there is no such binding. \r
+ * @param name - the name of the binding to find \r
+ * @return the value bound to that name, or null if the binding does \r
+ * not exist. \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */ \r
+ public Object getValue(String name) {\r
+ if (!isValid)\r
+ throw new IllegalStateException("Invalid session");\r
+ return values.get(name);\r
+ }\r
+\r
+ /**\r
+ * Binds the specified object into the session's application layer data \r
+ * with the given name. Any existing binding with the same name\r
+ * is replaced. New (or existing) values that implement the \r
+ * HttpSessionBindingListener interface will call its valueBound() method. \r
+ * @param name - the name to which the data object will be bound. \r
+ * This parameter cannot be null. \r
+ * @param value - the data object to be bound. This parameter cannot \r
+ * be null. \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public void putValue(String name, Object value)\r
+ {\r
+ if (!isValid)\r
+ throw new IllegalStateException("Invalid session");\r
+ removeValue(name);\r
+ // null check added in accordance with servlet api, tk. 23.10.2001\r
+ if (value != null) {\r
+ values.put(name, value);\r
+ if (value instanceof HttpSessionBindingListener)\r
+ valueBound((HttpSessionBindingListener)value, name);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Removes the object bound to the given name in the session's application\r
+ * layer data. Does nothing if there is no object bound to the\r
+ * given name. The value that implements the HttpSessionBindingListener \r
+ * interface will call its valueUnbound() method. \r
+ * @param name - the name of the object to remove \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public void removeValue(String name) {\r
+ if (!isValid)\r
+ throw new IllegalStateException("Invalid session");\r
+ Object value = values.get(name);\r
+ if (value != null) {\r
+ values.remove(name);\r
+ if (value instanceof HttpSessionBindingListener)\r
+ valueUnbound((HttpSessionBindingListener)value, name);\r
+ }\r
+ }\r
+\r
+ protected void valueBound(HttpSessionBindingListener value, String name) \r
+ {\r
+ value.valueBound(new HttpSessionBindingEvent(this, name));\r
+ }\r
+\r
+ protected void valueUnbound(HttpSessionBindingListener value, String name) \r
+ {\r
+ value.valueUnbound(new HttpSessionBindingEvent(this, name));\r
+ }\r
+\r
+ /**\r
+ * Returns an array of the names of all the application layer data objects\r
+ * bound into the session. For example, if you want to delete\r
+ * all of the data objects bound into the session, use this method to \r
+ * obtain their names. \r
+ * @return an array containing the names of all of the application layer\r
+ * data objects bound into the session \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ * @deprecated since jsdk2.2\r
+ */\r
+ public String[] getValueNames() {\r
+ if (!isValid)\r
+ throw new IllegalStateException("Invalid session");\r
+ String names[] = new String[values.size()];\r
+ Enumeration e = values.keys();\r
+ int i = 0;\r
+ while (e.hasMoreElements()) {\r
+ names[i++] = (String)e.nextElement();\r
+ }\r
+ return names;\r
+ }\r
+\r
+ /**\r
+ * A session is considered to be "new" if it has been created by the \r
+ * server, but the client has not yet acknowledged joining the\r
+ * session. For example, if the server supported only cookie-based \r
+ * sessions and the client had completely disabled the use of\r
+ * cookies, then calls to HttpServletRequest.getSession() would always \r
+ * return "new" sessions. \r
+ * @return true if the session has been created by the server but the \r
+ * client has not yet acknowledged joining the session; false otherwise \r
+ * @exception IllegalStateException if an attempt is made to access \r
+ * session data after the session has been invalidated \r
+ */\r
+ public boolean isNew() {\r
+ return isNew;\r
+ }\r
+\r
+ protected void setNoMoreNew() {\r
+ isNew = false;\r
+ }\r
+\r
+ protected boolean isValid() {\r
+ return isValid;\r
+ }\r
+\r
+ protected Cookie getCookie() {\r
+ return cookie;\r
+ }\r
+\r
+ //jsdk2.1\r
+\r
+ public void setMaxInactiveInterval(int interval) {\r
+ maxidle = interval;\r
+ }\r
+\r
+ public int getMaxInactiveInterval() {\r
+ return maxidle;\r
+ }\r
+\r
+ // jsdk 2.3\r
+ public ServletContext getServletContext() {\r
+ return servletContext;\r
+ }\r
+\r
+ public JigsawHttpSession(JigsawHttpSessionContext context, \r
+ JigsawServletContext servletContext,\r
+ Cookie cookie) {\r
+ this.values = new Hashtable();\r
+ this.creationTime = System.currentTimeMillis();\r
+ this.lastAccessedTime = creationTime;\r
+ this.sc = context;\r
+ this.id = context.addSession(this);\r
+ this.cookie = cookie;\r
+ this.servletContext = servletContext;\r
+ cookie.setValue(this.id);\r
+ isValid = true;\r
+ isNew = true;\r
+ }\r
+\r
+}\r