Drop Servlet 4 classpath check

This commit is contained in:
rstoyanchev 2022-10-07 18:14:23 +01:00
parent 1afc1fc90d
commit 16ea4692ba
2 changed files with 13 additions and 32 deletions

View File

@ -249,15 +249,15 @@ public abstract class ServletRequestPathUtils {
if (requestUri == null) { if (requestUri == null) {
requestUri = request.getRequestURI(); requestUri = request.getRequestURI();
} }
if (UrlPathHelper.servlet4Present) {
String servletPathPrefix = Servlet4Delegate.getServletPathPrefix(request); String servletPathPrefix = Servlet4Delegate.getServletPathPrefix(request);
if (StringUtils.hasText(servletPathPrefix)) { if (StringUtils.hasText(servletPathPrefix)) {
if (servletPathPrefix.endsWith("/")) { if (servletPathPrefix.endsWith("/")) {
servletPathPrefix = servletPathPrefix.substring(0, servletPathPrefix.length() - 1); servletPathPrefix = servletPathPrefix.substring(0, servletPathPrefix.length() - 1);
}
return new ServletRequestPath(requestUri, request.getContextPath(), servletPathPrefix);
} }
return new ServletRequestPath(requestUri, request.getContextPath(), servletPathPrefix);
} }
return RequestPath.parse(requestUri, request.getContextPath()); return RequestPath.parse(requestUri, request.getContextPath());
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
@ -61,9 +60,6 @@ public class UrlPathHelper {
*/ */
public static final String PATH_ATTRIBUTE = UrlPathHelper.class.getName() + ".PATH"; 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. * Special WebSphere request attribute, indicating the original request URI.
* Preferable over the standard Servlet 2.4 forward attribute on WebSphere, * 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 * or if the servlet has been mapped to root; {@code false} otherwise
*/ */
private boolean skipServletPathDetermination(HttpServletRequest request) { private boolean skipServletPathDetermination(HttpServletRequest request) {
if (servlet4Present) { HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING);
return Servlet4Delegate.skipServletPathDetermination(request); 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(); 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("/*")));
}
}
} }