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

import java.util.concurrent.ConcurrentHashMap;

import com.eactive.eai.common.util.Logger;

public class HttpAdapterFilterFactory {
	static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
	static private ConcurrentHashMap<String, HttpAdapterFilter> h = new ConcurrentHashMap<String, HttpAdapterFilter>();

	public HttpAdapterFilterFactory() {
		super();
	}

	@SuppressWarnings("rawtypes")
	public static HttpAdapterFilter createFilter(String type) {
		if (h.containsKey(type)) {
			return (HttpAdapterFilter) h.get(type);
		}

		HttpAdapterFilter filter = null;
		switch (HttpAdapterFilterType.getValue(type)) {
		case APIAUTHFILTER:
			filter = new ApiAuthFilter();
			break;
		case JWTAUTHFILTER:
			filter = new JwtAuthFilter();
			break;
		case IPWHITELISTFILTER:
			filter = new IpWhiteListFilter();
			break;
		case ADAPTERALLOWIPLISTFILTER:
			filter = new AdapterAllowIPListFilter();
			break;
		default:
			try {
				Class cl = Class.forName(type);
				filter = (HttpAdapterFilter) cl.newInstance();
			} catch (Exception e) {
				logger.error("Cannot create a HttpAdapterFilter. - {}", type);
			}
			break;
		}

		if (filter != null) {
			h.put(type, filter);
			return filter;
		} else {
			return null;
		}
	}
}
