KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > servlet > HttpSessionFacade


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.servlet;
50
51 import java.util.Enumeration JavaDoc;
52 import java.util.Vector JavaDoc;
53
54 import javax.servlet.ServletContext JavaDoc;
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpSession JavaDoc;
57 import javax.servlet.http.HttpSessionContext JavaDoc;
58
59 /**
60  * Implementation of the javax.servlet.http.HttpSession interface which provides a request-only session object. This
61  * session object will only create a real HttpSession object if that object is actually required.
62  *
63  * @author Anthony Eden
64  */

65
66 public class HttpSessionFacade implements HttpSession JavaDoc {
67
68     private static final Vector JavaDoc EMPTY_VECTOR = new Vector JavaDoc();
69
70     private HttpServletRequest JavaDoc request = null;
71
72     /**
73      * Construct a new HttpSessionWrapper.
74      */

75
76     public HttpSessionFacade(HttpServletRequest JavaDoc request) {
77         this.request = request;
78     }
79
80     /**
81      * Get the creation time for the session.
82      *
83      * <p>If the session has not yet been created then this method will return -1.</p>
84      *
85      * @return The creation time or -1
86      */

87
88     public long getCreationTime() {
89         HttpSession JavaDoc session = request.getSession(false);
90         if (session != null) {
91             return session.getCreationTime();
92         } else {
93             return -1;
94         }
95     }
96
97     /**
98      * Get the unique ID for the session.
99      *
100      * <p>If the session has not yet been created then this method will return null.</p>
101      *
102      * @return The ServletContext or null
103      */

104
105     public String JavaDoc getId() {
106         HttpSession JavaDoc session = request.getSession(false);
107         if (session != null) {
108             return session.getId();
109         } else {
110             return null;
111         }
112     }
113
114     /**
115      * Returns the last time the client sent a request associated with this session, as the number of milliseconds since
116      * midnight January 1, 1970 GMT, and marked by the time the container recieved the request.
117      *
118      * <p>If the session has not yet been created then this method will return -1.</p>
119      *
120      * @return The last accessed time
121      */

122
123     public long getLastAccessedTime() {
124         HttpSession JavaDoc session = request.getSession(false);
125         if (session != null) {
126             return session.getLastAccessedTime();
127         } else {
128             return -1;
129         }
130     }
131
132     /**
133      * Get the ServletContext which the session belongs to.
134      *
135      * @return The ServletContext or null
136      */

137
138     public ServletContext JavaDoc getServletContext() {
139         return request.getSession().getServletContext();
140     }
141
142     /**
143      * Get the maxium inactive interval.
144      *
145      * @return The maximum inactive interval
146      */

147
148     public int getMaxInactiveInterval() {
149         return request.getSession().getMaxInactiveInterval();
150     }
151
152     /**
153      * Set the maxium inactive interval.
154      *
155      * @param maxInactiveInterval The maximum inactive interval
156      */

157
158     public void setMaxInactiveInterval(int maxInactiveInterval) {
159         request.getSession().setMaxInactiveInterval(maxInactiveInterval);
160     }
161
162     /**
163      * Deprecated - will be removed.
164      *
165      * @deprecated
166      */

167
168     public HttpSessionContext JavaDoc getSessionContext() {
169         return request.getSession().getSessionContext();
170     }
171
172     /**
173      * Returns the object bound with the specified name in this session, or null if no object is bound under the name.
174      *
175      * <p>This implementation will not create a session object, it will only look in a session if the session already
176      * exists. This is possible because if the session doesn't yet exist then it is not possible for the attribute to
177      * exist.</p>
178      *
179      * @param name The attribute name
180      * @return The value or null
181      */

182
183     public Object JavaDoc getAttribute(String JavaDoc name) {
184         HttpSession JavaDoc session = request.getSession(false);
185         if (session == null) {
186             return null;
187         } else {
188             return session.getAttribute(name);
189         }
190     }
191
192     /**
193      * Binds an object to this session, using the name specified. If an object of the same name is already bound to the
194      * session, the object is replaced.
195      *
196      * @param name The attribute name
197      * @param value The new value
198      */

199
200     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
201         request.getSession().setAttribute(name, value);
202     }
203
204     /**
205      * Removes the object bound with the specified name from this session. If the session does not have an object bound
206      * with the specified name, this method does nothing.
207      *
208      * <p>This implementation will not create a session object, it will only look in a session if the session already
209      * exists. This is possible because if the session doesn't yet exist then it is not possible for the attribute to
210      * exist.</p>
211      *
212      * @param name The name of the attribute to remove
213      */

214
215     public void removeAttribute(String JavaDoc name) {
216         HttpSession JavaDoc session = request.getSession(false);
217         if (session != null) {
218             session.removeAttribute(name);
219         }
220     }
221
222     /**
223      * Returns an Enumeration of String objects containing the names of all the objects bound to this session.
224      *
225      * <p>This implementation will not create a session object, it will only look in a session if the session already
226      * exists. This is possible because if the session doesn't yet exist then it is not possible for any attributes to
227      * exist. In this case an empty Enumeration is returned.</p>
228      *
229      * @return An Enumeration of attribute names
230      */

231
232     public Enumeration JavaDoc getAttributeNames() {
233         HttpSession JavaDoc session = request.getSession(false);
234         if (session == null) {
235             return EMPTY_VECTOR.elements();
236         } else {
237             return session.getAttributeNames();
238         }
239     }
240
241     /**
242      * Deprecated - use <code>getAttribute()</code>.
243      *
244      * @deprecated
245      */

246
247     public Object JavaDoc getValue(String JavaDoc name) {
248         return getAttribute(name);
249     }
250
251     /**
252      * Deprecated - Use <code>setAttribute()</code>.
253      *
254      * @deprecated
255      */

256
257     public void putValue(String JavaDoc name, Object JavaDoc value) {
258         setAttribute(name, value);
259     }
260
261     /**
262      * Deprecated - Use <code>removeAttribute()</code>.
263      *
264      * @deprecated
265      */

266
267     public void removeValue(String JavaDoc name) {
268         removeAttribute(name);
269     }
270
271     /**
272      * Deprecated - Use <code>getAttributeNames()</code>.
273      *
274      * @deprecated
275      */

276
277     public String JavaDoc[] getValueNames() {
278         HttpSession JavaDoc session = request.getSession(false);
279         if (session == null) {
280             return new String JavaDoc[0];
281         } else {
282             return session.getValueNames();
283         }
284     }
285
286     /**
287      * Invalidates this session then unbinds any objects bound to it.
288      */

289
290     public void invalidate() {
291         request.getSession().invalidate();
292     }
293
294     /**
295      * Returns true if the client does not yet know about the session or if the client chooses not to join the session.
296      * For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then
297      * a session would be new on each request.
298      *
299      * @return true if the server has created a session, but the client has not yet joined
300      */

301
302     public boolean isNew() {
303         return request.getSession().isNew();
304     }
305
306 }
307
Popular Tags