001: // JigsawHttpSession.java
002: // $Id: JigsawHttpSession.java,v 1.11 2007/02/11 10:55:20 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import javax.servlet.ServletContext;
009:
010: import javax.servlet.http.Cookie;
011: import javax.servlet.http.HttpSession;
012: import javax.servlet.http.HttpSessionBindingEvent;
013: import javax.servlet.http.HttpSessionBindingListener;
014: import javax.servlet.http.HttpSessionContext;
015:
016: import java.util.Enumeration;
017: import java.util.Hashtable;
018:
019: import org.w3c.util.ArrayEnumeration;
020:
021: /**
022: * @version $Revision: 1.11 $
023: * @author Benoît Mahé (bmahe@w3.org)
024: */
025: public class JigsawHttpSession implements HttpSession {
026:
027: private JigsawHttpSessionContext sc = null;
028:
029: private JigsawServletContext servletContext = null;
030:
031: private String id = null;
032:
033: private long creationTime = -1;
034: private long lastAccessedTime = -1;
035:
036: private boolean isValid = false;
037: private boolean isNew = false;
038:
039: private Cookie cookie = null;
040:
041: private Hashtable values = null;
042:
043: private int maxidle = -1;
044:
045: /**
046: * Returns the identifier assigned to this session. An HttpSession's
047: * identifier is a unique string that is created and maintained by
048: * HttpSessionContext.
049: * @return the identifier assigned to this session
050: * @exception IllegalStateException if an attempt is made to access
051: * session data after the session has been invalidated
052: */
053: public String getId() {
054: return id;
055: }
056:
057: /**
058: * Returns the context in which this session is bound.
059: * @return the context in which this session is bound.
060: * @exception IllegalStateException if an attempt is made to access
061: * session data after the session has been invalidated
062: * @deprecated since jsdk2.1
063: */
064: public HttpSessionContext getSessionContext() {
065: return sc;
066: }
067:
068: /**
069: * Returns the object bound with the specified name in this session, or
070: * <code>null</code> if no object is bound under the name.
071: * @param name a string specifying the name of the object
072: * @return the object with the specified name
073: * @exception IllegalStateException if this method is called on an
074: * invalidated session
075: */
076: public Object getAttribute(String name) {
077: return getValue(name);
078: }
079:
080: /**
081: * Returns an <code>Enumeration</code> of <code>String</code> objects
082: * containing the names of all the objects bound to this session.
083: * @return an <code>Enumeration</code> of <code>String</code> objects
084: * specifying the names of all the objects bound to this session
085: * @exception IllegalStateException if this method is called on an
086: * invalidated session
087: */
088: public Enumeration getAttributeNames() {
089: if (!isValid)
090: throw new IllegalStateException("Invalid session");
091: return values.keys();
092: }
093:
094: /**
095: * Binds an object to this session, using the name specified.
096: * If an object of the same name is already bound to the session,
097: * the object is replaced.
098: *
099: * <p>After this method executes, and if the object
100: * implements <code>HttpSessionBindingListener</code>,
101: * the container calls
102: * <code>HttpSessionBindingListener.valueBound</code>.
103: *
104: * @param name the name to which the object is bound; cannot be null
105: * @param value the object to be bound; cannot be null
106: * @exception IllegalStateException if this method is called on an
107: * invalidated session
108: */
109:
110: public void setAttribute(String name, Object value) {
111: putValue(name, value);
112: }
113:
114: /**
115: * Removes the object bound with the specified name from
116: * this session. If the session does not have an object
117: * bound with the specified name, this method does nothing.
118: *
119: * <p>After this method executes, and if the object
120: * implements <code>HttpSessionBindingListener</code>,
121: * the container calls
122: * <code>HttpSessionBindingListener.valueUnbound</code>.
123: *
124: * @param name the name of the object to remove from this session
125: * @exception IllegalStateException if this method is called on an
126: * invalidated session
127: */
128: public void removeAttribute(String name) {
129: removeValue(name);
130: }
131:
132: /**
133: * Returns the time at which this session representation was created,
134: * in milliseconds since midnight, January 1, 1970 UTC.
135: * @return the time when the session was created
136: * @exception IllegalStateException if an attempt is made to access
137: * session data after the session has been invalidated
138: */
139: public long getCreationTime() {
140: return creationTime;
141: }
142:
143: /**
144: * Returns the last time the client sent a request carrying the identifier
145: * assigned to the session. Time is expressed as milliseconds
146: * since midnight, January 1, 1970 UTC. Application level operations,
147: * such as getting or setting a value associated with the session,
148: * does not affect the access time.
149: * @return the last time the client sent a request carrying the identifier
150: * assigned to the session
151: * @exception IllegalStateException if an attempt is made to access
152: * session data after the session has been invalidated
153: */
154: public long getLastAccessedTime() {
155: return lastAccessedTime;
156: }
157:
158: protected void setLastAccessedTime() {
159: lastAccessedTime = System.currentTimeMillis();
160: }
161:
162: /**
163: * Causes this representation of the session to be invalidated and removed
164: * from its context.
165: * @exception IllegalStateException if an attempt is made to access
166: * session data after the session has been invalidated
167: */
168: public void invalidate() {
169: // adding unbinding events during invalidate() in accordance
170: // with servlet api, tk, 23.10.2001
171: // start of modification
172: Enumeration names = values.keys();
173: while (names.hasMoreElements()) {
174: String name = (String) (names.nextElement());
175: removeValue(name);
176: }
177: // end of modification
178:
179: isValid = false;
180: sc.removeSession(id);
181: }
182:
183: /**
184: * Returns the object bound to the given name in the session's application
185: * layer data. Returns null if there is no such binding.
186: * @param name - the name of the binding to find
187: * @return the value bound to that name, or null if the binding does
188: * not exist.
189: * @exception IllegalStateException if an attempt is made to access
190: * session data after the session has been invalidated
191: */
192: public Object getValue(String name) {
193: if (!isValid)
194: throw new IllegalStateException("Invalid session");
195: return values.get(name);
196: }
197:
198: /**
199: * Binds the specified object into the session's application layer data
200: * with the given name. Any existing binding with the same name
201: * is replaced. New (or existing) values that implement the
202: * HttpSessionBindingListener interface will call its valueBound() method.
203: * @param name - the name to which the data object will be bound.
204: * This parameter cannot be null.
205: * @param value - the data object to be bound. This parameter cannot
206: * be null.
207: * @exception IllegalStateException if an attempt is made to access
208: * session data after the session has been invalidated
209: */
210: public void putValue(String name, Object value) {
211: if (!isValid)
212: throw new IllegalStateException("Invalid session");
213: removeValue(name);
214: // null check added in accordance with servlet api, tk. 23.10.2001
215: if (value != null) {
216: values.put(name, value);
217: if (value instanceof HttpSessionBindingListener)
218: valueBound((HttpSessionBindingListener) value, name);
219: }
220: }
221:
222: /**
223: * Removes the object bound to the given name in the session's application
224: * layer data. Does nothing if there is no object bound to the
225: * given name. The value that implements the HttpSessionBindingListener
226: * interface will call its valueUnbound() method.
227: * @param name - the name of the object to remove
228: * @exception IllegalStateException if an attempt is made to access
229: * session data after the session has been invalidated
230: */
231: public void removeValue(String name) {
232: if (!isValid)
233: throw new IllegalStateException("Invalid session");
234: Object value = values.get(name);
235: if (value != null) {
236: values.remove(name);
237: if (value instanceof HttpSessionBindingListener)
238: valueUnbound((HttpSessionBindingListener) value, name);
239: }
240: }
241:
242: protected void valueBound(HttpSessionBindingListener value,
243: String name) {
244: value.valueBound(new HttpSessionBindingEvent(this , name));
245: }
246:
247: protected void valueUnbound(HttpSessionBindingListener value,
248: String name) {
249: value.valueUnbound(new HttpSessionBindingEvent(this , name));
250: }
251:
252: /**
253: * Returns an array of the names of all the application layer data objects
254: * bound into the session. For example, if you want to delete
255: * all of the data objects bound into the session, use this method to
256: * obtain their names.
257: * @return an array containing the names of all of the application layer
258: * data objects bound into the session
259: * @exception IllegalStateException if an attempt is made to access
260: * session data after the session has been invalidated
261: * @deprecated since jsdk2.2
262: */
263: public String[] getValueNames() {
264: if (!isValid)
265: throw new IllegalStateException("Invalid session");
266: String names[] = new String[values.size()];
267: Enumeration e = values.keys();
268: int i = 0;
269: while (e.hasMoreElements()) {
270: names[i++] = (String) e.nextElement();
271: }
272: return names;
273: }
274:
275: /**
276: * A session is considered to be "new" if it has been created by the
277: * server, but the client has not yet acknowledged joining the
278: * session. For example, if the server supported only cookie-based
279: * sessions and the client had completely disabled the use of
280: * cookies, then calls to HttpServletRequest.getSession() would always
281: * return "new" sessions.
282: * @return true if the session has been created by the server but the
283: * client has not yet acknowledged joining the session; false otherwise
284: * @exception IllegalStateException if an attempt is made to access
285: * session data after the session has been invalidated
286: */
287: public boolean isNew() {
288: return isNew;
289: }
290:
291: protected void setNoMoreNew() {
292: isNew = false;
293: }
294:
295: protected boolean isValid() {
296: return isValid;
297: }
298:
299: protected Cookie getCookie() {
300: return cookie;
301: }
302:
303: //jsdk2.1
304:
305: public void setMaxInactiveInterval(int interval) {
306: maxidle = interval;
307: }
308:
309: public int getMaxInactiveInterval() {
310: return maxidle;
311: }
312:
313: // jsdk 2.3
314: public ServletContext getServletContext() {
315: return servletContext;
316: }
317:
318: public JigsawHttpSession(JigsawHttpSessionContext context,
319: JigsawServletContext servletContext, Cookie cookie) {
320: this .values = new Hashtable();
321: this .creationTime = System.currentTimeMillis();
322: this .lastAccessedTime = creationTime;
323: this .sc = context;
324: this .id = context.addSession(this );
325: this .cookie = cookie;
326: this .servletContext = servletContext;
327: cookie.setValue(this .id);
328: isValid = true;
329: isNew = true;
330: }
331:
332: }
|