Skip to content

Commit

Permalink
Polishing in HandlerMappingIntrospector
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 10, 2023
1 parent 7714110 commit b9bd98f
Showing 1 changed file with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,16 @@ public List<HandlerMapping> getHandlerMappings() {
*/
@Nullable
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
HttpServletRequest wrappedRequest = new AttributesPreservingRequest(request);

return doWithHandlerMapping(wrappedRequest, false, (mapping, executionChain) -> {
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(wrappedRequest);
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(requestToUse);
return new LookupPathMatchableHandlerMapping(pathPatternMapping, requestPath);
}
else {
String lookupPath = (String) wrappedRequest.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
String lookupPath = (String) requestToUse.getAttribute(UrlPathHelper.PATH_ATTRIBUTE);
return new LookupPathMatchableHandlerMapping((MatchableHandlerMapping) mapping, lookupPath);
}
}
Expand All @@ -185,18 +184,25 @@ public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest req
@Override
@Nullable
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
AttributesPreservingRequest wrappedRequest = new AttributesPreservingRequest(request);
return doWithHandlerMappingIgnoringException(wrappedRequest, (handlerMapping, executionChain) -> {
for (HandlerInterceptor interceptor : executionChain.getInterceptorList()) {
if (interceptor instanceof CorsConfigurationSource ccs) {
return ccs.getCorsConfiguration(wrappedRequest);
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 ccs) {
return ccs.getCorsConfiguration(wrappedRequest);
}
return null;
});
if (executionChain.getHandler() instanceof CorsConfigurationSource source) {
return source.getCorsConfiguration(requestToUse);
}
return null;
});
}
catch (Exception ex) {
// HandlerMapping exceptions have been ignored. Some more basic error perhaps like request parsing
throw new IllegalStateException(ex);
}
}

@Nullable
Expand Down Expand Up @@ -237,18 +243,6 @@ private <T> T doWithHandlerMapping(
return null;
}

@Nullable
private <T> T doWithHandlerMappingIgnoringException(
HttpServletRequest request, BiFunction<HandlerMapping, HandlerExecutionChain, T> matchHandler) {

try {
return doWithHandlerMapping(request, true, matchHandler);
}
catch (Exception ex) {
throw new IllegalStateException("HandlerMapping exception not suppressed", ex);
}
}


/**
* Request wrapper that buffers request attributes in order protect the
Expand Down

0 comments on commit b9bd98f

Please sign in to comment.