Project

General

Profile

« Previous | Next » 

Revision 4299

Added by daigle about 16 years ago

Added methods to get session data, parameters and cookies from request

View differences:

src/edu/ucsb/nceas/metacat/util/RequestUtil.java
27 27
package edu.ucsb.nceas.metacat.util;
28 28

  
29 29
import java.io.IOException;
30
import java.util.Enumeration;
30 31
import java.util.HashMap;
32
import java.util.Hashtable;
31 33
import java.util.Set;
32 34
import java.util.Vector;
33 35

  
34 36
import javax.servlet.ServletContext;
35 37
import javax.servlet.ServletException;
38
import javax.servlet.http.Cookie;
36 39
import javax.servlet.http.HttpServletRequest;
37 40
import javax.servlet.http.HttpServletResponse;
41
import javax.servlet.http.HttpSession;
38 42

  
39 43
import org.apache.commons.httpclient.HttpClient;
40 44
import org.apache.commons.httpclient.HttpException;
41 45
import org.apache.commons.httpclient.methods.PostMethod;
42 46
import org.apache.log4j.Logger;
43 47

  
48
import edu.ucsb.nceas.metacat.service.SessionService;
49

  
44 50
public class RequestUtil {
45 51
	
46 52
	private static Logger logMetacat = Logger.getLogger(RequestUtil.class);
......
109 115

  
110 116
		return null;
111 117
	}
118
	
119
	/**
120
	 * Get a cookie from a request by the cookie name
121
	 * 
122
	 * @param request
123
	 *            the request from which to get the cookie
124
	 * @param cookieName
125
	 *            the name of the cookie to look for
126
	 */
127
	public static Cookie getCookie(HttpServletRequest request, String cookieName)  {
128
		Cookie sessionCookies[] = request.getCookies();
112 129

  
130
		if (sessionCookies == null) {
131
			return null;
132
		}
133
		
134
		for (int i = 0; i < sessionCookies.length; i++) {
135
			if(sessionCookies[i].getName().equals(cookieName)) {
136
				return sessionCookies[i];
137
			}
138
		}	
139
		
140
		return null;
141
	}
113 142
	
114 143
	/**
144
	 * Get the session data from a request. The Scenarios we can run across
145
	 * here: 
146
	 * -- the session id parameter was set in the request parameters 
147
	 * -- request.getSession returns a new session. There is a chance that the
148
	 *    session id was set in a cookie. Check for a JSESSIONID cookie and use
149
	 *    that id if provided. 
150
	 * -- request.getSession returns a session that is a)
151
	 *    preexisting or b) new but without a JSESSIONID cookie. Use the session id
152
	 *    from this session
153
	 * 
154
	 * @param request
155
	 *            the request from which to get the session data
156
	 * @return the session data object representing the active session for this
157
	 *         request. If there is no active session, the public session data
158
	 *         is returned
159
	 */
160
	public static SessionData getSessionData(HttpServletRequest request) {
161
		SessionData sessionData = null;
162
		String sessionId = null;
163

  
164
		Hashtable<String, String[]> params = getParameters(request);
165

  
166
		if (params.containsKey("sessionid")) {
167
			// the session id is specified in the request parameters
168
			sessionId = ((String[]) params.get("sessionid"))[0];
169
			logMetacat.debug("session ID provided in request properties: " + sessionId);
170
		} else {
171
			HttpSession session = request.getSession(true);
172
			if (session.isNew()) {
173
				// this is a new session
174
				Cookie sessionCookie = RequestUtil.getCookie(request, "JSESSIONID");
175
				if (sessionCookie != null) {
176
					// and there is a JSESSIONID cookie
177
					sessionId = sessionCookie.getValue();
178
					logMetacat.debug("session ID provided in request cookie: "
179
							+ sessionId);
180
				}
181
			}
182
			if (sessionId == null) {
183
				// there is an existing session (session is old)
184
				sessionId = session.getId();
185
				logMetacat.debug("session ID retrieved from request: " + sessionId);
186
			}
187
		}
188

  
189
		// if the session id is registered in SessionService, get the
190
		// SessionData for it. Otherwise, use the public session.
191
		if (SessionService.isSessionRegistered(sessionId)) {
192
			logMetacat.debug("retrieving session data from session service "
193
					+ "for session id " + sessionId);
194
			sessionData = SessionService.getRegisteredSession(sessionId);
195
		} else {
196
			logMetacat.debug("using public session.  Given session id is "
197
					+ "registered: " + sessionId);
198
			sessionData = SessionService.getPublicSession();
199
		}
200
		
201
		return sessionData;
202
	}
203

  
204
	/**
205
	 * Get a cookie from a request by the cookie name
206
	 * 
207
	 * @param request
208
	 *            the request from which to get the cookie
209
	 * @param cookieName
210
	 *            the name of the cookie to look for
211
	 */
212
	public static Hashtable<String, String[]> getParameters(HttpServletRequest request)  {
213
		Hashtable<String, String[]> params = new Hashtable<String, String[]>();
214
		
215
		Enumeration<String> paramlist = (Enumeration<String>)request.getParameterNames();
216
		while (paramlist.hasMoreElements()) {
217
			String name = (String) paramlist.nextElement();
218
			String[] value = request.getParameterValues(name);
219
			params.put(name, value);
220
		}
221
		
222
		return params;
223
	}
224
	
225
	/**
115 226
	 * Add a list of errors to the request. The pages will pick up the errors
116 227
	 * and display them where appropriate.
117 228
	 * 

Also available in: Unified diff