Search in sources :

Example 1 with InterConfigBean

use of org.eweb4j.mvc.config.bean.InterConfigBean in project eweb4j-framework by laiweiwei.

the class InterExecution method findAndExecuteInter.

/**
	 * 找
	 * 
	 * @return
	 * @throws Exception
	 */
public boolean findAndExecuteInter() throws Exception {
    List<InterConfigBean> list = InterConfigBeanCache.getList();
    // 按优先级从高到低排序
    Collections.sort(list, new InterPriorityComparator());
    final int listSize = list.size();
    for (int index = 0; index < listSize; index++) {
        InterConfigBean inter = list.get(index);
        String _interType = inter.getType();
        if (!interType.equals(_interType))
            continue;
        String uri = this.context.getUri();
        if (uri.length() == 0)
            uri = " ";
        if (inter.getExcept().contains(uri))
            continue;
        String policy = inter.getPolicy();
        boolean isOR = "or".equalsIgnoreCase(policy) ? true : false;
        List<Uri> uris = inter.getUri();
        final int size = uris.size();
        // 默认不能处理
        boolean canProcess = false;
        for (int i = 0; i < size; i++) {
            Uri u = uris.get(i);
            String type = u.getType();
            String value = u.getValue();
            // 默认找到
            boolean found = true;
            if ("start".equalsIgnoreCase(type) && uri.startsWith(value))
                // 以url开头
                ;
            else if ("end".equalsIgnoreCase(type) && uri.endsWith(value))
                // 以url结尾
                ;
            else if ("contains".equalsIgnoreCase(type) && uri.contains(value))
                // 包含url
                ;
            else if ("all".equalsIgnoreCase(type) && uri.equals(value))
                // 完全匹配url
                ;
            else if ("regex".equalsIgnoreCase(type) && uri.matches(value))
                // 正则匹配
                ;
            else if ("actions".equalsIgnoreCase(type)) {
                if (!findActionUriMapping())
                    found = false;
            } else if ("!start".equalsIgnoreCase(type) && !uri.startsWith(value))
                // 不以url开头
                ;
            else if ("!end".equalsIgnoreCase(type) && !uri.endsWith(value))
                // 不以url结尾
                ;
            else if ("!contains".equalsIgnoreCase(type) && !uri.contains(value))
                // 不包含url
                ;
            else if ("!all".equalsIgnoreCase(type) && !uri.equals(value))
                // 不完全匹配url
                ;
            else if ("!regex".equalsIgnoreCase(type) && !uri.matches(value))
                // 不正则匹配
                ;
            else if ("!actions".equalsIgnoreCase(type)) {
                if (ActionConfigBeanCache.containsKey(uri) || (ActionConfigBeanCache.getByMatches(uri, context.getHttpMethod())) != null)
                    found = false;
            } else if ("*".equals(type)) {
                // 所有都匹配
                ;
            } else
                found = false;
            // 如果是 或者 
            if (isOR) {
                // 如果找到一个规则符合,就可以执行了。
                if (found) {
                    canProcess = true;
                    break;
                }
            } else {
                // 如果是 并且
                if (!found) {
                    //只要找到一个规则不符合,就退出,且不处理
                    canProcess = false;
                    break;
                } else {
                    canProcess = true;
                }
            }
        }
        if (!canProcess)
            continue;
        this.doIntercept(inter);
        if (this.error == null) {
            // 如果拦截处理之后没有任何错误信息,进入下一个拦截器继续处理
            continue;
        } else {
            // 否则显示错误信息, 并且退出方法
            log.debug("do interceptor -> " + inter.getClazz() + " error -> " + error);
            return true;
        }
    }
    return false;
}
Also used : InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) Uri(org.eweb4j.mvc.config.bean.Uri)

Example 2 with InterConfigBean

use of org.eweb4j.mvc.config.bean.InterConfigBean in project eweb4j-framework by laiweiwei.

the class InterPriorityComparator method main.

public static void main(String[] args) {
    List<InterConfigBean> list = new ArrayList<InterConfigBean>();
    InterConfigBean i1 = new InterConfigBean();
    i1.setPriority("1");
    list.add(i1);
    InterConfigBean i2 = new InterConfigBean();
    i2.setPriority("5");
    list.add(i2);
    InterConfigBean i3 = new InterConfigBean();
    i3.setPriority("2");
    list.add(i3);
    InterConfigBean i4 = new InterConfigBean();
    i4.setPriority("7");
    list.add(i4);
    System.out.println(list);
    Collections.sort(list, new InterPriorityComparator());
    System.out.println(list);
}
Also used : ArrayList(java.util.ArrayList) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean)

Example 3 with InterConfigBean

use of org.eweb4j.mvc.config.bean.InterConfigBean in project eweb4j-framework by laiweiwei.

the class InterceptorAnnotationConfig method handleClass.

/**
	 * handle class
	 * 
	 * @param clsName
	 */
public boolean handleClass(final String clsName) {
    Class<?> cls = null;
    try {
        cls = Thread.currentThread().getContextClassLoader().loadClass(clsName);
        if (cls == null)
            return false;
        Interceptor interAnn = cls.getAnnotation(Interceptor.class);
        if (interAnn == null)
            return false;
        Uri[] uris = interAnn.uri();
        if (uris == null || uris.length == 0)
            return false;
        InterConfigBean inter = new InterConfigBean();
        String name = "".equals(interAnn.name()) ? cls.getSimpleName() : interAnn.name();
        inter.setName(name);
        inter.setClazz(cls.getName());
        inter.setMethod(interAnn.method());
        String[] except = interAnn.except();
        if (except != null && except.length > 0) {
            List<String> list = Arrays.asList(except);
            inter.setExcept(new ArrayList<String>(list));
        }
        inter.setPolicy(interAnn.policy());
        inter.setType(interAnn.type());
        inter.setPriority(String.valueOf(interAnn.priority()));
        Singleton sin = cls.getAnnotation(Singleton.class);
        if (sin != null)
            inter.setScope("singleton");
        else
            inter.setScope("prototype");
        List<org.eweb4j.mvc.config.bean.Uri> uriList = new ArrayList<org.eweb4j.mvc.config.bean.Uri>();
        for (Uri u : uris) {
            org.eweb4j.mvc.config.bean.Uri uri = new org.eweb4j.mvc.config.bean.Uri();
            uri.setType(u.type());
            uri.setValue(u.value());
            uriList.add(uri);
        }
        inter.setUri(uriList);
        InterConfigBeanCache.add(inter);
    } catch (Throwable e) {
        log.warn("the interceptor class new instance failued -> " + clsName + " | " + e.toString(), e);
        return false;
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) Uri(org.eweb4j.mvc.interceptor.Uri) Singleton(org.eweb4j.mvc.action.annotation.Singleton) Interceptor(org.eweb4j.mvc.interceptor.Interceptor)

Example 4 with InterConfigBean

use of org.eweb4j.mvc.config.bean.InterConfigBean in project eweb4j-framework by laiweiwei.

the class InterceptorConfig method check.

public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb != null) {
        for (String filePath : cb.getMvc().getInterXmlFiles().getPath()) {
            if (filePath != null && filePath.length() > 0) {
                File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
                try {
                    XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
                    reader.setBeanName("interceptor");
                    reader.setClass("interceptor", InterConfigBean.class);
                    List<InterConfigBean> interList = reader.read();
                    if (interList == null || interList.isEmpty()) {
                        error = rebuildXmlFile(configFile, ConfigErrCons.CANNOT_READ_CONFIG_INFO);
                    } else {
                        for (Iterator<InterConfigBean> it = interList.iterator(); it.hasNext(); ) {
                            InterConfigBean inter = it.next();
                            String error1 = CheckConfigBean.checkMVCInterceptor(inter, filePath);
                            if (error1 != null) {
                                if (error != null) {
                                    error += error1;
                                } else {
                                    error = error1;
                                }
                            }
                        }
                        if (error == null) {
                            for (Iterator<InterConfigBean> it = interList.iterator(); it.hasNext(); ) {
                                InterConfigBean inter = it.next();
                                if (!"".equals(inter.getClazz())) {
                                    InterConfigBeanCache.add(inter);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    error = rebuildXmlFile(configFile, CommonUtil.getExceptionString(e));
                }
            }
        }
        if (error != null) {
            InterConfigBeanCache.clear();
        }
    }
    return error;
}
Also used : InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) CheckConfigBean(org.eweb4j.config.CheckConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) File(java.io.File) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 5 with InterConfigBean

use of org.eweb4j.mvc.config.bean.InterConfigBean in project eweb4j-framework by laiweiwei.

the class MVCConfigBeanCreator method getInterBean.

public static InterConfigBean getInterBean() {
    InterConfigBean icb = new InterConfigBean();
    List<Uri> urls = new ArrayList<Uri>();
    Uri url = new Uri();
    urls.add(url);
    icb.setUri(urls);
    List<String> excepts = new ArrayList<String>();
    excepts.add("");
    icb.setExcept(excepts);
    return icb;
}
Also used : ArrayList(java.util.ArrayList) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) Uri(org.eweb4j.mvc.config.bean.Uri)

Aggregations

InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)6 ArrayList (java.util.ArrayList)3 Uri (org.eweb4j.mvc.config.bean.Uri)3 ConfigBean (org.eweb4j.config.bean.ConfigBean)2 File (java.io.File)1 CheckConfigBean (org.eweb4j.config.CheckConfigBean)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)1 Singleton (org.eweb4j.mvc.action.annotation.Singleton)1 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)1 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)1 ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)1 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)1 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)1 Interceptor (org.eweb4j.mvc.interceptor.Interceptor)1 Uri (org.eweb4j.mvc.interceptor.Uri)1 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)1 DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)1 XMLReader (org.eweb4j.util.xml.XMLReader)1