Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements session utility methods 
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-10-02 12:59:59 -0700 (Fri, 02 Oct 2009) $'
10
 * '$Revision: 5070 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.service;
28

    
29
import java.io.PrintWriter;
30
import java.util.Calendar;
31
import java.util.Hashtable;
32

    
33
import javax.servlet.http.HttpServletResponse;
34

    
35
import org.apache.log4j.Logger;
36

    
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38
import edu.ucsb.nceas.metacat.shared.BaseService;
39
import edu.ucsb.nceas.metacat.shared.ServiceException;
40
import edu.ucsb.nceas.metacat.util.SessionData;
41
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
42

    
43
public class SessionService extends BaseService {
44
	
45
	private static SessionService sessionService = null;
46
	private static int sessionTimeoutMinutes;
47
	
48
	private static Logger logMetacat = Logger.getLogger(SessionService.class);
49
	private static Hashtable<String, SessionData> sessionHash = null;
50
	
51
	private static final String PUBLIC_SESSION_ID = "0";
52

    
53
	/**
54
	 * private constructor since this is a singleton
55
	 */
56
	private SessionService() throws ServiceException {		
57
		_serviceName = "SessionService";
58
		
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, "Public User");
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
		}
75
	}
76
	
77
	/**
78
	 * Get the single instance of SessionService.
79
	 * 
80
	 * @return the single instance of SessionService
81
	 */
82
	public static SessionService getInstance() throws ServiceException {
83
		if (sessionService == null) {
84
			sessionService = new SessionService();
85
		}
86
		return sessionService;
87
	}
88
	
89
	public boolean refreshable() {
90
		return false;
91
	}
92
	
93
	public void doRefresh() throws ServiceException {
94
		return;
95
	}
96
	
97
	public void stop() throws ServiceException {
98
		return;
99
	}
100
	
101
	/**
102
	 * Register a session in the session hash table.  This uses
103
	 * the parameters passed in to create a SessionData object.
104
	 * 
105
	 * @param sessionId
106
	 *            the ID of the session to register
107
	 * @param userName
108
	 *            the user name of the session
109
	 * @param groupNames
110
	 *            the group names for the session
111
	 * @param password
112
	 *            the password for the session
113
	 */
114
	public static void registerSession(String sessionId, String userName,
115
			String[] groupNames, String password, String name) throws ServiceException {
116
		if (sessionId == null) {
117
			throw new ServiceException("SessionService.registerSession - " + 
118
					"Cannot register a null session id");
119
		}
120
		logMetacat.debug("SessionService.registerSession - Registering session id: " + sessionId);
121
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
122
				password, name);
123
		sessionHash.put(sessionId, sessionData);
124
	}
125
	
126
	/**
127
	 * Register a session in the session hash table.
128
	 * 
129
	 * @param sessionData
130
	 *            the session data object to add to the session hash
131
	 */
132
	public static void registerSession(SessionData sessionData) throws ServiceException {
133
		if (sessionData == null) {
134
			throw new ServiceException("SessionService.registerSession - " + 
135
					"Cannot register null session data");
136
		}
137
		logMetacat.debug("SessionService.registerSession - Registering session " + 
138
				"data with id: " + sessionData.getId());
139
		sessionHash.put(sessionData.getId(), sessionData);
140
	}
141
	
142
	/**
143
	 * Unregister a session from the session hash table.
144
	 * 
145
	 * @param sessionId
146
	 *            the id of the session to remove.
147
	 */
148
	public static void unRegisterSession(String sessionId) {
149
		if (sessionId == null) {
150
			logMetacat.error("SessionService.unRegisterSession - trying to " + 
151
					"unregister a session with null id");
152
		}
153
		
154
		logMetacat.error("SessionService.unRegisterSession - unRegistering session: " + sessionId);
155
		sessionHash.remove(sessionId);
156
	}
157
	
158
	/**
159
	 * Check if a session is registered in the session hash table. 
160
	 * 
161
	 * @param sessionId
162
	 *            the id of the session to look for.
163
	 */
164
	public static boolean isSessionRegistered(String sessionId) {		
165
		if (sessionId == null) {
166
			logMetacat.error("SessionService.isSessionRegistered - trying to check if a " + 
167
					"session with null id is registered");
168
			return false;
169
		}
170
		
171
		checkTimeout(sessionId);
172
		
173
		return sessionHash.containsKey(sessionId);
174
	}
175
	
176
	/**
177
	 * Check if a session is registered in the session hash table. Write results
178
	 * in XML format to output.
179
	 * 
180
	 * @param out
181
	 *            the output stream to write to.
182
	 * @param sessionId
183
	 *            the id of the session to look for.
184
	 */
185
	public static void validateSession(PrintWriter out, HttpServletResponse response, 
186
			String sessionId) {		
187
		
188
		response.setContentType("text/xml");
189
		out.println("<?xml version=\"1.0\"?>");
190
		out.write("<validateSession><status>");
191
		if (validateSession(sessionId)) {
192
			out.write("valid");
193
		} else {
194
			out.write("invalid");
195
		}
196
		out.write("</status>");
197
		out.write("<sessionId>" + sessionId + "</sessionId></validateSession>");				
198
	}
199
	
200
	/**
201
	 * Check if a session is registered in the session hash table. Return
202
	 * true if the session is valid and false otherwise.
203
	 * 
204
	 * @param sessionId
205
	 *            the id of the session to look for.
206
	 */
207
	public static boolean validateSession(String sessionId) {				
208
		if (sessionId != null && !sessionId.equals(PUBLIC_SESSION_ID) && isSessionRegistered(sessionId)) {
209
			return true;
210
		} else {
211
			return false;
212
		}			
213
	}
214
	
215
	/**
216
	 * Get a registered session from the session hash table. 
217
	 * TODO MCD need to time sessions out
218
	 * 
219
	 * @param sessionId
220
	 *            the id of the session to retrieve.
221
	 */
222
	public static SessionData getRegisteredSession(String sessionId) {
223
		if (sessionId == null) {
224
			logMetacat.error("SessionService.getRegisteredSession - trying to get a session with null id");
225
			return null;
226
		}
227
		checkTimeout(sessionId);
228
		
229
		return sessionHash.get(sessionId);
230
	}
231
	
232
	/**
233
	 * Get the public session from the session hash table. 
234
	 */
235
	public static SessionData getPublicSession() {
236
		return sessionHash.get(PUBLIC_SESSION_ID);
237
	}
238
	
239
	/**
240
	 * Keep a session active by updating its last accessed time. 
241
	 * 
242
	 * @param sessionId
243
	 *            the id of the session to update.
244
	 */
245
	public static synchronized void touchSession(String sessionId) {
246
		if (sessionId == null) {
247
			logMetacat.error("SessionService.touchSession - trying to touch a session with null id");
248
		} else if (isSessionRegistered(sessionId)) {
249
			SessionData sessionData = getRegisteredSession(sessionId);
250
			sessionData.setLastAccessedTime();
251
		}
252
	}
253
	
254
	private static void checkTimeout (String sessionId) {
255
		SessionData sessionData = null;
256
		if ((sessionData = sessionHash.get(sessionId)) != null) {
257
			Calendar expireTime = Calendar.getInstance();
258
			Calendar lastAccessedTime = sessionData.getLastAccessedTime();
259
			expireTime.add(Calendar.MINUTE, 0 - sessionTimeoutMinutes);
260
			if(lastAccessedTime.compareTo(expireTime) < 0 ) {
261
				unRegisterSession(sessionId);
262
			}
263
		}
264
		
265
		
266
	}
267

    
268
}
(2-2/4)