@@ -113,25 +113,25 @@ public static void clearParsedRequestPath(ServletRequest request) {
113
113
// Methods to select either parsed RequestPath or resolved String lookupPath
114
114
115
115
/**
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)
118
118
* pre-parsed} {@code RequestPath}.
119
119
* <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
122
122
* {@link org.springframework.web.filter.ServletRequestPathFilter
123
123
* ServletRequestPathFilter}. In other cases where {@code HandlerMapping}s
124
124
* 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}.
126
126
* @param request the current request
127
127
* @return a String lookupPath or a {@code RequestPath}
128
128
* @throws IllegalArgumentException if neither is available
129
129
*/
130
130
public static Object getCachedPath (ServletRequest request ) {
131
131
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
135
135
136
136
String lookupPath = (String ) request .getAttribute (UrlPathHelper .PATH_ATTRIBUTE );
137
137
if (lookupPath != null ) {
@@ -246,41 +246,24 @@ public String toString() {
246
246
247
247
public static RequestPath parse (HttpServletRequest request ) {
248
248
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 ()));
262
254
}
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 {
271
255
272
256
@ Nullable
273
- public static String getServletPathPrefix (HttpServletRequest request ) {
257
+ private static String getServletPathPrefix (HttpServletRequest request ) {
274
258
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 );
280
265
}
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 ;
284
267
}
285
268
}
286
269
0 commit comments