1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 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.
26
26
import org .apache .commons .logging .LogFactory ;
27
27
28
28
import org .springframework .util .CollectionUtils ;
29
+ import org .springframework .util .ObjectUtils ;
29
30
30
31
/**
31
32
* Handler execution chain, consisting of handler object and any handler interceptors.
@@ -53,7 +54,7 @@ public class HandlerExecutionChain {
53
54
* @param handler the handler object to execute
54
55
*/
55
56
public HandlerExecutionChain (Object handler ) {
56
- this (handler , null );
57
+ this (handler , ( HandlerInterceptor []) null );
57
58
}
58
59
59
60
/**
@@ -62,7 +63,7 @@ public HandlerExecutionChain(Object handler) {
62
63
* @param interceptors the array of interceptors to apply
63
64
* (in the given order) before the handler itself executes
64
65
*/
65
- public HandlerExecutionChain (Object handler , HandlerInterceptor [] interceptors ) {
66
+ public HandlerExecutionChain (Object handler , HandlerInterceptor ... interceptors ) {
66
67
if (handler instanceof HandlerExecutionChain ) {
67
68
HandlerExecutionChain originalChain = (HandlerExecutionChain ) handler ;
68
69
this .handler = originalChain .getHandler ();
@@ -76,6 +77,7 @@ public HandlerExecutionChain(Object handler, HandlerInterceptor[] interceptors)
76
77
}
77
78
}
78
79
80
+
79
81
/**
80
82
* Return the handler object to execute.
81
83
* @return the handler object
@@ -85,25 +87,25 @@ public Object getHandler() {
85
87
}
86
88
87
89
public void addInterceptor (HandlerInterceptor interceptor ) {
88
- initInterceptorList ();
89
- this .interceptorList .add (interceptor );
90
+ initInterceptorList ().add (interceptor );
90
91
}
91
92
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 ));
96
96
}
97
97
}
98
98
99
- private void initInterceptorList () {
99
+ private List < HandlerInterceptor > initInterceptorList () {
100
100
if (this .interceptorList == null ) {
101
101
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
+ }
102
106
}
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 ;
107
109
}
108
110
109
111
/**
@@ -117,16 +119,18 @@ public HandlerInterceptor[] getInterceptors() {
117
119
return this .interceptors ;
118
120
}
119
121
122
+
120
123
/**
121
124
* Apply preHandle methods of registered interceptors.
122
125
* @return {@code true} if the execution chain should proceed with the
123
126
* next interceptor or the handler itself. Else, DispatcherServlet assumes
124
127
* that this interceptor has already dealt with the response itself.
125
128
*/
126
129
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 ];
130
134
if (!interceptor .preHandle (request , response , this .handler )) {
131
135
triggerAfterCompletion (request , response , null );
132
136
return false ;
@@ -141,12 +145,12 @@ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response)
141
145
* Apply postHandle methods of registered interceptors.
142
146
*/
143
147
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
+ }
150
154
}
151
155
}
152
156
@@ -158,16 +162,16 @@ void applyPostHandle(HttpServletRequest request, HttpServletResponse response, M
158
162
void triggerAfterCompletion (HttpServletRequest request , HttpServletResponse response , Exception ex )
159
163
throws Exception {
160
164
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
+ }
171
175
}
172
176
}
173
177
}
@@ -176,22 +180,23 @@ void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse resp
176
180
* Apply afterConcurrentHandlerStarted callback on mapped AsyncHandlerInterceptors.
177
181
*/
178
182
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
+ }
190
194
}
191
195
}
192
196
}
193
197
}
194
198
199
+
195
200
/**
196
201
* Delegates to the handler's {@code toString()}.
197
202
*/
0 commit comments