Project

General

Profile

« Previous | Next » 

Revision 4780

Added by daigle about 15 years ago

Handle sessions with null ids gracefully.

View differences:

SessionService.java
44 44
	/**
45 45
	 * private constructor since this is a singleton
46 46
	 */
47
	private SessionService() {
47
	private SessionService() throws ServiceException {
48 48
		sessionHash = new Hashtable<String, SessionData>();
49
		logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
49 50
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
50 51
	}
51 52
	
......
54 55
	 * 
55 56
	 * @return the single instance of SessionService
56 57
	 */
57
	public static SessionService getInstance() {
58
	public static SessionService getInstance() throws ServiceException {
58 59
		if (sessionService == null) {
59 60
			sessionService = new SessionService();
60 61
		}
......
83 84
	 *            the password for the session
84 85
	 */
85 86
	public static void registerSession(String sessionId, String userName,
86
			String[] groupNames, String password) {
87
			String[] groupNames, String password) throws ServiceException {
88
		if (sessionId == null) {
89
			throw new ServiceException("Cannot register a null session id");
90
		}
91
		logMetacat.debug("Registering session id: " + sessionId);
87 92
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
88 93
				password);
89 94
		sessionHash.put(sessionId, sessionData);
......
95 100
	 * @param sessionData
96 101
	 *            the session data object to add to the session hash
97 102
	 */
98
	public static void registerSession(SessionData sessionData) {
103
	public static void registerSession(SessionData sessionData) throws ServiceException {
104
		if (sessionData == null) {
105
			throw new ServiceException("Cannot register null session data");
106
		}
107
		logMetacat.debug("Registering session date with id: " + sessionData.getId());
99 108
		sessionHash.put(sessionData.getId(), sessionData);
100 109
	}
101 110
	
......
106 115
	 *            the id of the session to remove.
107 116
	 */
108 117
	public static void unRegisterSession(String sessionId) {
109
		sessionHash.remove(sessionId);
118
		if (sessionId != null) {
119
			logMetacat.error("trying to unregister a session with null id");
120
			sessionHash.remove(sessionId);
121
		}
110 122
	}
111 123
	
112 124
	/**
......
117 129
	 *            the id of the session to look for.
118 130
	 */
119 131
	public static boolean isSessionRegistered(String sessionId) {
132
		if (sessionId == null) {
133
			logMetacat.error("trying to check if a session with null id is registered");
134
			return false;
135
		}
120 136
		return sessionHash.containsKey(sessionId);
121 137
	}
122 138
	
......
128 144
	 *            the id of the session to retrieve.
129 145
	 */
130 146
	public static SessionData getRegisteredSession(String sessionId) {
147
		if (sessionId == null) {
148
			logMetacat.error("trying to get a session with null id");
149
			return null;
150
		}
131 151
		return sessionHash.get(sessionId);
132 152
	}
133 153
	
......
145 165
	 *            the id of the session to update.
146 166
	 */
147 167
	public static synchronized void touchSession(String sessionId) {
148
		if (sessionId != null && isSessionRegistered(sessionId)) {
168
		if (sessionId == null) {
169
			logMetacat.error("trying to touch a session with null id");
170
		} else if (isSessionRegistered(sessionId)) {
149 171
			SessionData sessionData = getRegisteredSession(sessionId);
150 172
			sessionData.setLastAccessedTime();
151 173
		}

Also available in: Unified diff