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 32 public class RequestCacheFilter implements Filter { 33 34 private HashMap cacheMap; 35 36 41 public void register(String request) { 42 cacheMap.put(request, Boolean.FALSE); 43 } 44 45 51 public void invalidate(String request) { 52 cacheMap.put(request, Boolean.FALSE); 53 } 54 55 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 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 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 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); } else { 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 } 150 } 151 | Popular Tags |