Skip to content

Commit b9bd98f

Browse files
committed
Polishing in HandlerMappingIntrospector
See gh-31588
1 parent 7714110 commit b9bd98f

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,16 @@ public List<HandlerMapping> getHandlerMappings() {
164164
*/
165165
@Nullable
166166
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
167-
HttpServletRequest wrappedRequest = new AttributesPreservingRequest(request);
168-
169-
return doWithHandlerMapping(wrappedRequest, false, (mapping, executionChain) -> {
167+
HttpServletRequest requestToUse = new AttributesPreservingRequest(request);
168+
return doWithHandlerMapping(requestToUse, false, (mapping, executionChain) -> {
170169
if (mapping instanceof MatchableHandlerMapping) {
171170
PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping);
172171
if (pathPatternMapping != null) {
173-
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(wrappedRequest);
172+
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(requestToUse);
174173
return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath);
175174
}
176175
else {
177-
String lookupPath = (String) wrappedRequest.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
176+
String lookupPath = (String) requestToUse.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
178177
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath);
179178
}
180179
}
@@ -185,18 +184,25 @@ public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest req
185184
@Override
186185
@Nullable
187186
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
188-
AttributesPreservingRequest wrappedRequest = new AttributesPreservingRequest(request);
189-
return doWithHandlerMappingIgnoringException(wrappedRequest, (handlerMapping, executionChain) -> {
190-
for (HandlerInterceptor interceptor : executionChain.getInterceptorList()) {
191-
if (interceptor instanceof CorsConfigurationSource ccs) {
192-
return ccs.getCorsConfiguration(wrappedRequest);
187+
try {
188+
boolean ignoreException = true;
189+
AttributesPreservingRequest requestToUse = new AttributesPreservingRequest(request);
190+
return doWithHandlerMapping(requestToUse, ignoreException, (handlerMapping, executionChain) -> {
191+
for (HandlerInterceptor interceptor : executionChain.getInterceptorList()) {
192+
if (interceptor instanceof CorsConfigurationSource source) {
193+
return source.getCorsConfiguration(requestToUse);
194+
}
193195
}
194-
}
195-
if (executionChain.getHandler() instanceof CorsConfigurationSource ccs) {
196-
return ccs.getCorsConfiguration(wrappedRequest);
197-
}
198-
return null;
199-
});
196+
if (executionChain.getHandler() instanceof CorsConfigurationSource source) {
197+
return source.getCorsConfiguration(requestToUse);
198+
}
199+
return null;
200+
});
201+
}
202+
catch (Exception ex) {
203+
// HandlerMapping exceptions have been ignored. Some more basic error perhaps like request parsing
204+
throw new IllegalStateException(ex);
205+
}
200206
}
201207

202208
@Nullable
@@ -237,18 +243,6 @@ private <T> T doWithHandlerMapping(
237243
return null;
238244
}
239245

240-
@Nullable
241-
private <T> T doWithHandlerMappingIgnoringException(
242-
HttpServletRequest request, BiFunction<HandlerMapping, HandlerExecutionChain, T> matchHandler) {
243-
244-
try {
245-
return doWithHandlerMapping(request, true, matchHandler);
246-
}
247-
catch (Exception ex) {
248-
throw new IllegalStateException("HandlerMapping exception not suppressed", ex);
249-
}
250-
}
251-
252246

253247
/**
254248
* Request wrapper that buffers request attributes in order protect the

0 commit comments

Comments
 (0)