KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 import javax.servlet.Filter JavaDoc;
34 import javax.servlet.FilterChain JavaDoc;
35 import javax.servlet.FilterConfig JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.ServletRequest JavaDoc;
38 import javax.servlet.ServletResponse JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.io.IOException JavaDoc;
41
42 /**
43  * Caches the servlet output for anonymous users. This filter adds
44  * an Expires header and the Cache-Control: x-anonymous header.
45  *
46  * <p>Requests from anonymous users, i.e. users with no JSESSIONID cookie
47  * or ;jsessionid= URL-rewriting, will be cached. So logged-in users
48  * will have customized pages, but anonymous users will see a cached page.
49  * Since there are generally more anonymous users, using the
50  * AnonymousExpiresFilter can dramatically improve performance without
51  * losing the ability to customize pages.
52  *
53  * <p>Pages should call <code>request.getSession(false)</code> to get their
54  * sessions, because a page that creates a session will not be cached.
55  * For the same reason, JSP pages would set
56  * <code>&lt;jsp:directive.page session='false'/></code>.
57  *
58  * <p>The cache-time init-parameter configures how long the page should be
59  * cached:
60  *
61  * <pre>
62  * &lt;filter>
63  * &lt;filter-name>anonymous-cache&lt;/filter-name>
64  * &lt;filter-class>com.caucho.http.filter.AnonymousExpiresFilter&lt;/filter-class>
65  * &lt;init-param cache-time='10s'/>
66  * &lt;/filter>
67  * </pre>
68  *
69  * The cache-time allows the standard extensions:
70  *
71  * <table>
72  * <tr><td>s<td>seconds
73  * <tr><td>m<td>minutes
74  * <tr><td>h<td>hours
75  * <tr><td>D<td>days
76  * <tr><td>W<td>weeks
77  * <tr><td>M<td>months
78  * <tr><td>Y<td>years
79  * </table>
80  *
81  * @since Resin 2.0.5
82  */

83 public class AnonymousExpiresFilter implements Filter JavaDoc {
84   /**
85    * How long to cache the file for.
86    */

87   private long _cacheTime = 2000;
88
89   /**
90    * Sets the file cache time.
91    */

92   public void setCacheTime(Period period)
93   {
94     _cacheTime = period.getPeriod();
95   }
96   
97   /**
98    * Filter init reads the filter configuration
99    */

100   public void init(FilterConfig JavaDoc config)
101     throws ServletException JavaDoc
102   {
103     String JavaDoc time = config.getInitParameter("cache-time");
104
105     if (time != null) {
106       try {
107         _cacheTime = Period.toPeriod(time);
108       } catch (Exception JavaDoc e) {
109         throw new ServletException JavaDoc(e);
110       }
111     }
112   }
113   
114   /**
115    * The filter add an Expires time in the future and adds
116    * the x-anonymous Cache-Control directive.
117    */

118   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
119                        FilterChain JavaDoc nextFilter)
120     throws ServletException JavaDoc, IOException JavaDoc
121   {
122     if (_cacheTime > 0) {
123       HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
124       
125       res.addHeader("Vary", "Cookie");
126       res.addHeader("Cache-Control", "s-maxage=" + _cacheTime);
127     }
128
129     nextFilter.doFilter(request, response);
130   }
131   
132   /**
133    * Any cleanup for the filter.
134    */

135   public void destroy()
136   {
137   }
138 }
139
Popular Tags