<commit_before>package org.wisdom.router;

import com.google.common.base.Preconditions;
import org.wisdom.api.Controller;
import org.wisdom.api.annotations.Interception;
import org.wisdom.api.http.Context;
import org.wisdom.api.http.HttpMethod;
import org.wisdom.api.http.Result;
import org.wisdom.api.http.Results;
import org.wisdom.api.interceptor.InterceptionContext;
import org.wisdom.api.interceptor.Interceptor;
import org.wisdom.api.router.Route;
import org.wisdom.api.router.RouteUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validator;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Delegated route used for interception purpose.
 */
public class RouteDelegate extends Route {

    private final Route route;
    private final RequestRouter router;
    private final boolean mustValidate;
    private final Map<String, Object> interceptors;

    public RouteDelegate(RequestRouter router, Route route) {
        this.route = route;
        this.router = router;
        this.mustValidate = detectValidationRequirement(route.getControllerMethod());
        this.interceptors = extractInterceptors();
    }

    private Map<String, Object> extractInterceptors() {
        Map<String, Object> map = new LinkedHashMap<>();
        Annotation[] classAnnotations = route.getControllerClass().getAnnotations();
        for (Annotation annotation : classAnnotations) {
            if (annotation.annotationType().isAnnotationPresent(Interception.class)) {
                // Interceptor detected.
                map.put(annotation.annotationType().getName(), annotation);
            }
        }
        // Check the method
        Annotation[] methodAnnotations = route.getControllerMethod().getAnnotations();
        for (Annotation annotation : methodAnnotations) {
            if (annotation.annotationType().isAnnotationPresent(Interception.class)) {
                // Interceptor detected.
                map.put(annotation.annotationType().getName(), annotation);
            }
        }

        return map;
    }

    private static boolean detectValidationRequirement(Method method) {
        Annotation[][] annotations = method.getParameterAnnotations();
        for (Annotation[] array : annotations) {
            for (Annotation annotation : array) {
                if (isConstraint(annotation)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Determines whether the given annotation is a 'constraint' or not.
     * It just checks if the annotation has the {@link Constraint} annotation on it or if the annotation is the {@link
     * Valid} annotation.
     *
     * @param annotation the annotation to check
     * @return {@code true} if the given annotation is a constraint
     */
    private static boolean isConstraint(Annotation annotation) {
        return annotation.annotationType().isAnnotationPresent(Constraint.class)
                || annotation.annotationType().equals(Valid.class);
    }

    @Override
    public String getUrl() {
        return route.getUrl();
    }

    @Override
    public HttpMethod getHttpMethod() {
        return route.getHttpMethod();
    }

    @Override
    public Class<? extends Controller> getControllerClass() {
        return route.getControllerClass();
    }

    @Override
    public Method getControllerMethod() {
        return route.getControllerMethod();
    }

    @Override
    public boolean matches(HttpMethod method, String uri) {
        return route.matches(method, uri);
    }

    @Override
    public boolean matches(String httpMethod, String uri) {
        return route.matches(httpMethod, uri);
    }

    @Override
    public Map<String, String> getPathParametersEncoded(String uri) {
        return route.getPathParametersEncoded(uri);
    }

    @Override
    public Controller getControllerObject() {
        return route.getControllerObject();
    }

    @Override
    public List<RouteUtils.Argument> getArguments() {
        return route.getArguments();
    }

    @Override
    public Result invoke() throws Throwable {
        Context context = Context.context.get();
        Preconditions.checkNotNull(context);
        List<RouteUtils.Argument> arguments = route.getArguments();
        Object[] parameters = new Object[arguments.size()];
        for (int i = 0; i < arguments.size(); i++) {
            RouteUtils.Argument argument = arguments.get(i);
            switch (argument.getSource()) {
                case PARAMETER:
                    parameters[i] = RouteUtils.getParameter(argument, context);
                    break;
                case BODY:
                    parameters[i] = context.body(argument.getType());
                    break;
                case ATTRIBUTE:
                    parameters[i] = RouteUtils.getAttribute(argument, context);
                    break;
                default:
                	break;
            }
        }

        if (mustValidate) {
            Validator validator = router.getValidator();
            if (validator != null) {
                Set<ConstraintViolation<Controller>> violations =
                        validator.forExecutables().validateParameters(getControllerObject(), getControllerMethod(),
                                parameters);

                if (!violations.isEmpty()) {
                    return Results.badRequest(violations).json();
                }
            }
        }

        // Build chain if needed.
        if (!interceptors.isEmpty()) {
            LinkedHashMap<Interceptor<?>, Object> chain = new LinkedHashMap<>();
            for (Map.Entry<String, Object> entry : interceptors.entrySet()) {
                final Interceptor<?> interceptor = getInterceptorForAnnotation(entry.getKey());
                if (interceptor == null) {
                    return Results.badRequest("Missing interceptor handling " + entry.getKey());<commit_msg>[TD] major - minor<commit_after>package org.wisdom.router;

import com.google.common.base.Preconditions;
import org.wisdom.api.Controller;
import org.wisdom.api.annotations.Interception;
import org.wisdom.api.http.Context;
import org.wisdom.api.http.HttpMethod;
import org.wisdom.api.http.Result;
import org.wisdom.api.http.Results;
import org.wisdom.api.interceptor.InterceptionContext;
import org.wisdom.api.interceptor.Interceptor;
import org.wisdom.api.router.Route;
import org.wisdom.api.router.RouteUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validator;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Delegated route used for interception purpose.
 */
public class RouteDelegate extends Route {

    private final Route route;
    private final RequestRouter router;
    private final boolean mustValidate;
    private final Map<String, Object> interceptors;

    public RouteDelegate(RequestRouter router, Route route) {
        this.route = route;
        this.router = router;
        this.mustValidate = detectValidationRequirement(route.getControllerMethod());
        this.interceptors = extractInterceptors();
    }

    private Map<String, Object> extractInterceptors() {
        Map<String, Object> map = new LinkedHashMap<>();
        Annotation[] classAnnotations = route.getControllerClass().getAnnotations();
        for (Annotation annotation : classAnnotations) {
            if (annotation.annotationType().isAnnotationPresent(Interception.class)) {
                // Interceptor detected.
                map.put(annotation.annotationType().getName(), annotation);
            }
        }
        // Check the method
        Annotation[] methodAnnotations = route.getControllerMethod().getAnnotations();
        for (Annotation annotation : methodAnnotations) {
            if (annotation.annotationType().isAnnotationPresent(Interception.class)) {
                // Interceptor detected.
                map.put(annotation.annotationType().getName(), annotation);
            }
        }

        return map;
    }

    private static boolean detectValidationRequirement(Method method) {
        Annotation[][] annotations = method.getParameterAnnotations();
        for (Annotation[] array : annotations) {
            for (Annotation annotation : array) {
                if (isConstraint(annotation)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Determines whether the given annotation is a 'constraint' or not.
     * It just checks if the annotation has the {@link Constraint} annotation on it or if the annotation is the {@link
     * Valid} annotation.
     *
     * @param annotation the annotation to check
     * @return {@code true} if the given annotation is a constraint
     */
    private static boolean isConstraint(Annotation annotation) {
        return annotation.annotationType().isAnnotationPresent(Constraint.class)
                || annotation.annotationType().equals(Valid.class);
    }

    @Override
    public String getUrl() {
        return route.getUrl();
    }

    @Override
    public HttpMethod getHttpMethod() {
        return route.getHttpMethod();
    }

    @Override
    public Class<? extends Controller> getControllerClass() {
        return route.getControllerClass();
    }

    @Override
    public Method getControllerMethod() {
        return route.getControllerMethod();
    }

    @Override
    public boolean matches(HttpMethod method, String uri) {
        return route.matches(method, uri);
    }

    @Override
    public boolean matches(String httpMethod, String uri) {
        return route.matches(httpMethod, uri);
    }

    @Override
    public Map<String, String> getPathParametersEncoded(String uri) {
        return route.getPathParametersEncoded(uri);
    }

    @Override
    public Controller getControllerObject() {
        return route.getControllerObject();
    }

    @Override
    public List<RouteUtils.Argument> getArguments() {
        return route.getArguments();
    }

    @Override
    public Result invoke() throws Throwable {
        Context context = Context.context.get();
        Preconditions.checkNotNull(context);
        List<RouteUtils.Argument> arguments = route.getArguments();
        Object[] parameters = new Object[arguments.size()];
        for (int i = 0; i < arguments.size(); i++) {
            RouteUtils.Argument argument = arguments.get(i);
            switch (argument.getSource()) {
            case PARAMETER:
                parameters[i] = RouteUtils.getParameter(argument, context);
                break;
            case BODY:
                parameters[i] = context.body(argument.getType());
                break;
            case ATTRIBUTE:
                parameters[i] = RouteUtils.getAttribute(argument, context);
                break;
            default:
                break;
            }
        }

        if (mustValidate) {
            Validator validator = router.getValidator();
            if (validator != null) {
                Set<ConstraintViolation<Controller>> violations =
                        validator.forExecutables().validateParameters(getControllerObject(), getControllerMethod(),
                                parameters);

                if (!violations.isEmpty()) {
                    return Results.badRequest(violations).json();
                }
            }
        }

        // Build chain if needed.
        if (!interceptors.isEmpty()) {
            Map<Interceptor<?>, Object> chain = new LinkedHashMap<>();
            for (Map.Entry<String, Object> entry : interceptors.entrySet()) {
                final Interceptor<?> interceptor = getInterceptorForAnnotation(entry.getKey());
                if (interceptor == null) {
                    return Results.badRequest("Missing interceptor handling " + entry.getKey());
{'fraction_non_alphanumeric': 0.09815900944933204, 'fraction_numerical': 0.00016291951775822744, 'mean_word_length': 2.4235917456776352, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '370832', 'n_tokens_mistral': 3079, 'n_tokens_neox': 2955, 'n_words': 725}