/** * Return the path to URL mappings within the current servlet including the * context path and the servlet path of the original request. This is useful * for building links to other resources within the application where a * servlet mapping of the style {@code "/main/*"} is used. * <p>Delegates to the UrlPathHelper to determine the context and servlet path. */ public String getPathToServlet() { String path = this.urlPathHelper.getOriginatingContextPath(this.request); if (StringUtils.hasText(this.urlPathHelper.getPathWithinServletMapping(this.request))) { path += this.urlPathHelper.getOriginatingServletPath(this.request); } return path; }
/** * Return the mapping lookup path for the given request, within the current * servlet mapping if applicable, else within the web application. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the lookup path * @see #getPathWithinApplication * @see #getPathWithinServletMapping */ public String getLookupPathForRequest(HttpServletRequest request) { // Always use full path within current servlet context? if (this.alwaysUseFullPath) { return getPathWithinApplication(request); } // Else, use path within current servlet mapping if applicable String rest = getPathWithinServletMapping(request); if (!"".equals(rest)) { return rest; } else { return getPathWithinApplication(request); } }
/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
/** * Return the path to URL mappings within the current servlet including the * context path and the servlet path of the original request. This is useful * for building links to other resources within the application where a * servlet mapping of the style {@code "/main/*"} is used. * <p>Delegates to the UrlPathHelper to determine the context and servlet path. */ public String getPathToServlet() { String path = this.urlPathHelper.getOriginatingContextPath(this.request); if (StringUtils.hasText(this.urlPathHelper.getPathWithinServletMapping(this.request))) { path += this.urlPathHelper.getOriginatingServletPath(this.request); } return path; }
/** * Return the mapping lookup path for the given request, within the current * servlet mapping if applicable, else within the web application. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the lookup path * @see #getPathWithinApplication * @see #getPathWithinServletMapping */ public String getLookupPathForRequest(HttpServletRequest request) { // Always use full path within current servlet context? if (this.alwaysUseFullPath) { return getPathWithinApplication(request); } // Else, use path within current servlet mapping if applicable String rest = getPathWithinServletMapping(request); if (!"".equals(rest)) { return rest; } else { return getPathWithinApplication(request); } }
/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
@Test public void getPathWithinServlet() { request.setContextPath("/petclinic"); request.setServletPath("/main"); request.setRequestURI("/petclinic/main/welcome.html"); assertEquals("Incorrect path returned", "/welcome.html", helper.getPathWithinServletMapping(request)); }
@Test public void getPathWithinServletWithoutUrlDecoding() { request.setContextPath("/SPR-11101"); request.setServletPath("/test_url_decoding/a/b"); request.setRequestURI("/test_url_decoding/a%2Fb"); helper.setUrlDecode(false); String actual = helper.getPathWithinServletMapping(request); assertEquals("/test_url_decoding/a%2Fb", actual); }
private boolean prependServletPath(HttpServletRequest request) { return !this.urlPathHelper.getPathWithinServletMapping(request).equals(""); }
@Override public String getPathInfo() { String pathInfo = super.getPathInfo(); if (pathInfo == null) { // this uses getServletPath() and should work both with and without // clearServletPath pathInfo = urlPathHelper.getPathWithinServletMapping(this); } return pathInfo; } }
/** * Return the path to URL mappings within the current servlet including the * context path and the servlet path of the original request. This is useful * for building links to other resources within the application where a * servlet mapping of the style {@code "/main/*"} is used. * <p>Delegates to the UrlPathHelper to determine the context and servlet path. */ public String getPathToServlet() { String path = this.urlPathHelper.getOriginatingContextPath(this.request); if (StringUtils.hasText(this.urlPathHelper.getPathWithinServletMapping(this.request))) { path += this.urlPathHelper.getOriginatingServletPath(this.request); } return path; }
/** * Returns the lookup-path for a given request. This is either the path * within the servlet-mapping (in case of a prefix mapping) or the path * within the application without the trailing suffix (in case of a suffix * mapping). */ public static String getPathWithoutServletMapping( HttpServletRequest request) { String path = urlPathHelper.getPathWithinServletMapping(request); if (path.length() == 0) { path = urlPathHelper.getPathWithinApplication(request); if (path.equals(getServletPrefix(request))) { return "/"; } int dotIndex = path.lastIndexOf('.'); if (dotIndex != -1 && dotIndex > path.lastIndexOf('/')) { path = path.substring(0, dotIndex); } } return path; }
/** * Return the path to URL mappings within the current servlet including the * context path and the servlet path of the original request. This is useful * for building links to other resources within the application where a * servlet mapping of the style {@code "/main/*"} is used. * <p>Delegates to the UrlPathHelper to determine the context and servlet path. */ public String getPathToServlet() { String path = this.urlPathHelper.getOriginatingContextPath(this.request); if (StringUtils.hasText(this.urlPathHelper.getPathWithinServletMapping(this.request))) { path += this.urlPathHelper.getOriginatingServletPath(this.request); } return path; }
/** * Prepare a builder from the host, port, scheme, context path, and * servlet mapping of the given HttpServletRequest. * <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path * will end with "/main". If the servlet is mapped otherwise, e.g. * {@code "/"} or {@code "*.do"}, the result will be the same as * if calling {@link #fromContextPath(HttpServletRequest)}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * <p>As of 4.3.15, this method replaces the contextPath with the value * of "X-Forwarded-Prefix" rather than prepending, thus aligning with * {@code ForwardedHeaderFilter}. */ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) { ServletUriComponentsBuilder builder = fromContextPath(request); if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
/** * Return the mapping lookup path for the given request, within the current * servlet mapping if applicable, else within the web application. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the lookup path * @see #getPathWithinApplication * @see #getPathWithinServletMapping */ public String getLookupPathForRequest(HttpServletRequest request) { // Always use full path within current servlet context? if (this.alwaysUseFullPath) { return getPathWithinApplication(request); } // Else, use path within current servlet mapping if applicable String rest = getPathWithinServletMapping(request); if (!"".equals(rest)) { return rest; } else { return getPathWithinApplication(request); } }
/** * Return the mapping lookup path for the given request, within the current * servlet mapping if applicable, else within the web application. * <p>Regards include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the lookup path * @see #getPathWithinApplication * @see #getPathWithinServletMapping */ public String getLookupPathForRequest(HttpServletRequest request) { // Always use full path within current servlet context? if (this.alwaysUseFullPath) { return getPathWithinApplication(request); } // Else, use path within current servlet mapping if applicable String rest = getPathWithinServletMapping(request); if (!"".equals(rest)) { return rest; } else { return getPathWithinApplication(request); } }
/** * Return the mapping lookup path for the given request, within the current * servlet mapping if applicable, else within the web application. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the lookup path * @see #getPathWithinApplication * @see #getPathWithinServletMapping */ public String getLookupPathForRequest(HttpServletRequest request) { // Always use full path within current servlet context? if (this.alwaysUseFullPath) { return getPathWithinApplication(request); } // Else, use path within current servlet mapping if applicable String rest = getPathWithinServletMapping(request); if (!"".equals(rest)) { return rest; } else { return getPathWithinApplication(request); } }
private static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request, String basePath) { ServletUriComponentsBuilder builder = fromContextPath(request); builder.replacePath(prependForwardedPrefix(request, basePath)); if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; }
private static ServletUriComponentsBuilder fromServletMapping( HttpServletRequest request, String basePath) { ServletUriComponentsBuilder builder = fromContextPath(request); XForwardPrefixPathAdjuster adjuster = new XForwardPrefixPathAdjuster(request); builder.replacePath(adjuster.adjustedPath(basePath)); if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; } }
private static ServletUriComponentsBuilder fromServletMapping( HttpServletRequest request, String basePath) { ServletUriComponentsBuilder builder = fromContextPath(request); SwaggerBootstrapUiXForwardPrefixPathAdjuster adjuster = new SwaggerBootstrapUiXForwardPrefixPathAdjuster(request); builder.replacePath(adjuster.adjustedPath(basePath)); if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) { builder.path(request.getServletPath()); } return builder; } }