File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\ChainedServletFilter.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.security;
25
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import javax.servlet.Filter;
33 import javax.servlet.FilterChain;
34 import javax.servlet.FilterConfig;
35 import javax.servlet.ServletException;
36 import javax.servlet.ServletRequest;
37 import javax.servlet.ServletResponse;
38
39 /**
40 * Servlet {@link Filter} that chains multiple {@link Filter}s.
41 *
42 * @author Kohsuke Kawaguchi
43 */
44 public class ChainedServletFilter implements Filter {
45 // array is assumed to be immutable once set
46 protected volatile Filter[] filters;
47
48 public ChainedServletFilter() {
49 filters = new Filter[0];
50 }
51
52 public ChainedServletFilter(Filter... filters) {
53 this(Arrays.asList(filters));
54 }
55
56 public ChainedServletFilter(Collection<? extends Filter> filters) {
57 setFilters(filters);
58 }
59
60 public void setFilters(Collection<? extends Filter> filters) {
61 this.filters = filters.toArray(new Filter[filters.size()]);
62 }
63
64 public void init(FilterConfig filterConfig) throws ServletException {
65 if (LOGGER.isLoggable(Level.FINEST))
66 for (Filter f : filters)
67 LOGGER.finest("ChainedServletFilter contains: " + f);
68
69 for (Filter f : filters)
70 f.init(filterConfig);
71 }
72
73 public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException {
74 LOGGER.entering(ChainedServletFilter.class.getName(), "doFilter");
75
76 new FilterChain() {
77 private int position=0;
78 // capture the array for thread-safety
79 private final Filter[] filters = ChainedServletFilter.this.filters;
80
81 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
82 if(position==filters.length) {
83 // reached to the end
84 chain.doFilter(request,response);
85 } else {
86 // call next
87 filters[position++].doFilter(request,response,this);
88 }
89 }
90 }.doFilter(request,response);
91 }
92
93 public void destroy() {
94 for (Filter f : filters)
95 f.destroy();
96 }
97
98 private static final Logger LOGGER = Logger.getLogger(ChainedServletFilter.class.getName());
99 }
100