1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.core.misc.URLUTF8Encoder; 68 69 import javax.servlet.http.HttpServletRequest ; 70 import java.util.Enumeration ; 71 import java.util.HashMap ; 72 import java.util.Iterator ; 73 import java.util.List ; 74 import java.util.Map ; 75 import java.util.Set ; 76 77 83 public class SecureRequestUtils { 84 87 90 private static final String HTTP = "http"; 91 private static final String HTTPS = "https"; 92 private static final String STD_HTTP_PORT = "80"; 93 private static final String STD_HTTPS_PORT = "443"; 94 95 private static final String STOWED_REQUEST_ATTRIBS = 96 "ssl.redirect.attrib.stowed"; 97 98 99 107 private static StringBuffer startNewUrlString(HttpServletRequest request, 108 String desiredScheme, 109 String desiredPort) { 110 StringBuffer url = new StringBuffer (128); 111 String serverName = request.getServerName(); 112 url.append(desiredScheme).append("://").append(serverName); 113 114 if ((HTTP.equals(desiredScheme) && !STD_HTTP_PORT.equals(desiredPort)) || 115 (HTTPS.equals(desiredScheme) && !STD_HTTPS_PORT.equals(desiredPort))) { 116 url.append(":").append(desiredPort); 117 } 118 119 return url; 120 } 121 122 123 129 public static String getRequestParameters(HttpServletRequest aRequest) { 130 Map m = aRequest.getParameterMap(); 131 return createQueryStringFromMap(m, "&").toString(); 132 } 133 134 141 public static StringBuffer createQueryStringFromMap(Map m, String ampersand) { 142 StringBuffer aReturn = new StringBuffer (128); 143 Set aEntryS = m.entrySet(); 144 Iterator aEntryI = aEntryS.iterator(); 145 while (aEntryI.hasNext()) { 146 Map.Entry aEntry = (Map.Entry ) aEntryI.next(); 147 Object value = aEntry.getValue(); 148 String [] aValues = new String [1]; 149 if (value == null) { 150 aValues[0] = ""; 151 } else if (value instanceof List ) { List aList = (List ) value; 153 aValues = (String []) aList.toArray(new String [aList.size()]); 154 } else if (value instanceof String ) { aValues[0] = (String ) value; 156 } else { aValues = (String []) value; } 159 for (int i = 0; i < aValues.length; i++) { 160 append(aEntry.getKey(), aValues[i], aReturn, ampersand); 161 } 162 } 163 return aReturn; 164 } 165 166 175 private static StringBuffer append(Object key, Object value, StringBuffer queryString, String ampersand) { 176 if (queryString.length() > 0) { 177 queryString.append(ampersand); 178 } 179 queryString.append(URLUTF8Encoder.encode(key.toString())); 180 queryString.append("="); 181 queryString.append(URLUTF8Encoder.encode(value.toString())); 182 183 return queryString; 184 } 185 186 193 public static boolean stowRequestAttributes(HttpServletRequest aRequest) { 194 195 if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) { 196 return false; 197 } 198 199 Enumeration anEnum = aRequest.getAttributeNames(); 200 Map map = new HashMap (); 201 while (anEnum.hasMoreElements()) { 202 String name = (String ) anEnum.nextElement(); 203 map.put(name, aRequest.getAttribute(name)); 204 } 205 aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map); 206 return true; 207 } 208 209 210 217 public static void reclaimRequestAttributes(HttpServletRequest aRequest, 218 boolean doRemove) { 219 Map map = (Map ) aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS); 220 221 if (map == null) { 222 return; 223 } 224 225 Iterator itr = map.keySet().iterator(); 226 while (itr.hasNext()) { 227 String name = (String ) itr.next(); 228 229 aRequest.setAttribute(name, map.get(name)); 230 } 231 232 if (doRemove) { 233 aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS); 234 } 235 } 236 237 238 247 static public String getRedirectString(HttpServletRequest request, 248 String httpPort, 249 String httpsPort, 250 boolean isSecure) { 251 252 253 String desiredScheme = isSecure ? HTTPS : HTTP; 256 String usingScheme = request.getScheme(); 257 258 String desiredPort = isSecure ? httpsPort : httpPort; 261 String usingPort = String.valueOf(request.getServerPort()); 262 263 String urlString = null; 264 265 if (!desiredScheme.equals(usingScheme) || !desiredPort.equals(usingPort)) { 267 268 urlString = buildNewUrlString(request, 269 desiredScheme, 270 usingScheme, 271 desiredPort, 272 usingPort); 273 274 if (!SecureRequestUtils.stowRequestAttributes(request)) { 276 SecureRequestUtils.reclaimRequestAttributes(request, false); 279 } 280 } else { 281 SecureRequestUtils.reclaimRequestAttributes(request, true); 283 } 284 285 return urlString; 286 } 287 288 289 300 private static String buildNewUrlString(HttpServletRequest request, 301 String desiredScheme, 302 String usingScheme, 303 String desiredPort, 304 String usingPort) { 305 306 307 StringBuffer url = startNewUrlString(request, desiredScheme, desiredPort); 308 309 url.append(request.getRequestURI()); 310 311 return addQueryString(request, url); 312 } 313 314 324 private static String addQueryString(HttpServletRequest request, StringBuffer url) { 325 String queryString = request.getQueryString(); 327 if (queryString != null && queryString.length() != 0) { 328 url.append("?" + queryString); 329 } else { 330 queryString = SecureRequestUtils.getRequestParameters(request); 331 if (queryString != null && queryString.length() != 0) { 332 url.append("?" + queryString); 333 } 334 } 335 336 return url.toString(); 337 } 338 } | Popular Tags |