Skip to content

Commit

Permalink
Minor refactoring in HandlerMappingIntrospector
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 10, 2023
1 parent b9bd98f commit 53fe5fa
Showing 1 changed file with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,23 @@ public List<HandlerMapping> getHandlerMappings() {
@Nullable
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
HttpServletRequest requestToUse = new AttributesPreservingRequest(request);
return doWithHandlerMapping(requestToUse, false, (mapping, executionChain) -> {
if (mapping instanceof MatchableHandlerMapping) {
PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping);
if (pathPatternMapping != null) {
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(requestToUse);
return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath);
}
else {
String lookupPath = (String) requestToUse.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath);
}
return doWithHandlerMapping(requestToUse, false,
(mapping, executionChain) -> createMatchableHandlerMapping(mapping, requestToUse));
}

private MatchableHandlerMapping createMatchableHandlerMapping(HandlerMapping mapping, HttpServletRequest request) {
if (mapping instanceof MatchableHandlerMapping) {
PathPatternMatchableHandlerMapping pathPatternMapping = this.pathPatternMappings.get(mapping);
if (pathPatternMapping != null) {
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(request);
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
Expand All @@ -187,24 +190,28 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
try {
boolean ignoreException = true;
AttributesPreservingRequest requestToUse = new AttributesPreservingRequest(request);
return doWithHandlerMapping(requestToUse, ignoreException, (handlerMapping, executionChain) -> {
for (HandlerInterceptor interceptor : executionChain.getInterceptorList()) {
if (interceptor instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(requestToUse);
}
}
if (executionChain.getHandler() instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(requestToUse);
}
return null;
});
return doWithHandlerMapping(requestToUse, ignoreException,
(handlerMapping, executionChain) -> getCorsConfiguration(requestToUse, executionChain));
}
catch (Exception ex) {
// HandlerMapping exceptions have been ignored. Some more basic error perhaps like request parsing
throw new IllegalStateException(ex);
}
}

@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
private <T> T doWithHandlerMapping(
HttpServletRequest request, boolean ignoreException,
Expand Down

0 comments on commit 53fe5fa

Please sign in to comment.