Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

#1164 Added Mapping Conflict Check to Spring WebMvc Configuration #1165

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,15 @@
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
Expand Down Expand Up @@ -474,6 +477,9 @@ public abstract class AbstractStormpathWebMvcConfiguration {
@Autowired //all view resolvers in the spring app context. key: bean name, value: resolver
private Map<String, org.springframework.web.servlet.ViewResolver> viewResolvers;

@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;

private static class AccessibleResourceHandlerRegistry extends ResourceHandlerRegistry {
public AccessibleResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext) {
super(applicationContext, servletContext);
Expand Down Expand Up @@ -1322,6 +1328,7 @@ private <T extends AbstractSocialCallbackController> T configure(T c) {
private <T extends AbstractController> T init(T c) {
try {
c.init();
assertUniqueMethodMapping(c);
return c;
} catch (Exception e) {
String msg = "Unable to initialize controller [" + c + "]: " + e.getMessage();
Expand Down Expand Up @@ -1563,5 +1570,24 @@ public List<String> stormpathCorsAllowedHeaders() {

return java.util.Collections.emptyList();
}

/**
* Fix for https://github.com/stormpath/stormpath-sdk-java/issues/1164
*
* @since 1.3.0
*/
private <T extends AbstractController> void assertUniqueMethodMapping(T c) {
Set<RequestMappingInfo> requestMappingInfoSet = requestMappingHandlerMapping.getHandlerMethods().keySet();
for (RequestMappingInfo requestMappingInfo : requestMappingInfoSet) {
Set<String> patterns = requestMappingInfo.getPatternsCondition().getPatterns();
for (String pattern: patterns) {
if (c.getUri() != null && c.getUri().equals(pattern)) {
HandlerMethod handlerMethod = requestMappingHandlerMapping.getHandlerMethods().get(requestMappingInfo);
throw new IllegalStateException("Mapping conflict: Stormpath cannot map '" + c.getUri() + "'. " +
handlerMethod.getBean() + "#" + handlerMethod + " is already mapped to this URI.");
}
}
}
}
}