package it.mice.voila.runtime.springmvc.security;


import it.mice.voila.runtime.security.ReloadableMethodSecurityMetadataSource;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

public class FilterChainProxyPostProcessor implements BeanPostProcessor {
	protected static Log logger = LogFactory
			.getLog(ReloadableMethodSecurityMetadataSource.class);

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof FilterChainProxy) {
			if (logger.isDebugEnabled()) {
				logger.debug("Post-processing bean: " + beanName);
			}
			FilterChainProxy filterChainProxy = (FilterChainProxy)bean;
			//UrlMatcher urlMatcher = filterChainProxy.getMatcher();
//			Map<RequestMatcher, List<Filter>> filterChainMap = (Map<RequestMatcher, List<Filter>>)filterChainProxy.getFilterChains();
//			Map<RequestMatcher, List<Filter>> filterChainMapNew = new LinkedHashMap<RequestMatcher, List<Filter>>();
			List<SecurityFilterChain> filterChainList = filterChainProxy.getFilterChains();
			List<SecurityFilterChain> filterChainListNew = new ArrayList<SecurityFilterChain>();
			ArrayList<Filter> emptyFilter = new ArrayList<Filter>();
//			filterChainMapNew.put(new AntPathRequestMatcher("/resources/**"), emptyFilter);
//			filterChainMapNew.put(new AntPathRequestMatcher("/**/*.gif"), emptyFilter);
//			filterChainMapNew.put(new AntPathRequestMatcher("/**/*.png"), emptyFilter);
//			filterChainMapNew.put(new AntPathRequestMatcher("/**/*.css"), emptyFilter);
//			filterChainMapNew.put(new AntPathRequestMatcher("/**/*.js"), emptyFilter);
//			filterChainMapNew.put(new AntPathRequestMatcher("/**/*.jpg"), emptyFilter);
//			filterChainMapNew.putAll(filterChainMap);
//			filterChainProxy.setFilterChainMap(filterChainMapNew);
			
//			List<SecurityFilterChain> filterChains = new ArrayList<SecurityFilterChain>(filterChainMapNew.size());
//
//	        for (Map.Entry<RequestMatcher,List<Filter>> entry : filterChainMapNew.entrySet()) {
//	            filterChains.add(new DefaultSecurityFilterChain(entry.getKey(), entry.getValue()));
//	        }
//	        FilterChainProxy newFilterChainProxy = new FilterChainProxy(filterChains);
			filterChainListNew.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/resources/**"), emptyFilter));
			filterChainListNew.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**/*.gif"), emptyFilter));
			filterChainListNew.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**/*.png"), emptyFilter));
			filterChainListNew.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**/*.css"), emptyFilter));
			filterChainListNew.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**/*.js"), emptyFilter));
			filterChainListNew.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**/*.jpg"), emptyFilter));
			filterChainListNew.addAll(filterChainList);
	        FilterChainProxy newFilterChainProxy = new FilterChainProxy(filterChainListNew);

		}
		return bean;
	}
}
