Support custom PathMatcher for MappedInterceptor's
Issue: SPR-11197
This commit is contained in:
@@ -19,10 +19,7 @@ package org.springframework.web.servlet.config;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.*;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.validation.constraints.NotNull;
|
||||
@@ -47,6 +44,7 @@ import org.springframework.mock.web.test.MockRequestDispatcher;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
@@ -68,10 +66,7 @@ import org.springframework.web.method.support.InvocableHandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
|
||||
import org.springframework.web.servlet.handler.*;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
|
||||
@@ -219,7 +214,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testInterceptors() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-interceptors.xml", 18);
|
||||
loadBeanDefinitions("mvc-config-interceptors.xml", 20);
|
||||
|
||||
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
@@ -231,11 +226,12 @@ public class MvcNamespaceTests {
|
||||
request.addParameter("theme", "green");
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(4, chain.getInterceptors().length);
|
||||
assertEquals(5, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof WebRequestHandlerInterceptorAdapter);
|
||||
assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[4] instanceof UserRoleAuthorizationInterceptor);
|
||||
|
||||
request.setRequestURI("/admin/users");
|
||||
chain = mapping.getHandler(request);
|
||||
@@ -584,4 +580,42 @@ public class MvcNamespaceTests {
|
||||
|
||||
public static class TestDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter { }
|
||||
|
||||
public static class TestPathMatcher implements PathMatcher {
|
||||
|
||||
@Override
|
||||
public boolean isPattern(String path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean match(String pattern, String path) {
|
||||
return path.matches(pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchStart(String pattern, String path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String extractPathWithinPattern(String pattern, String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> extractUriTemplateVariables(String pattern, String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<String> getPatternComparator(String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String combine(String pattern1, String pattern2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -27,6 +23,8 @@ import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.ui.ModelMap;
|
||||
@@ -40,6 +38,8 @@ import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapt
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture with a {@link InterceptorRegistry}, two {@link HandlerInterceptor}s and two
|
||||
* {@link WebRequestInterceptor}s.
|
||||
@@ -116,6 +116,15 @@ public class InterceptorRegistryTests {
|
||||
verifyAdaptedInterceptor(interceptors.get(1), webRequestInterceptor2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addInterceptorsWithCustomPathMatcher() {
|
||||
PathMatcher pathMatcher = Mockito.mock(PathMatcher.class);
|
||||
registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
|
||||
|
||||
MappedInterceptor mappedInterceptor = (MappedInterceptor) registry.getInterceptors().get(0);
|
||||
assertSame(pathMatcher, mappedInterceptor.getPathMatcher());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addWebRequestInterceptorsWithUrlPatterns() throws Exception {
|
||||
registry.addWebRequestInterceptor(webRequestInterceptor1).addPathPatterns("/path1");
|
||||
|
||||
@@ -20,8 +20,12 @@ import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link MappedInterceptor} tests.
|
||||
*
|
||||
@@ -75,4 +79,52 @@ public class MappedInterceptorTests {
|
||||
assertFalse(mappedInterceptor.matches("/admin/foo", pathMatcher));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPathMatcher() {
|
||||
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" }, this.interceptor);
|
||||
mappedInterceptor.setPathMatcher(new TestPathMatcher());
|
||||
|
||||
assertTrue(mappedInterceptor.matches("/foo/123", pathMatcher));
|
||||
assertFalse(mappedInterceptor.matches("/foo/bar", pathMatcher));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class TestPathMatcher implements PathMatcher {
|
||||
|
||||
@Override
|
||||
public boolean isPattern(String path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean match(String pattern, String path) {
|
||||
return path.matches(pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchStart(String pattern, String path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String extractPathWithinPattern(String pattern, String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> extractUriTemplateVariables(String pattern, String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<String> getPatternComparator(String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String combine(String pattern1, String pattern2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user