Skip to content

Commit 27153b2

Browse files
committed
Polish interceptor registration MVC config
1 parent 84c11a5 commit 27153b2

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,20 @@ protected Object getInterceptor() {
8585
if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
8686
return this.interceptor;
8787
}
88-
MappedInterceptor mappedInterceptor = new MappedInterceptor(
89-
toArray(this.includePatterns), toArray(this.excludePatterns), interceptor);
88+
89+
String[] include = toArray(this.includePatterns);
90+
String[] exclude = toArray(this.excludePatterns);
91+
MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor);
92+
9093
if (this.pathMatcher != null) {
9194
mappedInterceptor.setPathMatcher(this.pathMatcher);
9295
}
96+
9397
return mappedInterceptor;
9498
}
9599

96100
private static String[] toArray(List<String> list) {
97-
if (CollectionUtils.isEmpty(list)) {
98-
return null;
99-
}
100-
else {
101-
return list.toArray(new String[list.size()]);
102-
}
101+
return (CollectionUtils.isEmpty(list) ? null : list.toArray(new String[list.size()]));
103102
}
104103

105104
}

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,109 +53,107 @@ public class InterceptorRegistryTests {
5353

5454
private final HandlerInterceptor interceptor2 = new ThemeChangeInterceptor();
5555

56-
private TestWebRequestInterceptor webRequestInterceptor1;
56+
private TestWebRequestInterceptor webInterceptor1;
5757

58-
private TestWebRequestInterceptor webRequestInterceptor2;
58+
private TestWebRequestInterceptor webInterceptor2;
5959

6060
private final MockHttpServletRequest request = new MockHttpServletRequest();
6161

6262
private final MockHttpServletResponse response = new MockHttpServletResponse();
6363

64+
6465
@Before
6566
public void setUp() {
66-
registry = new InterceptorRegistry();
67-
webRequestInterceptor1 = new TestWebRequestInterceptor();
68-
webRequestInterceptor2 = new TestWebRequestInterceptor();
67+
this.registry = new InterceptorRegistry();
68+
this.webInterceptor1 = new TestWebRequestInterceptor();
69+
this.webInterceptor2 = new TestWebRequestInterceptor();
6970
}
7071

7172
@Test
7273
public void addInterceptor() {
73-
registry.addInterceptor(interceptor1);
74+
this.registry.addInterceptor(this.interceptor1);
7475
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
75-
76-
assertEquals(Arrays.asList(interceptor1), interceptors);
76+
assertEquals(Arrays.asList(this.interceptor1), interceptors);
7777
}
7878

7979
@Test
8080
public void addTwoInterceptors() {
81-
registry.addInterceptor(interceptor1);
82-
registry.addInterceptor(interceptor2);
81+
this.registry.addInterceptor(this.interceptor1);
82+
this.registry.addInterceptor(this.interceptor2);
8383
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
84-
85-
assertEquals(Arrays.asList(interceptor1, interceptor2), interceptors);
84+
assertEquals(Arrays.asList(this.interceptor1, this.interceptor2), interceptors);
8685
}
8786

8887
@Test
8988
public void addInterceptorsWithUrlPatterns() {
90-
registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").excludePathPatterns("/path1/secret");
91-
registry.addInterceptor(interceptor2).addPathPatterns("/path2");
89+
this.registry.addInterceptor(this.interceptor1).addPathPatterns("/path1/**").excludePathPatterns("/path1/secret");
90+
this.registry.addInterceptor(this.interceptor2).addPathPatterns("/path2");
9291

93-
assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1"));
94-
assertEquals(Arrays.asList(interceptor2), getInterceptorsForPath("/path2"));
92+
assertEquals(Arrays.asList(this.interceptor1), getInterceptorsForPath("/path1"));
93+
assertEquals(Arrays.asList(this.interceptor2), getInterceptorsForPath("/path2"));
9594
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
9695
}
9796

9897
@Test
9998
public void addWebRequestInterceptor() throws Exception {
100-
registry.addWebRequestInterceptor(webRequestInterceptor1);
99+
this.registry.addWebRequestInterceptor(this.webInterceptor1);
101100
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
102101

103102
assertEquals(1, interceptors.size());
104-
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1);
103+
verifyWebInterceptor(interceptors.get(0), this.webInterceptor1);
105104
}
106105

107106
@Test
108107
public void addWebRequestInterceptors() throws Exception {
109-
registry.addWebRequestInterceptor(webRequestInterceptor1);
110-
registry.addWebRequestInterceptor(webRequestInterceptor2);
108+
this.registry.addWebRequestInterceptor(this.webInterceptor1);
109+
this.registry.addWebRequestInterceptor(this.webInterceptor2);
111110
List<HandlerInterceptor> interceptors = getInterceptorsForPath(null);
112111

113112
assertEquals(2, interceptors.size());
114-
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1);
115-
verifyAdaptedInterceptor(interceptors.get(1), webRequestInterceptor2);
113+
verifyWebInterceptor(interceptors.get(0), this.webInterceptor1);
114+
verifyWebInterceptor(interceptors.get(1), this.webInterceptor2);
116115
}
117116

118117
@Test
119118
public void addInterceptorsWithCustomPathMatcher() {
120119
PathMatcher pathMatcher = Mockito.mock(PathMatcher.class);
121-
registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
120+
this.registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
122121

123-
MappedInterceptor mappedInterceptor = (MappedInterceptor) registry.getInterceptors().get(0);
122+
MappedInterceptor mappedInterceptor = (MappedInterceptor) this.registry.getInterceptors().get(0);
124123
assertSame(pathMatcher, mappedInterceptor.getPathMatcher());
125124
}
126125

127126
@Test
128127
public void addWebRequestInterceptorsWithUrlPatterns() throws Exception {
129-
registry.addWebRequestInterceptor(webRequestInterceptor1).addPathPatterns("/path1");
130-
registry.addWebRequestInterceptor(webRequestInterceptor2).addPathPatterns("/path2");
128+
this.registry.addWebRequestInterceptor(this.webInterceptor1).addPathPatterns("/path1");
129+
this.registry.addWebRequestInterceptor(this.webInterceptor2).addPathPatterns("/path2");
131130

132131
List<HandlerInterceptor> interceptors = getInterceptorsForPath("/path1");
133132
assertEquals(1, interceptors.size());
134-
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1);
133+
verifyWebInterceptor(interceptors.get(0), this.webInterceptor1);
135134

136135
interceptors = getInterceptorsForPath("/path2");
137136
assertEquals(1, interceptors.size());
138-
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2);
137+
verifyWebInterceptor(interceptors.get(0), this.webInterceptor2);
139138
}
140139

141-
/**
142-
* Test for SPR-11130
143-
*/
140+
// SPR-11130
141+
144142
@Test
145143
public void addInterceptorWithExcludePathPatternOnly() {
146-
registry.addInterceptor(interceptor1).excludePathPatterns("/path1/secret");
147-
registry.addInterceptor(interceptor2).addPathPatterns("/path2");
144+
this.registry.addInterceptor(this.interceptor1).excludePathPatterns("/path1/secret");
145+
this.registry.addInterceptor(this.interceptor2).addPathPatterns("/path2");
148146

149-
assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1"));
150-
assertEquals(Arrays.asList(interceptor1, interceptor2), getInterceptorsForPath("/path2"));
147+
assertEquals(Arrays.asList(this.interceptor1), getInterceptorsForPath("/path1"));
148+
assertEquals(Arrays.asList(this.interceptor1, this.interceptor2), getInterceptorsForPath("/path2"));
151149
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
152150
}
153151

154152

155153
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
156154
PathMatcher pathMatcher = new AntPathMatcher();
157155
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
158-
for (Object interceptor : registry.getInterceptors()) {
156+
for (Object interceptor : this.registry.getInterceptors()) {
159157
if (interceptor instanceof MappedInterceptor) {
160158
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
161159
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
@@ -172,10 +170,9 @@ else if (interceptor instanceof HandlerInterceptor) {
172170
return result;
173171
}
174172

175-
private void verifyAdaptedInterceptor(HandlerInterceptor interceptor, TestWebRequestInterceptor webInterceptor)
176-
throws Exception {
173+
private void verifyWebInterceptor(HandlerInterceptor interceptor, TestWebRequestInterceptor webInterceptor) throws Exception {
177174
assertTrue(interceptor instanceof WebRequestHandlerInterceptorAdapter);
178-
interceptor.preHandle(request, response, null);
175+
interceptor.preHandle(this.request, this.response, null);
179176
assertTrue(webInterceptor.preHandleInvoked);
180177
}
181178

@@ -195,7 +192,6 @@ public void postHandle(WebRequest request, ModelMap model) throws Exception {
195192
@Override
196193
public void afterCompletion(WebRequest request, Exception ex) throws Exception {
197194
}
198-
199195
}
200196

201197
}

0 commit comments

Comments
 (0)