All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wang.ramboll.extend.request.limit.RequestFrequencyLimitAnnotationScanner Maven / Gradle / Ivy

There is a newer version: 1.0.4-RC.1
Show newest version
package wang.ramboll.extend.request.limit;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import wang.ramboll.extend.basic.util.ClassScanUtils;
import wang.ramboll.extend.request.limit.annotation.RequestFrequencyLimit;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * RequestFrequencyLimit注解扫描器
 * @author WangRuibo
 */
public class RequestFrequencyLimitAnnotationScanner {
    /**
     * 扫描出指定包下所有Controller中标注有RequestFrequencyLimit注解的UrlMapping路径,用于配置拦截器拦截处理
     * @param controllerPackages 需要验证签名的controller类所在包名
     * @return 标注有RequestFrequencyLimit注解的UrlMapping路径列表
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static List scanRequestFrequencyLimitAnnotationMappingUrl(String ... controllerPackages) throws IOException, ClassNotFoundException {
        List> classList = new ArrayList<>();
        for(String s : controllerPackages){
            classList.addAll(ClassScanUtils.getAllClassWithAnnotationByPackageName(s, Controller.class));
        }
        List result = new ArrayList<>();
        for(Class cls : classList){
            RequestMapping clsRM = cls.getAnnotation(RequestMapping.class);
            for(Method method :cls.getMethods()){
                if(method.isAnnotationPresent(RequestFrequencyLimit.class)){
                    RequestMapping methodRM = method.getAnnotation(RequestMapping.class);
                    for(String path : clsRM.value()){
                        if(path.endsWith("/")){
                            path = path.substring(0,path.length()-1);
                        }
                        for (String url : methodRM.value()) {
                            String s = path + url ;
                            result.add(s);
                        }
                    }
                }
            }
        }
        return result ;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy