View Javadoc

1   /*
2    * $Id: CacheFilter.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.filters.cache;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.FilterChain;
28  import javax.servlet.ServletException;
29  import javax.servlet.ServletRequest;
30  import javax.servlet.ServletResponse;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.talika.tarsis.command.Command;
34  import org.talika.tarsis.filters.CommandFilter;
35  
36  /**
37   * Cache filter checks if command is cacheable and then sets no cache HTTP response
38   * headers.
39   *
40   * @author  Jose M. Palomar
41   * @version $Revision: 269 $
42   *
43   * @todo Move to org.talika.tarsis.filters package.
44   */
45  
46  public final class CacheFilter extends CommandFilter {
47  
48      // Fields
49  
50      // Methods
51      /**
52       * Checks if command is cacheable and then sets no cache HTTP response headers.
53       *
54       * @param servletRequest ServletRequest the <code>ServletRequest</code> object
55       * that contains the client's request.
56       * @param servletResponse ServletResponse the <code>ServletResponse</code> object
57       * that contains the servlet's response.
58       * @param filterChain FilterChain invocation chain of filtered request.
59       * @throws IOException if an input or output exception occurs
60       * @throws ServletException if an exception has occurred that interferes with the
61       * filter's normal operation
62       * @see javax.servlet.Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
63       */
64      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
65      FilterChain filterChain)
66      throws IOException, ServletException {
67  
68          if (getLogger().isDebugEnabled()) {
69              getLogger().logDebug(getFilterConfig().getFilterName() + ": invoked");
70          }
71  
72          try {
73              Command cmd = findCommand(servletRequest);
74              if (cmd != null) {
75                  if (!cmd.isCacheable()) {
76                      HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
77                      httpServletResponse.setHeader("Pragma", "No-cache");
78                      httpServletResponse.setHeader("Cache-Control", "no-cache");
79                      httpServletResponse.setDateHeader("Expires", 1);
80                  }
81              }
82          }
83          catch (Throwable t) {
84              throw new ServletException(t);
85          }
86  
87          filterChain.doFilter(servletRequest, servletResponse);
88  
89      }
90  
91  }