private int getLookupPathIndex(HttpServletRequest request) { UrlPathHelper pathHelper = getUrlPathHelper(); String requestUri = pathHelper.getRequestUri(request); String lookupPath = pathHelper.getLookupPathForRequest(request); return requestUri.indexOf(lookupPath); }
private void initLookupPath(ResourceUrlProvider urlProvider) { this.resourceUrlProvider = urlProvider; if (this.indexLookupPath == null) { UrlPathHelper pathHelper = this.resourceUrlProvider.getUrlPathHelper(); String requestUri = pathHelper.getRequestUri(this); String lookupPath = pathHelper.getLookupPathForRequest(this); this.indexLookupPath = requestUri.lastIndexOf(lookupPath); this.prefixLookupPath = requestUri.substring(0, this.indexLookupPath); if ("/".equals(lookupPath) && !"/".equals(requestUri)) { String contextPath = pathHelper.getContextPath(this); if (requestUri.equals(contextPath)) { this.indexLookupPath = requestUri.length(); this.prefixLookupPath = requestUri; } } } }
@Nullable private String decodeAndNormalizePath(@Nullable String path, HttpServletRequest request) { if (path != null) { path = getUrlPathHelper().decodeRequestString(request, path); if (path.charAt(0) != '/') { String requestUri = getUrlPathHelper().getRequestUri(request); path = requestUri.substring(0, requestUri.lastIndexOf('/') + 1) + path; path = StringUtils.cleanPath(path); } } return path; }
/** * Return the path within the web application for the given request. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the path within the web application */ public String getPathWithinApplication(HttpServletRequest request) { String contextPath = getContextPath(request); String requestUri = getRequestUri(request); String path = getRemainingPath(requestUri, contextPath, true); if (path != null) { // Normal case: URI contains context path. return (StringUtils.hasText(path) ? path : "/"); } else { return requestUri; } }
private int getLookupPathIndex(HttpServletRequest request) { UrlPathHelper pathHelper = getUrlPathHelper(); String requestUri = pathHelper.getRequestUri(request); String lookupPath = pathHelper.getLookupPathForRequest(request); return requestUri.indexOf(lookupPath); }
/** * Transform the given relative request path to an absolute path, * taking the path of the given request as a point of reference. * The resulting path is also cleaned from sequences like "path/..". * @param path the relative path to transform * @param request the referer request * @return the absolute request path for the given resource path */ protected String toAbsolutePath(String path, HttpServletRequest request) { String absolutePath = path; if (!path.startsWith("/")) { ResourceUrlProvider urlProvider = findResourceUrlProvider(request); Assert.state(urlProvider != null, "No ResourceUrlProvider"); String requestPath = urlProvider.getUrlPathHelper().getRequestUri(request); absolutePath = StringUtils.applyRelativePath(requestPath, path); } return StringUtils.cleanPath(absolutePath); }
private void initLookupPath(ResourceUrlProvider urlProvider) { this.resourceUrlProvider = urlProvider; if (this.indexLookupPath == null) { UrlPathHelper pathHelper = this.resourceUrlProvider.getUrlPathHelper(); String requestUri = pathHelper.getRequestUri(this); String lookupPath = pathHelper.getLookupPathForRequest(this); this.indexLookupPath = requestUri.lastIndexOf(lookupPath); this.prefixLookupPath = requestUri.substring(0, this.indexLookupPath); if ("/".equals(lookupPath) && !"/".equals(requestUri)) { String contextPath = pathHelper.getContextPath(this); if (requestUri.equals(contextPath)) { this.indexLookupPath = requestUri.length(); this.prefixLookupPath = requestUri; } } } }
@Nullable private String decodeAndNormalizePath(@Nullable String path, HttpServletRequest request) { if (path != null) { path = getUrlPathHelper().decodeRequestString(request, path); if (path.charAt(0) != '/') { String requestUri = getUrlPathHelper().getRequestUri(request); path = requestUri.substring(0, requestUri.lastIndexOf('/') + 1) + path; path = StringUtils.cleanPath(path); } } return path; }
/** * Return the path within the web application for the given request. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the path within the web application */ public String getPathWithinApplication(HttpServletRequest request) { String contextPath = getContextPath(request); String requestUri = getRequestUri(request); String path = getRemainingPath(requestUri, contextPath, true); if (path != null) { // Normal case: URI contains context path. return (StringUtils.hasText(path) ? path : "/"); } else { return requestUri; } }
/** * Transform the given relative request path to an absolute path, * taking the path of the given request as a point of reference. * The resulting path is also cleaned from sequences like "path/..". * @param path the relative path to transform * @param request the referer request * @return the absolute request path for the given resource path */ protected String toAbsolutePath(String path, HttpServletRequest request) { String absolutePath = path; if (!path.startsWith("/")) { ResourceUrlProvider urlProvider = findResourceUrlProvider(request); Assert.state(urlProvider != null, "No ResourceUrlProvider"); String requestPath = urlProvider.getUrlPathHelper().getRequestUri(request); absolutePath = StringUtils.applyRelativePath(requestPath, path); } return StringUtils.cleanPath(absolutePath); }
@Test public void getRequestUri() { request.setRequestURI("/welcome.html"); assertEquals("Incorrect path returned", "/welcome.html", helper.getRequestUri(request)); request.setRequestURI("/foo%20bar"); assertEquals("Incorrect path returned", "/foo bar", helper.getRequestUri(request)); request.setRequestURI("/foo+bar"); assertEquals("Incorrect path returned", "/foo+bar", helper.getRequestUri(request)); }
@Test public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException { helper.setRemoveSemicolonContent(false); request.setRequestURI("/foo;a=b;c=d"); assertEquals("/foo;a=b;c=d", helper.getRequestUri(request)); request.setRequestURI("/foo;jsessionid=c0o7fszeb1"); assertEquals("jsessionid should always be removed", "/foo", helper.getRequestUri(request)); request.setRequestURI("/foo;a=b;jsessionid=c0o7fszeb1;c=d"); assertEquals("jsessionid should always be removed", "/foo;a=b;c=d", helper.getRequestUri(request)); // SPR-10398 request.setRequestURI("/foo;a=b;JSESSIONID=c0o7fszeb1;c=d"); assertEquals("JSESSIONID should always be removed", "/foo;a=b;c=d", helper.getRequestUri(request)); }
@Test public void getRequestRemoveSemicolonContent() throws UnsupportedEncodingException { helper.setRemoveSemicolonContent(true); request.setRequestURI("/foo;f=F;o=O;o=O/bar;b=B;a=A;r=R"); assertEquals("/foo/bar", helper.getRequestUri(request)); // SPR-13455 request.setServletPath("/foo/1"); request.setRequestURI("/foo/;test/1"); assertEquals("/foo/1", helper.getRequestUri(request)); }
/** * Return the request URI for the given request, detecting an include request * URL if called within a RequestDispatcher include. * @see UrlPathHelper#getRequestUri(HttpServletRequest) */ public static String getRequestUri(HttpServletRequest request) { return urlPathHelper.getRequestUri(request); }
/** * Create a new NoSuchRequestHandlingMethodException for the given request. * @param request the offending HTTP request */ public NoSuchRequestHandlingMethodException(HttpServletRequest request) { this(new UrlPathHelper().getRequestUri(request), request.getMethod(), request.getParameterMap()); }
/** * No handler found -> set appropriate HTTP response status. * * @param request current HTTP request * @param response current HTTP response * @throws Exception if preparing the response failed */ protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception { String requestUri = urlPathHelper.getRequestUri(request); logger.logMessage(LogLevel.WARN, "No mapping found for HTTP request with URI [" + requestUri + "] in SpringMvcTinyProcessor"); response.sendError(HttpServletResponse.SC_NOT_FOUND); }
@Override protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception { String requestUri = urlPathHelper.getRequestUri(request); if (logger.isWarnEnabled()) { logger.warn("No mapping found for HTTP request with URI [" + requestUri + "] in DispatcherServlet with name '" + getServletName() + "'"); } String header = request.getHeader("X-Requested-With"); if ("XMLHttpRequest".equals(header)) { JsonResultUtils.writeAjaxErrorMessage(ResponseData.ERROR_NOT_FOUND, "未找到此资源("+requestUri+")", response); return; } response.sendRedirect(request.getContextPath() + "/system/exception/error/404"); }
private int getLookupPathIndex(HttpServletRequest request) { UrlPathHelper pathHelper = getUrlPathHelper(); String requestUri = pathHelper.getRequestUri(request); String lookupPath = pathHelper.getLookupPathForRequest(request); return requestUri.indexOf(lookupPath); }
@Override protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception { String requestUri = new UrlPathHelper().getRequestUri(request); throw new BadRequestException(requestUri, request.getParameterMap(), request.getMethod(), getServletName()); } }
private String decodeAndNormalizePath(String path, HttpServletRequest request) { if (path != null) { path = getUrlPathHelper().decodeRequestString(request, path); if (path.charAt(0) != '/') { String requestUri = getUrlPathHelper().getRequestUri(request); path = requestUri.substring(0, requestUri.lastIndexOf('/') + 1) + path; path = StringUtils.cleanPath(path); } } return path; }