package gov.usgs.cida.wqp.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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 org.springframework.stereotype.Component;

@Component
public class CORSFilter implements Filter {
	public static final String BIODATA = "BIODATA";
	public static final String NWIS = "NWIS";
	public static final String STEWARDS = "STEWARDS";
	public static final String STORET = "STORET";
	
	public static final List<String> ACCESS_CONTROL_HEADERS = new ArrayList<>(
			Arrays.asList(HttpConstants.HEADER_TOTAL_SITE_COUNT,
					BIODATA + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					NWIS + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					STEWARDS + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					STORET + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					HttpConstants.HEADER_TOTAL_RESULT_COUNT,
					BIODATA + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					NWIS + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					STEWARDS + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					STORET + HttpConstants.HEADER_DELIMITER + HttpConstants.HEADER_COUNT,
					HttpConstants.HEADER_WARNING,
					HttpConstants.HEADER_FATAL_ERROR));

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// nothing needed here
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletResponse resp = (HttpServletResponse) response;
		resp.setHeader(HttpConstants.HEADER_CORS, HttpConstants.HEADER_CORS_VALUE);
		resp.setHeader(HttpConstants.HEADER_CORS_METHODS, HttpConstants.HEADER_CORS_METHODS_VALUE);
		resp.setHeader(HttpConstants.HEADER_CORS_MAX_AGE, HttpConstants.HEADER_CORS_MAX_AGE_VALUE);
		resp.setHeader(HttpConstants.HEADER_CORS_ALLOW_HEADERS, HttpConstants.HEADER_CORS_ALLOW_HEADERS_VALUE);
		for (String header : ACCESS_CONTROL_HEADERS) {
			resp.addHeader(HttpConstants.HEADER_CORS_EXPOSE_HEADERS, header);
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		// nothing needed here
	}

}

@@ -881,32 +881,37 @@
 onstants.HEADER_
+SITE_
 COUNT,%0A%09%09%09%09%09NWIS
@@ -959,32 +959,37 @@
 onstants.HEADER_
+SITE_
 COUNT,%0A%09%09%09%09%09STEW
@@ -1041,32 +1041,37 @@
 onstants.HEADER_
+SITE_
 COUNT,%0A%09%09%09%09%09STOR
@@ -1121,32 +1121,37 @@
 onstants.HEADER_
+SITE_
 COUNT,%0A%09%09%09%09%09Http
@@ -1248,32 +1248,39 @@
 onstants.HEADER_
+RESULT_
 COUNT,%0A%09%09%09%09%09NWIS
@@ -1328,32 +1328,39 @@
 onstants.HEADER_
+RESULT_
 COUNT,%0A%09%09%09%09%09STEW
@@ -1412,32 +1412,39 @@
 onstants.HEADER_
+RESULT_
 COUNT,%0A%09%09%09%09%09STOR
@@ -1502,16 +1502,23 @@
 .HEADER_
+RESULT_
 COUNT,%0A%09

Fix CORS headers