Polish interceptor registration MVC config
This commit is contained in:
parent
84c11a5cc7
commit
27153b2982
|
|
@ -85,21 +85,20 @@ public class InterceptorRegistration {
|
|||
if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
|
||||
return this.interceptor;
|
||||
}
|
||||
MappedInterceptor mappedInterceptor = new MappedInterceptor(
|
||||
toArray(this.includePatterns), toArray(this.excludePatterns), interceptor);
|
||||
|
||||
String[] include = toArray(this.includePatterns);
|
||||
String[] exclude = toArray(this.excludePatterns);
|
||||
MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor);
|
||||
|
||||
if (this.pathMatcher != null) {
|
||||
mappedInterceptor.setPathMatcher(this.pathMatcher);
|
||||
}
|
||||
|
||||
return mappedInterceptor;
|
||||
}
|
||||
|
||||
private static String[] toArray(List<String> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
return (CollectionUtils.isEmpty(list) ? null : list.toArray(new String[list.size()]));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,101 +53,99 @@ public class InterceptorRegistryTests {
|
|||
|
||||
private final HandlerInterceptor interceptor2 = new ThemeChangeInterceptor();
|
||||
|
||||
private TestWebRequestInterceptor webRequestInterceptor1;
|
||||
private TestWebRequestInterceptor webInterceptor1;
|
||||
|
||||
private TestWebRequestInterceptor webRequestInterceptor2;
|
||||
private TestWebRequestInterceptor webInterceptor2;
|
||||
|
||||
private final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
private final MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
registry = new InterceptorRegistry();
|
||||
webRequestInterceptor1 = new TestWebRequestInterceptor();
|
||||
webRequestInterceptor2 = new TestWebRequestInterceptor();
|
||||
this.registry = new InterceptorRegistry();
|
||||
this.webInterceptor1 = new TestWebRequestInterceptor();
|
||||
this.webInterceptor2 = new TestWebRequestInterceptor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addInterceptor() {
|
||||
registry.addInterceptor(interceptor1);
|
||||
this.registry.addInterceptor(this.interceptor1);
|
||||
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
|
||||
|
||||
assertEquals(Arrays.asList(interceptor1), interceptors);
|
||||
assertEquals(Arrays.asList(this.interceptor1), interceptors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addTwoInterceptors() {
|
||||
registry.addInterceptor(interceptor1);
|
||||
registry.addInterceptor(interceptor2);
|
||||
this.registry.addInterceptor(this.interceptor1);
|
||||
this.registry.addInterceptor(this.interceptor2);
|
||||
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
|
||||
|
||||
assertEquals(Arrays.asList(interceptor1, interceptor2), interceptors);
|
||||
assertEquals(Arrays.asList(this.interceptor1, this.interceptor2), interceptors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addInterceptorsWithUrlPatterns() {
|
||||
registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").excludePathPatterns("/path1/secret");
|
||||
registry.addInterceptor(interceptor2).addPathPatterns("/path2");
|
||||
this.registry.addInterceptor(this.interceptor1).addPathPatterns("/path1/**").excludePathPatterns("/path1/secret");
|
||||
this.registry.addInterceptor(this.interceptor2).addPathPatterns("/path2");
|
||||
|
||||
assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1"));
|
||||
assertEquals(Arrays.asList(interceptor2), getInterceptorsForPath("/path2"));
|
||||
assertEquals(Arrays.asList(this.interceptor1), getInterceptorsForPath("/path1"));
|
||||
assertEquals(Arrays.asList(this.interceptor2), getInterceptorsForPath("/path2"));
|
||||
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addWebRequestInterceptor() throws Exception {
|
||||
registry.addWebRequestInterceptor(webRequestInterceptor1);
|
||||
this.registry.addWebRequestInterceptor(this.webInterceptor1);
|
||||
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
|
||||
|
||||
assertEquals(1, interceptors.size());
|
||||
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1);
|
||||
verifyWebInterceptor(interceptors.get(0), this.webInterceptor1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addWebRequestInterceptors() throws Exception {
|
||||
registry.addWebRequestInterceptor(webRequestInterceptor1);
|
||||
registry.addWebRequestInterceptor(webRequestInterceptor2);
|
||||
this.registry.addWebRequestInterceptor(this.webInterceptor1);
|
||||
this.registry.addWebRequestInterceptor(this.webInterceptor2);
|
||||
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
|
||||
|
||||
assertEquals(2, interceptors.size());
|
||||
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1);
|
||||
verifyAdaptedInterceptor(interceptors.get(1), webRequestInterceptor2);
|
||||
verifyWebInterceptor(interceptors.get(0), this.webInterceptor1);
|
||||
verifyWebInterceptor(interceptors.get(1), this.webInterceptor2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addInterceptorsWithCustomPathMatcher() {
|
||||
PathMatcher pathMatcher = Mockito.mock(PathMatcher.class);
|
||||
registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
|
||||
this.registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
|
||||
|
||||
MappedInterceptor mappedInterceptor = (MappedInterceptor) registry.getInterceptors().get(0);
|
||||
MappedInterceptor mappedInterceptor = (MappedInterceptor) this.registry.getInterceptors().get(0);
|
||||
assertSame(pathMatcher, mappedInterceptor.getPathMatcher());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addWebRequestInterceptorsWithUrlPatterns() throws Exception {
|
||||
registry.addWebRequestInterceptor(webRequestInterceptor1).addPathPatterns("/path1");
|
||||
registry.addWebRequestInterceptor(webRequestInterceptor2).addPathPatterns("/path2");
|
||||
this.registry.addWebRequestInterceptor(this.webInterceptor1).addPathPatterns("/path1");
|
||||
this.registry.addWebRequestInterceptor(this.webInterceptor2).addPathPatterns("/path2");
|
||||
|
||||
List<HandlerInterceptor> interceptors = getInterceptorsForPath("/path1");
|
||||
assertEquals(1, interceptors.size());
|
||||
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1);
|
||||
verifyWebInterceptor(interceptors.get(0), this.webInterceptor1);
|
||||
|
||||
interceptors = getInterceptorsForPath("/path2");
|
||||
assertEquals(1, interceptors.size());
|
||||
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2);
|
||||
verifyWebInterceptor(interceptors.get(0), this.webInterceptor2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for SPR-11130
|
||||
*/
|
||||
// SPR-11130
|
||||
|
||||
@Test
|
||||
public void addInterceptorWithExcludePathPatternOnly() {
|
||||
registry.addInterceptor(interceptor1).excludePathPatterns("/path1/secret");
|
||||
registry.addInterceptor(interceptor2).addPathPatterns("/path2");
|
||||
this.registry.addInterceptor(this.interceptor1).excludePathPatterns("/path1/secret");
|
||||
this.registry.addInterceptor(this.interceptor2).addPathPatterns("/path2");
|
||||
|
||||
assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1"));
|
||||
assertEquals(Arrays.asList(interceptor1, interceptor2), getInterceptorsForPath("/path2"));
|
||||
assertEquals(Arrays.asList(this.interceptor1), getInterceptorsForPath("/path1"));
|
||||
assertEquals(Arrays.asList(this.interceptor1, this.interceptor2), getInterceptorsForPath("/path2"));
|
||||
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
|
||||
}
|
||||
|
||||
|
|
@ -155,7 +153,7 @@ public class InterceptorRegistryTests {
|
|||
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
|
||||
for (Object interceptor : registry.getInterceptors()) {
|
||||
for (Object interceptor : this.registry.getInterceptors()) {
|
||||
if (interceptor instanceof MappedInterceptor) {
|
||||
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
|
||||
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
|
||||
|
|
@ -172,10 +170,9 @@ public class InterceptorRegistryTests {
|
|||
return result;
|
||||
}
|
||||
|
||||
private void verifyAdaptedInterceptor(HandlerInterceptor interceptor, TestWebRequestInterceptor webInterceptor)
|
||||
throws Exception {
|
||||
private void verifyWebInterceptor(HandlerInterceptor interceptor, TestWebRequestInterceptor webInterceptor) throws Exception {
|
||||
assertTrue(interceptor instanceof WebRequestHandlerInterceptorAdapter);
|
||||
interceptor.preHandle(request, response, null);
|
||||
interceptor.preHandle(this.request, this.response, null);
|
||||
assertTrue(webInterceptor.preHandleInvoked);
|
||||
}
|
||||
|
||||
|
|
@ -195,7 +192,6 @@ public class InterceptorRegistryTests {
|
|||
@Override
|
||||
public void afterCompletion(WebRequest request, Exception ex) throws Exception {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue