KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filters > RequestCacheFilter


1 package jodd.servlet.filters;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.Iterator;
8
9 import javax.servlet.Filter;
10 import javax.servlet.FilterChain;
11 import javax.servlet.FilterConfig;
12 import javax.servlet.ServletContext;
13 import javax.servlet.ServletException;
14 import javax.servlet.ServletOutputStream;
15 import javax.servlet.ServletRequest;
16 import javax.servlet.ServletResponse;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import jodd.file.FileUtil;
21 import jodd.servlet.HtmlEncoder;
22 import jodd.util.Util;
23
24 /**
25  * Caches http requests. Filter will put itself in ServletContext, so it can
26  * be reached from anywhere. Cached requests are stored in a folder and
27  * next time when a request comes, content will be read from the cached file,
28  * instead of creating it again.
29  *
30  * @2do add user-defined callback that determines if some request is cached or not.
31  */

32 public class RequestCacheFilter implements Filter {
33
34     private HashMap cacheMap;
35
36     /**
37      * Registers a request that will be cached.
38      *
39      * @param request request to specify
40      */

41     public void register(String request) {
42         cacheMap.put(request, Boolean.FALSE);
43     }
44
45     /**
46      * Invalidates a request cache. Next time when request is accessed
47      * cache will be created.
48      *
49      * @param request request to invalidate
50      */

51     public void invalidate(String request) {
52         cacheMap.put(request, Boolean.FALSE);
53     }
54
55     /**
56      * Invalidates all request cache.
57      */

58     public void invalidateAll() {
59         Iterator iterator = cacheMap.keySet().iterator();
60         while (iterator.hasNext()) {
61             String key = (String) iterator.next();
62             cacheMap.put(key, Boolean.FALSE);
63         }
64     }
65
66     /**
67      * Determine if request is cached.
68      *
69      * @param request request to check
70      * @return <code>null</code> if request is not registered,
71      * otherwies, cache status
72      */

73     public Boolean isCached(String request) {
74         return (Boolean) cacheMap.get(request);
75     }
76
77
78     private ServletContext sc;
79     private FilterConfig fc;
80     private String cacheFolder;
81
82     /**
83      * Filter initialization.
84      * @param filterConfig
85      * @throws ServletException
86      */

87     public void init(FilterConfig filterConfig) throws ServletException {
88         this.fc = filterConfig;
89         this.sc = filterConfig.getServletContext();
90         String realPath = sc.getRealPath("");
91         cacheFolder = realPath + fc.getInitParameter("folder");
92         cacheMap = new HashMap();
93         sc.setAttribute(fc.getInitParameter("ctxname"), this);
94     }
95
96     /**
97      * Performs filter.
98      * @param servletRequest
99      * @param servletResponse
100      * @param chain
101      * @throws IOException
102      * @throws ServletException
103      */

104     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
105         HttpServletRequest request = (HttpServletRequest) servletRequest;
106         HttpServletResponse response = (HttpServletResponse) servletResponse;
107
108         String uri = request.getRequestURI();
109         String query = request.getQueryString();
110         if (query == null) {
111             query = "";
112         } else {
113             query = "?" + query;
114         }
115         String ctxPath = request.getContextPath();
116         int i = uri.indexOf(ctxPath);
117         String req = uri.substring(i + ctxPath.length()) + query;
118         Boolean cached = isCached(req);
119
120         if (cached == null) {
121             chain.doFilter(request, response); // request is not registered, execute request
122
} else { // request is registered...
123
String fileName = cacheFolder + "/" + HtmlEncoder.encodeUrl(req) + ".cache";
124             File file = new File(fileName);
125             if (file.exists() == false) {
126                 invalidate(req);
127                 cached = Boolean.FALSE;
128             }
129             String result = null;
130             if (cached.equals(Boolean.FALSE)) {
131                 CharArrayResponseWrapper wrappedResponse = new CharArrayResponseWrapper(response);
132                 chain.doFilter(request, wrappedResponse);
133                 result = wrappedResponse.toString();
134                 FileUtil.writeString(fileName, result);
135                 cacheMap.put(req, Boolean.TRUE);
136             } else {
137                 result = FileUtil.readString(fileName);
138             }
139             response.setContentType(sc.getMimeType(uri));
140
141             FileInputStream fis = new FileInputStream(file);
142             ServletOutputStream sos = response.getOutputStream();
143             Util.copyPipe(fis, sos, 8192);
144         }
145     }
146
147     public void destroy() {
148         //To change body of implemented methods use File | Settings | File Templates.
149
}
150 }
151
Popular Tags