KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > filters > ExpiresFilter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
35 import javax.servlet.FilterChain JavaDoc;
36 import javax.servlet.FilterConfig JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.ServletRequest JavaDoc;
39 import javax.servlet.ServletResponse JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.io.IOException JavaDoc;
42
43 /**
44  * Caches the servlet output. ExpiresFilter sets the Expires header
45  * to a time in the filter. The page will be cached until that time
46  * expires.
47  *
48  * <p>The cache-time init-parameter configures how long the page should be
49  * cached:
50  *
51  * <pre>
52  * &lt;filter>
53  * &lt;filter-name>expires-cache&lt;/filter-name>
54  * &lt;filter-class>com.caucho.http.filter.ExpiresFilter&lt;/filter-class>
55  * &lt;init-param cache-time='10s'/>
56  * &lt;/filter>
57  * </pre>
58  *
59  * The cache-time allows the standard extensions:
60  *
61  * <table>
62  * <tr><td>s<td>seconds
63  * <tr><td>m<td>minutes
64  * <tr><td>h<td>hours
65  * <tr><td>D<td>days
66  * <tr><td>W<td>weeks
67  * <tr><td>M<td>months
68  * <tr><td>Y<td>years
69  * </table>
70  *
71  * @since Resin 2.0.5
72  */

73 public class ExpiresFilter implements Filter JavaDoc {
74   /**
75    * How long to cache the file for.
76    */

77   private long _cacheTime = 2000L;
78
79   public void setCacheTime(Period period)
80   {
81     _cacheTime = period.getPeriod();
82   }
83   
84   /**
85    * Filter init reads the filter configuration
86    */

87   public void init(FilterConfig JavaDoc config)
88     throws ServletException JavaDoc
89   {
90     String JavaDoc time = config.getInitParameter("cache-time");
91
92     if (time != null) {
93       try {
94         _cacheTime = Period.toPeriod(time);
95       } catch (Exception JavaDoc e) {
96         throw new ServletException JavaDoc(e);
97       }
98     }
99   }
100   
101   /**
102    * The filter add an Expires time in the future.
103    */

104   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
105                        FilterChain JavaDoc nextFilter)
106     throws ServletException JavaDoc, IOException JavaDoc
107   {
108     if (_cacheTime > 0) {
109       HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) 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   /**
120    * Any cleanup for the filter.
121    */

122   public void destroy()
123   {
124   }
125 }
126
Popular Tags