KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > WebSessionUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: WebSessionUtils.java,v 1.4 2007/01/07 06:14:09 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.www;
23
24 import java.security.Principal JavaDoc;
25
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 /**
29  * Collection of useful methods to work with sessions objects.
30  *
31  * @version $Id: WebSessionUtils.java,v 1.4 2007/01/07 06:14:09 bastafidli Exp $
32  * @author Miro Halas
33  * @code.reviewer Miro Halas
34  * @code.reviewed Initial revision
35  */

36 public final class WebSessionUtils
37 {
38    // Constants ////////////////////////////////////////////////////////////////
39

40    /**
41     * Name of the attribute in the session containing the internal session id.
42     * If it exists, then user is logged in.
43     */

44    public static final String JavaDoc INTERNAL_SESSIONID_SESSION_PARAM
45                                              = "oss.internal.sessionid";
46
47    /**
48     * Identification of logged in user.
49     */

50    public static final String JavaDoc LOGGEDIN_USERID_SESSION_PARAM
51                                              = "oss.loggedin.userid";
52
53    /**
54     * Object which tracks when the session is invalidated by the web container.
55     */

56    public static final String JavaDoc HTTP_SESSION_TRACKER
57                                              = "oss.session.tracker";
58
59    // Constructors /////////////////////////////////////////////////////////////
60

61    /**
62     * Private constructor since this class cannot be instantiated
63     */

64    private WebSessionUtils(
65    )
66    {
67       // Do nothing
68
}
69
70    // Public methods ///////////////////////////////////////////////////////////
71

72    /**
73     * If the user has logged in, this method will return true.
74     *
75     * @param hsSession - HTTP session object, can be null
76     * @return boolean
77     */

78    public static boolean isLoggedIn(
79       HttpSession JavaDoc hsSession
80    )
81    {
82       return (getSessionId(hsSession) != null);
83    }
84
85    /**
86     * If the user has logged in, this method will return information about who
87     * is logged in.
88     *
89     * @param hsSession - HTTP session object, can be null
90     * @return Object - identication of user who is logged in or null if nobody
91     * is logged in
92     */

93    public static Principal JavaDoc getLoggedInUserInfo(
94       HttpSession JavaDoc hsSession
95    )
96    {
97       Principal JavaDoc credentials = null;
98       
99       if (hsSession != null)
100       {
101          credentials = (Principal JavaDoc)hsSession.getAttribute(LOGGEDIN_USERID_SESSION_PARAM);
102       }
103       
104       return credentials;
105    }
106    
107    /**
108     * Get identifier of current user session.
109     *
110     * @param hsSession - current HTTP session user is using
111     * @return String - user session identifier or null if no user is logged in
112     */

113    public static String JavaDoc getSessionId(
114       HttpSession JavaDoc hsSession
115    )
116    {
117       String JavaDoc sessionId = null;
118       
119       if (hsSession != null)
120       {
121          sessionId = (String JavaDoc)hsSession.getAttribute(INTERNAL_SESSIONID_SESSION_PARAM);
122       }
123
124       return sessionId;
125    }
126
127    /**
128     * Set identifier of current user session and user information
129     *
130     * @param hsSession - current HTTP session user is using
131     * @param sessionId - user session identifier
132     * @param credentials - credentials for user
133     */

134    public static void setSessionAndUserInfo(
135       HttpSession JavaDoc hsSession,
136       String JavaDoc sessionId,
137       Principal JavaDoc credentials
138    )
139    {
140       if (hsSession != null)
141       {
142          hsSession.setAttribute(INTERNAL_SESSIONID_SESSION_PARAM, sessionId);
143          hsSession.setAttribute(LOGGEDIN_USERID_SESSION_PARAM, credentials);
144       }
145    }
146
147    /**
148     * Reset identifier of current user session and user information. If the user
149     * is still logged in, this will expire his server session.
150     *
151     * @param hsSession - current HTTP session user is using
152     */

153    public static void resetSessionAndUserInfo(
154       HttpSession JavaDoc hsSession
155    )
156    {
157       hsSession.removeAttribute(INTERNAL_SESSIONID_SESSION_PARAM);
158       hsSession.removeAttribute(LOGGEDIN_USERID_SESSION_PARAM);
159
160       // When this object is removed, the session tracker will remove the server
161
// session
162
hsSession.removeAttribute(HTTP_SESSION_TRACKER);
163    }
164 }
165
Popular Tags