Revision 5041
Added by daigle over 14 years ago
SessionService.java | ||
---|---|---|
26 | 26 |
|
27 | 27 |
package edu.ucsb.nceas.metacat.service; |
28 | 28 |
|
29 |
import java.io.PrintWriter; |
|
30 |
import java.util.Calendar; |
|
29 | 31 |
import java.util.Hashtable; |
30 | 32 |
|
33 |
import javax.servlet.http.HttpServletResponse; |
|
34 |
|
|
31 | 35 |
import org.apache.log4j.Logger; |
32 | 36 |
|
37 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
|
33 | 38 |
import edu.ucsb.nceas.metacat.shared.BaseService; |
34 | 39 |
import edu.ucsb.nceas.metacat.shared.ServiceException; |
35 | 40 |
import edu.ucsb.nceas.metacat.util.SessionData; |
41 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
36 | 42 |
|
37 | 43 |
public class SessionService extends BaseService { |
38 | 44 |
|
39 | 45 |
private static SessionService sessionService = null; |
46 |
private static int sessionTimeoutMinutes; |
|
40 | 47 |
|
41 | 48 |
private static Logger logMetacat = Logger.getLogger(SessionService.class); |
42 | 49 |
private static Hashtable<String, SessionData> sessionHash = null; |
... | ... | |
49 | 56 |
private SessionService() throws ServiceException { |
50 | 57 |
_serviceName = "SessionService"; |
51 | 58 |
|
52 |
sessionHash = new Hashtable<String, SessionData>(); |
|
53 |
logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID); |
|
54 |
registerSession(PUBLIC_SESSION_ID, "public", null, null); |
|
59 |
String sessionTimeoutStr = null; |
|
60 |
try { |
|
61 |
sessionHash = new Hashtable<String, SessionData>(); |
|
62 |
sessionTimeoutStr = PropertyService.getProperty("session.timeoutMinutes"); |
|
63 |
sessionTimeoutMinutes = Integer.parseInt(sessionTimeoutStr); |
|
64 |
|
|
65 |
logMetacat.debug("SessionService() - Registering public session id: " + |
|
66 |
PUBLIC_SESSION_ID); |
|
67 |
registerSession(PUBLIC_SESSION_ID, "public", null, null); |
|
68 |
} catch (PropertyNotFoundException pnfe) { |
|
69 |
throw new ServiceException("SessionService() - Error getting property: " + |
|
70 |
pnfe.getMessage()); |
|
71 |
} catch (NumberFormatException nfe) { |
|
72 |
throw new ServiceException("SessionService() - Error parsing session timeout minutes: " + |
|
73 |
sessionTimeoutStr); |
|
74 |
} |
|
55 | 75 |
} |
56 | 76 |
|
57 | 77 |
/** |
... | ... | |
94 | 114 |
public static void registerSession(String sessionId, String userName, |
95 | 115 |
String[] groupNames, String password) throws ServiceException { |
96 | 116 |
if (sessionId == null) { |
97 |
throw new ServiceException("Cannot register a null session id"); |
|
117 |
throw new ServiceException("SessionService.registerSession - " + |
|
118 |
"Cannot register a null session id"); |
|
98 | 119 |
} |
99 |
logMetacat.debug("Registering session id: " + sessionId); |
|
120 |
logMetacat.debug("SessionService.registerSession - Registering session id: " + sessionId);
|
|
100 | 121 |
SessionData sessionData = new SessionData(sessionId, userName, groupNames, |
101 | 122 |
password); |
102 | 123 |
sessionHash.put(sessionId, sessionData); |
... | ... | |
110 | 131 |
*/ |
111 | 132 |
public static void registerSession(SessionData sessionData) throws ServiceException { |
112 | 133 |
if (sessionData == null) { |
113 |
throw new ServiceException("Cannot register null session data"); |
|
134 |
throw new ServiceException("SessionService.registerSession - " + |
|
135 |
"Cannot register null session data"); |
|
114 | 136 |
} |
115 |
logMetacat.debug("Registering session date with id: " + sessionData.getId()); |
|
137 |
logMetacat.debug("SessionService.registerSession - Registering session " + |
|
138 |
"data with id: " + sessionData.getId()); |
|
116 | 139 |
sessionHash.put(sessionData.getId(), sessionData); |
117 | 140 |
} |
118 | 141 |
|
... | ... | |
124 | 147 |
*/ |
125 | 148 |
public static void unRegisterSession(String sessionId) { |
126 | 149 |
if (sessionId != null) { |
127 |
logMetacat.error("trying to unregister a session with null id"); |
|
150 |
logMetacat.error("SessionService.unRegisterSession - trying to " + |
|
151 |
"unregister a session with null id"); |
|
128 | 152 |
sessionHash.remove(sessionId); |
129 | 153 |
} |
130 | 154 |
} |
131 | 155 |
|
132 | 156 |
/** |
133 | 157 |
* Check if a session is registered in the session hash table. |
134 |
* TODO MCD need to time sessions out |
|
135 | 158 |
* |
136 | 159 |
* @param sessionId |
137 | 160 |
* the id of the session to look for. |
138 | 161 |
*/ |
139 |
public static boolean isSessionRegistered(String sessionId) { |
|
162 |
public static boolean isSessionRegistered(String sessionId) {
|
|
140 | 163 |
if (sessionId == null) { |
141 |
logMetacat.error("trying to check if a session with null id is registered"); |
|
164 |
logMetacat.error("SessionService.isSessionRegistered - trying to check if a " + |
|
165 |
"session with null id is registered"); |
|
142 | 166 |
return false; |
143 | 167 |
} |
168 |
|
|
169 |
checkTimeout(sessionId); |
|
170 |
|
|
144 | 171 |
return sessionHash.containsKey(sessionId); |
145 | 172 |
} |
146 | 173 |
|
147 | 174 |
/** |
175 |
* Check if a session is registered in the session hash table. Write results |
|
176 |
* in XML format to output. |
|
177 |
* |
|
178 |
* @param out |
|
179 |
* the output stream to write to. |
|
180 |
* @param sessionId |
|
181 |
* the id of the session to look for. |
|
182 |
*/ |
|
183 |
public static void validateSession(PrintWriter out, HttpServletResponse response, |
|
184 |
String sessionId) { |
|
185 |
|
|
186 |
response.setContentType("text/xml"); |
|
187 |
out.println("<?xml version=\"1.0\"?>"); |
|
188 |
out.write("<validateSession><status>"); |
|
189 |
if (sessionId != null && isSessionRegistered(sessionId)) { |
|
190 |
out.write("valid"); |
|
191 |
} else { |
|
192 |
out.write("invalid"); |
|
193 |
} |
|
194 |
out.write("</status>"); |
|
195 |
out.write("<sessionId>" + sessionId + "</sessionId></validateSession>"); |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
148 | 199 |
* Get a registered session from the session hash table. |
149 | 200 |
* TODO MCD need to time sessions out |
150 | 201 |
* |
... | ... | |
153 | 204 |
*/ |
154 | 205 |
public static SessionData getRegisteredSession(String sessionId) { |
155 | 206 |
if (sessionId == null) { |
156 |
logMetacat.error("trying to get a session with null id"); |
|
207 |
logMetacat.error("SessionService.getRegisteredSession - trying to get a session with null id");
|
|
157 | 208 |
return null; |
158 | 209 |
} |
210 |
checkTimeout(sessionId); |
|
211 |
|
|
159 | 212 |
return sessionHash.get(sessionId); |
160 | 213 |
} |
161 | 214 |
|
... | ... | |
174 | 227 |
*/ |
175 | 228 |
public static synchronized void touchSession(String sessionId) { |
176 | 229 |
if (sessionId == null) { |
177 |
logMetacat.error("trying to touch a session with null id"); |
|
230 |
logMetacat.error("SessionService.touchSession - trying to touch a session with null id");
|
|
178 | 231 |
} else if (isSessionRegistered(sessionId)) { |
179 | 232 |
SessionData sessionData = getRegisteredSession(sessionId); |
180 | 233 |
sessionData.setLastAccessedTime(); |
181 | 234 |
} |
182 | 235 |
} |
236 |
|
|
237 |
private static void checkTimeout (String sessionId) { |
|
238 |
SessionData sessionData = null; |
|
239 |
if ((sessionData = sessionHash.get(sessionId)) != null) { |
|
240 |
Calendar expireTime = Calendar.getInstance(); |
|
241 |
Calendar lastAccessedTime = sessionData.getLastAccessedTime(); |
|
242 |
expireTime.add(Calendar.MINUTE, 0 - sessionTimeoutMinutes); |
|
243 |
if(lastAccessedTime.compareTo(expireTime) < 0 ) { |
|
244 |
unRegisterSession(sessionId); |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
|
|
249 |
} |
|
183 | 250 |
|
184 | 251 |
} |
Also available in: Unified diff
Add session Validation action and session timeout functionality.