package org.xdi.service.security;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import org.xdi.service.el.ExpressionEvaluator;

/**
 * @author Yuriy Movchan Date: 05/22/2017
 */
@Interceptor
@InterceptSecure({})
public class SecurityInterceptor implements Serializable {

	private static final String KEY = "org.jboss.weld.interceptor.bindings";
	
	@Inject
	private SecurityExtension securityExtension;
    
    @Inject
    private ExpressionEvaluator expressionEvaluator;

    @AroundInvoke
    public Object invoke(InvocationContext ctx) throws Exception {
    	InterceptSecure is = securityExtension.getInterceptSecure(ctx.getMethod());

        // SecurityChecking  restrictions
        Secure[] constraints = (is == null) ? new Secure[0] : is.value();

        // Getting the parameter values
        Map<String, Object> secureVars = computeParameterValues(ctx);

        for (Secure constraint : constraints) {
            Boolean expressionValue = expressionEvaluator.evaluateValueExpression(constraint.value(), Boolean.class, secureVars);

            if ((expressionValue == null) || !expressionValue) {
                throw new SecurityEvaluationException();
            }
        }

        return ctx.proceed();
    }

    private Map<String, Object> computeParameterValues(InvocationContext ctx) {
        Annotation[][] parametersAnnotations = ctx.getMethod().getParameterAnnotations();
        Map<String, Object> secureVariables = new HashMap<String, Object>();
        for (int i = 0; i < parametersAnnotations.length; i++) {
            Annotation[] parameterAnnotations = parametersAnnotations[i];
            for (Annotation parameterAnnotation : parameterAnnotations) {
                if (SecureVariable.class.isAssignableFrom(parameterAnnotation.annotationType())) {
                	SecureVariable secureVariable = (SecureVariable) parameterAnnotation;
                    Object paramValue = ctx.getParameters()[i];
                    secureVariables.put(secureVariable.value(), paramValue);
                }
            }
        }

        return secureVariables;
    }

}

@@ -304,24 +304,50 @@
 Context;%0D%0A%0D%0A
+import org.slf4j.Logger;%0D%0A
 import org.x
@@ -609,16 +609,50 @@
 dings%22;%0D
+%0A%0D%0A%09@Inject%0D%0A%09private Logger log;%0D
 %0A%09%0D%0A%09@In
@@ -1428,24 +1428,138 @@
 onValue) %7B%0D%0A
+            %09log.debug(%22Method: '%7B%7D' constrain '%7B%7D' evaluation is null or false!%22, ctx.getMethod(), constraint);%0D%0A
             

Log method name with contains if evaluation is false