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

import it.mice.voila.runtime.security.AbstractFilterSecurityInterceptorPostProcessor;
import it.mice.voila.runtime.springmvc.controller.DefaultController;

import java.lang.reflect.Method;
import java.util.Collection;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.method.AbstractMethodSecurityMetadataSource;
import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.web.bind.annotation.RequestMapping;

public class SkyGenMethodSecurityMetadataSource extends
		AbstractMethodSecurityMetadataSource {

    private AbstractFilterSecurityInterceptorPostProcessor abstractFilterSecurityInterceptorPostProcessor;

    public Collection<ConfigAttribute> getAttributes(Method method,
			Class<?> targetClass) {

    	if (targetClass.isAssignableFrom(DefaultController.class)) {
			RequestMapping mapping = AnnotationUtils.findAnnotation(targetClass, RequestMapping.class);
			String[] values = mapping.value();
			if (values.length > 0) {
				String value = values[0];
				value = value.substring(0, value.lastIndexOf("/") + 1);
			}
		}
		return null;
	}

	public Collection<ConfigAttribute> getAllConfigAttributes() {
		// TODO Auto-generated method stub
		DefaultFilterInvocationSecurityMetadataSource metadataSource = (DefaultFilterInvocationSecurityMetadataSource) getFilterSecurityInterceptor()
				.obtainSecurityMetadataSource();
		Collection<ConfigAttribute> attr = metadataSource.getAllConfigAttributes();
		return attr;
	}

	public FilterSecurityInterceptor getFilterSecurityInterceptor() {
		return getFilterSecurityInterceptorPostProcessor().getFilterSecurityInterceptor();
	}

	public AbstractFilterSecurityInterceptorPostProcessor getFilterSecurityInterceptorPostProcessor() {
		return abstractFilterSecurityInterceptorPostProcessor;
	}

	public void setFilterSecurityInterceptorPostProcessor(
			AbstractFilterSecurityInterceptorPostProcessor abstractFilterSecurityInterceptorPostProcessor) {
		this.abstractFilterSecurityInterceptorPostProcessor = abstractFilterSecurityInterceptorPostProcessor;
	}

}
