Allow interceptors with excludePathPattern only

Issue: SPR-11130
This commit is contained in:
Marten Deinum 2014-06-04 14:25:39 +02:00 committed by Rossen Stoyanchev
parent 52f1be7ade
commit 84c11a5cc7
2 changed files with 22 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,7 +82,7 @@ public class InterceptorRegistration {
* {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}. * {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}.
*/ */
protected Object getInterceptor() { protected Object getInterceptor() {
if (this.includePatterns.isEmpty()) { if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
return this.interceptor; return this.interceptor;
} }
MappedInterceptor mappedInterceptor = new MappedInterceptor( MappedInterceptor mappedInterceptor = new MappedInterceptor(

View File

@ -138,21 +138,35 @@ public class InterceptorRegistryTests {
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2); verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2);
} }
/**
* Test for SPR-11130
*/
@Test
public void addInterceptorWithExcludePathPatternOnly() {
registry.addInterceptor(interceptor1).excludePathPatterns("/path1/secret");
registry.addInterceptor(interceptor2).addPathPatterns("/path2");
assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1"));
assertEquals(Arrays.asList(interceptor1, interceptor2), getInterceptorsForPath("/path2"));
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
}
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) { private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
PathMatcher pathMatcher = new AntPathMatcher(); PathMatcher pathMatcher = new AntPathMatcher();
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>(); List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
for (Object i : registry.getInterceptors()) { for (Object interceptor : registry.getInterceptors()) {
if (i instanceof MappedInterceptor) { if (interceptor instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) i; MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
if (mappedInterceptor.matches(lookupPath, pathMatcher)) { if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
result.add(mappedInterceptor.getInterceptor()); result.add(mappedInterceptor.getInterceptor());
} }
} }
else if (i instanceof HandlerInterceptor){ else if (interceptor instanceof HandlerInterceptor) {
result.add((HandlerInterceptor) i); result.add((HandlerInterceptor) interceptor);
} }
else { else {
fail("Unexpected interceptor type: " + i.getClass().getName()); fail("Unexpected interceptor type: " + interceptor.getClass().getName());
} }
} }
return result; return result;