001: package com.meterware.servletunit;
002:
003: /********************************************************************************************************************
004: * $Id: ServletUnitHttpSession.java,v 1.12 2004/09/23 22:48:19 russgold Exp $
005: *
006: * Copyright (c) 2000-2004 Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.util.Date;
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026: import java.net.URL;
027:
028: import javax.servlet.http.HttpSession;
029: import javax.servlet.http.HttpSessionContext;
030: import javax.servlet.ServletContext;
031:
032: class ServletUnitHttpSession implements HttpSession {
033:
034: final static public String SESSION_COOKIE_NAME = "JSESSION";
035:
036: private ServletContext _servletContext;
037: private SessionListenerDispatcher _listenerDispatcher;
038:
039: ServletUnitHttpSession(ServletContext servletContext,
040: SessionListenerDispatcher listenerDispatcher) {
041: _servletContext = servletContext;
042: _listenerDispatcher = listenerDispatcher;
043: }
044:
045: /**
046: * Returns the maximum time interval, in seconds, that the servlet engine will keep this session open
047: * between client requests. You can set the maximum time interval with the setMaxInactiveInterval method.
048: **/
049: public int getMaxInactiveInterval() {
050: if (_invalid)
051: throw new IllegalStateException();
052: return _maxInactiveInterval;
053: }
054:
055: /**
056: * Specifies the maximum length of time, in seconds, that the servlet engine keeps this session
057: * if no user requests have been made of the session.
058: **/
059: public void setMaxInactiveInterval(int interval) {
060: if (_invalid)
061: throw new IllegalStateException();
062: _maxInactiveInterval = interval;
063: }
064:
065: /**
066: * Returns a string containing the unique identifier assigned to this session.
067: * The identifier is assigned by the servlet engine and is implementation dependent.
068: **/
069: public String getId() {
070: if (_invalid)
071: throw new IllegalStateException();
072: return _id;
073: }
074:
075: /**
076: * Returns the time when this session was created, measured
077: * in milliseconds since midnight January 1, 1970 GMT.
078: *
079: * @exception IllegalStateException if you attempt to get the session's
080: * creation time after the session has
081: * been invalidated
082: **/
083: public long getCreationTime() {
084: if (_invalid)
085: throw new IllegalStateException();
086: return _creationTime;
087: }
088:
089: /**
090: * Returns the last time the client sent a request associated with this session,
091: * as the number of milliseconds since midnight January 1, 1970 GMT.
092: **/
093: public long getLastAccessedTime() {
094: if (_invalid)
095: throw new IllegalStateException();
096: return _lastAccessedTime;
097: }
098:
099: /**
100: * Returns true if the Web server has created a session but the client
101: * has not yet joined. For example, if the server used only
102: * cookie-based sessions, and the client had disabled the use of cookies,
103: * then a session would be new.
104: **/
105: public boolean isNew() {
106: return _isNew;
107: }
108:
109: /**
110: * Invalidates this session and unbinds any objects bound to it.
111: **/
112: public void invalidate() {
113: _listenerDispatcher.sendSessionDestroyed(this );
114: _invalid = true;
115: _values.clear();
116: }
117:
118: /**
119: * @deprecated no replacement.
120: **/
121: public HttpSessionContext getSessionContext() {
122: return null;
123: }
124:
125: /**
126: * @deprecated as of JSDK 2.2, use getAttribute
127: **/
128: public Object getValue(String name) {
129: return getAttribute(name);
130: }
131:
132: /**
133: * @deprecated as of JSDK 2.2, use setAttribute
134: **/
135: public void putValue(String name, Object value) {
136: setAttribute(name, value);
137: }
138:
139: /**
140: * @deprecated as of JSDK 2.2, use removeAttribute
141: **/
142: public void removeValue(String name) {
143: removeAttribute(name);
144: }
145:
146: /**
147: * @deprecated as of JSDK 2.2, use getAttributeNames.
148: **/
149: public String[] getValueNames() {
150: if (_invalid)
151: throw new IllegalStateException();
152: return (String[]) _values.keySet().toArray(
153: new String[_values.size()]);
154: }
155:
156: /**
157: * Returns the object bound with the specified name in this session or null if no object of that name exists.
158: **/
159: public Object getAttribute(String name) {
160: if (_invalid)
161: throw new IllegalStateException();
162: return _values.get(name);
163: }
164:
165: /**
166: * Binds an object to this session, using the name specified. If an object of the same name
167: * is already bound to the session, the object is replaced.
168: **/
169: public void setAttribute(String name, Object value) {
170: if (_invalid)
171: throw new IllegalStateException();
172:
173: if (value == null) {
174: removeAttribute(name);
175: } else if (!_values.containsKey(name)) {
176: _values.put(name, value);
177: _listenerDispatcher.sendAttributeAdded(this , name, value);
178: } else {
179: Object oldValue = _values.get(name);
180: _values.put(name, value);
181: _listenerDispatcher.sendAttributeReplaced(this , name,
182: oldValue);
183: }
184: }
185:
186: /**
187: * Removes the object bound with the specified name from this session. If the session does not
188: * have an object bound with the specified name, this method does nothing.
189: **/
190: public void removeAttribute(String name) {
191: if (_invalid)
192: throw new IllegalStateException();
193: if (_values.containsKey(name)) {
194: Object oldValue = _values.get(name);
195: _values.remove(name);
196: _listenerDispatcher.sendAttributeRemoved(this , name,
197: oldValue);
198: }
199: }
200:
201: /**
202: * Returns an array containing the names of all the objects bound to this session.
203: * This method is useful, for example, when you want to delete all the objects bound to this session.
204: **/
205: public Enumeration getAttributeNames() {
206: if (_invalid)
207: throw new IllegalStateException();
208: return _values.keys();
209: }
210:
211: //---------------------------- methods added to HttpSession in JSDK 2.3 ----------------------------------------
212:
213: /**
214: * Returns the ServletContext to which this session belongs.
215: *
216: * @since 1.3
217: **/
218: public ServletContext getServletContext() {
219: return _servletContext;
220: }
221:
222: //-------------------------------------------- package members -------------------------------------------------
223:
224: /**
225: * This method should be invoked when a servlet joins an existing session. It will update the last access time
226: * and mark the session as no longer new.
227: **/
228: void access() {
229: _lastAccessedTime = new Date().getTime();
230: _isNew = false;
231: }
232:
233: URL getOriginalURL() {
234: return _originalURL;
235: }
236:
237: void setOriginalURL(URL originalURL) {
238: _originalURL = originalURL;
239: }
240:
241: /**
242: * Sets the authenticated user information for a session.
243: *
244: * @param userName the name the user supplied when logging in
245: * @param roles an array of role names assigned to the user
246: **/
247: void setUserInformation(String userName, String[] roles) {
248: _userName = userName;
249: _roles = roles;
250: }
251:
252: String getUserName() {
253: return _userName;
254: }
255:
256: String[] getRoles() {
257: return _roles;
258: }
259:
260: boolean isInvalid() {
261: return _invalid;
262: }
263:
264: //------------------------------------- private members ---------------------------------------
265:
266: private static int _NextID = 1;
267:
268: private final long _creationTime = new Date().getTime();
269:
270: private final String _id = Integer.toString(_NextID++);
271:
272: private int _maxInactiveInterval;
273:
274: private long _lastAccessedTime = new Date().getTime();
275:
276: private boolean _invalid;
277:
278: private Hashtable _values = new Hashtable();
279:
280: private boolean _isNew = true;
281:
282: private String _userName;
283:
284: private String[] _roles;
285:
286: private URL _originalURL;
287:
288: }
|