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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
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.util;
25
26 import hudson.ExtensionPoint;
27 import hudson.security.SecurityRealm;
28
29 import javax.servlet.Filter;
30 import javax.servlet.FilterChain;
31 import javax.servlet.FilterConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletRequest;
34 import javax.servlet.ServletResponse;
35 import java.io.IOException;
36 import java.util.List;
37 import java.util.Vector;
38
39 /**
40 * Servlet {@link Filter} that chains multiple {@link Filter}s, provided by plugins
41 *
42 * <p>
43 * While this class by itself is not an extension point, I'm marking this class
44 * as an extension point so that this class will be more discoverable.
45 *
46 * <p>
47 * {@link SecurityRealm} that wants to contribute {@link Filter}s should first
48 * check if {@link SecurityRealm#createFilter(FilterConfig)} is more appropriate.
49 *
50 * @see SecurityRealm
51 */
52 public class PluginServletFilter implements Filter, ExtensionPoint {
53
54 private static final List<Filter> LIST = new Vector<Filter>();
55
56 private static FilterConfig filterConfig;
57
58 public PluginServletFilter() {
59 }
60
61 @edu.umd.cs.findbugs.annotations.SuppressWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
62 public void init(FilterConfig filterConfig) throws ServletException {
63 PluginServletFilter.filterConfig = filterConfig;
64 synchronized (LIST) {
65 for (Filter f : LIST) {
66 f.init(filterConfig);
67 }
68 }
69 }
70
71 public static void addFilter(Filter filter) throws ServletException {
72 synchronized (LIST) {
73 if (filterConfig != null) {
74 filter.init(filterConfig);
75 }
76 LIST.add(filter);
77 }
78 }
79
80 public static void removeFilter(Filter filter) throws ServletException {
81 synchronized (LIST) {
82 LIST.remove(filter);
83 }
84 }
85
86 public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException {
87 new FilterChain() {
88 private int position=0;
89 // capture the array for thread-safety
90 private final Filter[] filters = LIST.toArray(new Filter[LIST.size()]);
91
92 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
93 if(position==filters.length) {
94 // reached to the end
95 chain.doFilter(request,response);
96 } else {
97 // call next
98 filters[position++].doFilter(request,response,this);
99 }
100 }
101 }.doFilter(request,response);
102 }
103
104 public void destroy() {
105 synchronized (LIST) {
106 for (Filter f : LIST)
107 f.destroy();
108 }
109 }
110 }
111