package com.eactive.eai.adapter.http.dynamic.filter;

import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.property.PropManager;

import java.util.Arrays;
import java.util.stream.Stream;

public class ReflectAllHeaderFilter implements HttpAdapterFilter  {
	
	public ReflectAllHeaderFilter() {
		super();
	}
	
	protected static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
	
	public static final String PROPERTIES_GROUP_NAME = "HttpHeaderFilter";
	public static final String HEADER_KEY_NAMES = "AllHeaderFilter.blackList";

	String[] HttpHeaderRelayBlackList = {
			"Content-Length",
			"Transfer-Encoding",
			"Host",
			"Authorization",
			"Accept",
			"Host",
			"Cookie",
			"Connection",
			"Keep-Alive",
			"Proxy-Authenticate",
			"Proxy-Authorization",
			"TE",
			"Trailer",
			"Transfer-Encoding",
			"Upgrade",
			"Content-Type",
			"Content-Encoding",
			"Content-Language",
			"Content-Location",
			"Content-MD5",
			"Expect",
	};
	

	@Override
	public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.debug("doPreFilter ReflectAllHeaderFilter Start.");
		
		String propValue = PropManager.getInstance().getProperty(PROPERTIES_GROUP_NAME, HEADER_KEY_NAMES, "").trim();
		String[] userSettingBlackList = propValue.split(",");
		
		if( userSettingBlackList.length > 0 ) {
			HttpHeaderRelayBlackList = Stream
			        .concat(Arrays.stream(HttpHeaderRelayBlackList), Arrays.stream(userSettingBlackList))
			        .toArray(String[]::new);
		}
		
		try {
			Enumeration<String> headerNames = request.getHeaderNames();
			while (headerNames.hasMoreElements()) {
								
			    String headerName = headerNames.nextElement();
			    String headerValue = request.getHeader(headerName);
			    
				if( StringUtils.equalsAnyIgnoreCase( headerName, HttpHeaderRelayBlackList ) ) {
					logger.debug("Skip Processing Key ["+headerName+"], value ["+headerValue+"] in HttpHeaderRelayBlackList");
					continue;
				}				
				
			    response.setHeader(headerName, headerValue);
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		
		logger.debug("doPreFilter ReflectAllHeaderFilter End.");
		
		return message;
	}

	@Override
	public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		logger.debug("doPostFilter ReflectAllHeaderFilter Start.");
		
		String propValue = PropManager.getInstance().getProperty(PROPERTIES_GROUP_NAME, HEADER_KEY_NAMES, "").trim();
		String[] userSettingBlackList = propValue.split(",");
		
		if( userSettingBlackList.length > 0 ) {
			HttpHeaderRelayBlackList = Stream
			        .concat(Arrays.stream(HttpHeaderRelayBlackList), Arrays.stream(userSettingBlackList))
			        .toArray(String[]::new);
		}
		
		try {
			Enumeration<String> headerNames = request.getHeaderNames();
			while (headerNames.hasMoreElements()) {
								
			    String headerName = headerNames.nextElement();
			    String headerValue = request.getHeader(headerName);
			    
				if( StringUtils.equalsAnyIgnoreCase( headerName, HttpHeaderRelayBlackList ) ) {
					logger.debug("Skip Processing Key ["+headerName+"], value ["+headerValue+"] in HttpHeaderRelayBlackList");
					continue;
				}				
				
			    response.setHeader(headerName, headerValue);
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		
		logger.debug("doPostFilter ReflectAllHeaderFilter End.");

		return resultMessage;
	}
}

