1 |
4080
|
daigle
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements administrative 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$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
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.util;
|
28 |
|
|
|
29 |
5027
|
daigle
|
import java.io.InputStreamReader;
|
30 |
4080
|
daigle
|
import java.io.IOException;
|
31 |
5027
|
daigle
|
import java.io.BufferedReader;
|
32 |
|
|
import java.io.PrintWriter;
|
33 |
|
|
import java.net.MalformedURLException;
|
34 |
|
|
import java.net.URL;
|
35 |
|
|
import java.net.URLConnection;
|
36 |
4299
|
daigle
|
import java.util.Enumeration;
|
37 |
4201
|
daigle
|
import java.util.HashMap;
|
38 |
4299
|
daigle
|
import java.util.Hashtable;
|
39 |
4201
|
daigle
|
import java.util.Set;
|
40 |
4080
|
daigle
|
import java.util.Vector;
|
41 |
|
|
|
42 |
|
|
import javax.servlet.ServletContext;
|
43 |
|
|
import javax.servlet.ServletException;
|
44 |
4299
|
daigle
|
import javax.servlet.http.Cookie;
|
45 |
4080
|
daigle
|
import javax.servlet.http.HttpServletRequest;
|
46 |
|
|
import javax.servlet.http.HttpServletResponse;
|
47 |
4299
|
daigle
|
import javax.servlet.http.HttpSession;
|
48 |
4080
|
daigle
|
|
49 |
4201
|
daigle
|
import org.apache.commons.httpclient.HttpClient;
|
50 |
|
|
import org.apache.commons.httpclient.HttpException;
|
51 |
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
52 |
4080
|
daigle
|
import org.apache.log4j.Logger;
|
53 |
|
|
|
54 |
5076
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
55 |
4299
|
daigle
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
56 |
5027
|
daigle
|
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
|
57 |
5076
|
daigle
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
58 |
4299
|
daigle
|
|
59 |
4080
|
daigle
|
public class RequestUtil {
|
60 |
|
|
|
61 |
|
|
private static Logger logMetacat = Logger.getLogger(RequestUtil.class);
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* private constructor - all methods are static so there is no
|
65 |
|
|
* no need to instantiate.
|
66 |
|
|
*/
|
67 |
|
|
private RequestUtil() {}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* Forward a request that was received by this servlet on to another JSP
|
71 |
|
|
* page or servlet to continue handling the request.
|
72 |
|
|
*
|
73 |
|
|
* @param request
|
74 |
|
|
* to be forwarded
|
75 |
|
|
* @param response
|
76 |
|
|
* that can be used for writing output to the client
|
77 |
|
|
* @param destination
|
78 |
|
|
* the context-relative URL to which the request is forwarded
|
79 |
5076
|
daigle
|
* @param params the request parameters. these will be added to the request
|
80 |
4080
|
daigle
|
*/
|
81 |
5076
|
daigle
|
public static void forwardRequest(HttpServletRequest request, HttpServletResponse response,
|
82 |
|
|
String destinationUrl, Hashtable<String, String[]> params) throws MetacatUtilException {
|
83 |
4080
|
daigle
|
|
84 |
5027
|
daigle
|
destinationUrl += "?" + paramsToQuery(params);
|
85 |
|
|
|
86 |
|
|
logMetacat.debug("Forwarding request to " + destinationUrl);
|
87 |
4080
|
daigle
|
ServletContext servletContext = request.getSession()
|
88 |
|
|
.getServletContext();
|
89 |
|
|
|
90 |
5076
|
daigle
|
try {
|
91 |
|
|
servletContext.getRequestDispatcher(destinationUrl).forward(request, response);
|
92 |
|
|
} catch (IOException ioe) {
|
93 |
|
|
throw new MetacatUtilException("RequestUtil.forwardRequest - I/O error when forwarding to " +
|
94 |
|
|
destinationUrl + " : " + ioe.getMessage());
|
95 |
|
|
} catch (ServletException se) {
|
96 |
|
|
throw new MetacatUtilException("RequestUtil.forwardRequest - Servlet error when forwarding to " +
|
97 |
|
|
destinationUrl + " : " + se.getMessage());
|
98 |
|
|
}
|
99 |
4080
|
daigle
|
}
|
100 |
5076
|
daigle
|
|
101 |
|
|
/**
|
102 |
|
|
* Forward a request that was received by this servlet on to another JSP
|
103 |
|
|
* page or servlet to continue handling the request. In this case, the page
|
104 |
|
|
* must be referenced in a paramter named "forwardto". If the qformat is
|
105 |
|
|
* provided, the file will be retrieved from that skin. Otherwise, the file
|
106 |
|
|
* will be retrieved from the system default skin.
|
107 |
|
|
*
|
108 |
|
|
* For more specific file location, use: forwardRequest(request,response, destinationUrl, params)
|
109 |
|
|
*
|
110 |
|
|
* @param request
|
111 |
|
|
* to be forwarded
|
112 |
|
|
* @param response
|
113 |
|
|
* that can be used for writing output to the client
|
114 |
|
|
* @param params
|
115 |
|
|
* the request parameters. these will be added to the request.
|
116 |
|
|
*/
|
117 |
|
|
public static void forwardRequest(HttpServletRequest request, HttpServletResponse response,
|
118 |
|
|
Hashtable<String, String[]> params) throws MetacatUtilException {
|
119 |
4201
|
daigle
|
|
120 |
5076
|
daigle
|
String forwardTos[] = params.get("forwardto");
|
121 |
|
|
if (forwardTos == null || forwardTos[0].equals("")) {
|
122 |
|
|
throw new MetacatUtilException("RequestUtil.forwardRequest - forwardto must be set in parameters when forwarding.");
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
String forwardTo = forwardTos[0];
|
126 |
|
|
String qformat = null;
|
127 |
|
|
|
128 |
|
|
String qformats[] = params.get("qformat");
|
129 |
|
|
if (qformats == null || qformats.length == 0) {
|
130 |
|
|
try {
|
131 |
|
|
qformat = PropertyService.getProperty("application.default-style");
|
132 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
133 |
|
|
qformat = "default";
|
134 |
|
|
logMetacat.warn("RequestUtil.forwardRequest - could not get property " +
|
135 |
|
|
"'application.default-style'. Using 'default'");
|
136 |
|
|
}
|
137 |
|
|
} else {
|
138 |
|
|
qformat = qformats[0];
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
String destinationUrl = "/style/skins/" + qformat + "/" + forwardTo;
|
142 |
|
|
destinationUrl += "?" + paramsToQuery(params);
|
143 |
|
|
|
144 |
|
|
logMetacat.debug("RequestUtil.forwardRequest - Forwarding request to " + destinationUrl);
|
145 |
|
|
ServletContext servletContext = request.getSession()
|
146 |
|
|
.getServletContext();
|
147 |
|
|
try {
|
148 |
|
|
servletContext.getRequestDispatcher(destinationUrl).forward(request, response);
|
149 |
|
|
} catch (IOException ioe) {
|
150 |
|
|
throw new MetacatUtilException("RequestUtil.forwardRequest - I/O error when forwarding to " +
|
151 |
|
|
destinationUrl + " : " + ioe.getMessage());
|
152 |
|
|
} catch (ServletException se) {
|
153 |
|
|
throw new MetacatUtilException("RequestUtil.forwardRequest - Servlet error when forwarding to " +
|
154 |
|
|
destinationUrl + " : " + se.getMessage());
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
4201
|
daigle
|
/**
|
161 |
|
|
* Post a request and return the response body
|
162 |
|
|
*
|
163 |
|
|
* @param httpClient
|
164 |
|
|
* The HttpClient to use in the post. This is passed in because
|
165 |
|
|
* the same client may be used in several posts
|
166 |
|
|
* @param url
|
167 |
|
|
* the url to post to
|
168 |
|
|
* @param paramMap
|
169 |
|
|
* map of parameters to add to the post
|
170 |
|
|
* @returns a string holding the response body
|
171 |
|
|
*/
|
172 |
|
|
public static String post(HttpClient httpClient, String url,
|
173 |
|
|
HashMap<String, String> paramMap) throws IOException, HttpException {
|
174 |
|
|
|
175 |
|
|
PostMethod method = new PostMethod(url);
|
176 |
|
|
|
177 |
|
|
// Configure the form parameters
|
178 |
|
|
if (paramMap != null) {
|
179 |
|
|
Set<String> paramNames = paramMap.keySet();
|
180 |
|
|
for (String paramName : paramNames) {
|
181 |
|
|
method.addParameter(paramName, paramMap.get(paramName));
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
// Execute the POST method
|
186 |
|
|
int statusCode = httpClient.executeMethod(method);
|
187 |
|
|
if (statusCode != -1) {
|
188 |
|
|
String contents = method.getResponseBodyAsString();
|
189 |
|
|
method.releaseConnection();
|
190 |
|
|
return (contents);
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
return null;
|
194 |
|
|
}
|
195 |
4299
|
daigle
|
|
196 |
5027
|
daigle
|
public static String get(String urlString, Hashtable<String, String[]> params) throws MetacatUtilException {
|
197 |
|
|
try {
|
198 |
|
|
URL url = new URL(urlString);
|
199 |
|
|
URLConnection urlConn = url.openConnection();
|
200 |
|
|
|
201 |
|
|
urlConn.setDoOutput(true);
|
202 |
|
|
|
203 |
|
|
PrintWriter pw = new PrintWriter(urlConn.getOutputStream());
|
204 |
5043
|
daigle
|
String queryString = paramsToQuery(params);
|
205 |
|
|
logMetacat.debug("Sending get request: " + urlString + "?" + queryString);
|
206 |
|
|
pw.print(queryString);
|
207 |
5027
|
daigle
|
pw.close();
|
208 |
|
|
|
209 |
|
|
// get the input from the request
|
210 |
|
|
BufferedReader in =
|
211 |
|
|
new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
|
212 |
|
|
|
213 |
|
|
StringBuffer sb = new StringBuffer();
|
214 |
|
|
String line;
|
215 |
|
|
while ((line = in.readLine()) != null) {
|
216 |
|
|
sb.append(line);
|
217 |
|
|
}
|
218 |
|
|
in.close();
|
219 |
|
|
|
220 |
|
|
return sb.toString();
|
221 |
|
|
} catch (MalformedURLException mue) {
|
222 |
|
|
throw new MetacatUtilException("URL error when contacting: " + urlString + " : " + mue.getMessage());
|
223 |
|
|
} catch (IOException ioe) {
|
224 |
|
|
throw new MetacatUtilException("I/O error when contacting: " + urlString + " : " + ioe.getMessage());
|
225 |
|
|
}
|
226 |
|
|
}
|
227 |
|
|
|
228 |
4299
|
daigle
|
/**
|
229 |
|
|
* Get a cookie from a request by the cookie name
|
230 |
|
|
*
|
231 |
|
|
* @param request
|
232 |
|
|
* the request from which to get the cookie
|
233 |
|
|
* @param cookieName
|
234 |
|
|
* the name of the cookie to look for
|
235 |
|
|
*/
|
236 |
|
|
public static Cookie getCookie(HttpServletRequest request, String cookieName) {
|
237 |
|
|
Cookie sessionCookies[] = request.getCookies();
|
238 |
4201
|
daigle
|
|
239 |
4299
|
daigle
|
if (sessionCookies == null) {
|
240 |
|
|
return null;
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
for (int i = 0; i < sessionCookies.length; i++) {
|
244 |
|
|
if(sessionCookies[i].getName().equals(cookieName)) {
|
245 |
|
|
return sessionCookies[i];
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
return null;
|
250 |
|
|
}
|
251 |
4080
|
daigle
|
|
252 |
|
|
/**
|
253 |
4299
|
daigle
|
* Get the session data from a request. The Scenarios we can run across
|
254 |
|
|
* here:
|
255 |
|
|
* -- the session id parameter was set in the request parameters
|
256 |
|
|
* -- request.getSession returns a new session. There is a chance that the
|
257 |
|
|
* session id was set in a cookie. Check for a JSESSIONID cookie and use
|
258 |
|
|
* that id if provided.
|
259 |
|
|
* -- request.getSession returns a session that is a)
|
260 |
|
|
* preexisting or b) new but without a JSESSIONID cookie. Use the session id
|
261 |
|
|
* from this session
|
262 |
|
|
*
|
263 |
|
|
* @param request
|
264 |
|
|
* the request from which to get the session data
|
265 |
|
|
* @return the session data object representing the active session for this
|
266 |
|
|
* request. If there is no active session, the public session data
|
267 |
|
|
* is returned
|
268 |
|
|
*/
|
269 |
|
|
public static SessionData getSessionData(HttpServletRequest request) {
|
270 |
|
|
SessionData sessionData = null;
|
271 |
|
|
String sessionId = null;
|
272 |
|
|
|
273 |
|
|
Hashtable<String, String[]> params = getParameters(request);
|
274 |
|
|
|
275 |
|
|
if (params.containsKey("sessionid")) {
|
276 |
|
|
// the session id is specified in the request parameters
|
277 |
|
|
sessionId = ((String[]) params.get("sessionid"))[0];
|
278 |
|
|
logMetacat.debug("session ID provided in request properties: " + sessionId);
|
279 |
|
|
} else {
|
280 |
|
|
HttpSession session = request.getSession(true);
|
281 |
|
|
if (session.isNew()) {
|
282 |
|
|
// this is a new session
|
283 |
|
|
Cookie sessionCookie = RequestUtil.getCookie(request, "JSESSIONID");
|
284 |
|
|
if (sessionCookie != null) {
|
285 |
|
|
// and there is a JSESSIONID cookie
|
286 |
|
|
sessionId = sessionCookie.getValue();
|
287 |
|
|
logMetacat.debug("session ID provided in request cookie: "
|
288 |
|
|
+ sessionId);
|
289 |
|
|
}
|
290 |
|
|
}
|
291 |
|
|
if (sessionId == null) {
|
292 |
|
|
// there is an existing session (session is old)
|
293 |
|
|
sessionId = session.getId();
|
294 |
|
|
logMetacat.debug("session ID retrieved from request: " + sessionId);
|
295 |
|
|
}
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
// if the session id is registered in SessionService, get the
|
299 |
|
|
// SessionData for it. Otherwise, use the public session.
|
300 |
5374
|
berkley
|
if (SessionService.getInstance().isSessionRegistered(sessionId)) {
|
301 |
4299
|
daigle
|
logMetacat.debug("retrieving session data from session service "
|
302 |
|
|
+ "for session id " + sessionId);
|
303 |
5374
|
berkley
|
sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
|
304 |
4299
|
daigle
|
} else {
|
305 |
|
|
logMetacat.debug("using public session. Given session id is "
|
306 |
|
|
+ "registered: " + sessionId);
|
307 |
5374
|
berkley
|
sessionData = SessionService.getInstance().getPublicSession();
|
308 |
4299
|
daigle
|
}
|
309 |
|
|
|
310 |
|
|
return sessionData;
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
/**
|
314 |
|
|
* Get a cookie from a request by the cookie name
|
315 |
|
|
*
|
316 |
|
|
* @param request
|
317 |
|
|
* the request from which to get the cookie
|
318 |
|
|
* @param cookieName
|
319 |
|
|
* the name of the cookie to look for
|
320 |
|
|
*/
|
321 |
5076
|
daigle
|
@SuppressWarnings("unchecked")
|
322 |
4299
|
daigle
|
public static Hashtable<String, String[]> getParameters(HttpServletRequest request) {
|
323 |
|
|
Hashtable<String, String[]> params = new Hashtable<String, String[]>();
|
324 |
|
|
|
325 |
|
|
Enumeration<String> paramlist = (Enumeration<String>)request.getParameterNames();
|
326 |
|
|
while (paramlist.hasMoreElements()) {
|
327 |
|
|
String name = (String) paramlist.nextElement();
|
328 |
|
|
String[] value = request.getParameterValues(name);
|
329 |
|
|
params.put(name, value);
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
return params;
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
/**
|
336 |
4080
|
daigle
|
* Add a list of errors to the request. The pages will pick up the errors
|
337 |
|
|
* and display them where appropriate.
|
338 |
|
|
*
|
339 |
|
|
* @param request
|
340 |
|
|
* the request that will get forwarded
|
341 |
|
|
* @param errorVector
|
342 |
|
|
* a list of error strings
|
343 |
|
|
*/
|
344 |
|
|
public static void setRequestErrors(HttpServletRequest request,
|
345 |
|
|
Vector<String> errorVector) {
|
346 |
|
|
request.setAttribute("formErrors", "true");
|
347 |
|
|
request.setAttribute("processingErrors", errorVector);
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
/**
|
351 |
|
|
* Add a list of form errors to the request. The pages will pick up the
|
352 |
|
|
* errors and display them where appropriate.
|
353 |
|
|
*
|
354 |
|
|
* @param request
|
355 |
|
|
* the request that will get forwarded
|
356 |
|
|
* @param errorVector
|
357 |
|
|
* a list of form error strings
|
358 |
|
|
*/
|
359 |
|
|
public static void setRequestFormErrors(HttpServletRequest request,
|
360 |
|
|
Vector<String> errorVector) {
|
361 |
|
|
request.setAttribute("formErrors", "true");
|
362 |
|
|
request.setAttribute("formFieldErrors", errorVector);
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
/**
|
366 |
|
|
* Add a list of success messages to the request. The pages will pick up the
|
367 |
|
|
* messages and display them where appropriate.
|
368 |
|
|
*
|
369 |
|
|
* @param request
|
370 |
|
|
* the request that will get forwarded
|
371 |
|
|
* @param errorVector
|
372 |
|
|
* a list of success message strings
|
373 |
|
|
*/
|
374 |
|
|
public static void setRequestSuccess(HttpServletRequest request,
|
375 |
|
|
Vector<String> successVector) {
|
376 |
|
|
request.setAttribute("formSuccess", "true");
|
377 |
|
|
request.setAttribute("processingSuccess", successVector);
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
/**
|
381 |
|
|
* Add a list of general messages to the request. The pages will pick up the
|
382 |
|
|
* messages and display them where appropriate.
|
383 |
|
|
*
|
384 |
|
|
* @param request
|
385 |
|
|
* the request that will get forwarded
|
386 |
|
|
* @param errorVector
|
387 |
|
|
* a list of general message strings
|
388 |
|
|
*/
|
389 |
|
|
public static void setRequestMessage(HttpServletRequest request,
|
390 |
|
|
Vector<String> messageVector) {
|
391 |
|
|
request.setAttribute("formMessage", "true");
|
392 |
|
|
request.setAttribute("processingMessage", messageVector);
|
393 |
|
|
}
|
394 |
|
|
|
395 |
|
|
/**
|
396 |
|
|
* Add a list of general messages to the request. The pages will pick up the
|
397 |
|
|
* messages and display them where appropriate.
|
398 |
|
|
*
|
399 |
|
|
* @param request
|
400 |
|
|
* the request that will get forwarded
|
401 |
|
|
* @param errorVector
|
402 |
|
|
* a list of general message strings
|
403 |
|
|
*/
|
404 |
|
|
public static void clearRequestMessages(HttpServletRequest request) {
|
405 |
|
|
request.setAttribute("formMessage", null);
|
406 |
|
|
request.setAttribute("formSuccess", null);
|
407 |
|
|
request.setAttribute("formErrors", null);
|
408 |
|
|
request.setAttribute("processingMessage", null);
|
409 |
|
|
request.setAttribute("processingSuccess", null);
|
410 |
|
|
request.setAttribute("formFieldErrors", null);
|
411 |
|
|
request.setAttribute("processingErrors", null);
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
/**
|
415 |
|
|
* Add the user's login id to the session on this request
|
416 |
|
|
*
|
417 |
|
|
* @param request
|
418 |
|
|
* the request that will get forwarded
|
419 |
|
|
* @param userId
|
420 |
|
|
* the user's login id
|
421 |
|
|
*/
|
422 |
|
|
public static void setUserId(HttpServletRequest request, String userId) {
|
423 |
|
|
request.getSession().setAttribute("userId", userId);
|
424 |
|
|
}
|
425 |
4951
|
daigle
|
|
426 |
5027
|
daigle
|
private static String paramsToQuery(Hashtable<String, String[]> params) {
|
427 |
|
|
String query = "";
|
428 |
|
|
if (params != null) {
|
429 |
|
|
boolean firstParam = true;
|
430 |
|
|
for (String paramName : params.keySet()) {
|
431 |
|
|
if (firstParam) {
|
432 |
|
|
firstParam = false;
|
433 |
|
|
} else {
|
434 |
|
|
query += "&";
|
435 |
|
|
}
|
436 |
|
|
query += paramName + "=" + params.get(paramName)[0];
|
437 |
|
|
}
|
438 |
|
|
}
|
439 |
|
|
|
440 |
|
|
return query;
|
441 |
|
|
}
|
442 |
|
|
|
443 |
4951
|
daigle
|
|
444 |
4080
|
daigle
|
}
|