package org.appfuse.webapp.util;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.RequestUtils;
public class RequestUtil extends BaseUtil {
private static final String STOWED_REQUEST_ATTRIBS =
"ssl.redirect.attrib.stowed";
private static Log log = LogFactory.getLog(RequestUtil.class);
public static String getRequestParameters(HttpServletRequest aRequest) {
Map m = aRequest.getParameterMap();
return createQueryStringFromMap(m, "&").toString();
}
public static StringBuffer createQueryStringFromMap(Map m, String ampersand) {
StringBuffer aReturn = new StringBuffer("");
Set aEntryS = m.entrySet();
Iterator aEntryI = aEntryS.iterator();
while (aEntryI.hasNext()) {
Map.Entry aEntry = (Map.Entry) aEntryI.next();
Object o = aEntry.getValue();
if (o == null) {
append(aEntry.getKey(), "", aReturn, ampersand);
} else if (o instanceof String) {
append(aEntry.getKey(), o, aReturn, ampersand);
} else if (o instanceof String[]) {
String[] aValues = (String[]) o;
for (int i = 0; i < aValues.length; i++) {
append(aEntry.getKey(), aValues[i], aReturn, ampersand);
}
} else {
append(aEntry.getKey(), o, aReturn, ampersand);
}
}
return aReturn;
}
private static StringBuffer append(Object key, Object value,
StringBuffer queryString,
String ampersand) {
if (queryString.length() > 0) {
queryString.append(ampersand);
}
queryString.append(RequestUtils.encodeURL(key.toString()));
queryString.append("=");
queryString.append(RequestUtils.encodeURL(value.toString()));
return queryString;
}
public static void stowRequestAttributes(HttpServletRequest aRequest) {
if (aRequest.getSession(false).getAttribute(STOWED_REQUEST_ATTRIBS) != null) {
return;
}
Enumeration enum = aRequest.getAttributeNames();
Map map = new HashMap();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
map.put(name, aRequest.getAttribute(name));
}
aRequest.getSession(false).setAttribute(STOWED_REQUEST_ATTRIBS, map);
}
public static void reclaimRequestAttributes(HttpServletRequest aRequest) {
Map map =
(Map) aRequest.getSession(false).getAttribute(STOWED_REQUEST_ATTRIBS);
if (map == null) {
return;
}
Iterator itr = map.keySet().iterator();
while (itr.hasNext()) {
String name = (String) itr.next();
aRequest.setAttribute(name, map.get(name));
}
aRequest.getSession(false).removeAttribute(STOWED_REQUEST_ATTRIBS);
}
public static HttpServletResponse setCookie(HttpServletResponse response,
String name, String value,
String path) {
if (log.isDebugEnabled()) {
log.debug("Setting cookie '" + name + "' on path '" + path + "'");
}
Cookie cookie = new Cookie(name, value);
cookie.setSecure(false);
cookie.setPath(path);
cookie.setMaxAge(3600 * 24 * 30);
response.addCookie(cookie);
return response;
}
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
Cookie returnCookie = null;
if (cookies == null) {
return returnCookie;
}
for (int i = 0; i < cookies.length; i++) {
Cookie thisCookie = cookies[i];
if (thisCookie.getName().equals(name)) {
if (!thisCookie.getValue().equals("")) {
returnCookie = thisCookie;
break;
}
}
}
return returnCookie;
}
public static HttpServletResponse deleteCookie(HttpServletResponse response,
Cookie cookie, String path) {
if (cookie == null) {
return response;
}
cookie.setMaxAge(0);
cookie.setPath(path);
response.addCookie(cookie);
return response;
}
}