KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > filters > cache > CacheFilter


1 /*
2  * $$Id: CacheFilter.java,v 1.3 2005/06/07 12:32:31 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK ***** The contents of this file are subject to the
5  * Mozilla Public License Version 1.1 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of the License
7  * at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
11  * the specific language governing rights and limitations under the License.
12  *
13  * The Original Code is JGossip forum code.
14  *
15  * The Initial Developer of the Original Code is the JResearch, Org. Portions
16  * created by the Initial Developer are Copyright (C) 2004 the Initial
17  * Developer. All Rights Reserved.
18  *
19  * Contributor(s): Dmitry Belov <bel@jresearch.org>, Jayson Falkner
20  * <jayson@jspinsider.com>
21  *
22  * ***** END LICENSE BLOCK *****
23  */

24 package org.jresearch.gossip.filters.cache;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.util.Calendar JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Locale JavaDoc;
34
35 import javax.servlet.Filter JavaDoc;
36 import javax.servlet.FilterChain JavaDoc;
37 import javax.servlet.FilterConfig JavaDoc;
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.ServletOutputStream JavaDoc;
41 import javax.servlet.ServletRequest JavaDoc;
42 import javax.servlet.ServletResponse JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45
46 import org.jresearch.gossip.IConst;
47 import org.jresearch.gossip.beans.user.User;
48
49 public class CacheFilter implements Filter JavaDoc {
50
51     ServletContext JavaDoc sc;
52
53     FilterConfig JavaDoc fc;
54
55     long cacheTimeout = Long.MAX_VALUE;
56
57     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc res,
58             FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
59         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
60         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) res;
61
62         // check uri -- fix this, shouldn't have to ignore these
63
String JavaDoc uri = request.getRequestURI();
64         if (uri == null || uri.equals("") || uri.equals("/")) {
65             chain.doFilter(request, response);
66             return;
67         }
68         // check if was a resource that shouldn't be cached.
69
String JavaDoc r = sc.getRealPath("");
70         String JavaDoc path = fc.getInitParameter(uri);
71         if (path != null && path.equals("nocache")) {
72             chain.doFilter(request, response);
73             return;
74         }
75         path = r + path;
76
77         // customize to match parameters
78
String JavaDoc id = request.getRequestURI()
79                 + request.getQueryString()
80                 + "__"
81                 + ((User) request.getSession().getAttribute(
82                         IConst.SESSION.USER_KEY)).getName();
83         // optionally append i18n sensitivity
84
String JavaDoc localeSensitive = fc.getInitParameter("locale-sensitive");
85         if (localeSensitive != null) {
86             StringWriter JavaDoc ldata = new StringWriter JavaDoc();
87             Enumeration JavaDoc locales = request.getLocales();
88             while (locales.hasMoreElements()) {
89                 Locale JavaDoc locale = (Locale JavaDoc) locales.nextElement();
90                 ldata.write(locale.getISO3Language());
91             }
92             id = id + ldata.toString();
93         }
94         File JavaDoc tempDir = (File JavaDoc) sc.getAttribute("javax.servlet.context.tempdir");
95
96         // get possible cache
97
String JavaDoc temp = tempDir.getAbsolutePath();
98         System.out.println(temp);
99         File JavaDoc file = new File JavaDoc(temp + id);
100
101         // get current resource
102
if (path == null) {
103             path = sc.getRealPath(request.getRequestURI());
104         }
105         File JavaDoc current = new File JavaDoc(path);
106
107         try {
108             long now = Calendar.getInstance().getTimeInMillis();
109             // set timestamp check
110
if (!file.exists()
111                     || (file.exists() && current.lastModified() > file
112                             .lastModified())
113                     || cacheTimeout < now - file.lastModified()) {
114                 String JavaDoc name = file.getAbsolutePath();
115
116                 name = name.substring(0,
117                         name.lastIndexOf(File.separatorChar) == -1 ? 0 : name
118                                 .lastIndexOf(File.separatorChar));
119                 new File JavaDoc(name).mkdirs();
120
121                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
122                 CacheResponseWrapper wrappedResponse = new CacheResponseWrapper(
123                         response, fos);
124                 chain.doFilter(req, wrappedResponse);
125
126                 fos.flush();
127                 fos.close();
128             }
129         } catch (ServletException JavaDoc e) {
130             if (!file.exists()) {
131                 throw new ServletException JavaDoc(e);
132             }
133         } catch (IOException JavaDoc e) {
134             if (!file.exists()) {
135                 throw e;
136             }
137         }
138
139         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
140         String JavaDoc mt = sc.getMimeType(request.getRequestURI());
141         response.setContentType(mt);
142         ServletOutputStream JavaDoc sos = res.getOutputStream();
143         for (int i = fis.read(); i != -1; i = fis.read()) {
144             sos.write((byte) i);
145         }
146     }
147
148     public void init(FilterConfig JavaDoc filterConfig) {
149         this.fc = filterConfig;
150         // set the inital timeout
151
String JavaDoc ct = fc.getInitParameter("cacheTimeout");
152         if (ct != null) {
153             cacheTimeout = 60 * 1000 * Long.parseLong(ct);
154         }
155         // set reference to servlet context
156
this.sc = filterConfig.getServletContext();
157     }
158
159     public void destroy() {
160         this.sc = null;
161         this.fc = null;
162     }
163 }
Popular Tags