Skip to content

Commit b4f5c28

Browse files
author
Kyle Anderson
committed
stormpath#1164 Added Mapping Conflict Check to Spring WebMvc Configuration
1 parent 415961b commit b4f5c28

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

extensions/spring/stormpath-spring-webmvc/src/main/java/com/stormpath/spring/config/AbstractStormpathWebMvcConfiguration.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,15 @@
182182
import org.springframework.web.cors.CorsConfiguration;
183183
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
184184
import org.springframework.web.filter.CorsFilter;
185+
import org.springframework.web.method.HandlerMethod;
185186
import org.springframework.web.servlet.HandlerInterceptor;
186187
import org.springframework.web.servlet.HandlerMapping;
187188
import org.springframework.web.servlet.LocaleResolver;
188189
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
189190
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
190191
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
192+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
193+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
191194
import org.springframework.web.servlet.view.InternalResourceViewResolver;
192195
import org.springframework.web.servlet.view.JstlView;
193196
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
@@ -474,6 +477,9 @@ public abstract class AbstractStormpathWebMvcConfiguration {
474477
@Autowired //all view resolvers in the spring app context. key: bean name, value: resolver
475478
private Map<String, org.springframework.web.servlet.ViewResolver> viewResolvers;
476479

480+
@Autowired
481+
private RequestMappingHandlerMapping requestMappingHandlerMapping;
482+
477483
private static class AccessibleResourceHandlerRegistry extends ResourceHandlerRegistry {
478484
public AccessibleResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext) {
479485
super(applicationContext, servletContext);
@@ -1322,6 +1328,7 @@ private <T extends AbstractSocialCallbackController> T configure(T c) {
13221328
private <T extends AbstractController> T init(T c) {
13231329
try {
13241330
c.init();
1331+
assertUniqueMethodMapping(c);
13251332
return c;
13261333
} catch (Exception e) {
13271334
String msg = "Unable to initialize controller [" + c + "]: " + e.getMessage();
@@ -1563,5 +1570,25 @@ public List<String> stormpathCorsAllowedHeaders() {
15631570

15641571
return java.util.Collections.emptyList();
15651572
}
1573+
1574+
/**
1575+
* Fix for https://github.com/stormpath/stormpath-sdk-java/issues/1164
1576+
*
1577+
* @since 1.2.3
1578+
*/
1579+
private <T extends AbstractController> void assertUniqueMethodMapping(T c) {
1580+
Set<RequestMappingInfo> requestMappingInfoSet = requestMappingHandlerMapping.getHandlerMethods().keySet();
1581+
for(RequestMappingInfo requestMappingInfo : requestMappingInfoSet){
1582+
Set<String> patterns = requestMappingInfo.getPatternsCondition().getPatterns();
1583+
for(String pattern: patterns){
1584+
if(c.getUri() != null && c.getUri().equals(pattern)){
1585+
HandlerMethod handlerMethod = requestMappingHandlerMapping.getHandlerMethods().get(requestMappingInfo);
1586+
throw new IllegalStateException("Mapping conflict. Stormpath cannot map '" + c.getUri() + "'. " +
1587+
"There is already '" + handlerMethod.getBean() +
1588+
"' bean method\n" + handlerMethod + " mapped.");
1589+
}
1590+
}
1591+
}
1592+
}
15661593
}
15671594

0 commit comments

Comments
 (0)