KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > filters > ReplyHeaderFilter


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 JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import javax.servlet.Filter JavaDoc;
28 import javax.servlet.FilterChain JavaDoc;
29 import javax.servlet.FilterConfig JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.ServletRequest JavaDoc;
32 import javax.servlet.ServletResponse JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc
64 {
65    static Logger log = Logger.getLogger(ReplyHeaderFilter.class);
66    private String JavaDoc[][] replyHeaders = {{}};
67
68    public void init(FilterConfig JavaDoc config)
69    {
70       Enumeration JavaDoc names = config.getInitParameterNames();
71       ArrayList JavaDoc tmp = new ArrayList JavaDoc();
72       while( names.hasMoreElements() )
73       {
74          String JavaDoc name = (String JavaDoc) names.nextElement();
75          String JavaDoc value = config.getInitParameter(name);
76          log.debug("Adding header name: "+name+"='"+value+"'");
77          String JavaDoc[] pair = {name, value};
78          tmp.add(pair);
79       }
80       replyHeaders = new String JavaDoc[tmp.size()][2];
81       tmp.toArray(replyHeaders);
82    }
83
84    public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
85       FilterChain JavaDoc chain)
86       throws IOException JavaDoc, ServletException JavaDoc
87    {
88       // Apply the headers
89
HttpServletResponse JavaDoc httpResponse = (HttpServletResponse JavaDoc) response;
90       for(int n = 0; n < replyHeaders.length; n ++)
91       {
92          String JavaDoc name = replyHeaders[n][0];
93          String JavaDoc value = replyHeaders[n][1];
94          httpResponse.addHeader(name, value);
95       }
96       chain.doFilter(request, response);
97    }
98
99    public void destroy()
100    {
101    }
102 }
103
Popular Tags