1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.webapp.taglib.core.grid.GridTag; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.commons.validator.GenericValidator; 23 24 import javax.servlet.http.Cookie ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import java.io.UnsupportedEncodingException ; 28 import java.net.URLDecoder ; 29 import java.net.URLEncoder ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Enumeration ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 39 52 public class RequestUtil { 53 private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed"; 54 protected transient final Log log = LogFactory.getLog(RequestUtil.class); 55 56 57 63 public static String getRequestParameters(HttpServletRequest request) { 64 Map m = request.getParameterMap(); 67 return createQueryStringFromMap(m, "&").toString(); 68 } 69 70 71 77 public static Map getRequestParametersFromString(String queryString) { 78 HashMap parameterMap = new HashMap (); 79 80 for ( int k = 0; k < queryString.length(); ) { 81 int ampPos = queryString.indexOf('&', k); 82 if ( ampPos == -1 ) { 83 ampPos = queryString.length(); 84 } 85 String parameter = queryString.substring(k, ampPos); 86 int equalsSignPos = parameter.indexOf('='); 87 if (equalsSignPos != -1) { 88 String key = parameter.substring(0, equalsSignPos); 89 String value = parameter.substring(equalsSignPos + 1); 90 try { 91 key = URLDecoder.decode(key, Constants.DEFAULT_ENCODING); 92 value = URLDecoder.decode(value, Constants.DEFAULT_ENCODING); 93 parameterMap.put(key, mergeValues(parameterMap.get(key), value)); 94 } catch ( UnsupportedEncodingException e ) { 95 } 97 } 98 k = ampPos + 1; 99 } 100 101 return parameterMap; 102 } 103 104 105 111 public static Map getRequestParametersFromUri(String uri) { 112 if (GenericValidator.isBlankOrNull(uri)) { 113 return new HashMap (); 114 } 115 116 int qSignPos = uri.indexOf('?'); 117 if (qSignPos == -1) { 118 return new HashMap (); 119 } 120 121 return RequestUtil.getRequestParametersFromString(uri.substring(qSignPos + 1)); 122 } 123 124 125 131 public static String getBaseFromUri(String uri) { 132 if (GenericValidator.isBlankOrNull(uri)) { 133 return ""; 134 } 135 136 int qSignPos = uri.indexOf('?'); 137 if (qSignPos == -1) { 138 return uri; 139 } 140 141 return uri.substring(0, qSignPos); 142 } 143 144 145 153 public static StringBuffer createQueryStringFromMap(Map m, String ampersand, boolean encode) { 154 StringBuffer result = new StringBuffer (""); 155 Set entrySet = m.entrySet(); 156 Iterator entrySetIterator = entrySet.iterator(); 157 158 while ( entrySetIterator.hasNext() ) { 159 Map.Entry entry = (Map.Entry ) entrySetIterator.next(); 160 Object o = entry.getValue(); 161 162 if ( o == null ) { 163 append(entry.getKey(), "", result, ampersand, encode); 164 } else if ( o instanceof String ) { 165 append(entry.getKey(), o, result, ampersand, encode); 166 } else if ( o instanceof String [] ) { 167 String [] values = (String []) o; 168 169 for ( int i = 0; i < values.length; i++ ) { 170 append(entry.getKey(), values[i], result, ampersand, encode); 171 } 172 } else { 173 append(entry.getKey(), o, result, ampersand, encode); 174 } 175 } 176 177 return result; 178 } 179 180 187 public static StringBuffer createQueryStringFromMap(Map m, String ampersand) { 188 return createQueryStringFromMap(m, ampersand, true); 189 } 190 191 198 public static String appendParams(String uri, Map params) { 199 String delim = (uri.indexOf('?') == -1) ? "?" : "&"; 200 return uri + delim + RequestUtil.createQueryStringFromMap(params, "&").toString(); 201 } 202 203 209 public static Map prepareParameterMap(Map parameterMap) { 210 parameterMap.remove("clearFilter"); 211 parameterMap.remove("clearAllFilters"); 212 parameterMap.remove("sortField"); 213 parameterMap.remove("pageNumber"); 214 parameterMap.remove("gridName"); 215 return parameterMap; 216 } 217 218 224 public static String getPageUrl(GridTag parentGridTag) { 225 String pageUrl = parentGridTag.getPageUrl(); 226 Map parameterMap = prepareParameterMap(parentGridTag.getParameterMap()); 227 String queryString = RequestUtil.createQueryStringFromMap(parameterMap, "&").toString(); 228 if ( queryString != null && queryString.length() != 0 ) { 229 queryString = "?" + queryString; 230 } 231 pageUrl += queryString; 232 return pageUrl; 233 } 234 235 245 private static StringBuffer append(Object key, Object value, 246 StringBuffer queryString, 247 String ampersand, boolean encode) { 248 if ( queryString.length() > 0 ) { 249 queryString.append(ampersand); 250 } 251 252 try { 253 if ( encode ) { 254 key = URLEncoder.encode(key.toString(), Constants.DEFAULT_ENCODING); 255 value = URLEncoder.encode(value.toString(), Constants.DEFAULT_ENCODING); 256 } 257 queryString.append(key); 258 queryString.append("="); 259 queryString.append(value); 260 } catch ( UnsupportedEncodingException e ) { 261 } 263 return queryString; 264 } 265 266 271 public static void stowRequestAttributes(HttpServletRequest aRequest) { 272 if ( aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null ) { 273 return; 274 } 275 276 Enumeration e = aRequest.getAttributeNames(); 277 Map map = new HashMap (); 278 279 while ( e.hasMoreElements() ) { 280 String name = (String ) e.nextElement(); 281 map.put(name, aRequest.getAttribute(name)); 282 } 283 284 aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map); 285 } 286 287 293 public static void reclaimRequestAttributes(HttpServletRequest aRequest) { 294 Map map = 295 (Map ) aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS); 296 297 if ( map == null ) { 298 return; 299 } 300 301 Iterator itr = map.keySet().iterator(); 302 303 while ( itr.hasNext() ) { 304 String name = (String ) itr.next(); 305 aRequest.setAttribute(name, map.get(name)); 306 } 307 308 aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS); 309 } 310 311 319 public static void setCookie(HttpServletResponse response, String name, 320 String value, String path) { 321 Log log = LogFactory.getLog(RequestUtil.class); 322 323 if ( log.isDebugEnabled() ) { 324 log.debug("Setting cookie '" + name + "' on path '" + path + "'"); 325 } 326 327 Cookie cookie = new Cookie (name, value); 328 cookie.setSecure(false); 329 cookie.setPath(path); 330 cookie.setMaxAge(3600 * 24 * 30); 332 response.addCookie(cookie); 333 } 334 335 342 public static Cookie getCookie(HttpServletRequest request, String name) { 343 Cookie [] cookies = request.getCookies(); 344 Cookie returnCookie = null; 345 346 if ( cookies == null ) { 347 return returnCookie; 348 } 349 350 for ( int i = 0; i < cookies.length; i++ ) { 351 Cookie thisCookie = cookies[i]; 352 353 if ( thisCookie.getName().equals(name) ) { 354 if ( !thisCookie.getValue().equals("") ) { 356 returnCookie = thisCookie; 357 358 break; 359 } 360 } 361 } 362 363 return returnCookie; 364 } 365 366 373 public static void deleteCookie(HttpServletResponse response, 374 Cookie cookie, String path) { 375 if ( cookie != null ) { 376 cookie.setMaxAge(0); 378 cookie.setPath(path); 379 response.addCookie(cookie); 380 } 381 } 382 383 390 public static String getAppURL(HttpServletRequest request) { 391 StringBuffer url = new StringBuffer (); 392 int port = request.getServerPort(); 393 if ( port < 0 ) { 394 port = 80; } 396 String scheme = request.getScheme(); 397 url.append(scheme); 398 url.append("://"); 399 url.append(request.getServerName()); 400 if ( (scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443)) ) { 401 url.append(':'); 402 url.append(port); 403 } 404 url.append(request.getContextPath()); 405 return url.toString(); 406 } 407 408 417 public static String [] mergeValues(Object v1, Object v2) { 418 String [] values1 = null; 419 String [] values2 = null; 420 421 if ( v1 == null ) { 423 values1 = new String [0]; 424 } else if ( v1 instanceof String [] ) { 425 values1 = (String []) v1; 426 } else if ( v1 instanceof Collection ) { 427 Collection c = (Collection ) v1; 428 values1 = (String []) new ArrayList (c).toArray(new String [0]); 429 } else { 430 values1 = new String []{String.valueOf(v1)}; 431 } 432 433 if ( v2 == null ) { 435 values2 = new String [0]; 436 } else if ( v2 instanceof String [] ) { 437 values2 = (String []) v2; 438 } else if ( v2 instanceof Collection ) { 439 Collection c = (Collection ) v2; 440 values2 = (String []) new ArrayList (c).toArray(new String [0]); 441 } else { 442 values2 = new String []{String.valueOf(v2)}; 443 } 444 445 String [] result = new String [values1.length + values2.length]; 447 System.arraycopy(values1, 0, result, 0, values1.length); 448 System.arraycopy(values2, 0, result, values1.length, values2.length); 449 450 return result; 451 } 452 } 453 | Popular Tags |