Polish AbstractHandlerMethodMapping

Issue: SPR-11541
master
Rossen Stoyanchev 10 years ago
parent 5538863dc9
commit ff7d4eebd8
  1. 9
      spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java
  2. 35
      spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

@ -390,10 +390,15 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
/**
* Retrieve the CORS configuration for the given handler.
* @param handler the handler to check (never {@code null}).
* @param request the current request.
* @return the CORS configuration for the handler or {@code null}.
*/
protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) {
handler = (handler instanceof HandlerExecutionChain) ? ((HandlerExecutionChain) handler).getHandler() : handler;
if (handler != null && handler instanceof CorsConfigurationSource) {
if (handler instanceof HandlerExecutionChain) {
handler = ((HandlerExecutionChain) handler).getHandler();
}
if (handler instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) handler).getCorsConfiguration(request);
}
return null;

@ -83,7 +83,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
private final MultiValueMap<String, HandlerMethod> nameMap = new LinkedMultiValueMap<String, HandlerMethod>();
private final Map<Method, CorsConfiguration> corsConfigurations = new LinkedHashMap<Method, CorsConfiguration>();
private final Map<Method, CorsConfiguration> corsMap = new LinkedHashMap<Method, CorsConfiguration>();
/**
@ -113,20 +113,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
return Collections.unmodifiableMap(this.handlerMethods);
}
protected Map<Method, CorsConfiguration> getCorsConfigurations() {
return corsConfigurations;
}
@Override
protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) {
CorsConfiguration config = super.getCorsConfiguration(handler, request);
if (config == null && handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod)handler;
config = this.getCorsConfigurations().get(handlerMethod.getMethod());
}
return config;
}
/**
* Return the handler methods mapped to the mapping with the given name.
* @param mappingName the mapping name
@ -159,10 +145,9 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class));
for (String beanName : beanNames) {
if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX) &&
isHandler(getApplicationContext().getType(beanName))){
detectHandlerMethods(beanName);
for (String name : beanNames) {
if (!name.startsWith(SCOPED_TARGET_NAME_PREFIX) && isHandler(getApplicationContext().getType(name))) {
detectHandlerMethods(name);
}
}
registerMultiMatchCorsConfiguration();
@ -175,7 +160,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
config.addAllowedMethod("*");
config.addAllowedHeader("*");
config.setAllowCredentials(true);
this.corsConfigurations.put(PREFLIGHT_MULTI_MATCH_HANDLER_METHOD.getMethod(), config);
this.corsMap.put(PREFLIGHT_MULTI_MATCH_HANDLER_METHOD.getMethod(), config);
}
/**
@ -262,7 +247,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
CorsConfiguration config = initCorsConfiguration(handler, method, mapping);
if (config != null) {
this.corsConfigurations.put(method, config);
this.corsMap.put(method, config);
}
}
@ -442,6 +427,14 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
return null;
}
@Override
protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) {
if (handler instanceof HandlerMethod) {
this.corsMap.get(((HandlerMethod) handler).getMethod());
}
return null;
}
/**
* A thin wrapper around a matched HandlerMethod and its mapping, for the purpose of

Loading…
Cancel
Save