1 package com.panogenesis.webapp.util;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.Enumeration;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9 import java.util.Set;
10
11 import javax.servlet.http.Cookie;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18
19 /***
20 * RequestUtil utility class Good ol' copy-n-paste from <a
21 * href="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt">
22 * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt</a>
23 * which is referenced in the following article: <a
24 * href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html">
25 * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html</a>
26 */
27 public class RequestUtil {
28 private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed";
29 private transient static Log log = LogFactory.getLog(RequestUtil.class);
30
31 /***
32 * Creates query String from request body parameters
33 */
34 public static String getRequestParameters(HttpServletRequest aRequest) {
35
36
37 Map m = aRequest.getParameterMap();
38
39 return createQueryStringFromMap(m, "&").toString();
40 }
41
42 /***
43 * Builds a query string from a given map of parameters
44 *
45 * @param m A map of parameters
46 * @param ampersand String to use for ampersands (e.g. "&" or "&" )
47 *
48 * @return query string (with no leading "?")
49 */
50 public static StringBuffer createQueryStringFromMap(Map m, String ampersand) {
51 StringBuffer aReturn = new StringBuffer("");
52 Set aEntryS = m.entrySet();
53 Iterator aEntryI = aEntryS.iterator();
54
55 while (aEntryI.hasNext()) {
56 Map.Entry aEntry = (Map.Entry) aEntryI.next();
57 Object o = aEntry.getValue();
58
59 if (o == null) {
60 append(aEntry.getKey(), "", aReturn, ampersand);
61 } else if (o instanceof String) {
62 append(aEntry.getKey(), o, aReturn, ampersand);
63 } else if (o instanceof String[]) {
64 String[] aValues = (String[]) o;
65
66 for (int i = 0; i < aValues.length; i++) {
67 append(aEntry.getKey(), aValues[i], aReturn, ampersand);
68 }
69 } else {
70 append(aEntry.getKey(), o, aReturn, ampersand);
71 }
72 }
73
74 return aReturn;
75 }
76
77 /***
78 * Appends new key and value pair to query string
79 *
80 * @param key parameter name
81 * @param value value of parameter
82 * @param queryString existing query string
83 * @param ampersand string to use for ampersand (e.g. "&" or "&")
84 *
85 * @return query string (with no leading "?")
86 */
87 private static StringBuffer append(Object key, Object value,
88 StringBuffer queryString,
89 String ampersand) {
90 if (queryString.length() > 0) {
91 queryString.append(ampersand);
92 }
93
94 try {
95 queryString.append(URLEncoder.encode(key.toString(), "UTF-8"));
96 queryString.append("=");
97 queryString.append(URLEncoder.encode(value.toString(), "UTF-8"));
98 } catch (UnsupportedEncodingException e) {
99
100 }
101 return queryString;
102 }
103
104 /***
105 * Stores request attributes in session
106 *
107 * @param aRequest the current request
108 */
109 public static void stowRequestAttributes(HttpServletRequest aRequest) {
110 if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) {
111 return;
112 }
113
114 Enumeration e = aRequest.getAttributeNames();
115 Map map = new HashMap();
116
117 while (e.hasMoreElements()) {
118 String name = (String) e.nextElement();
119 map.put(name, aRequest.getAttribute(name));
120 }
121
122 aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map);
123 }
124
125 /***
126 * Returns request attributes from session to request
127 *
128 * @param aRequest DOCUMENT ME!
129 */
130 public static void reclaimRequestAttributes(HttpServletRequest aRequest) {
131 Map map =
132 (Map) aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS);
133
134 if (map == null) {
135 return;
136 }
137
138 Iterator itr = map.keySet().iterator();
139
140 while (itr.hasNext()) {
141 String name = (String) itr.next();
142 aRequest.setAttribute(name, map.get(name));
143 }
144
145 aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS);
146 }
147
148 /***
149 * Convenience method to set a cookie
150 *
151 * @param response
152 * @param name
153 * @param value
154 * @param path
155 */
156 public static void setCookie(HttpServletResponse response, String name,
157 String value, String path) {
158 if (log.isDebugEnabled()) {
159 log.debug("Setting cookie '" + name + "' on path '" + path + "'");
160 }
161
162 Cookie cookie = new Cookie(name, value);
163 cookie.setSecure(false);
164 cookie.setPath(path);
165 cookie.setMaxAge(3600 * 24 * 30);
166
167 response.addCookie(cookie);
168 }
169
170 /***
171 * Convenience method to get a cookie by name
172 *
173 * @param request the current request
174 * @param name the name of the cookie to find
175 *
176 * @return the cookie (if found), null if not found
177 */
178 public static Cookie getCookie(HttpServletRequest request, String name) {
179 Cookie[] cookies = request.getCookies();
180 Cookie returnCookie = null;
181
182 if (cookies == null) {
183 return returnCookie;
184 }
185
186 for (int i = 0; i < cookies.length; i++) {
187 Cookie thisCookie = cookies[i];
188
189 if (thisCookie.getName().equals(name)) {
190
191 if (!thisCookie.getValue().equals("")) {
192 returnCookie = thisCookie;
193
194 break;
195 }
196 }
197 }
198
199 return returnCookie;
200 }
201
202 /***
203 * Convenience method for deleting a cookie by name
204 *
205 * @param response the current web response
206 * @param cookie the cookie to delete
207 * @param path the path on which the cookie was set (i.e. /tracker)
208 */
209 public static void deleteCookie(HttpServletResponse response,
210 Cookie cookie, String path) {
211 if (cookie != null) {
212
213 cookie.setMaxAge(0);
214 cookie.setPath(path);
215 response.addCookie(cookie);
216 }
217 }
218
219 /***
220 * Convenience method to get the application's URL based on request
221 * variables.
222 */
223 public static String getAppURL(HttpServletRequest request) {
224 StringBuffer url = new StringBuffer();
225 int port = request.getServerPort();
226 if (port < 0) {
227 port = 80;
228 }
229 String scheme = request.getScheme();
230 url.append(scheme);
231 url.append("://");
232 url.append(request.getServerName());
233 if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
234 url.append(':');
235 url.append(port);
236 }
237 url.append(request.getContextPath());
238 return url.toString();
239 }
240 }