Skip to content

Commit 4cc8312

Browse files
committed
Revise Servlet 4 HttpServletMapping check
Closes gh-26112
1 parent 41835ba commit 4cc8312

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Properties;
2323

2424
import javax.servlet.ServletRequest;
25+
import javax.servlet.http.HttpServletMapping;
2526
import javax.servlet.http.HttpServletRequest;
2627
import javax.servlet.http.MappingMatch;
2728

@@ -60,9 +61,8 @@ public class UrlPathHelper {
6061
*/
6162
public static final String PATH_ATTRIBUTE = UrlPathHelper.class.getName() + ".PATH";
6263

63-
private static final boolean isServlet4Present =
64-
ClassUtils.isPresent("javax.servlet.http.HttpServletMapping",
65-
UrlPathHelper.class.getClassLoader());
64+
private static final boolean servlet4Present =
65+
ClassUtils.hasMethod(HttpServletRequest.class, "getHttpServletMapping");
6666

6767
/**
6868
* Special WebSphere request attribute, indicating the original request URI.
@@ -260,12 +260,15 @@ public String getLookupPathForRequest(HttpServletRequest request) {
260260
}
261261
}
262262

263+
/**
264+
* Check whether servlet path determination can be skipped for the given request.
265+
* @param request current HTTP request
266+
* @return {@code true} if the request mapping has not been achieved using a path
267+
* or if the servlet has been mapped to root; {@code false} otherwise
268+
*/
263269
private boolean skipServletPathDetermination(HttpServletRequest request) {
264-
if (isServlet4Present) {
265-
if (request.getHttpServletMapping().getMappingMatch() != null) {
266-
return !request.getHttpServletMapping().getMappingMatch().equals(MappingMatch.PATH) ||
267-
request.getHttpServletMapping().getPattern().equals("/*");
268-
}
270+
if (servlet4Present) {
271+
return Servlet4Delegate.skipServletPathDetermination(request);
269272
}
270273
return false;
271274
}
@@ -763,4 +766,18 @@ public String removeSemicolonContent(String requestUri) {
763766
rawPathInstance.setReadOnly();
764767
}
765768

769+
770+
/**
771+
* Inner class to avoid a hard dependency on Servlet 4 {@link HttpServletMapping}
772+
* and {@link MappingMatch} at runtime.
773+
*/
774+
private static class Servlet4Delegate {
775+
776+
public static boolean skipServletPathDetermination(HttpServletRequest request) {
777+
HttpServletMapping mapping = request.getHttpServletMapping();
778+
MappingMatch match = mapping.getMappingMatch();
779+
return (match != null && (!match.equals(MappingMatch.PATH) || mapping.getPattern().equals("/*")));
780+
}
781+
}
782+
766783
}

0 commit comments

Comments
 (0)