Minor refactoring in HandlerMappingIntrospector

See gh-31588
This commit is contained in:
rstoyanchev 2023-11-10 17:27:54 +00:00
parent b9bd98fc5b
commit 53fe5fafed
1 changed files with 31 additions and 24 deletions

View File

@ -165,20 +165,23 @@ public class HandlerMappingIntrospector
@Nullable @Nullable
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception { public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
HttpServletRequest requestToUse = new AttributesPreservingRequest(request); HttpServletRequest requestToUse = new AttributesPreservingRequest(request);
return doWithHandlerMapping(requestToUse, false, (mapping, executionChain) -> { return doWithHandlerMapping(requestToUse, false,
if (mapping instanceof MatchableHandlerMapping) { (mapping, executionChain) -> createMatchableHandlerMapping(mapping, requestToUse));
PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping); }
if (pathPatternMapping != null) {
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(requestToUse); private MatchableHandlerMapping createMatchableHandlerMapping(HandlerMapping mapping, HttpServletRequest request) {
return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath); if (mapping instanceof MatchableHandlerMapping) {
} PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping);
else { if (pathPatternMapping != null) {
String lookupPath = (String) requestToUse.getAttribute(UrlPathHelper.PATH_ATTRIBUTE); RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(request);
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath); return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath);
}
} }
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping"); else {
}); String lookupPath = (String) request.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath);
}
}
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
} }
@Override @Override
@ -187,17 +190,8 @@ public class HandlerMappingIntrospector
try { try {
boolean ignoreException = true; boolean ignoreException = true;
AttributesPreservingRequest requestToUse = new AttributesPreservingRequest(request); AttributesPreservingRequest requestToUse = new AttributesPreservingRequest(request);
return doWithHandlerMapping(requestToUse, ignoreException, (handlerMapping, executionChain) -> { return doWithHandlerMapping(requestToUse, ignoreException,
for (HandlerInterceptor interceptor : executionChain.getInterceptorList()) { (handlerMapping, executionChain) -> getCorsConfiguration(requestToUse, executionChain));
if (interceptor instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(requestToUse);
}
}
if (executionChain.getHandler() instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(requestToUse);
}
return null;
});
} }
catch (Exception ex) { catch (Exception ex) {
// HandlerMapping exceptions have been ignored. Some more basic error perhaps like request parsing // HandlerMapping exceptions have been ignored. Some more basic error perhaps like request parsing
@ -205,6 +199,19 @@ public class HandlerMappingIntrospector
} }
} }
@Nullable
private static CorsConfiguration getCorsConfiguration(HttpServletRequest request, HandlerExecutionChain chain) {
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
if (interceptor instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(request);
}
}
if (chain.getHandler() instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(request);
}
return null;
}
@Nullable @Nullable
private <T> T doWithHandlerMapping( private <T> T doWithHandlerMapping(
HttpServletRequest request, boolean ignoreException, HttpServletRequest request, boolean ignoreException,