diff --git a/spring-web/src/main/java/org/springframework/web/util/ServletRequestPathUtils.java b/spring-web/src/main/java/org/springframework/web/util/ServletRequestPathUtils.java index 8dd025f67ef..c57bd4b5e4d 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ServletRequestPathUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/ServletRequestPathUtils.java @@ -249,15 +249,15 @@ public abstract class ServletRequestPathUtils { if (requestUri == null) { requestUri = request.getRequestURI(); } - if (UrlPathHelper.servlet4Present) { - String servletPathPrefix = Servlet4Delegate.getServletPathPrefix(request); - if (StringUtils.hasText(servletPathPrefix)) { - if (servletPathPrefix.endsWith("/")) { - servletPathPrefix = servletPathPrefix.substring(0, servletPathPrefix.length() - 1); - } - return new ServletRequestPath(requestUri, request.getContextPath(), servletPathPrefix); + + String servletPathPrefix = Servlet4Delegate.getServletPathPrefix(request); + if (StringUtils.hasText(servletPathPrefix)) { + if (servletPathPrefix.endsWith("/")) { + servletPathPrefix = servletPathPrefix.substring(0, servletPathPrefix.length() - 1); } + return new ServletRequestPath(requestUri, request.getContextPath(), servletPathPrefix); } + return RequestPath.parse(requestUri, request.getContextPath()); } } diff --git a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java index b85ef80844a..419f6ac4294 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java +++ b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -61,9 +60,6 @@ public class UrlPathHelper { */ public static final String PATH_ATTRIBUTE = UrlPathHelper.class.getName() + ".PATH"; - static final boolean servlet4Present = - ClassUtils.hasMethod(HttpServletRequest.class, "getHttpServletMapping"); - /** * Special WebSphere request attribute, indicating the original request URI. * Preferable over the standard Servlet 2.4 forward attribute on WebSphere, @@ -266,10 +262,12 @@ public class UrlPathHelper { * or if the servlet has been mapped to root; {@code false} otherwise */ private boolean skipServletPathDetermination(HttpServletRequest request) { - if (servlet4Present) { - return Servlet4Delegate.skipServletPathDetermination(request); + HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING); + if (mapping == null) { + mapping = request.getHttpServletMapping(); } - return false; + MappingMatch match = mapping.getMappingMatch(); + return (match != null && (!match.equals(MappingMatch.PATH) || mapping.getPattern().equals("/*"))); } /** @@ -767,21 +765,4 @@ public class UrlPathHelper { rawPathInstance.setReadOnly(); } - - /** - * Inner class to avoid a hard dependency on Servlet 4 {@link HttpServletMapping} - * and {@link MappingMatch} at runtime. - */ - private static class Servlet4Delegate { - - public static boolean skipServletPathDetermination(HttpServletRequest request) { - HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING); - if (mapping == null) { - mapping = request.getHttpServletMapping(); - } - MappingMatch match = mapping.getMappingMatch(); - return (match != null && (!match.equals(MappingMatch.PATH) || mapping.getPattern().equals("/*"))); - } - } - }