/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.utils.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * @author CBO
 * Filtre permettant d'ignorer les headers de type "no cache"
 */
public class IgnoreNoCacheHeadersFilter implements Filter {

  public void init(FilterConfig filterConfig) throws ServletException {
  }

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//    if ((response instanceof HttpServletResponse) && ! (response instanceof IgnoreNoCacheHeaderResponseWrapper)
//            && (
//                "true".equalsIgnoreCase(request.getParameter("ignore_no_cache"))
//                || (request.getAttribute("ignore_no_cache")!=null && "true".equalsIgnoreCase(request.getAttribute("ignore_no_cache").toString())))
//            ) {
//    }
    response = new IgnoreNoCacheHeaderResponseWrapper((HttpServletResponse)response);
    chain.doFilter(request, response);
  }

  public void destroy() {
  }

  static public class IgnoreNoCacheHeaderResponseWrapper extends HttpServletResponseWrapper {

    public IgnoreNoCacheHeaderResponseWrapper(HttpServletResponse response) {
      super(response);
    }

    @Override
    public void addHeader(String name, String value) {
      if (!"Cache-Control".equalsIgnoreCase(name) && !"Expires".equalsIgnoreCase(name) && !("Pragma".equalsIgnoreCase(name) && "No-cache".equalsIgnoreCase(value))) {
        super.addHeader(name, value);
      }
    }

    @Override
    public void setHeader(String name, String value) {
      if (!"Cache-Control".equalsIgnoreCase(name) && !"Expires".equalsIgnoreCase(name) && !("Pragma".equalsIgnoreCase(name) && "No-cache".equalsIgnoreCase(value))) {
        super.setHeader(name, value);
      }
    }

    @Override
    public void addDateHeader(String name, long value) {
      if (!"Cache-Control".equalsIgnoreCase(name) && !"Expires".equalsIgnoreCase(name)) {
        super.addDateHeader(name, value);
      }
    }

    @Override
    public void addIntHeader(String name, int value) {
      if (!"Cache-Control".equalsIgnoreCase(name) && !"Expires".equalsIgnoreCase(name)) {
        super.addIntHeader(name, value);
      }
    }

    @Override
    public void setDateHeader(String name, long value) {
      if (!"Cache-Control".equalsIgnoreCase(name) && !"Expires".equalsIgnoreCase(name)) {
        super.setDateHeader(name, value);
      }
    }
  }
}
