1 18 package org.apache.roller.ui.core.util; 19 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import javax.servlet.http.Cookie ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.taglib.TagUtils; 31 32 40 public class RequestUtil 41 { 42 private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed"; 43 private transient static Log log = LogFactory.getLog(RequestUtil.class); 44 45 48 public static String getRequestParameters(HttpServletRequest aRequest) 49 { 50 Map m = aRequest.getParameterMap(); 53 return createQueryStringFromMap(m, "&").toString(); 54 } 55 56 66 public static StringBuffer createQueryStringFromMap(Map m, String ampersand) 67 { 68 StringBuffer aReturn = new StringBuffer (""); 69 Set aEntryS = m.entrySet(); 70 Iterator aEntryI = aEntryS.iterator(); 71 while (aEntryI.hasNext()) 72 { 73 Map.Entry aEntry = (Map.Entry ) aEntryI.next(); 74 Object o = aEntry.getValue(); 75 if (o == null) 76 { 77 append(aEntry.getKey(), "", aReturn, ampersand); 78 } 79 else if (o instanceof String ) 80 { 81 append(aEntry.getKey(), o, aReturn, ampersand); 82 } 83 else if (o instanceof String []) 84 { 85 String [] aValues = (String []) o; 86 for (int i = 0; i < aValues.length; i++) 87 { 88 append(aEntry.getKey(), aValues[i], aReturn, ampersand); 89 } 90 } 91 else 92 { 93 append(aEntry.getKey(), o, aReturn, ampersand); 94 } 95 } 96 return aReturn; 97 } 98 99 113 private static StringBuffer append(Object key, Object value, 114 StringBuffer queryString, String ampersand) 115 { 116 if (queryString.length() > 0) 117 { 118 queryString.append(ampersand); 119 } 120 TagUtils tagUtils = TagUtils.getInstance(); 121 queryString.append(tagUtils.encodeURL(key.toString())); 124 queryString.append("="); 125 queryString.append(tagUtils.encodeURL(value.toString())); 126 return queryString; 127 } 128 129 135 public static void stowRequestAttributes(HttpServletRequest aRequest) 136 { 137 if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) 138 { 139 return; 140 } 141 Enumeration e = aRequest.getAttributeNames(); 142 Map map = new HashMap (); 143 while (e.hasMoreElements()) 144 { 145 String name = (String ) e.nextElement(); 146 map.put(name, aRequest.getAttribute(name)); 147 } 148 aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map); 149 } 150 151 157 public static void reclaimRequestAttributes(HttpServletRequest aRequest) 158 { 159 Map map = (Map ) aRequest.getSession().getAttribute( 160 STOWED_REQUEST_ATTRIBS); 161 if (map == null) 162 { 163 return; 164 } 165 Iterator itr = map.keySet().iterator(); 166 while (itr.hasNext()) 167 { 168 String name = (String ) itr.next(); 169 aRequest.setAttribute(name, map.get(name)); 170 } 171 aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS); 172 } 173 174 182 public static void setCookie(HttpServletResponse response, String name, 183 String value, String path) 184 { 185 if (log.isDebugEnabled()) 186 { 187 log.debug("Setting cookie '" + name + "' on path '" + path + "'"); 188 } 189 Cookie cookie = new Cookie (name, value); 190 cookie.setSecure(false); 191 cookie.setPath((path.length() == 0) ? "/" : path); 194 cookie.setMaxAge(3600 * 24 * 30); response.addCookie(cookie); 196 } 197 198 208 public static Cookie getCookie(HttpServletRequest request, String name) 209 { 210 Cookie [] cookies = request.getCookies(); 211 Cookie returnCookie = null; 212 if (cookies == null) 213 { 214 return returnCookie; 215 } 216 for (int i = 0; i < cookies.length; i++) 217 { 218 Cookie thisCookie = cookies[i]; 219 if (thisCookie.getName().equals(name)) 220 { 221 if (!thisCookie.getValue().equals("")) 223 { 224 returnCookie = thisCookie; 225 break; 226 } 227 } 228 } 229 return returnCookie; 230 } 231 232 242 public static void deleteCookie(HttpServletResponse response, 243 Cookie cookie, String path) 244 { 245 if (cookie != null) 246 { 247 cookie.setMaxAge(0); 249 cookie.setPath(path); 250 response.addCookie(cookie); 251 } 252 } 253 254 258 public static String getAppURL(HttpServletRequest request) 259 { 260 StringBuffer url = new StringBuffer (); 261 int port = request.getServerPort(); 262 if (port < 0) 263 { 264 port = 80; } 266 String scheme = request.getScheme(); 267 url.append(scheme); 268 url.append("://"); 269 url.append(request.getServerName()); 270 if ((scheme.equals("http") && (port != 80)) 271 || (scheme.equals("https") && (port != 443))) 272 { 273 url.append(':'); 274 url.append(port); 275 } 276 return url.toString(); 277 } 278 } | Popular Tags |