Search in sources :

Example 1 with FilterFuckOff

use of org.axe.annotation.mvc.FilterFuckOff in project Axe by DongyuCai.

the class ControllerHelper method generateActionMap.

@SuppressWarnings("unchecked")
private static void generateActionMap(String nodeName, Map<String, Object> node, Class<?> controllerClass, Method actionMethod, String[] subNodeNameLineAry, String requestMethod, String mappingPath) {
    //如果nodeName中有pathParam,全部替换成占位符?
    nodeName = RequestUtil.castPathParam(nodeName);
    if (subNodeNameLineAry.length > 0) {
        Object nodeValue = null;
        if (node.containsKey(nodeName)) {
            nodeValue = node.get(nodeName);
        } else {
            nodeValue = new HashMap<String, Object>();
            node.put(nodeName, nodeValue);
        }
        String nodeName_next = subNodeNameLineAry[0];
        String[] subNodeNameLineAry_next = new String[subNodeNameLineAry.length - 1];
        for (int i = 0; i < subNodeNameLineAry_next.length; i++) {
            subNodeNameLineAry_next[i] = subNodeNameLineAry[i + 1];
        }
        generateActionMap(nodeName_next, (Map<String, Object>) nodeValue, controllerClass, actionMethod, subNodeNameLineAry_next, requestMethod, mappingPath);
    } else {
        //到最后了
        nodeName = requestMethod + ":" + nodeName;
        if (!node.containsKey(nodeName)) {
            //##Filter 链
            List<Filter> filterList = new ArrayList<>();
            for (Filter filter : FilterHelper.getSortedFilterList()) {
                //#首先判断是否匹配mappingPath
                if (filter.setMapping() == null) {
                    throw new RuntimeException("invalid filter[" + filter.getClass() + "] setMapping is null");
                }
                Matcher mappingPathMatcher = filter.setMapping().matcher(requestMethod + ":" + mappingPath);
                if (!mappingPathMatcher.find())
                    continue;
                //还要判断是否在刨除的规则里,此规则可以为null,直接跳过
                if (filter.setNotMapping() != null) {
                    Matcher notMappingPathMatcher = filter.setNotMapping().matcher(requestMethod + ":" + mappingPath);
                    if (notMappingPathMatcher.find())
                        continue;
                }
                //#其次,说明匹配上了,判断controller是否排除了这个Filter
                if (controllerClass.isAnnotationPresent(FilterFuckOff.class)) {
                    FilterFuckOff filterFuckOff = controllerClass.getAnnotation(FilterFuckOff.class);
                    if (filterFuckOff.value().length == 0)
                        continue;
                    boolean findFuckOffFilter = false;
                    for (Class<?> filterClass : filterFuckOff.value()) {
                        if (ReflectionUtil.compareType(filter.getClass(), filterClass)) {
                            findFuckOffFilter = true;
                            break;
                        }
                    }
                    if (findFuckOffFilter)
                        continue;
                }
                //最后,说明Controller上没有排除此Filter,需要判断方法上是否排除
                if (actionMethod.isAnnotationPresent(FilterFuckOff.class)) {
                    FilterFuckOff filterFuckOff = actionMethod.getAnnotation(FilterFuckOff.class);
                    if (filterFuckOff.value().length == 0)
                        continue;
                    boolean findFuckOffFilter = false;
                    for (Class<?> filterClass : filterFuckOff.value()) {
                        if (ReflectionUtil.compareType(filter.getClass(), filterClass)) {
                            findFuckOffFilter = true;
                            break;
                        }
                    }
                    if (findFuckOffFilter)
                        continue;
                }
                //到这里,说明此过滤器匹配上了这个actionMethod,而且没被排除
                filterList.add(filter);
            }
            //##Interceptor 列表
            Map<Class<? extends Interceptor>, Interceptor> interceptorMap = new HashMap<>();
            //#controller上指定的拦截器
            if (controllerClass.isAnnotationPresent(org.axe.annotation.mvc.Interceptor.class)) {
                org.axe.annotation.mvc.Interceptor interceptorAnnotation = controllerClass.getAnnotation(org.axe.annotation.mvc.Interceptor.class);
                Class<? extends Interceptor>[] interceptorClassAry = interceptorAnnotation.value();
                Map<Class<? extends Interceptor>, Interceptor> INTERCEPTOR_MAP = InterceptorHelper.getInterceptorMap();
                if (interceptorClassAry != null) {
                    for (Class<? extends Interceptor> interceptorClass : interceptorClassAry) {
                        if (INTERCEPTOR_MAP.containsKey(interceptorClass)) {
                            interceptorMap.put(interceptorClass, INTERCEPTOR_MAP.get(interceptorClass));
                        }
                    }
                }
            }
            //#action method上指定的拦截器,注意去重
            if (actionMethod.isAnnotationPresent(org.axe.annotation.mvc.Interceptor.class)) {
                org.axe.annotation.mvc.Interceptor interceptorAnnotation = actionMethod.getAnnotation(org.axe.annotation.mvc.Interceptor.class);
                Class<? extends Interceptor>[] interceptorClassAry = interceptorAnnotation.value();
                Map<Class<? extends Interceptor>, Interceptor> INTERCEPTOR_MAP = InterceptorHelper.getInterceptorMap();
                if (interceptorClassAry != null) {
                    for (Class<? extends Interceptor> interceptorClass : interceptorClassAry) {
                        if (INTERCEPTOR_MAP.containsKey(interceptorClass) && !interceptorMap.containsKey(interceptorClass)) {
                            interceptorMap.put(interceptorClass, INTERCEPTOR_MAP.get(interceptorClass));
                        }
                    }
                }
            }
            List<Interceptor> interceptorList = new ArrayList<>(interceptorMap.values());
            Handler handler = new Handler(requestMethod, mappingPath, controllerClass, actionMethod, filterList, interceptorList);
            //构建树
            node.put(nodeName, handler);
            //存根到ACTIN_LIST
            ACTION_LIST.add(handler);
        } else {
            Handler handler = (Handler) node.get(nodeName);
            throw new RuntimeException("find the same action: " + actionMethod.toGenericString() + " === " + handler.getActionMethod().toGenericString());
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Handler(org.axe.bean.mvc.Handler) Filter(org.axe.interface_.mvc.Filter) FilterFuckOff(org.axe.annotation.mvc.FilterFuckOff) Interceptor(org.axe.interface_.mvc.Interceptor)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 FilterFuckOff (org.axe.annotation.mvc.FilterFuckOff)1 Handler (org.axe.bean.mvc.Handler)1 Filter (org.axe.interface_.mvc.Filter)1 Interceptor (org.axe.interface_.mvc.Interceptor)1