View Javadoc

    1   /*
    2    * Copyright 2010 University Corporation for Advanced Internet Development, Inc.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    * http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package edu.internet2.middleware.shibboleth.idp.util;
    18
    19  import java.io.IOException;
    20
    21  import javax.servlet.Filter;
    22  import javax.servlet.FilterChain;
    23  import javax.servlet.FilterConfig;
    24  import javax.servlet.ServletException;
    25  import javax.servlet.ServletRequest;
    26  import javax.servlet.ServletResponse;
    27  import javax.servlet.http.HttpServletResponse;
    28
    29  /**
    30   * An HTTP filter that adds the following headers/values to the {@link HttpServletResponse} and thus, hopefully,
    31   * prevents caching of the response on all browser.
    32   * <ul>
    33   * <li>Expires: 0</li>
    34   * <li>Cache-Control: no-cache, no-store, must-revalidate, max-age=0
    35   * <li>
    36   * <li>Pragma: no-cache</li>
    37   * </ul>
    38   */
    39  public class NoCacheFilter implements Filter {
    40
    41      /** {@inheritDoc} */
    42      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
    43              ServletException {
    44          HttpServletResponse httpResponse = (HttpServletResponse) response;
    45
    46          httpResponse.setHeader("Expires", "0");
    47          httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
    48          httpResponse.setHeader("Pragma", "no-cache");
    49          chain.doFilter(request, response);
    50      }
    51
    52      /** {@inheritDoc} */
    53      public void init(FilterConfig filterConfig) throws ServletException {
    54          // nothing to do here
    55      }
    56
    57      /** {@inheritDoc} */
    58      public void destroy() {
    59          // nothing to do here
    60      }
    61  }