Skip to content

Commit 6b3023c

Browse files
committed
HandlerExecutionChain prevents re-adding the interceptors array to the list (and declares varargs now)
Issue: SPR-12566
1 parent d55af2b commit 6b3023c

File tree

3 files changed

+69
-59
lines changed

3 files changed

+69
-59
lines changed

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/HandlerExecutionChain.java

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.util.CollectionUtils;
24+
import org.springframework.util.ObjectUtils;
2425

2526
/**
2627
* Handler execution chain, consisting of handler object and any handler interceptors.
@@ -45,7 +46,7 @@ public class HandlerExecutionChain {
4546
* @param handler the handler object to execute
4647
*/
4748
public HandlerExecutionChain(Object handler) {
48-
this(handler, null);
49+
this(handler, (HandlerInterceptor[]) null);
4950
}
5051

5152
/**
@@ -54,7 +55,7 @@ public HandlerExecutionChain(Object handler) {
5455
* @param interceptors the array of interceptors to apply
5556
* (in the given order) before the handler itself executes
5657
*/
57-
public HandlerExecutionChain(Object handler, HandlerInterceptor[] interceptors) {
58+
public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
5859
if (handler instanceof HandlerExecutionChain) {
5960
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
6061
this.handler = originalChain.getHandler();
@@ -78,25 +79,25 @@ public Object getHandler() {
7879
}
7980

8081
public void addInterceptor(HandlerInterceptor interceptor) {
81-
initInterceptorList();
82-
this.interceptorList.add(interceptor);
82+
initInterceptorList().add(interceptor);
8383
}
8484

85-
public void addInterceptors(HandlerInterceptor[] interceptors) {
86-
if (interceptors != null) {
87-
initInterceptorList();
88-
this.interceptorList.addAll(Arrays.asList(interceptors));
85+
public void addInterceptors(HandlerInterceptor... interceptors) {
86+
if (!ObjectUtils.isEmpty(interceptors)) {
87+
initInterceptorList().addAll(Arrays.asList(interceptors));
8988
}
9089
}
9190

92-
private void initInterceptorList() {
91+
private List<HandlerInterceptor> initInterceptorList() {
9392
if (this.interceptorList == null) {
9493
this.interceptorList = new ArrayList<HandlerInterceptor>();
94+
if (this.interceptors != null) {
95+
// An interceptor array specified through the constructor
96+
this.interceptorList.addAll(Arrays.asList(this.interceptors));
97+
}
9598
}
96-
if (this.interceptors != null) {
97-
this.interceptorList.addAll(Arrays.asList(this.interceptors));
98-
this.interceptors = null;
99-
}
99+
this.interceptors = null;
100+
return this.interceptorList;
100101
}
101102

102103
/**

spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerExecutionChain.java

+49-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.apache.commons.logging.LogFactory;
2727

2828
import org.springframework.util.CollectionUtils;
29+
import org.springframework.util.ObjectUtils;
2930

3031
/**
3132
* Handler execution chain, consisting of handler object and any handler interceptors.
@@ -53,7 +54,7 @@ public class HandlerExecutionChain {
5354
* @param handler the handler object to execute
5455
*/
5556
public HandlerExecutionChain(Object handler) {
56-
this(handler, null);
57+
this(handler, (HandlerInterceptor[]) null);
5758
}
5859

5960
/**
@@ -62,7 +63,7 @@ public HandlerExecutionChain(Object handler) {
6263
* @param interceptors the array of interceptors to apply
6364
* (in the given order) before the handler itself executes
6465
*/
65-
public HandlerExecutionChain(Object handler, HandlerInterceptor[] interceptors) {
66+
public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
6667
if (handler instanceof HandlerExecutionChain) {
6768
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
6869
this.handler = originalChain.getHandler();
@@ -76,6 +77,7 @@ public HandlerExecutionChain(Object handler, HandlerInterceptor[] interceptors)
7677
}
7778
}
7879

80+
7981
/**
8082
* Return the handler object to execute.
8183
* @return the handler object
@@ -85,25 +87,25 @@ public Object getHandler() {
8587
}
8688

8789
public void addInterceptor(HandlerInterceptor interceptor) {
88-
initInterceptorList();
89-
this.interceptorList.add(interceptor);
90+
initInterceptorList().add(interceptor);
9091
}
9192

92-
public void addInterceptors(HandlerInterceptor[] interceptors) {
93-
if (interceptors != null) {
94-
initInterceptorList();
95-
this.interceptorList.addAll(Arrays.asList(interceptors));
93+
public void addInterceptors(HandlerInterceptor... interceptors) {
94+
if (!ObjectUtils.isEmpty(interceptors)) {
95+
initInterceptorList().addAll(Arrays.asList(interceptors));
9696
}
9797
}
9898

99-
private void initInterceptorList() {
99+
private List<HandlerInterceptor> initInterceptorList() {
100100
if (this.interceptorList == null) {
101101
this.interceptorList = new ArrayList<HandlerInterceptor>();
102+
if (this.interceptors != null) {
103+
// An interceptor array specified through the constructor
104+
this.interceptorList.addAll(Arrays.asList(this.interceptors));
105+
}
102106
}
103-
if (this.interceptors != null) {
104-
this.interceptorList.addAll(Arrays.asList(this.interceptors));
105-
this.interceptors = null;
106-
}
107+
this.interceptors = null;
108+
return this.interceptorList;
107109
}
108110

109111
/**
@@ -117,16 +119,18 @@ public HandlerInterceptor[] getInterceptors() {
117119
return this.interceptors;
118120
}
119121

122+
120123
/**
121124
* Apply preHandle methods of registered interceptors.
122125
* @return {@code true} if the execution chain should proceed with the
123126
* next interceptor or the handler itself. Else, DispatcherServlet assumes
124127
* that this interceptor has already dealt with the response itself.
125128
*/
126129
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
127-
if (getInterceptors() != null) {
128-
for (int i = 0; i < getInterceptors().length; i++) {
129-
HandlerInterceptor interceptor = getInterceptors()[i];
130+
HandlerInterceptor[] interceptors = getInterceptors();
131+
if (!ObjectUtils.isEmpty(interceptors)) {
132+
for (int i = 0; i < interceptors.length; i++) {
133+
HandlerInterceptor interceptor = interceptors[i];
130134
if (!interceptor.preHandle(request, response, this.handler)) {
131135
triggerAfterCompletion(request, response, null);
132136
return false;
@@ -141,12 +145,12 @@ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response)
141145
* Apply postHandle methods of registered interceptors.
142146
*/
143147
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {
144-
if (getInterceptors() == null) {
145-
return;
146-
}
147-
for (int i = getInterceptors().length - 1; i >= 0; i--) {
148-
HandlerInterceptor interceptor = getInterceptors()[i];
149-
interceptor.postHandle(request, response, this.handler, mv);
148+
HandlerInterceptor[] interceptors = getInterceptors();
149+
if (!ObjectUtils.isEmpty(interceptors)) {
150+
for (int i = interceptors.length - 1; i >= 0; i--) {
151+
HandlerInterceptor interceptor = interceptors[i];
152+
interceptor.postHandle(request, response, this.handler, mv);
153+
}
150154
}
151155
}
152156

@@ -158,16 +162,16 @@ void applyPostHandle(HttpServletRequest request, HttpServletResponse response, M
158162
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)
159163
throws Exception {
160164

161-
if (getInterceptors() == null) {
162-
return;
163-
}
164-
for (int i = this.interceptorIndex; i >= 0; i--) {
165-
HandlerInterceptor interceptor = getInterceptors()[i];
166-
try {
167-
interceptor.afterCompletion(request, response, this.handler, ex);
168-
}
169-
catch (Throwable ex2) {
170-
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
165+
HandlerInterceptor[] interceptors = getInterceptors();
166+
if (!ObjectUtils.isEmpty(interceptors)) {
167+
for (int i = this.interceptorIndex; i >= 0; i--) {
168+
HandlerInterceptor interceptor = interceptors[i];
169+
try {
170+
interceptor.afterCompletion(request, response, this.handler, ex);
171+
}
172+
catch (Throwable ex2) {
173+
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
174+
}
171175
}
172176
}
173177
}
@@ -176,22 +180,23 @@ void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse resp
176180
* Apply afterConcurrentHandlerStarted callback on mapped AsyncHandlerInterceptors.
177181
*/
178182
void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response) {
179-
if (getInterceptors() == null) {
180-
return;
181-
}
182-
for (int i = getInterceptors().length - 1; i >= 0; i--) {
183-
if (interceptors[i] instanceof AsyncHandlerInterceptor) {
184-
try {
185-
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) this.interceptors[i];
186-
asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);
187-
}
188-
catch (Throwable ex) {
189-
logger.error("Interceptor [" + interceptors[i] + "] failed in afterConcurrentHandlingStarted", ex);
183+
HandlerInterceptor[] interceptors = getInterceptors();
184+
if (!ObjectUtils.isEmpty(interceptors)) {
185+
for (int i = interceptors.length - 1; i >= 0; i--) {
186+
if (interceptors[i] instanceof AsyncHandlerInterceptor) {
187+
try {
188+
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) interceptors[i];
189+
asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);
190+
}
191+
catch (Throwable ex) {
192+
logger.error("Interceptor [" + interceptors[i] + "] failed in afterConcurrentHandlingStarted", ex);
193+
}
190194
}
191195
}
192196
}
193197
}
194198

199+
195200
/**
196201
* Delegates to the handler's {@code toString()}.
197202
*/

spring-webmvc/src/test/java/org/springframework/web/servlet/HandlerExecutionChainTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ public class HandlerExecutionChainTests {
4646

4747
private AsyncHandlerInterceptor interceptor3;
4848

49+
4950
@Before
5051
public void setup() {
5152
this.request = new MockHttpServletRequest();
@@ -60,9 +61,12 @@ public void setup() {
6061

6162
this.chain.addInterceptor(this.interceptor1);
6263
this.chain.addInterceptor(this.interceptor2);
64+
assertEquals(2, this.chain.getInterceptors().length);
6365
this.chain.addInterceptor(this.interceptor3);
66+
assertEquals(3, this.chain.getInterceptors().length);
6467
}
6568

69+
6670
@Test
6771
public void successScenario() throws Exception {
6872
ModelAndView mav = new ModelAndView();

0 commit comments

Comments
 (0)