001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.filters;
030:
031: import com.caucho.config.types.Period;
032: import com.caucho.util.Alarm;
033:
034: import javax.servlet.Filter;
035: import javax.servlet.FilterChain;
036: import javax.servlet.FilterConfig;
037: import javax.servlet.ServletException;
038: import javax.servlet.ServletRequest;
039: import javax.servlet.ServletResponse;
040: import javax.servlet.http.HttpServletResponse;
041: import java.io.IOException;
042:
043: /**
044: * Caches the servlet output. ExpiresFilter sets the Expires header
045: * to a time in the filter. The page will be cached until that time
046: * expires.
047: *
048: * <p>The cache-time init-parameter configures how long the page should be
049: * cached:
050: *
051: * <pre>
052: * <filter>
053: * <filter-name>expires-cache</filter-name>
054: * <filter-class>com.caucho.http.filter.ExpiresFilter</filter-class>
055: * <init-param cache-time='10s'/>
056: * </filter>
057: * </pre>
058: *
059: * The cache-time allows the standard extensions:
060: *
061: * <table>
062: * <tr><td>s<td>seconds
063: * <tr><td>m<td>minutes
064: * <tr><td>h<td>hours
065: * <tr><td>D<td>days
066: * <tr><td>W<td>weeks
067: * <tr><td>M<td>months
068: * <tr><td>Y<td>years
069: * </table>
070: *
071: * @since Resin 2.0.5
072: */
073: public class ExpiresFilter implements Filter {
074: /**
075: * How long to cache the file for.
076: */
077: private long _cacheTime = 2000L;
078:
079: public void setCacheTime(Period period) {
080: _cacheTime = period.getPeriod();
081: }
082:
083: /**
084: * Filter init reads the filter configuration
085: */
086: public void init(FilterConfig config) throws ServletException {
087: String time = config.getInitParameter("cache-time");
088:
089: if (time != null) {
090: try {
091: _cacheTime = Period.toPeriod(time);
092: } catch (Exception e) {
093: throw new ServletException(e);
094: }
095: }
096: }
097:
098: /**
099: * The filter add an Expires time in the future.
100: */
101: public void doFilter(ServletRequest request,
102: ServletResponse response, FilterChain nextFilter)
103: throws ServletException, IOException {
104: if (_cacheTime > 0) {
105: HttpServletResponse res = (HttpServletResponse) response;
106:
107: res.addHeader("Cache-Control", "max-age="
108: + (_cacheTime / 1000));
109:
110: res.setDateHeader("Expires", Alarm.getCurrentTime()
111: + _cacheTime);
112: }
113:
114: nextFilter.doFilter(request, response);
115: }
116:
117: /**
118: * Any cleanup for the filter.
119: */
120: public void destroy() {
121: }
122: }
|