1 28 29 package com.caucho.filters; 30 31 import com.caucho.config.types.Period; 32 import com.caucho.util.Alarm; 33 34 import javax.servlet.Filter ; 35 import javax.servlet.FilterChain ; 36 import javax.servlet.FilterConfig ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.ServletRequest ; 39 import javax.servlet.ServletResponse ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.io.IOException ; 42 43 73 public class ExpiresFilter implements Filter { 74 77 private long _cacheTime = 2000L; 78 79 public void setCacheTime(Period period) 80 { 81 _cacheTime = period.getPeriod(); 82 } 83 84 87 public void init(FilterConfig config) 88 throws ServletException 89 { 90 String time = config.getInitParameter("cache-time"); 91 92 if (time != null) { 93 try { 94 _cacheTime = Period.toPeriod(time); 95 } catch (Exception e) { 96 throw new ServletException (e); 97 } 98 } 99 } 100 101 104 public void doFilter(ServletRequest request, ServletResponse response, 105 FilterChain nextFilter) 106 throws ServletException , IOException 107 { 108 if (_cacheTime > 0) { 109 HttpServletResponse res = (HttpServletResponse ) response; 110 111 res.addHeader("Cache-Control", "max-age=" + (_cacheTime / 1000)); 112 113 res.setDateHeader("Expires", Alarm.getCurrentTime() + _cacheTime); 114 } 115 116 nextFilter.doFilter(request, response); 117 } 118 119 122 public void destroy() 123 { 124 } 125 } 126 | Popular Tags |