Package de.odysseus.calyxo.control.misc

Source Code of de.odysseus.calyxo.control.misc.NoCacheFilter

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.control.misc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.odysseus.calyxo.base.ModuleContext;

import de.odysseus.calyxo.control.Command;
import de.odysseus.calyxo.control.Filter;
import de.odysseus.calyxo.control.Plugin;
import de.odysseus.calyxo.control.PluginContext;
import de.odysseus.calyxo.control.conf.DispatchConfig;
import de.odysseus.calyxo.control.conf.FilterConfig;
import de.odysseus.calyxo.control.conf.ParamConfig;
import de.odysseus.calyxo.control.conf.PluginConfig;

/**
* Add headers to response to prevent browsers from caching it.
* <p/>
* This class also implements the plugin interface. The default
* filter name is <code>no-cache</code> and may be overridden by plugin
* parameter <code>name</code>.
*
* @author Christoph Beck
*/
public class NoCacheFilter implements Plugin, Filter {

  /**
   * Register the receiver's class as filter class.
   * The filter name defaults to <code>no-cache</code> and may be overridden
   * in parameter <code>name</code>.
   * @see de.odysseus.calyxo.control.Plugin#init(de.odysseus.calyxo.control.conf.PluginConfig, de.odysseus.calyxo.control.PluginContext)
   */
  public void init(PluginConfig config, PluginContext context) throws Exception {
    ParamConfig param = config.getParamConfig("name");
    String name = param == null ? "no-cache" : param.getValue();
    context.setFilterClass(name, getClass());
  }

  /**
   * Do nothing.
   * @see de.odysseus.calyxo.control.Plugin#destroy()
   */
  public void destroy() {
  }

  /**
   * Do nothing.
   * @see de.odysseus.calyxo.control.Filter#init(de.odysseus.calyxo.control.conf.FilterConfig, de.odysseus.calyxo.base.ModuleContext)
   */
  public void init(FilterConfig config, ModuleContext module) {
  }

  /**
   * If the response has not been committed,
   * set some headers in the response to prevent browsers from caching it.
   * Then, execute command.
   */
  public DispatchConfig filter(
    HttpServletRequest request,
    HttpServletResponse response,
    Command command) throws Exception {

    if (!response.isCommitted()) {
      response.setHeader("Pragma", "No-cache");
      response.setHeader("Cache-Control", "no-cache");
      response.setDateHeader("Expires", 1);
    }
    return command.execute(request, response);
  }
}
TOP

Related Classes of de.odysseus.calyxo.control.misc.NoCacheFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.