Skip to content

Commit 53fe5fa

Browse files
committed
Minor refactoring in HandlerMappingIntrospector
See gh-31588
1 parent b9bd98f commit 53fe5fa

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,23 @@ public List<HandlerMapping> getHandlerMappings() {
165165
@Nullable
166166
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
167167
HttpServletRequest requestToUse = new AttributesPreservingRequest(request);
168-
return doWithHandlerMapping(requestToUse, false, (mapping, executionChain) -> {
169-
if (mapping instanceof MatchableHandlerMapping) {
170-
PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping);
171-
if (pathPatternMapping != null) {
172-
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(requestToUse);
173-
return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath);
174-
}
175-
else {
176-
String lookupPath = (String) requestToUse.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
177-
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath);
178-
}
168+
return doWithHandlerMapping(requestToUse, false,
169+
(mapping, executionChain) -> createMatchableHandlerMapping(mapping, requestToUse));
170+
}
171+
172+
private MatchableHandlerMapping createMatchableHandlerMapping(HandlerMapping mapping, HttpServletRequest request) {
173+
if (mapping instanceof MatchableHandlerMapping) {
174+
PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping);
175+
if (pathPatternMapping != null) {
176+
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(request);
177+
return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath);
179178
}
180-
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
181-
});
179+
else {
180+
String lookupPath = (String) request.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
181+
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath);
182+
}
183+
}
184+
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
182185
}
183186

184187
@Override
@@ -187,24 +190,28 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
187190
try {
188191
boolean ignoreException = true;
189192
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-
}
195-
}
196-
if (executionChain.getHandler() instanceof CorsConfigurationSource source) {
197-
return source.getCorsConfiguration(requestToUse);
198-
}
199-
return null;
200-
});
193+
return doWithHandlerMapping(requestToUse, ignoreException,
194+
(handlerMapping, executionChain) -> getCorsConfiguration(requestToUse, executionChain));
201195
}
202196
catch (Exception ex) {
203197
// HandlerMapping exceptions have been ignored. Some more basic error perhaps like request parsing
204198
throw new IllegalStateException(ex);
205199
}
206200
}
207201

202+
@Nullable
203+
private static CorsConfiguration getCorsConfiguration(HttpServletRequest request, HandlerExecutionChain chain) {
204+
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
205+
if (interceptor instanceof CorsConfigurationSource source) {
206+
return source.getCorsConfiguration(request);
207+
}
208+
}
209+
if (chain.getHandler() instanceof CorsConfigurationSource source) {
210+
return source.getCorsConfiguration(request);
211+
}
212+
return null;
213+
}
214+
208215
@Nullable
209216
private <T> T doWithHandlerMapping(
210217
HttpServletRequest request, boolean ignoreException,

0 commit comments

Comments
 (0)