1 16 17 package org.springframework.web.util; 18 19 import java.io.UnsupportedEncodingException ; 20 import java.net.URLDecoder ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.springframework.core.JdkVersion; 28 import org.springframework.util.StringUtils; 29 30 44 public class UrlPathHelper { 45 46 50 public static final String INCLUDE_URI_REQUEST_ATTRIBUTE = WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE; 51 52 56 public static final String INCLUDE_CONTEXT_PATH_REQUEST_ATTRIBUTE = WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE; 57 58 62 public static final String INCLUDE_SERVLET_PATH_REQUEST_ATTRIBUTE = WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE; 63 64 65 private final Log logger = LogFactory.getLog(getClass()); 66 67 private boolean alwaysUseFullPath = false; 68 69 private boolean urlDecode = false; 70 71 private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; 72 73 74 80 public void setAlwaysUseFullPath(boolean alwaysUseFullPath) { 81 this.alwaysUseFullPath = alwaysUseFullPath; 82 } 83 84 101 public void setUrlDecode(boolean urlDecode) { 102 this.urlDecode = urlDecode; 103 } 104 105 118 public void setDefaultEncoding(String defaultEncoding) { 119 this.defaultEncoding = defaultEncoding; 120 } 121 122 125 protected String getDefaultEncoding() { 126 return defaultEncoding; 127 } 128 129 130 139 public String getLookupPathForRequest(HttpServletRequest request) { 140 if (this.alwaysUseFullPath) { 142 return getPathWithinApplication(request); 143 } 144 String rest = getPathWithinServletMapping(request); 146 if (!"".equals(rest)) { 147 return rest; 148 } 149 else { 150 return getPathWithinApplication(request); 151 } 152 } 153 154 165 public String getPathWithinServletMapping(HttpServletRequest request) { 166 String pathWithinApp = getPathWithinApplication(request); 167 String servletPath = getServletPath(request); 168 if (pathWithinApp.startsWith(servletPath)) { 169 return pathWithinApp.substring(servletPath.length()); 171 } 172 else { 173 return servletPath; 177 } 178 } 179 180 186 public String getPathWithinApplication(HttpServletRequest request) { 187 String contextPath = getContextPath(request); 188 String requestUri = getRequestUri(request); 189 if (StringUtils.startsWithIgnoreCase(requestUri, contextPath)) { 190 String path = requestUri.substring(contextPath.length()); 192 return (StringUtils.hasText(path) ? path : "/"); 193 } 194 else { 195 return requestUri; 197 } 198 } 199 200 201 212 public String getRequestUri(HttpServletRequest request) { 213 String uri = (String ) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE); 214 if (uri == null) { 215 uri = request.getRequestURI(); 216 } 217 return decodeAndCleanUriString(request, uri); 218 } 219 220 228 public String getContextPath(HttpServletRequest request) { 229 String contextPath = (String ) request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE); 230 if (contextPath == null) { 231 contextPath = request.getContextPath(); 232 } 233 return decodeRequestString(request, contextPath); 234 } 235 236 244 public String getServletPath(HttpServletRequest request) { 245 String servletPath = (String ) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE); 246 if (servletPath == null) { 247 servletPath = request.getServletPath(); 248 } 249 return servletPath; 250 } 251 252 253 259 public String getOriginatingRequestUri(HttpServletRequest request) { 260 String uri = (String ) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE); 261 if (uri == null) { 262 uri = request.getRequestURI(); 263 } 264 return decodeAndCleanUriString(request, uri); 265 } 266 267 275 public String getOriginatingContextPath(HttpServletRequest request) { 276 String contextPath = (String ) request.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE); 277 if (contextPath == null) { 278 contextPath = request.getContextPath(); 279 } 280 return decodeRequestString(request, contextPath); 281 } 282 283 289 public String getOriginatingQueryString(HttpServletRequest request) { 290 String queryString = (String ) request.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE); 291 if (queryString == null) { 292 queryString = request.getQueryString(); 293 } 294 return queryString; 295 } 296 297 298 301 private String decodeAndCleanUriString(HttpServletRequest request, String uri) { 302 uri = decodeRequestString(request, uri); 303 int semicolonIndex = uri.indexOf(';'); 304 return (semicolonIndex != -1 ? uri.substring(0, semicolonIndex) : uri); 305 } 306 307 321 public String decodeRequestString(HttpServletRequest request, String source) { 322 if (this.urlDecode) { 323 String enc = determineEncoding(request); 324 try { 325 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 326 throw new UnsupportedEncodingException ("JDK 1.3 URLDecoder does not support custom encoding"); 327 } 328 return URLDecoder.decode(source, enc); 329 } 330 catch (UnsupportedEncodingException ex) { 331 if (logger.isWarnEnabled()) { 332 logger.warn("Could not decode request string [" + source + "] with encoding '" + enc + 333 "': falling back to platform default encoding; exception message: " + ex.getMessage()); 334 } 335 return URLDecoder.decode(source); 336 } 337 } 338 return source; 339 } 340 341 351 protected String determineEncoding(HttpServletRequest request) { 352 String enc = request.getCharacterEncoding(); 353 if (enc == null) { 354 enc = getDefaultEncoding(); 355 } 356 return enc; 357 } 358 359 } 360 | Popular Tags |