Skip to content

Commit bcdc250

Browse files
committed
Avoid repeated calls to getPathWithinApplication from getLookupPathForRequest
Closes gh-25669
1 parent 3a73533 commit bcdc250

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -164,17 +164,18 @@ protected String getDefaultEncoding() {
164164
* @see #getPathWithinApplication
165165
*/
166166
public String getLookupPathForRequest(HttpServletRequest request) {
167+
String pathWithinApp = getPathWithinApplication(request);
167168
// Always use full path within current servlet context?
168169
if (this.alwaysUseFullPath) {
169-
return getPathWithinApplication(request);
170+
return pathWithinApp;
170171
}
171172
// Else, use path within current servlet mapping if applicable
172-
String rest = getPathWithinServletMapping(request);
173+
String rest = getPathWithinServletMapping(request, pathWithinApp);
173174
if (StringUtils.hasLength(rest)) {
174175
return rest;
175176
}
176177
else {
177-
return getPathWithinApplication(request);
178+
return pathWithinApp;
178179
}
179180
}
180181

@@ -198,6 +199,18 @@ public String getLookupPathForRequest(HttpServletRequest request, @Nullable Stri
198199
return getLookupPathForRequest(request);
199200
}
200201

202+
/**
203+
* Return the path within the servlet mapping for the given request,
204+
* i.e. the part of the request's URL beyond the part that called the servlet,
205+
* or "" if the whole URL has been used to identify the servlet.
206+
* @param request current HTTP request
207+
* @return the path within the servlet mapping, or ""
208+
* @see #getPathWithinServletMapping(HttpServletRequest, String)
209+
*/
210+
public String getPathWithinServletMapping(HttpServletRequest request) {
211+
return getPathWithinServletMapping(request, getPathWithinApplication(request));
212+
}
213+
201214
/**
202215
* Return the path within the servlet mapping for the given request,
203216
* i.e. the part of the request's URL beyond the part that called the servlet,
@@ -209,11 +222,12 @@ public String getLookupPathForRequest(HttpServletRequest request, @Nullable Stri
209222
* <p>E.g.: servlet mapping = "/test"; request URI = "/test" -> "".
210223
* <p>E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".
211224
* @param request current HTTP request
225+
* @param pathWithinApp a precomputed path within the application
212226
* @return the path within the servlet mapping, or ""
227+
* @since 5.2.9
213228
* @see #getLookupPathForRequest
214229
*/
215-
public String getPathWithinServletMapping(HttpServletRequest request) {
216-
String pathWithinApp = getPathWithinApplication(request);
230+
protected String getPathWithinServletMapping(HttpServletRequest request, String pathWithinApp) {
217231
String servletPath = getServletPath(request);
218232
String sanitizedPathWithinApp = getSanitizedPath(pathWithinApp);
219233
String path;

0 commit comments

Comments
 (0)