package uk.gov.hscic.common.filters;

import java.util.Enumeration;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpException;
import org.apache.log4j.Logger;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class DefaultHeaderFilter implements Filter {
    private static final Logger LOG = Logger.getLogger(DefaultHeaderFilter.class);

    @Override
    public void init(FilterConfig config) throws ServletException { }

    @Override
    public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain chain) throws java.io.IOException, ServletException {
        try {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
         
          
            String headerOutput = "Headers ( ";
         
            Enumeration<String> headerNames = httpRequest.getHeaderNames();
            while(headerNames.hasMoreElements()){
                String headerName = headerNames.nextElement();
                headerOutput += "'" + headerName + "' : '" + httpRequest.getHeader(headerName) + "'";
            }
        
            LOG.info(headerOutput + " )");
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            if(response.getHeader("Cache-Control") == null){response.addHeader("Cache-Control", "no-cache, no-store");};
            if(response.getHeader("Expires") == null){response.addHeader("Expires", "0");};
            if(response.getHeader("Pragma") == null){response.addHeader("Pragma", "no-cache");};
            chain.doFilter(new HeaderRequestWrapper((HttpServletRequest) request), response);
          
        } catch (HttpException e) {
            LOG.error("Error passed back to Default Header Filter");
        }
    }

    @Override
    public void destroy() { }
    
 


}

@@ -1552,35 +1552,32 @@
 se;%0A            
-if(
 response.getHead
@@ -1573,56 +1573,11 @@
 nse.
-getHeader(%22Cache-Control%22) == null)%7Bresponse.add
+set
 Head
@@ -1614,26 +1614,24 @@
  no-store%22);
-%7D;
 %0A           
@@ -1623,35 +1623,32 @@
 %22);%0A            
-if(
 response.getHead
@@ -1632,33 +1632,33 @@
        response.
-g
+s
 etHeader(%22Expire
@@ -1663,56 +1663,15 @@
 res%22
-) == null)%7Bresponse.addHeader(%22Expires%22
 , %220%22);
-%7D;
 %0A   
@@ -1683,11 +1683,8 @@
     
-if(
 resp
@@ -1688,17 +1688,17 @@
 esponse.
-g
+s
 etHeader
@@ -1710,46 +1710,8 @@
 gma%22
-) == null)%7Bresponse.addHeader(%22Pragma%22
 , %22n
@@ -1724,10 +1724,8 @@
 e%22);
-%7D;
 %0A   

Fix cache headers