1 package org.roller.presentation.util; 2 3 import java.util.Enumeration ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Set ; 8 import javax.servlet.http.Cookie ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.apache.struts.taglib.TagUtils; 14 15 23 public class RequestUtil 24 { 25 private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed"; 26 private transient static Log log = LogFactory.getLog(RequestUtil.class); 27 28 31 public static String getRequestParameters(HttpServletRequest aRequest) 32 { 33 Map m = aRequest.getParameterMap(); 36 return createQueryStringFromMap(m, "&").toString(); 37 } 38 39 49 public static StringBuffer createQueryStringFromMap(Map m, String ampersand) 50 { 51 StringBuffer aReturn = new StringBuffer (""); 52 Set aEntryS = m.entrySet(); 53 Iterator aEntryI = aEntryS.iterator(); 54 while (aEntryI.hasNext()) 55 { 56 Map.Entry aEntry = (Map.Entry ) aEntryI.next(); 57 Object o = aEntry.getValue(); 58 if (o == null) 59 { 60 append(aEntry.getKey(), "", aReturn, ampersand); 61 } 62 else if (o instanceof String ) 63 { 64 append(aEntry.getKey(), o, aReturn, ampersand); 65 } 66 else if (o instanceof String []) 67 { 68 String [] aValues = (String []) o; 69 for (int i = 0; i < aValues.length; i++) 70 { 71 append(aEntry.getKey(), aValues[i], aReturn, ampersand); 72 } 73 } 74 else 75 { 76 append(aEntry.getKey(), o, aReturn, ampersand); 77 } 78 } 79 return aReturn; 80 } 81 82 96 private static StringBuffer append(Object key, Object value, 97 StringBuffer queryString, String ampersand) 98 { 99 if (queryString.length() > 0) 100 { 101 queryString.append(ampersand); 102 } 103 TagUtils tagUtils = TagUtils.getInstance(); 104 queryString.append(tagUtils.encodeURL(key.toString())); 107 queryString.append("="); 108 queryString.append(tagUtils.encodeURL(value.toString())); 109 return queryString; 110 } 111 112 118 public static void stowRequestAttributes(HttpServletRequest aRequest) 119 { 120 if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) 121 { 122 return; 123 } 124 Enumeration e = aRequest.getAttributeNames(); 125 Map map = new HashMap (); 126 while (e.hasMoreElements()) 127 { 128 String name = (String ) e.nextElement(); 129 map.put(name, aRequest.getAttribute(name)); 130 } 131 aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map); 132 } 133 134 140 public static void reclaimRequestAttributes(HttpServletRequest aRequest) 141 { 142 Map map = (Map ) aRequest.getSession().getAttribute( 143 STOWED_REQUEST_ATTRIBS); 144 if (map == null) 145 { 146 return; 147 } 148 Iterator itr = map.keySet().iterator(); 149 while (itr.hasNext()) 150 { 151 String name = (String ) itr.next(); 152 aRequest.setAttribute(name, map.get(name)); 153 } 154 aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS); 155 } 156 157 165 public static void setCookie(HttpServletResponse response, String name, 166 String value, String path) 167 { 168 if (log.isDebugEnabled()) 169 { 170 log.debug("Setting cookie '" + name + "' on path '" + path + "'"); 171 } 172 Cookie cookie = new Cookie (name, value); 173 cookie.setSecure(false); 174 cookie.setPath((path.length() == 0) ? "/" : path); 177 cookie.setMaxAge(3600 * 24 * 30); response.addCookie(cookie); 179 } 180 181 191 public static Cookie getCookie(HttpServletRequest request, String name) 192 { 193 Cookie [] cookies = request.getCookies(); 194 Cookie returnCookie = null; 195 if (cookies == null) 196 { 197 return returnCookie; 198 } 199 for (int i = 0; i < cookies.length; i++) 200 { 201 Cookie thisCookie = cookies[i]; 202 if (thisCookie.getName().equals(name)) 203 { 204 if (!thisCookie.getValue().equals("")) 206 { 207 returnCookie = thisCookie; 208 break; 209 } 210 } 211 } 212 return returnCookie; 213 } 214 215 225 public static void deleteCookie(HttpServletResponse response, 226 Cookie cookie, String path) 227 { 228 if (cookie != null) 229 { 230 cookie.setMaxAge(0); 232 cookie.setPath(path); 233 response.addCookie(cookie); 234 } 235 } 236 237 241 public static String getAppURL(HttpServletRequest request) 242 { 243 StringBuffer url = new StringBuffer (); 244 int port = request.getServerPort(); 245 if (port < 0) 246 { 247 port = 80; } 249 String scheme = request.getScheme(); 250 url.append(scheme); 251 url.append("://"); 252 url.append(request.getServerName()); 253 if ((scheme.equals("http") && (port != 80)) 254 || (scheme.equals("https") && (port != 443))) 255 { 256 url.append(':'); 257 url.append(port); 258 } 259 return url.toString(); 260 } 261 } | Popular Tags |