KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > filter > BrowserCacheFilter


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.filter;
17
18 import com.blandware.atleap.webapp.util.core.GlobalProperties;
19 import com.blandware.atleap.webapp.util.core.WebappConstants;
20
21 import javax.servlet.*;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * <p>Filter to apply Cache-Control HTTP header if it does not exist in order to allow browser to cache mapped resource</p>
28  * <p>Also it applies character encoding for all filtered requests</p>
29  * <p/>
30  * <p><a HREF="BrowserCacheFilter.java.htm"><i>View Source</i></a></p>
31  *
32  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
33  * @version $Revision: 1.4 $ $Date: 2005/08/24 07:29:42 $
34  * @web.filter name="browserCacheFilter"
35  */

36 public class BrowserCacheFilter implements Filter {
37
38     protected FilterConfig config = null;
39
40     /**
41      * Initializes the filter with a given filter config
42      *
43      * @param config The config to use
44      * @throws ServletException
45      */

46     public void init(FilterConfig config) throws ServletException {
47         this.config = config;
48     }
49
50     /**
51      * Destroys the filter.
52      */

53     public void destroy() {
54         config = null;
55     }
56
57     /**
58      * Filters a request: adds cache control headers
59      *
60      * @param req Filtered request
61      * @param resp Response that will be result of filtering
62      * @param chain Chain of following filters
63      * @throws IOException
64      * @throws ServletException
65      */

66     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc resp,
67                          FilterChain chain)
68             throws IOException JavaDoc, ServletException {
69         // cast to the types I want to use
70
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
71         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) resp;
72
73         int maxAge = ((Integer JavaDoc) GlobalProperties.getInstance(config.getServletContext()).getInteger(WebappConstants.CACHE_RESOURCE_MAXAGE_PROPERTY, -1)).intValue();
74         if ( maxAge >= 0 ) {
75             response.setHeader("Cache-Control", "public,max-age=" + maxAge);
76             response.setHeader("Pragma", "");
77         } else {
78             response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
79             response.setHeader("Pragma", "no-cache");
80         }
81
82         response.setCharacterEncoding("utf-8");
83
84         chain.doFilter(request, response);
85     }
86 }
87
Popular Tags