001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.ui.core.util;
019:
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025: import javax.servlet.http.Cookie;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.struts.taglib.TagUtils;
031:
032: /**
033: * RequestUtil utility class Good ol' copy-n-paste from <a
034: * href="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt">
035: * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt </a> which
036: * is referenced in the following article: <a
037: * href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html">
038: * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html </a>
039: */
040: public class RequestUtil {
041: private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed";
042: private transient static Log log = LogFactory
043: .getLog(RequestUtil.class);
044:
045: /**
046: * Creates query String from request body parameters
047: */
048: public static String getRequestParameters(
049: HttpServletRequest aRequest) {
050: // set the ALGORIGTHM as defined for the application
051: //ALGORITHM = (String) aRequest.getAttribute(Constants.ENC_ALGORITHM);
052: Map m = aRequest.getParameterMap();
053: return createQueryStringFromMap(m, "&").toString();
054: }
055:
056: /**
057: * Builds a query string from a given map of parameters
058: *
059: * @param m
060: * A map of parameters
061: * @param ampersand
062: * String to use for ampersands (e.g. "&" or "&" )
063: *
064: * @return query string (with no leading "?")
065: */
066: public static StringBuffer createQueryStringFromMap(Map m,
067: String ampersand) {
068: StringBuffer aReturn = new StringBuffer("");
069: Set aEntryS = m.entrySet();
070: Iterator aEntryI = aEntryS.iterator();
071: while (aEntryI.hasNext()) {
072: Map.Entry aEntry = (Map.Entry) aEntryI.next();
073: Object o = aEntry.getValue();
074: if (o == null) {
075: append(aEntry.getKey(), "", aReturn, ampersand);
076: } else if (o instanceof String) {
077: append(aEntry.getKey(), o, aReturn, ampersand);
078: } else if (o instanceof String[]) {
079: String[] aValues = (String[]) o;
080: for (int i = 0; i < aValues.length; i++) {
081: append(aEntry.getKey(), aValues[i], aReturn,
082: ampersand);
083: }
084: } else {
085: append(aEntry.getKey(), o, aReturn, ampersand);
086: }
087: }
088: return aReturn;
089: }
090:
091: /**
092: * Appends new key and value pair to query string
093: *
094: * @param key
095: * parameter name
096: * @param value
097: * value of parameter
098: * @param queryString
099: * existing query string
100: * @param ampersand
101: * string to use for ampersand (e.g. "&" or "&")
102: *
103: * @return query string (with no leading "?")
104: */
105: private static StringBuffer append(Object key, Object value,
106: StringBuffer queryString, String ampersand) {
107: if (queryString.length() > 0) {
108: queryString.append(ampersand);
109: }
110: TagUtils tagUtils = TagUtils.getInstance();
111: // Use encodeURL from Struts' RequestUtils class - it's JDK 1.3 and 1.4
112: // compliant
113: queryString.append(tagUtils.encodeURL(key.toString()));
114: queryString.append("=");
115: queryString.append(tagUtils.encodeURL(value.toString()));
116: return queryString;
117: }
118:
119: /**
120: * Stores request attributes in session
121: *
122: * @param aRequest
123: * the current request
124: */
125: public static void stowRequestAttributes(HttpServletRequest aRequest) {
126: if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) {
127: return;
128: }
129: Enumeration e = aRequest.getAttributeNames();
130: Map map = new HashMap();
131: while (e.hasMoreElements()) {
132: String name = (String) e.nextElement();
133: map.put(name, aRequest.getAttribute(name));
134: }
135: aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map);
136: }
137:
138: /**
139: * Returns request attributes from session to request
140: *
141: * @param aRequest
142: * DOCUMENT ME!
143: */
144: public static void reclaimRequestAttributes(
145: HttpServletRequest aRequest) {
146: Map map = (Map) aRequest.getSession().getAttribute(
147: STOWED_REQUEST_ATTRIBS);
148: if (map == null) {
149: return;
150: }
151: Iterator itr = map.keySet().iterator();
152: while (itr.hasNext()) {
153: String name = (String) itr.next();
154: aRequest.setAttribute(name, map.get(name));
155: }
156: aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS);
157: }
158:
159: /**
160: * Convenience method to set a cookie
161: *
162: * @param response
163: * @param name
164: * @param value
165: * @param path
166: */
167: public static void setCookie(HttpServletResponse response,
168: String name, String value, String path) {
169: if (log.isDebugEnabled()) {
170: log.debug("Setting cookie '" + name + "' on path '" + path
171: + "'");
172: }
173: Cookie cookie = new Cookie(name, value);
174: cookie.setSecure(false);
175: // if path is nothing, use "/" so remember me will work
176: // when installed as root app
177: cookie.setPath((path.length() == 0) ? "/" : path);
178: cookie.setMaxAge(3600 * 24 * 30); // 30 days
179: response.addCookie(cookie);
180: }
181:
182: /**
183: * Convenience method to get a cookie by name
184: *
185: * @param request
186: * the current request
187: * @param name
188: * the name of the cookie to find
189: *
190: * @return the cookie (if found), null if not found
191: */
192: public static Cookie getCookie(HttpServletRequest request,
193: String name) {
194: Cookie[] cookies = request.getCookies();
195: Cookie returnCookie = null;
196: if (cookies == null) {
197: return returnCookie;
198: }
199: for (int i = 0; i < cookies.length; i++) {
200: Cookie this Cookie = cookies[i];
201: if (this Cookie.getName().equals(name)) {
202: // cookies with no value do me no good!
203: if (!this Cookie.getValue().equals("")) {
204: returnCookie = this Cookie;
205: break;
206: }
207: }
208: }
209: return returnCookie;
210: }
211:
212: /**
213: * Convenience method for deleting a cookie by name
214: *
215: * @param response
216: * the current web response
217: * @param cookie
218: * the cookie to delete
219: * @param path
220: * the path on which the cookie was set (i.e. /appfuse)
221: */
222: public static void deleteCookie(HttpServletResponse response,
223: Cookie cookie, String path) {
224: if (cookie != null) {
225: // Delete the cookie by setting its maximum age to zero
226: cookie.setMaxAge(0);
227: cookie.setPath(path);
228: response.addCookie(cookie);
229: }
230: }
231:
232: /**
233: * Convenience method to get the application's URL based on request
234: * variables.
235: */
236: public static String getAppURL(HttpServletRequest request) {
237: StringBuffer url = new StringBuffer();
238: int port = request.getServerPort();
239: if (port < 0) {
240: port = 80; // Work around java.net.URL bug
241: }
242: String scheme = request.getScheme();
243: url.append(scheme);
244: url.append("://");
245: url.append(request.getServerName());
246: if ((scheme.equals("http") && (port != 80))
247: || (scheme.equals("https") && (port != 443))) {
248: url.append(':');
249: url.append(port);
250: }
251: return url.toString();
252: }
253: }
|