1 24 package org.riotfamily.common.web.util; 25 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.servlet.ServletContext ; 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import org.riotfamily.common.util.FormatUtils; 41 import org.riotfamily.common.xml.DocumentReader; 42 import org.springframework.util.StringUtils; 43 import org.springframework.util.xml.DomUtils; 44 import org.springframework.web.context.support.ServletContextResource; 45 import org.springframework.web.util.UrlPathHelper; 46 import org.springframework.web.util.WebUtils; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Element ; 49 50 public final class ServletUtils { 51 52 public static final String INCLUDE_URI_REQUEST_ATTRIBUTE = 53 "javax.servlet.include.request_uri"; 54 55 private static final String PRAGMA_HEADER = "Pragma"; 56 57 private static final String EXPIRES_HEADER = "Expires"; 58 59 private static final String CACHE_CONTROL_HEADER = "Cache-Control"; 60 61 public static final String REQUESTED_WITH_HEADER = "X-Requested-With"; 62 63 public static final String XML_HTTP_REQUEST = "XMLHttpRequest"; 64 65 public static final String SCHEME_HTTP = "http"; 66 67 public static final String SCHEME_HTTPS = "https"; 68 69 82 public static final String VALID_SCHEME_CHARS = 83 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-"; 84 85 private static UrlPathHelper urlPathHelper = new UrlPathHelper(); 86 87 private ServletUtils() { 88 } 89 90 public static String getOriginatingContextPath(HttpServletRequest request) { 91 return urlPathHelper.getOriginatingContextPath(request); 92 } 93 94 public static String getOriginatingRequestUri(HttpServletRequest request) { 95 return urlPathHelper.getOriginatingRequestUri(request); 96 } 97 98 public static String getOriginatingServletPath(HttpServletRequest request) { 99 String servletPath = (String ) request.getAttribute( 100 WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE); 101 102 if (servletPath == null) { 103 servletPath = request.getServletPath(); 104 } 105 return servletPath; 106 } 107 108 112 public static String getPathWithinApplication(HttpServletRequest request) { 113 return urlPathHelper.getPathWithinApplication(request); 114 } 115 116 121 public static String getOriginatingPathWithinApplication(HttpServletRequest request) { 122 String contextPath = getOriginatingContextPath(request); 123 String requestUri = getOriginatingRequestUri(request); 124 if (StringUtils.startsWithIgnoreCase(requestUri, contextPath)) { 125 String path = requestUri.substring(contextPath.length()); 127 return (StringUtils.hasText(path) ? path : "/"); 128 } 129 else { 130 return requestUri; 132 } 133 } 134 135 145 public static String getOriginatingPathWithinServletMapping(HttpServletRequest request) { 146 String pathWithinApp = getOriginatingPathWithinApplication(request); 147 String servletPath = getOriginatingServletPath(request); 148 if (pathWithinApp.startsWith(servletPath)) { 149 return pathWithinApp.substring(servletPath.length()); 151 } 152 else { 153 return servletPath; 157 } 158 } 159 160 166 public static String getPathWithoutServletMapping( 167 HttpServletRequest request) { 168 169 String path = urlPathHelper.getPathWithinServletMapping(request); 170 if (path.length() == 0) { 171 path = urlPathHelper.getPathWithinApplication(request); 172 if (path.equals(getServletPrefix(request))) { 173 return "/"; 174 } 175 int dotIndex = path.lastIndexOf('.'); 176 if (dotIndex != -1 && dotIndex > path.lastIndexOf('/')) { 177 path = path.substring(0, dotIndex); 178 } 179 } 180 return path; 181 } 182 183 189 public static String getOriginatingPathWithoutServletMapping( 190 HttpServletRequest request) { 191 192 String path = getOriginatingPathWithinServletMapping(request); 193 if (path.length() == 0) { 194 path = getOriginatingPathWithinApplication(request); 195 if (path.equals(getServletPrefix(request))) { 196 return "/"; 197 } 198 int dotIndex = path.lastIndexOf('.'); 199 if (dotIndex >= 0) { 200 path = path.substring(0, dotIndex); 201 } 202 } 203 return path; 204 } 205 206 210 public static String getServletPrefix(HttpServletRequest request) { 211 String path = urlPathHelper.getPathWithinApplication(request); 212 String servletPath = urlPathHelper.getServletPath(request); 213 if (path.length() > servletPath.length() 214 || (path.equals(servletPath) && path.indexOf('.') == -1)) { 215 216 return servletPath; 217 } 218 return ""; 219 } 220 221 225 public static String getServletSuffix(HttpServletRequest request) { 226 String path = urlPathHelper.getPathWithinApplication(request); 227 if (path.equals(urlPathHelper.getServletPath(request))) { 228 int dotIndex = path.lastIndexOf('.'); 229 if (dotIndex >= 0) { 230 return path.substring(dotIndex); 231 } 232 } 233 return ""; 234 } 235 236 240 public static String addServletMapping(String path, 241 HttpServletRequest request) { 242 243 String suffix = getServletPrefix(request); 244 if (suffix.length() > 0) { 245 return path + suffix; 246 } 247 return getServletPrefix(request) + path; 248 } 249 250 254 public static String getRootPath(HttpServletRequest request) { 255 StringBuffer path = new StringBuffer (); 256 path.append(getOriginatingContextPath(request)); 257 path.append(getServletPrefix(request)); 258 path.append('/'); 259 return path.toString(); 260 } 261 262 266 public static boolean isAbsoluteUrl(String url) { 267 if (url == null) { 269 return false; 270 } 271 int colonPos; 273 if ((colonPos = url.indexOf(":")) == -1) { 274 return false; 275 } 276 for (int i = 0; i < colonPos; i++) { 279 if (VALID_SCHEME_CHARS.indexOf(url.charAt(i)) == -1) { 280 return false; 281 } 282 } 283 return true; 285 } 286 287 290 public static boolean isHttpUrl(String url) { 291 return isAbsoluteUrl(url) && url.startsWith("http"); 292 } 293 294 public static String resolveUrl(String url, HttpServletRequest request) { 295 if (url == null || isAbsoluteUrl(url)) { 296 return url; 297 } 298 if (url.startsWith("/")) { 299 url = request.getContextPath() + url; 300 } 301 return url; 302 } 303 304 public static String resolveAndEncodeUrl(String url, 305 HttpServletRequest request, HttpServletResponse response) { 306 307 if (url == null || isAbsoluteUrl(url)) { 308 return url; 309 } 310 url = resolveUrl(url, request); 311 return response.encodeURL(url); 312 } 313 314 public static String getIncludeUri(HttpServletRequest request) { 315 String uri = (String ) request.getAttribute( 316 INCLUDE_URI_REQUEST_ATTRIBUTE); 317 318 if (uri == null) { 319 uri = request.getRequestURI(); 320 } 321 return uri; 322 } 323 324 public static Map takeAttributesSnapshot(HttpServletRequest request) { 325 Map snapshot = new HashMap (); 326 Enumeration attrNames = request.getAttributeNames(); 327 while (attrNames.hasMoreElements()) { 328 String attrName = (String ) attrNames.nextElement(); 329 snapshot.put(attrName, request.getAttribute(attrName)); 330 } 331 return snapshot; 332 } 333 334 337 public static void restoreAttributes(HttpServletRequest request, 338 Map attributesSnapshot) { 339 340 Set attrsToCheck = new HashSet (); 342 Enumeration attrNames = request.getAttributeNames(); 343 while (attrNames.hasMoreElements()) { 344 String attrName = (String ) attrNames.nextElement(); 345 attrsToCheck.add(attrName); 346 } 347 348 Iterator it = attrsToCheck.iterator(); 349 while (it.hasNext()) { 350 String attrName = (String ) it.next(); 351 Object attrValue = attributesSnapshot.get(attrName); 352 if (attrValue != null) { 353 request.setAttribute(attrName, attrValue); 354 } 355 else { 356 request.removeAttribute(attrName); 357 } 358 } 359 } 360 361 367 public static Map getSingularParameterMap(HttpServletRequest request) { 368 HashMap params = new HashMap (); 369 Enumeration names = request.getParameterNames(); 370 while (names.hasMoreElements()) { 371 String name = (String ) names.nextElement(); 372 params.put(name, request.getParameter(name)); 373 } 374 return params; 375 } 376 377 383 public static String getPath(String uri) { 384 try { 385 return new URI (uri).getPath(); 386 } 387 catch (URISyntaxException e) { 388 throw new IllegalArgumentException (e.getMessage()); 389 } 390 } 391 392 396 public static StringBuffer getAbsoluteUrlPrefix(HttpServletRequest request) { 397 StringBuffer url = new StringBuffer (); 398 String scheme = request.getScheme(); 399 int port = request.getServerPort(); 400 if (port <= 0) { 401 port = 80; 402 } 403 url.append(scheme); 404 url.append("://"); 405 url.append(request.getServerName()); 406 if ((scheme.equals(SCHEME_HTTP) && port != 80) 407 || (scheme.equals(SCHEME_HTTPS) && port != 443)) { 408 409 url.append(':'); 410 url.append(port); 411 } 412 return url; 413 } 414 415 419 public static boolean isXmlHttpRequest(HttpServletRequest request) { 420 return XML_HTTP_REQUEST.equals(request.getHeader(REQUESTED_WITH_HEADER)); 421 } 422 423 427 public static void setNoCacheHeaders(HttpServletResponse response) { 428 response.setHeader(PRAGMA_HEADER, "No-cache"); 429 response.setDateHeader(EXPIRES_HEADER, 1L); 430 response.setHeader(CACHE_CONTROL_HEADER, "no-cache"); 431 response.addHeader(CACHE_CONTROL_HEADER, "no-store"); 432 } 433 434 440 public static void setCacheHeaders(HttpServletResponse response, String period) { 441 long millis = FormatUtils.parseMillis(period); 442 response.setDateHeader(EXPIRES_HEADER, System.currentTimeMillis() + millis); 443 response.setHeader(CACHE_CONTROL_HEADER, "max-age=" + millis / 1000L); 444 } 445 446 451 public static String getServletMapping(String servletName, 452 ServletContext servletContext) { 453 454 DocumentReader reader = new DocumentReader(new ServletContextResource( 455 servletContext, "/WEB-INF/web.xml")); 456 457 Document doc = reader.readDocument(); 458 Iterator it = DomUtils.getChildElementsByTagName( 459 doc.getDocumentElement(), "servlet-mapping").iterator(); 460 461 while (it.hasNext()) { 462 Element e = (Element ) it.next(); 463 Element name = DomUtils.getChildElementByTagName(e, "servlet-name"); 464 if (servletName.equals(DomUtils.getTextValue(name))) { 465 return DomUtils.getTextValue(DomUtils.getChildElementByTagName( 466 e, "url-pattern")).trim(); 467 } 468 } 469 return null; 470 } 471 472 public static String addParameter(String url, String name, String value) { 473 StringBuffer sb = new StringBuffer (url); 474 boolean first = url.indexOf('?') == -1; 475 sb.append(first ? '?' : '&'); 476 sb.append(name).append('=').append(FormatUtils.uriEscape(value)); 477 return sb.toString(); 478 } 479 480 public static String getRequestUrlWithQueryString( 481 HttpServletRequest request) { 482 483 request.getRequestURI(); 484 StringBuffer sb = request.getRequestURL(); 485 String queryString = request.getQueryString(); 486 if (queryString != null) { 487 sb.append('?').append(queryString); 488 } 489 return sb.toString(); 490 } 491 492 } 493 | Popular Tags |