Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpSession.java, revision 1.6.4.3
1.1 bmahe 1: // JigsawHttpSession.java
1.6.4.3 ! bmahe 2: // $Id: JigsawHttpSession.java,v 1.6.4.2 2000/02/10 13:39:04 bmahe Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT and INRIA, 1998.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package org.w3c.jigsaw.servlet;
7:
8: import java.io.*;
9: import javax.servlet.*;
10: import javax.servlet.http.*;
11: import java.net.*;
12: import java.util.*;
13:
1.6.4.2 bmahe 14: import org.w3c.util.ArrayEnumeration;
15:
1.1 bmahe 16: /**
1.6.4.3 ! bmahe 17: * @version $Revision: 1.6.4.2 $
1.1 bmahe 18: * @author Benoît Mahé (bmahe@w3.org)
19: */
20: public class JigsawHttpSession implements HttpSession {
21:
22: private JigsawHttpSessionContext sc = null;
23:
24: private String id = null;
25:
26: private long creationTime = -1;
27: private long lastAccessedTime = -1;
28:
29: private boolean isValid = false;
30: private boolean isNew = false;
31:
32: private Cookie cookie = null;
33:
34: private Hashtable values = null;
35:
1.6 bmahe 36: private int maxidle = -1;
37:
1.5 bmahe 38: /**
39: * Returns the identifier assigned to this session. An HttpSession's
40: * identifier is a unique string that is created and maintained by
41: * HttpSessionContext.
42: * @return the identifier assigned to this session
43: * @exception IllegalStateException if an attempt is made to access
44: * session data after the session has been invalidated
45: */
1.1 bmahe 46: public String getId() {
47: return id;
48: }
49:
1.5 bmahe 50: /**
51: * Returns the context in which this session is bound.
52: * @return the context in which this session is bound.
53: * @exception IllegalStateException if an attempt is made to access
54: * session data after the session has been invalidated
1.6 bmahe 55: * @deprecated since jsdk2.1
1.5 bmahe 56: */
1.1 bmahe 57: public HttpSessionContext getSessionContext() {
58: return sc;
1.6.4.1 bmahe 59: }
60:
61: /**
62: * Returns the object bound with the specified name in this session, or
63: * <code>null</code> if no object is bound under the name.
64: * @param name a string specifying the name of the object
65: * @return the object with the specified name
66: * @exception IllegalStateException if this method is called on an
67: * invalidated session
68: */
69: public Object getAttribute(String name) {
1.6.4.2 bmahe 70: return getValue(name);
1.6.4.1 bmahe 71: }
72:
73: /**
74: * Returns an <code>Enumeration</code> of <code>String</code> objects
75: * containing the names of all the objects bound to this session.
76: * @return an <code>Enumeration</code> of <code>String</code> objects
77: * specifying the names of all the objects bound to this session
78: * @exception IllegalStateException if this method is called on an
79: * invalidated session
80: */
81: public Enumeration getAttributeNames() {
1.6.4.3 ! bmahe 82: if (!isValid)
! 83: throw new IllegalStateException("Invalid session");
! 84: return values.keys();
1.6.4.1 bmahe 85: }
86:
87: /**
88: * Binds an object to this session, using the name specified.
89: * If an object of the same name is already bound to the session,
90: * the object is replaced.
91: *
92: * <p>After this method executes, and if the object
93: * implements <code>HttpSessionBindingListener</code>,
94: * the container calls
95: * <code>HttpSessionBindingListener.valueBound</code>.
96: *
97: * @param name the name to which the object is bound; cannot be null
98: * @param value the object to be bound; cannot be null
99: * @exception IllegalStateException if this method is called on an
100: * invalidated session
101: */
102:
103: public void setAttribute(String name, Object value) {
1.6.4.2 bmahe 104: putValue(name, value);
1.6.4.1 bmahe 105: }
106:
107: /**
108: * Removes the object bound with the specified name from
109: * this session. If the session does not have an object
110: * bound with the specified name, this method does nothing.
111: *
112: * <p>After this method executes, and if the object
113: * implements <code>HttpSessionBindingListener</code>,
114: * the container calls
115: * <code>HttpSessionBindingListener.valueUnbound</code>.
116: *
117: * @param name the name of the object to remove from this session
118: * @exception IllegalStateException if this method is called on an
119: * invalidated session
120: */
121: public void removeAttribute(String name) {
1.6.4.2 bmahe 122: removeValue(name);
1.1 bmahe 123: }
124:
1.5 bmahe 125: /**
126: * Returns the time at which this session representation was created,
127: * in milliseconds since midnight, January 1, 1970 UTC.
128: * @return the time when the session was created
129: * @exception IllegalStateException if an attempt is made to access
130: * session data after the session has been invalidated
131: */
1.1 bmahe 132: public long getCreationTime() {
133: return creationTime;
134: }
135:
1.5 bmahe 136: /**
137: * Returns the last time the client sent a request carrying the identifier
138: * assigned to the session. Time is expressed as milliseconds
139: * since midnight, January 1, 1970 UTC. Application level operations,
140: * such as getting or setting a value associated with the session,
141: * does not affect the access time.
142: * @return the last time the client sent a request carrying the identifier
143: * assigned to the session
144: * @exception IllegalStateException if an attempt is made to access
145: * session data after the session has been invalidated
146: */
1.1 bmahe 147: public long getLastAccessedTime() {
148: return lastAccessedTime;
149: }
150:
1.5 bmahe 151: protected void setLastAccessedTime() {
1.1 bmahe 152: lastAccessedTime = System.currentTimeMillis();
153: }
154:
1.5 bmahe 155: /**
156: * Causes this representation of the session to be invalidated and removed
157: * from its context.
158: * @exception IllegalStateException if an attempt is made to access
159: * session data after the session has been invalidated
160: */
1.1 bmahe 161: public void invalidate() {
162: isValid = false;
1.3 bmahe 163: sc.removeSession(id);
1.1 bmahe 164: }
165:
1.5 bmahe 166: /**
167: * Returns the object bound to the given name in the session's application
168: * layer data. Returns null if there is no such binding.
169: * @param name - the name of the binding to find
170: * @return the value bound to that name, or null if the binding does
171: * not exist.
172: * @exception IllegalStateException if an attempt is made to access
173: * session data after the session has been invalidated
174: */
1.1 bmahe 175: public Object getValue(String name) {
176: if (!isValid)
177: throw new IllegalStateException("Invalid session");
178: return values.get(name);
179: }
180:
1.5 bmahe 181: /**
182: * Binds the specified object into the session's application layer data
183: * with the given name. Any existing binding with the same name
184: * is replaced. New (or existing) values that implement the
185: * HttpSessionBindingListener interface will call its valueBound() method.
186: * @param name - the name to which the data object will be bound.
187: * This parameter cannot be null.
188: * @param value - the data object to be bound. This parameter cannot
189: * be null.
190: * @exception IllegalStateException if an attempt is made to access
191: * session data after the session has been invalidated
192: */
1.1 bmahe 193: public void putValue(String name, Object value)
194: {
195: if (!isValid)
196: throw new IllegalStateException("Invalid session");
197: removeValue(name);
198: values.put(name, value);
199: if (value instanceof HttpSessionBindingListener)
200: valueBound((HttpSessionBindingListener)value, name);
201: }
202:
1.5 bmahe 203: /**
204: * Removes the object bound to the given name in the session's application
205: * layer data. Does nothing if there is no object bound to the
206: * given name. The value that implements the HttpSessionBindingListener
207: * interface will call its valueUnbound() method.
208: * @param name - the name of the object to remove
209: * @exception IllegalStateException if an attempt is made to access
210: * session data after the session has been invalidated
211: */
1.1 bmahe 212: public void removeValue(String name) {
213: if (!isValid)
214: throw new IllegalStateException("Invalid session");
215: Object value = values.get(name);
216: if (value != null) {
217: values.remove(name);
218: if (value instanceof HttpSessionBindingListener)
219: valueUnbound((HttpSessionBindingListener)value, name);
220: }
221: }
222:
223: protected void valueBound(HttpSessionBindingListener value, String name)
224: {
225: value.valueBound(new HttpSessionBindingEvent(this, name));
226: }
227:
228: protected void valueUnbound(HttpSessionBindingListener value, String name)
229: {
230: value.valueUnbound(new HttpSessionBindingEvent(this, name));
231: }
232:
1.5 bmahe 233: /**
234: * Returns an array of the names of all the application layer data objects
235: * bound into the session. For example, if you want to delete
236: * all of the data objects bound into the session, use this method to
237: * obtain their names.
238: * @return an array containing the names of all of the application layer
239: * data objects bound into the session
240: * @exception IllegalStateException if an attempt is made to access
241: * session data after the session has been invalidated
242: */
1.1 bmahe 243: public String[] getValueNames() {
244: if (!isValid)
245: throw new IllegalStateException("Invalid session");
246: String names[] = new String[values.size()];
247: Enumeration enum = values.keys();
248: int i = 0;
249: while (enum.hasMoreElements())
250: names[i++] = (String)enum.nextElement();
251: return names;
252: }
253:
1.5 bmahe 254: /**
255: * A session is considered to be "new" if it has been created by the
256: * server, but the client has not yet acknowledged joining the
257: * session. For example, if the server supported only cookie-based
258: * sessions and the client had completely disabled the use of
259: * cookies, then calls to HttpServletRequest.getSession() would always
260: * return "new" sessions.
261: * @return true if the session has been created by the server but the
262: * client has not yet acknowledged joining the session; false otherwise
263: * @exception IllegalStateException if an attempt is made to access
264: * session data after the session has been invalidated
265: */
1.1 bmahe 266: public boolean isNew() {
267: return isNew;
1.2 bmahe 268: }
269:
1.4 bmahe 270: protected void setNoMoreNew() {
1.2 bmahe 271: isNew = false;
1.1 bmahe 272: }
273:
1.5 bmahe 274: protected boolean isValid() {
1.1 bmahe 275: return isValid;
276: }
277:
1.5 bmahe 278: protected Cookie getCookie() {
1.1 bmahe 279: return cookie;
1.6 bmahe 280: }
281:
282: //jsdk2.1
283:
284: public void setMaxInactiveInterval(int interval) {
285: maxidle = interval;
286: }
287:
288: public int getMaxInactiveInterval() {
289: return maxidle;
1.1 bmahe 290: }
291:
292: public JigsawHttpSession(JigsawHttpSessionContext context, Cookie cookie) {
293: this.values = new Hashtable();
294: this.creationTime = System.currentTimeMillis();
295: this.lastAccessedTime = creationTime;
296: this.sc = context;
297: this.id = context.addSession(this);
298: this.cookie = cookie;
299: cookie.setValue(this.id);
300: isValid = true;
301: isNew = true;
302: }
303:
304: }
Webmaster