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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, id:digerata
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;
25
26 import java.util.*;
27
28 import javax.servlet.Filter;
29 import javax.servlet.FilterChain;
30 import javax.servlet.FilterConfig;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.IOException;
36
37 /**
38 * This filter allows you to modify headers set by the container or other servlets
39 * that are out of your control. The particular headers you wish to change are configured
40 * in web.xml.
41 * <p>
42 * One particular header you you may wish to deal with is "Cache-Control: no-cache"
43 * This is a problem with Tomcat when security is used. Continue reading for further details.
44 * <p>
45 * If a web app has a &lt;security-constraint&gt; in its web.xml, Tomcat will
46 * add a Cache-Control header to every file it serves from that location. This
47 * header will prevent browsers from caching the file locally and this drastically slows
48 * down Hudson page load times.
49 * <p>
50 * To enable this filter, edit the web.xml file to include:
51 *
52 * <pre>
53 * &lt;filter&gt;
54 * &lt;filter-name&gt;change-headers-filter&lt;/filter-name&gt;
55 * &lt;filter-class&gt;hudson.ResponseHeaderFilter&lt;/filter-class&gt;
56 * &lt;init-param&gt;
57 * &lt;param-name&gt;Pragma&lt;/param-name&gt;
58 * &lt;param-value&gt;public&lt;/param-value&gt;
59 * &lt;/init-param&gt;
60 * &lt;init-param&gt;
61 * &lt;param-name&gt;Cache-Control&lt;/param-name&gt;
62 * &lt;param-value&gt;max-age=86400, public&lt;/param-value&gt;
63 * &lt;/init-param&gt;
64 * &lt;/filter&gt;
65 *
66 * And down below that:
67 *
68 * &lt;filter-mapping&gt;
69 * &lt;filter-name&gt;Headers&lt;/filter-name&gt;
70 * &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
71 * &lt;/filter-mapping&gt;
72 * </pre>
73 *
74 * <p>
75 * In the case of the tomcat cache problem, it is important that the url-pattern for
76 * the filter matches the url-pattern set for the security-constraint.
77 *
78 * @author Mike Wille
79 */
80 public class ResponseHeaderFilter implements Filter {
81 private FilterConfig config;
82
83 public void init(FilterConfig filterConfig) throws ServletException {
84 config = filterConfig;
85 }
86
87 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
88 ServletException {
89 HttpServletResponse httpResp = (HttpServletResponse) resp;
90
91 Enumeration e = config.getInitParameterNames();
92
93 // for each configuration element...
94 while(e.hasMoreElements()) {
95 String headerName = (String) e.nextElement();
96 String headerValue = config.getInitParameter(headerName);
97 // set the header with the given name and value
98 httpResp.setHeader(headerName, headerValue);
99 }
100 chain.doFilter(req, resp);
101 }
102
103 public void destroy() {
104 }
105 }
106