Skip to content

Commit a7af950

Browse files
committed
Avoid filling up AntPathMatcher cache
This change avoid use of AntPathMatcher to extract URI template variables when the lookup path is used as the best matching pattern. Issue: SPR-10803
1 parent 48b9e9a commit a7af950

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@
1616

1717
package org.springframework.web.servlet.mvc.method;
1818

19-
import java.util.ArrayList;
20-
import java.util.Comparator;
21-
import java.util.HashSet;
22-
import java.util.LinkedHashMap;
23-
import java.util.LinkedHashSet;
24-
import java.util.Map;
19+
import java.util.*;
2520
import java.util.Map.Entry;
26-
import java.util.Set;
2721

2822
import javax.servlet.ServletException;
2923
import javax.servlet.http.HttpServletRequest;
@@ -97,16 +91,28 @@ public int compare(RequestMappingInfo info1, RequestMappingInfo info2) {
9791
protected void handleMatch(RequestMappingInfo info, String lookupPath, HttpServletRequest request) {
9892
super.handleMatch(info, lookupPath, request);
9993

94+
String bestPattern;
95+
Map<String, String> uriVariables;
96+
Map<String, String> decodedUriVariables;
97+
10098
Set<String> patterns = info.getPatternsCondition().getPatterns();
101-
String bestPattern = patterns.isEmpty() ? lookupPath : patterns.iterator().next();
102-
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern);
99+
if (patterns.isEmpty()) {
100+
bestPattern = lookupPath;
101+
uriVariables = Collections.emptyMap();
102+
decodedUriVariables = Collections.emptyMap();
103+
}
104+
else {
105+
bestPattern = patterns.iterator().next();
106+
uriVariables = getPathMatcher().extractUriTemplateVariables(bestPattern, lookupPath);
107+
decodedUriVariables = getUrlPathHelper().decodePathVariables(request, uriVariables);
108+
}
103109

104-
Map<String, String> uriVariables = getPathMatcher().extractUriTemplateVariables(bestPattern, lookupPath);
105-
Map<String, String> decodedUriVariables = getUrlPathHelper().decodePathVariables(request, uriVariables);
110+
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern);
106111
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, decodedUriVariables);
107112

108113
if (isMatrixVariableContentAvailable()) {
109-
request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, extractMatrixVariables(request, uriVariables));
114+
Map<String, MultiValueMap<String, String>> matrixVars = extractMatrixVariables(request, uriVariables);
115+
request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, matrixVars);
110116
}
111117

112118
if (!info.getProducesCondition().getProducibleMediaTypes().isEmpty()) {

0 commit comments

Comments
 (0)