Revision 4297
Added by daigle over 16 years ago
src/edu/ucsb/nceas/metacat/service/SessionService.java | ||
---|---|---|
42 | 42 |
private static Logger logMetacat = Logger.getLogger(SessionService.class); |
43 | 43 |
private static Hashtable<String, SessionData> sessionHash = null; |
44 | 44 |
|
45 |
public static final String PUBLIC_SESSION_ID = "0";
|
|
45 |
private static final String PUBLIC_SESSION_ID = "0";
|
|
46 | 46 |
|
47 | 47 |
/** |
48 | 48 |
* private constructor since this is a singleton |
... | ... | |
63 | 63 |
} |
64 | 64 |
return sessionService; |
65 | 65 |
} |
66 |
|
|
66 |
|
|
67 |
/** |
|
68 |
* Register a session in the session hash table. This uses |
|
69 |
* the parameters passed in to create a SessionData object. |
|
70 |
* |
|
71 |
* @param sessionId |
|
72 |
* the ID of the session to register |
|
73 |
* @param userName |
|
74 |
* the user name of the session |
|
75 |
* @param groupNames |
|
76 |
* the group names for the session |
|
77 |
* @param password |
|
78 |
* the password for the session |
|
79 |
*/ |
|
67 | 80 |
public static void registerSession(String sessionId, String userName, |
68 | 81 |
String[] groupNames, String password) { |
69 | 82 |
SessionData sessionData = new SessionData(sessionId, userName, groupNames, |
... | ... | |
71 | 84 |
sessionHash.put(sessionId, sessionData); |
72 | 85 |
} |
73 | 86 |
|
87 |
/** |
|
88 |
* Register a session in the session hash table. |
|
89 |
* |
|
90 |
* @param sessionData |
|
91 |
* the session data object to add to the session hash |
|
92 |
*/ |
|
74 | 93 |
public static void registerSession(SessionData sessionData) { |
75 | 94 |
sessionHash.put(sessionData.getId(), sessionData); |
76 | 95 |
} |
77 | 96 |
|
97 |
/** |
|
98 |
* Unregister a session from the session hash table. |
|
99 |
* |
|
100 |
* @param sessionId |
|
101 |
* the id of the session to remove. |
|
102 |
*/ |
|
78 | 103 |
public static void unRegisterSession(String sessionId) { |
79 | 104 |
sessionHash.remove(sessionId); |
80 | 105 |
} |
81 | 106 |
|
107 |
/** |
|
108 |
* Check if a session is registered in the session hash table. |
|
109 |
* TODO MCD need to time sessions out |
|
110 |
* |
|
111 |
* @param sessionId |
|
112 |
* the id of the session to look for. |
|
113 |
*/ |
|
82 | 114 |
public static boolean isSessionRegistered(String sessionId) { |
83 | 115 |
return sessionHash.containsKey(sessionId); |
84 | 116 |
} |
85 | 117 |
|
118 |
/** |
|
119 |
* Get a registered session from the session hash table. |
|
120 |
* TODO MCD need to time sessions out |
|
121 |
* |
|
122 |
* @param sessionId |
|
123 |
* the id of the session to retrieve. |
|
124 |
*/ |
|
86 | 125 |
public static SessionData getRegisteredSession(String sessionId) { |
87 | 126 |
return sessionHash.get(sessionId); |
88 | 127 |
} |
89 | 128 |
|
90 |
public static String getSessionIDFromRequest(HttpServletRequest request) { |
|
91 |
String sessionId = null; |
|
92 |
Cookie[] cookies = request.getCookies(); |
|
93 |
|
|
94 |
if (cookies == null) { |
|
95 |
return null; |
|
96 |
} |
|
97 |
|
|
98 |
for (int i = 0; i < cookies.length; i++) { |
|
99 |
if (cookies[i].getName().equals("JSESSIONID")) { |
|
100 |
sessionId = cookies[i].getValue(); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
logMetacat.debug("Request session ID: " + sessionId); |
|
105 |
return sessionId; |
|
129 |
/** |
|
130 |
* Get the public session from the session hash table. |
|
131 |
*/ |
|
132 |
public static SessionData getPublicSession() { |
|
133 |
return sessionHash.get(PUBLIC_SESSION_ID); |
|
106 | 134 |
} |
107 | 135 |
|
136 |
/** |
|
137 |
* Keep a session active by updating its last accessed time. |
|
138 |
* |
|
139 |
* @param sessionId |
|
140 |
* the id of the session to update. |
|
141 |
*/ |
|
108 | 142 |
public static synchronized void touchSession(String sessionId) { |
109 | 143 |
if (isSessionRegistered(sessionId)) { |
110 | 144 |
SessionData sessionData = getRegisteredSession(sessionId); |
Also available in: Unified diff
Add comments. Added getPublicSession method