1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.web.tomcat.filters;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.jboss.logging.Logger;
36
37 /** A servlet filter that simply adds all header specified in its config
38 to replies the filter is mapped to. An example would be to set the cache
39 control max age:
40
41 <filter>
42 <filter-name>CacheControlFilter</filter-name>
43 <filter-class>filter.ReplyHeaderFilter</filter-class>
44 <init-param>
45 <param-name>Cache-Control</param-name>
46 <param-value>max-age=3600</param-value>
47 </init-param>
48 </filter>
49
50 <filter-mapping>
51 <filter-name>CacheControlFilter</filter-name>
52 <url-pattern>/images/*</url-pattern>
53 </filter-mapping>
54 <filter-mapping>
55 <filter-name>CacheControlFilter</filter-name>
56 <url-pattern>*.js</url-pattern>
57 </filter-mapping>
58
59
60 @author Scott.Stark@jboss.org
61 @version $Revison:$
62 */
63 public class ReplyHeaderFilter implements Filter
64 {
65 static Logger log = Logger.getLogger(ReplyHeaderFilter.class);
66 private String[][] replyHeaders = {{}};
67
68 public void init(FilterConfig config)
69 {
70 Enumeration names = config.getInitParameterNames();
71 ArrayList tmp = new ArrayList();
72 while( names.hasMoreElements() )
73 {
74 String name = (String) names.nextElement();
75 String value = config.getInitParameter(name);
76 log.debug("Adding header name: "+name+"='"+value+"'");
77 String[] pair = {name, value};
78 tmp.add(pair);
79 }
80 replyHeaders = new String[tmp.size()][2];
81 tmp.toArray(replyHeaders);
82 }
83
84 public void doFilter(ServletRequest request, ServletResponse response,
85 FilterChain chain)
86 throws IOException, ServletException
87 {
88 // Apply the headers
89 HttpServletResponse httpResponse = (HttpServletResponse) response;
90 for(int n = 0; n < replyHeaders.length; n ++)
91 {
92 String name = replyHeaders[n][0];
93 String value = replyHeaders[n][1];
94 httpResponse.addHeader(name, value);
95 }
96 chain.doFilter(request, response);
97 }
98
99 public void destroy()
100 {
101 }
102 }