Skip to content

Commit ae7cff3

Browse files
committed
Polishing
1 parent 8282be6 commit ae7cff3

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

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

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,25 @@ public static void clearParsedRequestPath(ServletRequest request) {
113113
// Methods to select either parsed RequestPath or resolved String lookupPath
114114

115115
/**
116-
* Return the {@link UrlPathHelper#resolveAndCacheLookupPath pre-resolved}
117-
* String lookupPath or the {@link #parseAndCache(HttpServletRequest)
116+
* Return either a {@link UrlPathHelper#resolveAndCacheLookupPath pre-resolved}
117+
* String lookupPath or a {@link #parseAndCache(HttpServletRequest)
118118
* pre-parsed} {@code RequestPath}.
119119
* <p>In Spring MVC, when at least one {@code HandlerMapping} has parsed
120-
* {@code PathPatterns} enabled, the {@code DispatcherServlet} eagerly parses
121-
* and caches the {@code RequestPath} and the same can be also done earlier with
120+
* {@code PathPatterns} enabled, the {@code DispatcherServlet} parses and
121+
* caches the {@code RequestPath} which can be also done even earlier with
122122
* {@link org.springframework.web.filter.ServletRequestPathFilter
123123
* ServletRequestPathFilter}. In other cases where {@code HandlerMapping}s
124124
* use String pattern matching with {@code PathMatcher}, the String
125-
* lookupPath is resolved separately by each {@code HandlerMapping}.
125+
* lookupPath is resolved separately in each {@code HandlerMapping}.
126126
* @param request the current request
127127
* @return a String lookupPath or a {@code RequestPath}
128128
* @throws IllegalArgumentException if neither is available
129129
*/
130130
public static Object getCachedPath(ServletRequest request) {
131131

132-
// The RequestPath is pre-parsed if any HandlerMapping uses PathPatterns.
133-
// The lookupPath is re-resolved or cleared per HandlerMapping.
134-
// So check for lookupPath first.
132+
// RequestPath is parsed once and cached in the DispatcherServlet if any HandlerMapping uses PathPatterns.
133+
// A String lookupPath is resolved and cached in each HandlerMapping that uses String matching.
134+
// So we try lookupPath first, then RequestPath second
135135

136136
String lookupPath = (String) request.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
137137
if (lookupPath != null) {
@@ -246,41 +246,24 @@ public String toString() {
246246

247247
public static RequestPath parse(HttpServletRequest request) {
248248
String requestUri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
249-
if (requestUri == null) {
250-
requestUri = request.getRequestURI();
251-
}
252-
253-
String servletPathPrefix = Servlet4Delegate.getServletPathPrefix(request);
254-
if (StringUtils.hasText(servletPathPrefix)) {
255-
if (servletPathPrefix.endsWith("/")) {
256-
servletPathPrefix = servletPathPrefix.substring(0, servletPathPrefix.length() - 1);
257-
}
258-
return new ServletRequestPath(requestUri, request.getContextPath(), servletPathPrefix);
259-
}
260-
261-
return RequestPath.parse(requestUri, request.getContextPath());
249+
requestUri = (requestUri != null ? requestUri : request.getRequestURI());
250+
String servletPathPrefix = getServletPathPrefix(request);
251+
return (StringUtils.hasText(servletPathPrefix) ?
252+
new ServletRequestPath(requestUri, request.getContextPath(), servletPathPrefix) :
253+
RequestPath.parse(requestUri, request.getContextPath()));
262254
}
263-
}
264-
265-
266-
/**
267-
* Inner class to avoid a hard dependency on Servlet 4 {@link HttpServletMapping}
268-
* and {@link MappingMatch} at runtime.
269-
*/
270-
private static class Servlet4Delegate {
271255

272256
@Nullable
273-
public static String getServletPathPrefix(HttpServletRequest request) {
257+
private static String getServletPathPrefix(HttpServletRequest request) {
274258
HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING);
275-
if (mapping == null) {
276-
mapping = request.getHttpServletMapping();
277-
}
278-
if (!ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) {
279-
return null;
259+
mapping = (mapping != null ? mapping : request.getHttpServletMapping());
260+
if (ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) {
261+
String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
262+
servletPath = (servletPath != null ? servletPath : request.getServletPath());
263+
servletPath = (servletPath.endsWith("/") ? servletPath.substring(0, servletPath.length() - 1) : servletPath);
264+
return UriUtils.encodePath(servletPath, StandardCharsets.UTF_8);
280265
}
281-
String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
282-
servletPath = (servletPath != null ? servletPath : request.getServletPath());
283-
return UriUtils.encodePath(servletPath, StandardCharsets.UTF_8);
266+
return null;
284267
}
285268
}
286269

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public String getLookupPathForRequest(HttpServletRequest request, @Nullable Stri
242242
public String getLookupPathForRequest(HttpServletRequest request) {
243243
String pathWithinApp = getPathWithinApplication(request);
244244
// Always use full path within current servlet context?
245-
if (this.alwaysUseFullPath || skipServletPathDetermination(request)) {
245+
if (this.alwaysUseFullPath || ignoreServletPath(request)) {
246246
return pathWithinApp;
247247
}
248248
// Else, use path within current servlet mapping if applicable
@@ -256,16 +256,13 @@ public String getLookupPathForRequest(HttpServletRequest request) {
256256
}
257257

258258
/**
259-
* Check whether servlet path determination can be skipped for the given request.
260-
* @param request current HTTP request
261-
* @return {@code true} if the request mapping has not been achieved using a path
262-
* or if the servlet has been mapped to root; {@code false} otherwise
259+
* Whether we can ignore the servletPath and pathInfo for mapping purposes,
260+
* which is the case when we can establish that the Servlet is not mapped
261+
* by servletPath prefix.
263262
*/
264-
private boolean skipServletPathDetermination(HttpServletRequest request) {
263+
private boolean ignoreServletPath(HttpServletRequest request) {
265264
HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING);
266-
if (mapping == null) {
267-
mapping = request.getHttpServletMapping();
268-
}
265+
mapping = (mapping == null ? request.getHttpServletMapping() : mapping);
269266
MappingMatch match = mapping.getMappingMatch();
270267
return (match != null && (!match.equals(MappingMatch.PATH) || mapping.getPattern().equals("/*")));
271268
}

0 commit comments

Comments
 (0)