1 16 17 package org.springframework.web.util; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.TreeMap ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.http.Cookie ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpSession ; 31 32 import org.springframework.util.Assert; 33 import org.springframework.util.StringUtils; 34 35 42 public abstract class WebUtils { 43 44 49 public static final String INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri"; 50 public static final String INCLUDE_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.include.context_path"; 51 public static final String INCLUDE_SERVLET_PATH_ATTRIBUTE = "javax.servlet.include.servlet_path"; 52 public static final String INCLUDE_PATH_INFO_ATTRIBUTE = "javax.servlet.include.path_info"; 53 public static final String INCLUDE_QUERY_STRING_ATTRIBUTE = "javax.servlet.include.query_string"; 54 55 60 public static final String FORWARD_REQUEST_URI_ATTRIBUTE = "javax.servlet.forward.request_uri"; 61 public static final String FORWARD_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.forward.context_path"; 62 public static final String FORWARD_SERVLET_PATH_ATTRIBUTE = "javax.servlet.forward.servlet_path"; 63 public static final String FORWARD_PATH_INFO_ATTRIBUTE = "javax.servlet.forward.path_info"; 64 public static final String FORWARD_QUERY_STRING_ATTRIBUTE = "javax.servlet.forward.query_string"; 65 66 67 70 public static final String CONTENT_TYPE_CHARSET_PREFIX = ";charset="; 71 72 77 public static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1"; 78 79 83 public static final String TEMP_DIR_CONTEXT_ATTRIBUTE = "javax.servlet.context.tempdir"; 84 85 89 public static final String HTML_ESCAPE_CONTEXT_PARAM = "defaultHtmlEscape"; 90 91 95 public static final String WEB_APP_ROOT_KEY_PARAM = "webAppRootKey"; 96 97 98 public static final String DEFAULT_WEB_APP_ROOT_KEY = "webapp.root"; 99 100 101 public static final String [] SUBMIT_IMAGE_SUFFIXES = {".x", ".y"}; 102 103 104 public static final String SESSION_MUTEX_ATTRIBUTE = WebUtils.class.getName() + ".MUTEX"; 105 106 107 121 public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException { 122 Assert.notNull(servletContext, "ServletContext must not be null"); 123 String root = servletContext.getRealPath("/"); 124 if (root == null) { 125 throw new IllegalStateException ( 126 "Cannot set web app root system property when WAR file is not expanded"); 127 } 128 String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); 129 String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); 130 String oldValue = System.getProperty(key); 131 if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) { 132 throw new IllegalStateException ( 133 "Web app root system property already set to different value: '" + 134 key + "' = [" + oldValue + "] instead of [" + root + "] - " + 135 "Choose unique values for the 'webAppRootKey' context-param in your web.xml files!"); 136 } 137 System.setProperty(key, root); 138 servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]"); 139 } 140 141 147 public static void removeWebAppRootSystemProperty(ServletContext servletContext) { 148 Assert.notNull(servletContext, "ServletContext must not be null"); 149 String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); 150 String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); 151 System.getProperties().remove(key); 152 } 153 154 161 public static boolean isDefaultHtmlEscape(ServletContext servletContext) { 162 Assert.notNull(servletContext, "ServletContext must not be null"); 163 String param = servletContext.getInitParameter(HTML_ESCAPE_CONTEXT_PARAM); 164 return Boolean.valueOf(param).booleanValue(); 165 } 166 167 173 public static File getTempDir(ServletContext servletContext) { 174 Assert.notNull(servletContext, "ServletContext must not be null"); 175 return (File ) servletContext.getAttribute(TEMP_DIR_CONTEXT_ATTRIBUTE); 176 } 177 178 191 public static String getRealPath(ServletContext servletContext, String path) throws FileNotFoundException { 192 Assert.notNull(servletContext, "ServletContext must not be null"); 193 if (!path.startsWith("/")) { 195 path = "/" + path; 196 } 197 String realPath = servletContext.getRealPath(path); 198 if (realPath == null) { 199 throw new FileNotFoundException ( 200 "ServletContext resource [" + path + "] cannot be resolved to absolute file path - " + 201 "web application archive not expanded?"); 202 } 203 return realPath; 204 } 205 206 207 212 public static String getSessionId(HttpServletRequest request) { 213 Assert.notNull(request, "Request must not be null"); 214 HttpSession session = request.getSession(false); 215 return (session != null ? session.getId() : null); 216 } 217 218 226 public static Object getSessionAttribute(HttpServletRequest request, String name) { 227 Assert.notNull(request, "Request must not be null"); 228 HttpSession session = request.getSession(false); 229 return (session != null ? session.getAttribute(name) : null); 230 } 231 232 241 public static Object getRequiredSessionAttribute(HttpServletRequest request, String name) 242 throws IllegalStateException { 243 244 Object attr = getSessionAttribute(request, name); 245 if (attr == null) { 246 throw new IllegalStateException ("No session attribute '" + name + "' found"); 247 } 248 return attr; 249 } 250 251 259 public static void setSessionAttribute(HttpServletRequest request, String name, Object value) { 260 Assert.notNull(request, "Request must not be null"); 261 if (value != null) { 262 request.getSession().setAttribute(name, value); 263 } 264 else { 265 HttpSession session = request.getSession(false); 266 if (session != null) { 267 session.removeAttribute(name); 268 } 269 } 270 } 271 272 282 public static Object getOrCreateSessionAttribute(HttpSession session, String name, Class clazz) 283 throws IllegalArgumentException { 284 285 Assert.notNull(session, "Session must not be null"); 286 Object sessionObject = session.getAttribute(name); 287 if (sessionObject == null) { 288 try { 289 sessionObject = clazz.newInstance(); 290 } 291 catch (InstantiationException ex) { 292 throw new IllegalArgumentException ( 293 "Could not instantiate class [" + clazz.getName() + 294 "] for session attribute '" + name + "': " + ex.getMessage()); 295 } 296 catch (IllegalAccessException ex) { 297 throw new IllegalArgumentException ( 298 "Could not access default constructor of class [" + clazz.getName() + 299 "] for session attribute '" + name + "': " + ex.getMessage()); 300 } 301 session.setAttribute(name, sessionObject); 302 } 303 return sessionObject; 304 } 305 306 326 public static Object getSessionMutex(HttpSession session) { 327 Assert.notNull(session, "Session must not be null"); 328 Object mutex = session.getAttribute(SESSION_MUTEX_ATTRIBUTE); 329 if (mutex == null) { 330 mutex = session; 331 } 332 return mutex; 333 } 334 335 336 345 public static boolean isIncludeRequest(ServletRequest request) { 346 return (request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null); 347 } 348 349 362 public static void exposeForwardRequestAttributes(HttpServletRequest request) { 363 if (request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE) == null) { 364 request.setAttribute(FORWARD_REQUEST_URI_ATTRIBUTE, request.getRequestURI()); 365 } 366 if (request.getAttribute(FORWARD_CONTEXT_PATH_ATTRIBUTE) == null) { 367 request.setAttribute(FORWARD_CONTEXT_PATH_ATTRIBUTE, request.getContextPath()); 368 } 369 if (request.getAttribute(FORWARD_SERVLET_PATH_ATTRIBUTE) == null) { 370 request.setAttribute(FORWARD_SERVLET_PATH_ATTRIBUTE, request.getServletPath()); 371 } 372 if (request.getAttribute(FORWARD_PATH_INFO_ATTRIBUTE) == null) { 373 request.setAttribute(FORWARD_PATH_INFO_ATTRIBUTE, request.getPathInfo()); 374 } 375 if (request.getAttribute(FORWARD_QUERY_STRING_ATTRIBUTE) == null) { 376 request.setAttribute(FORWARD_QUERY_STRING_ATTRIBUTE, request.getQueryString()); 377 } 378 } 379 380 387 public static void exposeRequestAttributes(ServletRequest request, Map attributes) 388 throws IllegalArgumentException { 389 390 Assert.notNull(request, "Request must not be null"); 391 Iterator it = attributes.entrySet().iterator(); 392 while (it.hasNext()) { 393 Map.Entry entry = (Map.Entry ) it.next(); 394 if (!(entry.getKey() instanceof String )) { 395 throw new IllegalArgumentException ( 396 "Invalid key [" + entry.getKey() + "] in attributes Map - only Strings allowed as attribute keys"); 397 } 398 request.setAttribute((String ) entry.getKey(), entry.getValue()); 399 } 400 } 401 402 409 public static Cookie getCookie(HttpServletRequest request, String name) { 410 Assert.notNull(request, "Request must not be null"); 411 Cookie cookies[] = request.getCookies(); 412 if (cookies != null) { 413 for (int i = 0; i < cookies.length; i++) { 414 if (name.equals(cookies[i].getName())) { 415 return cookies[i]; 416 } 417 } 418 } 419 return null; 420 } 421 422 431 public static boolean hasSubmitParameter(ServletRequest request, String name) { 432 Assert.notNull(request, "Request must not be null"); 433 if (request.getParameter(name) != null) { 434 return true; 435 } 436 for (int i = 0; i < SUBMIT_IMAGE_SUFFIXES.length; i++) { 437 String suffix = SUBMIT_IMAGE_SUFFIXES[i]; 438 if (request.getParameter(name + suffix) != null) { 439 return true; 440 } 441 } 442 return false; 443 } 444 445 461 public static Map getParametersStartingWith(ServletRequest request, String prefix) { 462 Assert.notNull(request, "Request must not be null"); 463 Enumeration paramNames = request.getParameterNames(); 464 Map params = new TreeMap (); 465 if (prefix == null) { 466 prefix = ""; 467 } 468 while (paramNames != null && paramNames.hasMoreElements()) { 469 String paramName = (String ) paramNames.nextElement(); 470 if ("".equals(prefix) || paramName.startsWith(prefix)) { 471 String unprefixed = paramName.substring(prefix.length()); 472 String [] values = request.getParameterValues(paramName); 473 if (values == null) { 474 } 476 else if (values.length > 1) { 477 params.put(unprefixed, values); 478 } 479 else { 480 params.put(unprefixed, values[0]); 481 } 482 } 483 } 484 return params; 485 } 486 487 493 public static String extractFilenameFromUrlPath(String urlPath) { 494 int begin = urlPath.lastIndexOf('/') + 1; 495 int end = urlPath.indexOf(';'); 496 if (end == -1) { 497 end = urlPath.indexOf('?'); 498 if (end == -1) { 499 end = urlPath.length(); 500 } 501 } 502 String filename = urlPath.substring(begin, end); 503 int dotIndex = filename.lastIndexOf('.'); 504 if (dotIndex != -1) { 505 filename = filename.substring(0, dotIndex); 506 } 507 return filename; 508 } 509 510 } 511 | Popular Tags |