1
|
/**
|
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: daigle $'
|
9
|
* '$Date: 2008-08-22 16:26:10 -0700 (Fri, 22 Aug 2008) $'
|
10
|
* '$Revision: 4299 $'
|
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
|
import java.io.IOException;
|
30
|
import java.util.Enumeration;
|
31
|
import java.util.HashMap;
|
32
|
import java.util.Hashtable;
|
33
|
import java.util.Set;
|
34
|
import java.util.Vector;
|
35
|
|
36
|
import javax.servlet.ServletContext;
|
37
|
import javax.servlet.ServletException;
|
38
|
import javax.servlet.http.Cookie;
|
39
|
import javax.servlet.http.HttpServletRequest;
|
40
|
import javax.servlet.http.HttpServletResponse;
|
41
|
import javax.servlet.http.HttpSession;
|
42
|
|
43
|
import org.apache.commons.httpclient.HttpClient;
|
44
|
import org.apache.commons.httpclient.HttpException;
|
45
|
import org.apache.commons.httpclient.methods.PostMethod;
|
46
|
import org.apache.log4j.Logger;
|
47
|
|
48
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
49
|
|
50
|
public class RequestUtil {
|
51
|
|
52
|
private static Logger logMetacat = Logger.getLogger(RequestUtil.class);
|
53
|
|
54
|
/**
|
55
|
* private constructor - all methods are static so there is no
|
56
|
* no need to instantiate.
|
57
|
*/
|
58
|
private RequestUtil() {}
|
59
|
|
60
|
/**
|
61
|
* Forward a request that was received by this servlet on to another JSP
|
62
|
* page or servlet to continue handling the request.
|
63
|
*
|
64
|
* @param request
|
65
|
* to be forwarded
|
66
|
* @param response
|
67
|
* that can be used for writing output to the client
|
68
|
* @param destination
|
69
|
* the context-relative URL to which the request is forwarded
|
70
|
*/
|
71
|
public static void forwardRequest(HttpServletRequest request,
|
72
|
HttpServletResponse response, String destination)
|
73
|
throws IOException, ServletException {
|
74
|
|
75
|
logMetacat.debug("Forwarding request to " + destination);
|
76
|
ServletContext servletContext = request.getSession()
|
77
|
.getServletContext();
|
78
|
|
79
|
servletContext.getRequestDispatcher(destination).forward(request,
|
80
|
response);
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Post a request and return the response body
|
85
|
*
|
86
|
* @param httpClient
|
87
|
* The HttpClient to use in the post. This is passed in because
|
88
|
* the same client may be used in several posts
|
89
|
* @param url
|
90
|
* the url to post to
|
91
|
* @param paramMap
|
92
|
* map of parameters to add to the post
|
93
|
* @returns a string holding the response body
|
94
|
*/
|
95
|
public static String post(HttpClient httpClient, String url,
|
96
|
HashMap<String, String> paramMap) throws IOException, HttpException {
|
97
|
|
98
|
PostMethod method = new PostMethod(url);
|
99
|
|
100
|
// Configure the form parameters
|
101
|
if (paramMap != null) {
|
102
|
Set<String> paramNames = paramMap.keySet();
|
103
|
for (String paramName : paramNames) {
|
104
|
method.addParameter(paramName, paramMap.get(paramName));
|
105
|
}
|
106
|
}
|
107
|
|
108
|
// Execute the POST method
|
109
|
int statusCode = httpClient.executeMethod(method);
|
110
|
if (statusCode != -1) {
|
111
|
String contents = method.getResponseBodyAsString();
|
112
|
method.releaseConnection();
|
113
|
return (contents);
|
114
|
}
|
115
|
|
116
|
return null;
|
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();
|
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
|
}
|
142
|
|
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
|
/**
|
226
|
* Add a list of errors to the request. The pages will pick up the errors
|
227
|
* and display them where appropriate.
|
228
|
*
|
229
|
* @param request
|
230
|
* the request that will get forwarded
|
231
|
* @param errorVector
|
232
|
* a list of error strings
|
233
|
*/
|
234
|
public static void setRequestErrors(HttpServletRequest request,
|
235
|
Vector<String> errorVector) {
|
236
|
request.setAttribute("formErrors", "true");
|
237
|
request.setAttribute("processingErrors", errorVector);
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Add a list of form errors to the request. The pages will pick up the
|
242
|
* errors and display them where appropriate.
|
243
|
*
|
244
|
* @param request
|
245
|
* the request that will get forwarded
|
246
|
* @param errorVector
|
247
|
* a list of form error strings
|
248
|
*/
|
249
|
public static void setRequestFormErrors(HttpServletRequest request,
|
250
|
Vector<String> errorVector) {
|
251
|
request.setAttribute("formErrors", "true");
|
252
|
request.setAttribute("formFieldErrors", errorVector);
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Add a list of success messages to the request. The pages will pick up the
|
257
|
* messages and display them where appropriate.
|
258
|
*
|
259
|
* @param request
|
260
|
* the request that will get forwarded
|
261
|
* @param errorVector
|
262
|
* a list of success message strings
|
263
|
*/
|
264
|
public static void setRequestSuccess(HttpServletRequest request,
|
265
|
Vector<String> successVector) {
|
266
|
request.setAttribute("formSuccess", "true");
|
267
|
request.setAttribute("processingSuccess", successVector);
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* Add a list of general messages to the request. The pages will pick up the
|
272
|
* messages and display them where appropriate.
|
273
|
*
|
274
|
* @param request
|
275
|
* the request that will get forwarded
|
276
|
* @param errorVector
|
277
|
* a list of general message strings
|
278
|
*/
|
279
|
public static void setRequestMessage(HttpServletRequest request,
|
280
|
Vector<String> messageVector) {
|
281
|
request.setAttribute("formMessage", "true");
|
282
|
request.setAttribute("processingMessage", messageVector);
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Add a list of general messages to the request. The pages will pick up the
|
287
|
* messages and display them where appropriate.
|
288
|
*
|
289
|
* @param request
|
290
|
* the request that will get forwarded
|
291
|
* @param errorVector
|
292
|
* a list of general message strings
|
293
|
*/
|
294
|
public static void clearRequestMessages(HttpServletRequest request) {
|
295
|
request.setAttribute("formMessage", null);
|
296
|
request.setAttribute("formSuccess", null);
|
297
|
request.setAttribute("formErrors", null);
|
298
|
request.setAttribute("processingMessage", null);
|
299
|
request.setAttribute("processingSuccess", null);
|
300
|
request.setAttribute("formFieldErrors", null);
|
301
|
request.setAttribute("processingErrors", null);
|
302
|
}
|
303
|
|
304
|
/**
|
305
|
* Add the user's login id to the session on this request
|
306
|
*
|
307
|
* @param request
|
308
|
* the request that will get forwarded
|
309
|
* @param userId
|
310
|
* the user's login id
|
311
|
*/
|
312
|
public static void setUserId(HttpServletRequest request, String userId) {
|
313
|
request.getSession().setAttribute("userId", userId);
|
314
|
}
|
315
|
}
|