Refine search
/** * 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; }
/** * Extract a URL path from the given request, * suitable for view name extraction. * @param request current HTTP request * @return the URL to use for view name extraction */ protected String extractOperableUrl(HttpServletRequest request) { String urlPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); if (!StringUtils.hasText(urlPath)) { urlPath = getUrlPathHelper().getLookupPathForRequest(request); } return urlPath; }
String pathWithinApp = getPathWithinApplication(request); String servletPath = getServletPath(request); String sanitizedPathWithinApp = getSanitizedPath(pathWithinApp); String path; path = getRemainingPath(sanitizedPathWithinApp, servletPath, false); path = getRemainingPath(pathWithinApp, servletPath, false); String pathInfo = request.getPathInfo(); if (pathInfo != null) { path = getRemainingPath(decodeInternal(request, pathWithinApp), servletPath, false); if (path != null) { return pathWithinApp;
public ForwardedHeaderFilter() { this.pathHelper = new UrlPathHelper(); this.pathHelper.setUrlDecode(false); this.pathHelper.setRemoveSemicolonContent(false); }
/** * 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 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; } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { String path = urlPathHelper.getLookupPathForRequest(request); String ctxPath = urlPathHelper.getOriginatingContextPath(request); if (adminContextPath.equals(path)) { try { HttpSession session = request.getSession(); SysSite site = siteComponent.getSite(request.getServerName()); SysUser user = initUser(ControllerUtils.getAdminFromSession(session), LogLoginService.CHANNEL_WEB_MANAGER, CommonConstants.getCookiesAdmin(), site, request, response); if (null == user) { try { redirectLogin(ctxPath, path, request.getQueryString(), request.getHeader("X-Requested-With"), response); return false; } catch (IllegalStateException | IOException e) {
if (request.getParameter("headerFlash") != null) { model.addAttribute("headerFlash", request.getParameter("headerFlash")); String originatingUri = new UrlPathHelper().getOriginatingRequestUri(request); int startIndex = request.getContextPath().length();
@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()); } }
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, servletContext); WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext()); this.urlPathHelper = new UrlPathHelper();
@Override public String getSectionKey(boolean withTypeKey) { HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest(); if (request != null) { String originatingUri = new UrlPathHelper().getOriginatingRequestUri(request); int startIndex = request.getContextPath().length(); String sectionKey = originatingUri.substring(startIndex); int endIndex = sectionKey.indexOf("/", 1); if (endIndex > 0) { // If we want a 'typeKey', grab a new end index if (withTypeKey) { endIndex = sectionKey.indexOf("/", endIndex); } // check again to make sure there is an end index if (endIndex > 0) { sectionKey = sectionKey.substring(0, endIndex); } } return sectionKey; } return null; } }
@Override protected Resource getResource(HttpServletRequest request) throws IOException { String path = urlPathHelper.getLookupPathForRequest(request); if (path.endsWith(CommonConstants.SEPARATOR)) { path += CommonConstants.getDefaultPage(); } SysSite site = siteComponent.getSite(request.getServerName()); Resource resource = new FileSystemResource(siteComponent.getWebFilePath(site, path)); if (resource.exists()) { if (resource.isReadable()) { return resource; } } else if (null != site.getParentId()) { resource = new FileSystemResource(siteComponent.getParentSiteWebFilePath(site, path)); if (resource.exists() && resource.isReadable()) { return resource; } } return null; } }
@SuppressWarnings("unchecked") @Test // SPR-9098 public void handleMatchUriTemplateVariablesDecode() { RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb"); UrlPathHelper pathHelper = new UrlPathHelper(); pathHelper.setUrlDecode(false); String lookupPath = pathHelper.getLookupPathForRequest(request); this.handlerMapping.setUrlPathHelper(pathHelper); this.handlerMapping.handleMatch(key, lookupPath, request); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name); assertNotNull(uriVariables); assertEquals("group", uriVariables.get("group")); assertEquals("a/b", uriVariables.get("identifier")); }
@Override @Nullable public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); for (Map.Entry<String, CorsConfiguration> entry : this.corsConfigurations.entrySet()) { if (this.pathMatcher.match(entry.getKey(), lookupPath)) { return entry.getValue(); } } return null; }
@Nullable private String initRequestUri() { if (this.forwardedPrefix != null) { return this.forwardedPrefix + this.pathHelper.getPathWithinApplication(this.delegate.get()); } return null; }
/** * Return a global {@link UrlPathHelper} instance for path matching * patterns in {@link HandlerMapping HandlerMappings}. * This instance can be configured using the {@link PathMatchConfigurer} * in {@link #configurePathMatch(PathMatchConfigurer)}. * @since 4.1 */ @Bean public UrlPathHelper mvcUrlPathHelper() { UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper(); return (pathHelper != null ? pathHelper : new UrlPathHelper()); }
@SuppressWarnings("unchecked") @Test public void handleMatchUriTemplateVariables() { RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/{path2}").build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); String lookupPath = new UrlPathHelper().getLookupPathForRequest(request); this.handlerMapping.handleMatch(key, lookupPath, request); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name); assertNotNull(uriVariables); assertEquals("1", uriVariables.get("path1")); assertEquals("2", uriVariables.get("path2")); }
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; } } } }
/** * 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 int getLookupPathIndex(HttpServletRequest request) { UrlPathHelper pathHelper = getUrlPathHelper(); String requestUri = pathHelper.getRequestUri(request); String lookupPath = pathHelper.getLookupPathForRequest(request); return requestUri.indexOf(lookupPath); }