1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .web .servlet ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
21
+ import java .util .Collections ;
20
22
import java .util .List ;
21
23
22
24
import javax .servlet .http .HttpServletRequest ;
27
29
28
30
import org .springframework .lang .Nullable ;
29
31
import org .springframework .util .CollectionUtils ;
30
- import org .springframework .util .ObjectUtils ;
31
32
32
33
/**
33
34
* Handler execution chain, consisting of handler object and any handler interceptors.
@@ -43,11 +44,7 @@ public class HandlerExecutionChain {
43
44
44
45
private final Object handler ;
45
46
46
- @ Nullable
47
- private HandlerInterceptor [] interceptors ;
48
-
49
- @ Nullable
50
- private List <HandlerInterceptor > interceptorList ;
47
+ private final List <HandlerInterceptor > interceptorList = new ArrayList <>();
51
48
52
49
private int interceptorIndex = -1 ;
53
50
@@ -67,17 +64,26 @@ public HandlerExecutionChain(Object handler) {
67
64
* (in the given order) before the handler itself executes
68
65
*/
69
66
public HandlerExecutionChain (Object handler , @ Nullable HandlerInterceptor ... interceptors ) {
67
+ this (handler , (interceptors != null ? Arrays .asList (interceptors ) : Collections .emptyList ()));
68
+ }
69
+
70
+ /**
71
+ * Create a new HandlerExecutionChain.
72
+ * @param handler the handler object to execute
73
+ * @param interceptorList the list of interceptors to apply
74
+ * (in the given order) before the handler itself executes
75
+ * @since 5.3
76
+ */
77
+ public HandlerExecutionChain (Object handler , List <HandlerInterceptor > interceptorList ) {
70
78
if (handler instanceof HandlerExecutionChain ) {
71
79
HandlerExecutionChain originalChain = (HandlerExecutionChain ) handler ;
72
80
this .handler = originalChain .getHandler ();
73
- this .interceptorList = new ArrayList <>();
74
- CollectionUtils .mergeArrayIntoCollection (originalChain .getInterceptors (), this .interceptorList );
75
- CollectionUtils .mergeArrayIntoCollection (interceptors , this .interceptorList );
81
+ this .interceptorList .addAll (originalChain .interceptorList );
76
82
}
77
83
else {
78
84
this .handler = handler ;
79
- this .interceptors = interceptors ;
80
85
}
86
+ this .interceptorList .addAll (interceptorList );
81
87
}
82
88
83
89
@@ -88,30 +94,25 @@ public Object getHandler() {
88
94
return this .handler ;
89
95
}
90
96
97
+ /**
98
+ * Add the given interceptor to the end of this chain.
99
+ */
91
100
public void addInterceptor (HandlerInterceptor interceptor ) {
92
- initInterceptorList () .add (interceptor );
101
+ this . interceptorList .add (interceptor );
93
102
}
94
103
104
+ /**
105
+ * Add the given interceptor at the specified index of this chain.
106
+ */
95
107
public void addInterceptor (int index , HandlerInterceptor interceptor ) {
96
- initInterceptorList () .add (index , interceptor );
108
+ this . interceptorList .add (index , interceptor );
97
109
}
98
110
111
+ /**
112
+ * Add the given interceptors to the end of this chain.
113
+ */
99
114
public void addInterceptors (HandlerInterceptor ... interceptors ) {
100
- if (!ObjectUtils .isEmpty (interceptors )) {
101
- CollectionUtils .mergeArrayIntoCollection (interceptors , initInterceptorList ());
102
- }
103
- }
104
-
105
- private List <HandlerInterceptor > initInterceptorList () {
106
- if (this .interceptorList == null ) {
107
- this .interceptorList = new ArrayList <>();
108
- if (this .interceptors != null ) {
109
- // An interceptor array specified through the constructor
110
- CollectionUtils .mergeArrayIntoCollection (this .interceptors , this .interceptorList );
111
- }
112
- }
113
- this .interceptors = null ;
114
- return this .interceptorList ;
115
+ CollectionUtils .mergeArrayIntoCollection (interceptors , this .interceptorList );
115
116
}
116
117
117
118
/**
@@ -120,10 +121,17 @@ private List<HandlerInterceptor> initInterceptorList() {
120
121
*/
121
122
@ Nullable
122
123
public HandlerInterceptor [] getInterceptors () {
123
- if (this .interceptors == null && this .interceptorList != null ) {
124
- this .interceptors = this .interceptorList .toArray (new HandlerInterceptor [0 ]);
125
- }
126
- return this .interceptors ;
124
+ return (!this .interceptorList .isEmpty () ? this .interceptorList .toArray (new HandlerInterceptor [0 ]) : null );
125
+ }
126
+
127
+ /**
128
+ * Return the list of interceptors to apply (in the given order).
129
+ * @return the list of HandlerInterceptors instances (potentially empty)
130
+ * @since 5.3
131
+ */
132
+ public List <HandlerInterceptor > getInterceptorList () {
133
+ return (!this .interceptorList .isEmpty () ? Collections .unmodifiableList (this .interceptorList ) :
134
+ Collections .emptyList ());
127
135
}
128
136
129
137
@@ -134,16 +142,13 @@ public HandlerInterceptor[] getInterceptors() {
134
142
* that this interceptor has already dealt with the response itself.
135
143
*/
136
144
boolean applyPreHandle (HttpServletRequest request , HttpServletResponse response ) throws Exception {
137
- HandlerInterceptor [] interceptors = getInterceptors ();
138
- if (!ObjectUtils .isEmpty (interceptors )) {
139
- for (int i = 0 ; i < interceptors .length ; i ++) {
140
- HandlerInterceptor interceptor = interceptors [i ];
141
- if (!interceptor .preHandle (request , response , this .handler )) {
142
- triggerAfterCompletion (request , response , null );
143
- return false ;
144
- }
145
- this .interceptorIndex = i ;
145
+ for (int i = 0 ; i < this .interceptorList .size (); i ++) {
146
+ HandlerInterceptor interceptor = this .interceptorList .get (i );
147
+ if (!interceptor .preHandle (request , response , this .handler )) {
148
+ triggerAfterCompletion (request , response , null );
149
+ return false ;
146
150
}
151
+ this .interceptorIndex = i ;
147
152
}
148
153
return true ;
149
154
}
@@ -154,12 +159,9 @@ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response)
154
159
void applyPostHandle (HttpServletRequest request , HttpServletResponse response , @ Nullable ModelAndView mv )
155
160
throws Exception {
156
161
157
- HandlerInterceptor [] interceptors = getInterceptors ();
158
- if (!ObjectUtils .isEmpty (interceptors )) {
159
- for (int i = interceptors .length - 1 ; i >= 0 ; i --) {
160
- HandlerInterceptor interceptor = interceptors [i ];
161
- interceptor .postHandle (request , response , this .handler , mv );
162
- }
162
+ for (int i = this .interceptorList .size () - 1 ; i >= 0 ; i --) {
163
+ HandlerInterceptor interceptor = this .interceptorList .get (i );
164
+ interceptor .postHandle (request , response , this .handler , mv );
163
165
}
164
166
}
165
167
@@ -168,19 +170,14 @@ void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @
168
170
* Will just invoke afterCompletion for all interceptors whose preHandle invocation
169
171
* has successfully completed and returned true.
170
172
*/
171
- void triggerAfterCompletion (HttpServletRequest request , HttpServletResponse response , @ Nullable Exception ex )
172
- throws Exception {
173
-
174
- HandlerInterceptor [] interceptors = getInterceptors ();
175
- if (!ObjectUtils .isEmpty (interceptors )) {
176
- for (int i = this .interceptorIndex ; i >= 0 ; i --) {
177
- HandlerInterceptor interceptor = interceptors [i ];
178
- try {
179
- interceptor .afterCompletion (request , response , this .handler , ex );
180
- }
181
- catch (Throwable ex2 ) {
182
- logger .error ("HandlerInterceptor.afterCompletion threw exception" , ex2 );
183
- }
173
+ void triggerAfterCompletion (HttpServletRequest request , HttpServletResponse response , @ Nullable Exception ex ) {
174
+ for (int i = this .interceptorIndex ; i >= 0 ; i --) {
175
+ HandlerInterceptor interceptor = this .interceptorList .get (i );
176
+ try {
177
+ interceptor .afterCompletion (request , response , this .handler , ex );
178
+ }
179
+ catch (Throwable ex2 ) {
180
+ logger .error ("HandlerInterceptor.afterCompletion threw exception" , ex2 );
184
181
}
185
182
}
186
183
}
@@ -189,16 +186,16 @@ void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse resp
189
186
* Apply afterConcurrentHandlerStarted callback on mapped AsyncHandlerInterceptors.
190
187
*/
191
188
void applyAfterConcurrentHandlingStarted (HttpServletRequest request , HttpServletResponse response ) {
192
- HandlerInterceptor [] interceptors = getInterceptors ();
193
- if (! ObjectUtils . isEmpty ( interceptors )) {
194
- for ( int i = interceptors . length - 1 ; i >= 0 ; i -- ) {
195
- if ( interceptors [ i ] instanceof AsyncHandlerInterceptor ) {
196
- try {
197
- AsyncHandlerInterceptor asyncInterceptor = ( AsyncHandlerInterceptor ) interceptors [ i ] ;
198
- asyncInterceptor . afterConcurrentHandlingStarted ( request , response , this . handler );
199
- }
200
- catch ( Throwable ex ) {
201
- logger .error ("Interceptor [" + interceptors [ i ] + "] failed in afterConcurrentHandlingStarted" , ex );
189
+ for ( int i = this . interceptorList . size () - 1 ; i >= 0 ; i --) {
190
+ HandlerInterceptor interceptor = this . interceptorList . get ( i );
191
+ if ( interceptor instanceof AsyncHandlerInterceptor ) {
192
+ try {
193
+ AsyncHandlerInterceptor asyncInterceptor = ( AsyncHandlerInterceptor ) interceptor ;
194
+ asyncInterceptor . afterConcurrentHandlingStarted ( request , response , this . handler ) ;
195
+ }
196
+ catch ( Throwable ex ) {
197
+ if ( logger . isErrorEnabled () ) {
198
+ logger .error ("Interceptor [" + interceptor + "] failed in afterConcurrentHandlingStarted" , ex );
202
199
}
203
200
}
204
201
}
@@ -207,23 +204,11 @@ void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServlet
207
204
208
205
209
206
/**
210
- * Delegates to the handler and interceptors' {@code toString()}.
207
+ * Delegates to the handler's {@code toString()} implementation .
211
208
*/
212
209
@ Override
213
210
public String toString () {
214
- Object handler = getHandler ();
215
- StringBuilder sb = new StringBuilder ();
216
- sb .append ("HandlerExecutionChain with [" ).append (handler ).append ("] and " );
217
- if (this .interceptorList != null ) {
218
- sb .append (this .interceptorList .size ());
219
- }
220
- else if (this .interceptors != null ) {
221
- sb .append (this .interceptors .length );
222
- }
223
- else {
224
- sb .append (0 );
225
- }
226
- return sb .append (" interceptors" ).toString ();
211
+ return "HandlerExecutionChain with [" + getHandler () + "] and " + this .interceptorList .size () + " interceptors" ;
227
212
}
228
213
229
214
}
0 commit comments