Skip to content

Commit 5ca7928

Browse files
committed
Use Servlet 4 mapping type if available
See gh-25100
1 parent 23233c3 commit 5ca7928

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

spring-web/spring-web.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88
optional(project(":spring-aop"))
99
optional(project(":spring-context"))
1010
optional(project(":spring-oxm"))
11-
optional("javax.servlet:javax.servlet-api:3.1.0")
11+
optional("javax.servlet:javax.servlet-api") // Servlet 4 for mapping type
1212
optional("javax.servlet.jsp:javax.servlet.jsp-api")
1313
optional("javax.el:javax.el-api")
1414
optional("javax.faces:javax.faces-api")

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

+40-17
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.util.Properties;
2424

2525
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.MappingMatch;
2627

2728
import org.apache.commons.logging.Log;
2829
import org.apache.commons.logging.LogFactory;
2930

3031
import org.springframework.lang.Nullable;
32+
import org.springframework.util.ClassUtils;
3133
import org.springframework.util.LinkedMultiValueMap;
3234
import org.springframework.util.MultiValueMap;
3335
import org.springframework.util.StringUtils;
@@ -49,6 +51,10 @@
4951
*/
5052
public class UrlPathHelper {
5153

54+
private static boolean isServlet4Present =
55+
ClassUtils.isPresent("javax.servlet.http.HttpServletMapping",
56+
UrlPathHelper.class.getClassLoader());
57+
5258
/**
5359
* Special WebSphere request attribute, indicating the original request URI.
5460
* Preferable over the standard Servlet 2.4 forward attribute on WebSphere,
@@ -154,6 +160,26 @@ protected String getDefaultEncoding() {
154160
}
155161

156162

163+
/**
164+
* Variant of {@link #getLookupPathForRequest(HttpServletRequest)} that
165+
* automates checking for a previously computed lookupPath saved as a
166+
* request attribute. The attribute is only used for lookup purposes.
167+
* @param request current HTTP request
168+
* @param lookupPathAttributeName the request attribute to check
169+
* @return the lookup path
170+
* @since 5.2
171+
* @see org.springframework.web.servlet.HandlerMapping#LOOKUP_PATH
172+
*/
173+
public String getLookupPathForRequest(HttpServletRequest request, @Nullable String lookupPathAttributeName) {
174+
if (lookupPathAttributeName != null) {
175+
String result = (String) request.getAttribute(lookupPathAttributeName);
176+
if (result != null) {
177+
return result;
178+
}
179+
}
180+
return getLookupPathForRequest(request);
181+
}
182+
157183
/**
158184
* Return the mapping lookup path for the given request, within the current
159185
* servlet mapping if applicable, else within the web application.
@@ -165,7 +191,7 @@ protected String getDefaultEncoding() {
165191
*/
166192
public String getLookupPathForRequest(HttpServletRequest request) {
167193
// Always use full path within current servlet context?
168-
if (this.alwaysUseFullPath) {
194+
if (this.alwaysUseFullPath || skipServletPathDetermination(request)) {
169195
return getPathWithinApplication(request);
170196
}
171197
// Else, use path within current servlet mapping if applicable
@@ -178,24 +204,14 @@ public String getLookupPathForRequest(HttpServletRequest request) {
178204
}
179205
}
180206

181-
/**
182-
* Variant of {@link #getLookupPathForRequest(HttpServletRequest)} that
183-
* automates checking for a previously computed lookupPath saved as a
184-
* request attribute. The attribute is only used for lookup purposes.
185-
* @param request current HTTP request
186-
* @param lookupPathAttributeName the request attribute to check
187-
* @return the lookup path
188-
* @since 5.2
189-
* @see org.springframework.web.servlet.HandlerMapping#LOOKUP_PATH
190-
*/
191-
public String getLookupPathForRequest(HttpServletRequest request, @Nullable String lookupPathAttributeName) {
192-
if (lookupPathAttributeName != null) {
193-
String result = (String) request.getAttribute(lookupPathAttributeName);
194-
if (result != null) {
195-
return result;
207+
private boolean skipServletPathDetermination(HttpServletRequest request) {
208+
if (isServlet4Present) {
209+
if (request.getHttpServletMapping().getMappingMatch() != null) {
210+
return !request.getHttpServletMapping().getMappingMatch().equals(MappingMatch.PATH) ||
211+
request.getHttpServletMapping().getPattern().equals("/*");
196212
}
197213
}
198-
return getLookupPathForRequest(request);
214+
return false;
199215
}
200216

201217
/**
@@ -658,4 +674,11 @@ public void setDefaultEncoding(String defaultEncoding) {
658674
throw new UnsupportedOperationException();
659675
}
660676
};
677+
678+
679+
private static class HttpServletMappingHelper {
680+
681+
682+
683+
}
661684
}

0 commit comments

Comments
 (0)