-
Notifications
You must be signed in to change notification settings - Fork 138
/
ApplicationFilterChain.java
356 lines (296 loc) · 12.4 KB
/
ApplicationFilterChain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.core;
import org.apache.catalina.Globals;
import org.apache.catalina.LogFacade;
import org.apache.catalina.security.SecurityUtil;
import org.apache.catalina.util.InstanceSupport;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.Principal;
import java.security.PrivilegedActionException;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import static org.apache.catalina.InstanceEvent.EventType.AFTER_FILTER_EVENT;
import static org.apache.catalina.InstanceEvent.EventType.BEFORE_FILTER_EVENT;
/**
* Implementation of <code>jakarta.servlet.FilterChain</code> used to manage
* the execution of a set of filters for a particular request. When the
* set of defined filters has all been executed, the next call to
* <code>doFilter()</code> will execute the servlet's <code>service()</code>
* method itself.
*
* @author Craig R. McClanahan
* @version $Revision: 1.6 $ $Date: 2006/11/21 17:39:39 $
*/
final class ApplicationFilterChain implements FilterChain {
private static final Logger log = LogFacade.getLogger();
private static final ResourceBundle rb = log.getResourceBundle();
// -------------------------------------------------------------- Constants
public static final int INCREMENT = 10;
// ----------------------------------------------------------- Constructors
/**
* Construct a new chain instance with no defined filters.
*/
public ApplicationFilterChain() {
super();
}
// ----------------------------------------------------- Instance Variables
/**
* Filters.
*/
private ApplicationFilterConfig[] filters =
new ApplicationFilterConfig[0];
/**
* The int which is used to maintain the current position
* in the filter chain.
*/
private int pos = 0;
/**
* The int which gives the current number of filters in the chain.
*/
private int n = 0;
/**
* The servlet instance to be executed by this chain.
*/
private Servlet servlet = null;
/**
* The wrapper around the servlet instance to be executed by this chain.
*/
private StandardWrapper wrapper = null;
/**
* Static class array used when the SecurityManager is turned on and
* <code>doFilter</code is invoked.
*/
private static Class<?>[] classType = new Class[]{ServletRequest.class,
ServletResponse.class,
FilterChain.class};
/**
* Static class array used when the SecurityManager is turned on and
* <code>service</code is invoked.
*/
/* IASRI 4665318
private static Class<?>[] classTypeUsedInService = new Class[]{
ServletRequest.class,
ServletResponse.class};
*/
// ---------------------------------------------------- FilterChain Methods
/**
* Invoke the next filter in this chain, passing the specified request
* and response. If there are no more filters in this chain, invoke
* the <code>service()</code> method of the servlet itself.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (Globals.IS_SECURITY_ENABLED) {
final ServletRequest req = request;
final ServletResponse res = response;
try {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<Void>() {
public Void run()
throws ServletException, IOException {
internalDoFilter(req,res);
return null;
}
}
);
} catch( PrivilegedActionException pe) {
Exception e = pe.getException();
if (e instanceof ServletException)
throw (ServletException) e;
else if (e instanceof IOException)
throw (IOException) e;
else if (e instanceof RuntimeException)
throw (RuntimeException) e;
else
throw new ServletException(e.getMessage(), e);
}
} else {
internalDoFilter(request,response);
}
}
private void internalDoFilter(ServletRequest request,
ServletResponse response)
throws IOException, ServletException {
if (wrapper == null) {
throw new IllegalStateException("Missing wrapper");
}
InstanceSupport support = wrapper.getInstanceSupport();
// Call the next filter if there is one
if (pos < n) {
ApplicationFilterConfig filterConfig = filters[pos++];
if (!filterConfig.isAsyncSupported()) {
RequestFacadeHelper reqFacHelper = RequestFacadeHelper.getInstance(request);
if (reqFacHelper != null) {
reqFacHelper.disableAsyncSupport();
}
}
Filter filter = null;
try {
filter = filterConfig.getFilter();
support.fireInstanceEvent(BEFORE_FILTER_EVENT,
filter, request, response);
if( SecurityUtil.isPackageProtectionEnabled() ) {
final ServletRequest req = request;
final ServletResponse res = response;
Principal principal =
((HttpServletRequest) req).getUserPrincipal();
Object[] filterType = new Object[3];
filterType[0] = req;
filterType[1] = res;
filterType[2] = this;
SecurityUtil.doAsPrivilege
("doFilter", filter, classType, filterType, principal);
} else {
filter.doFilter(request, response, this);
}
support.fireInstanceEvent(AFTER_FILTER_EVENT,
filter, request, response);
} catch (IOException e) {
if (filter != null)
support.fireInstanceEvent(AFTER_FILTER_EVENT,
filter, request, response, e);
throw e;
} catch (ServletException e) {
if (filter != null)
support.fireInstanceEvent(AFTER_FILTER_EVENT,
filter, request, response, e);
throw e;
} catch (RuntimeException e) {
if (filter != null)
support.fireInstanceEvent(AFTER_FILTER_EVENT,
filter, request, response, e);
throw e;
} catch (Throwable e) {
if (filter != null)
support.fireInstanceEvent(AFTER_FILTER_EVENT,
filter, request, response, e);
throw new ServletException
(rb.getString(LogFacade.FILTER_EXECUTION_EXCEPTION), e);
}
return;
}
// We fell off the end of the chain -- call the servlet instance
/* IASRI 4665318
try {
support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
servlet, request, response);
if ((request instanceof HttpServletRequest) &&
(response instanceof HttpServletResponse)) {
// START SJS WS 7.0 6236329
//if( System.getSecurityManager() != null) {
if ( SecurityUtil.executeUnderSubjectDoAs() ){
// END OF SJS WS 7.0 6236329
final ServletRequest req = request;
final ServletResponse res = response;
Principal principal =
((HttpServletRequest) req).getUserPrincipal();
Object[] serviceType = new Object[2];
serviceType[0] = req;
serviceType[1] = res;
SecurityUtil.doAsPrivilege("service",
servlet,
classTypeUsedInService,
serviceType,
principal);
serviceType = null;
} else {
servlet.service((HttpServletRequest) request,
(HttpServletResponse) response);
}
} else {
servlet.service(request, response);
}
support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
servlet, request, response);
} catch (IOException e) {
support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
servlet, request, response, e);
throw e;
} catch (ServletException e) {
support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
servlet, request, response, e);
throw e;
} catch (RuntimeException e) {
support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
servlet, request, response, e);
throw e;
} catch (Throwable e) {
support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
servlet, request, response, e);
throw new ServletException
(sm.getString("filterChain.servlet"), e);
//filterChain.servlet=PWC1244: Servlet execution threw an exception
}
*/
// START IASRI 4665318
wrapper.service(request, response, servlet);
// END IASRI 4665318
}
// -------------------------------------------------------- Package Methods
/**
* Add a filter to the set of filters that will be executed in this chain.
*
* @param filterConfig The FilterConfig for the servlet to be executed
*/
void addFilter(ApplicationFilterConfig filterConfig) {
if (n == filters.length) {
ApplicationFilterConfig[] newFilters =
new ApplicationFilterConfig[n + INCREMENT];
System.arraycopy(filters, 0, newFilters, 0, n);
filters = newFilters;
}
filters[n++] = filterConfig;
}
/**
* Release references to the filters and wrapper executed by this chain.
*/
void release() {
n = 0;
pos = 0;
servlet = null;
wrapper = null;
}
/**
* Sets the Servlet instance that will be executed at the end of this
* Filter chain.
*
* @param servlet the Servlet instance that will be executed at the end
* of this Filter chain.
*/
void setServlet(Servlet servlet) {
this.servlet = servlet;
}
/**
* Sets the wrapper of the Servlet that will be executed at the end of
* this Filter chain.
*
* @param wrapper the wrapper of the Servlet that will be executed at
* the end of this Filter chain.
*/
void setWrapper(StandardWrapper wrapper) {
this.wrapper = wrapper;
}
}