Search in sources :

Example 16 with ParamConfigBean

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

the class ForbidWordValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    for (FieldConfigBean f : val.getField()) {
        String[] value = context.getQueryParamMap().get(f.getName());
        if (value == null || value.length == 0)
            continue;
        String mess = f.getMessage();
        param: for (ParamConfigBean p : f.getParam()) {
            if (Validators.FORBID_WORD_PARAM.equalsIgnoreCase(p.getName())) {
                String[] forbidWord = p.getValue().split("#");
                if (forbidWord == null || forbidWord.length == 0)
                    continue;
                boolean flag = false;
                for (String v : value) {
                    for (String word : forbidWord) {
                        if (v.indexOf(word) != -1) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                        continue;
                    valError.put(f.getName(), mess.replace("{words}", p.getValue()));
                    break param;
                }
            }
        }
        context.getRequest().setAttribute(f.getName(), value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 17 with ParamConfigBean

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

the class IntegerSizeValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    for (FieldConfigBean f : val.getField()) {
        String[] value = context.getQueryParamMap().get(f.getName());
        if (value == null || value.length == 0)
            continue;
        String mess = f.getMessage();
        int min = 0;
        int max = 0;
        boolean minFlag = false;
        boolean maxFlag = false;
        for (ParamConfigBean p : f.getParam()) {
            if (Validators.MIN_SIZE_PARAM.equals(p.getName())) {
                min = Integer.parseInt(p.getValue());
                minFlag = true;
            } else if (Validators.MAX_SIZE_PARAM.equals(p.getName())) {
                max = Integer.parseInt(p.getValue());
                maxFlag = true;
            }
            if (minFlag && maxFlag)
                break;
        }
        for (String v : value) {
            if (!v.matches(RegexList.integer_regexp))
                continue;
            int iv = Integer.parseInt(v);
            mess = mess.replace("{min}", min + "").replace("{max}", max + "");
            if (iv < min) {
                valError.put(f.getName(), mess);
                break;
            } else if (iv > max) {
                valError.put(f.getName(), mess);
                break;
            }
        }
        context.getRequest().setAttribute(f.getName(), value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 18 with ParamConfigBean

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

the class StringLengthValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    for (FieldConfigBean f : val.getField()) {
        String[] value = context.getQueryParamMap().get(f.getName());
        if (value == null || value.length == 0)
            continue;
        String mess = f.getMessage();
        int min = 0;
        int max = 0;
        boolean minFlag = false;
        boolean maxFlag = false;
        for (ParamConfigBean p : f.getParam()) {
            if (Validators.MIN_LENGTH_PARAM.equals(p.getName())) {
                min = Integer.parseInt(p.getValue());
                minFlag = true;
            } else if (Validators.MAX_LENGTH_PARAM.equals(p.getName())) {
                max = Integer.parseInt(p.getValue());
                maxFlag = true;
            }
            if (minFlag && maxFlag)
                break;
        }
        mess = mess.replace("{min}", min + "").replace("{max}", max + "");
        for (String v : value) {
            if (v.length() < min) {
                valError.put(f.getName(), mess);
                break;
            } else if (v.length() > max) {
                valError.put(f.getName(), mess);
                break;
            }
        }
        context.getRequest().setAttribute(f.getName(), value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 19 with ParamConfigBean

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

the class UploadValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    long countAllSize = 0;
    for (FieldConfigBean f : val.getField()) {
        String fieldName = f.getName();
        String msg = f.getMessage();
        List<UploadFile> files = context.getUploadMap().get(fieldName);
        if (files == null || files.isEmpty())
            continue;
        for (UploadFile file : files) {
            String fileName = file.getFileName();
            String fileContentType = file.getContentType();
            String tmpDir = null;
            long memoryMax = 0;
            String memoryMaxStr = null;
            long allSizeMax = 0;
            String allSizeMaxStr = null;
            long oneSizeMax = 0;
            String oneSizeMaxStr = null;
            String[] suffixArray = null;
            for (ParamConfigBean p : f.getParam()) {
                if (Validators.UPLOAD_TMP_DIR.equalsIgnoreCase(p.getName())) {
                    tmpDir = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_MAX_MEMORY_SIZE.equalsIgnoreCase(p.getName())) {
                    memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(p.getValue()) + "");
                    memoryMaxStr = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_MAX_REQ_SIZE.equalsIgnoreCase(p.getName())) {
                    allSizeMax = CommonUtil.parseFileSize(p.getValue());
                    allSizeMaxStr = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_MAX_FILE_SIZE.equalsIgnoreCase(p.getName())) {
                    oneSizeMax = CommonUtil.parseFileSize(p.getValue());
                    oneSizeMaxStr = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_SUFFIX.equalsIgnoreCase(p.getName())) {
                    suffixArray = p.getValue().split(",");
                    continue;
                }
            }
            if (suffixArray != null && suffixArray.length > 0) {
                boolean isOk = false;
                for (String suffix : suffixArray) {
                    if (fileName.toLowerCase().endsWith("." + suffix.toLowerCase())) {
                        isOk = true;
                        break;
                    }
                }
                if (!isOk) {
                    valError.put(fieldName, getParamValue(Validators.UPLOAD_SUFFIX_MSG, f.getParam()).replace("{suffix}", Arrays.asList(suffixArray).toString()));
                    break;
                }
            }
            long fileSize = file.getSize();
            if (fileSize > oneSizeMax) {
                valError.put(fieldName, getParamValue(Validators.UPLOAD_MAX_FILE_SIZE_MSG, f.getParam()).replace("{maxFileSize}", oneSizeMaxStr));
                break;
            }
            countAllSize += fileSize;
            if (countAllSize > allSizeMax) {
                valError.put(fieldName, getParamValue(Validators.UPLOAD_MAX_REQ_SIZE_MSG, f.getParam()).replace("{maxRequestSize}", allSizeMaxStr));
                break;
            }
        }
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) UploadFile(org.eweb4j.mvc.upload.UploadFile) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 20 with ParamConfigBean

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

the class CheckConfigBean method checkMVCValidator.

/**
	 * Check the Validator part of MVC components configuration
	 * 
	 * @param rList
	 * @return
	 */
public static String checkMVCValidator(List<ValidatorConfigBean> vList, String beanID, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equals(cb.getMvc().getOpen()) || "1".equals(cb.getMvc().getOpen())) {
        if (vList != null && !vList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (Iterator<ValidatorConfigBean> it = vList.iterator(); it.hasNext(); ) {
                ValidatorConfigBean v = it.next();
                if (!"".equals(v.getName())) {
                    if (!Validators.REQUIRED.equalsIgnoreCase(v.getName()) && !Validators.INT.equalsIgnoreCase(v.getName()) && !Validators.EMAIL.equalsIgnoreCase(v.getName()) && !Validators.DATE.equalsIgnoreCase(v.getName()) && !Validators.URL.equalsIgnoreCase(v.getName()) && !Validators.ID_CARD.equalsIgnoreCase(v.getName()) && !Validators.ZIP.equalsIgnoreCase(v.getName()) && !Validators.PHONE.equalsIgnoreCase(v.getName()) && !Validators.QQ.equalsIgnoreCase(v.getName()) && !Validators.IP.equals(v.getName()) && !Validators.CHINESE.equalsIgnoreCase(v.getName()) && !Validators.LENGTH.equalsIgnoreCase(v.getName()) && !Validators.SIZE.equalsIgnoreCase(v.getName()) && !Validators.FORBID.equalsIgnoreCase(v.getName()) && !Validators.ENUM.equalsIgnoreCase(v.getName())) {
                        sb.append("当前您填写的:( name=").append(v.getName());
                        sb.append(" )是错误的!它只能填写为:required|int|");
                        sb.append("email|date|url|idCard|zip|phone|qq|ip|");
                        sb.append("chinese|length|size|forbid|enum|留空  中的一种 ,忽略大小写 ;\n");
                    } else if (Validators.SIZE.equalsIgnoreCase(v.getName())) {
                        int minSize = 0;
                        for (FieldConfigBean f : v.getField()) {
                            for (ParamConfigBean p : f.getParam()) {
                                if (Validators.MIN_SIZE_PARAM.equalsIgnoreCase(p.getName())) {
                                    if (!(p.getValue().matches(RegexList.integer_regexp))) {
                                        sb.append("<param>当前您填写的:( value=").append(p.getValue());
                                        sb.append(" )是错误的!它应该填写为数字");
                                    } else {
                                        minSize = Integer.parseInt(p.getValue());
                                    }
                                } else if (Validators.MAX_SIZE_PARAM.equalsIgnoreCase(p.getName())) {
                                    if (!(p.getValue().matches(RegexList.integer_regexp))) {
                                        sb.append("<param>当前您填写的:( value=").append(p.getValue());
                                        sb.append(" )是错误的!它应该填写为数字");
                                    } else {
                                        int maxSize = Integer.parseInt(p.getValue());
                                        if (minSize > maxSize) {
                                            sb.append("<param>当前您填写的:( value=").append(p.getValue());
                                            sb.append(" )是错误的!它不能比minSize的值");
                                            sb.append(minSize).append("更小");
                                        }
                                    }
                                } else {
                                    sb.append("<param>当前您填写的:( name=").append(p.getName());
                                    sb.append(" )是错误的!它只能填写为:minSize|maxSize|");
                                    sb.append("中的一种 ,忽略大小写 ;\n");
                                }
                            }
                        }
                    } else if (Validators.LENGTH.equalsIgnoreCase(v.getName())) {
                        int minLength = 0;
                        for (FieldConfigBean f : v.getField()) {
                            for (ParamConfigBean p : f.getParam()) {
                                if (Validators.MIN_LENGTH_PARAM.equalsIgnoreCase(p.getName())) {
                                    if (!(p.getValue().matches(RegexList.integer_regexp))) {
                                        sb.append("<param>当前您填写的:( value=").append(p.getValue());
                                        sb.append(" )是错误的!它应该填写为数字");
                                    } else {
                                        minLength = Integer.parseInt(p.getValue());
                                    }
                                } else if (Validators.MAX_LENGTH_PARAM.equalsIgnoreCase(p.getName())) {
                                    if (!(p.getValue().matches(RegexList.integer_regexp))) {
                                        sb.append("<param>当前您填写的:( value=").append(p.getValue());
                                        sb.append(" )是错误的!它应该填写为数字");
                                    } else {
                                        int maxLength = Integer.parseInt(p.getValue());
                                        if (minLength > maxLength) {
                                            sb.append("<param>当前您填写的:( value=").append(p.getValue());
                                            sb.append(" )是错误的!它不能比minLength的值");
                                            sb.append(minLength).append("更小");
                                        }
                                    }
                                } else {
                                    sb.append("<param>当前您填写的:( name=").append(p.getName());
                                    sb.append(" )是错误的!它只能填写为:minLength|maxLength|");
                                    sb.append("中的一种 ,忽略大小写 ;\n");
                                }
                            }
                        }
                    } else if (Validators.FORBID.equalsIgnoreCase(v.getName())) {
                        for (FieldConfigBean f : v.getField()) {
                            for (ParamConfigBean p : f.getParam()) {
                                if (!Validators.FORBID_WORD_PARAM.equalsIgnoreCase(p.getName())) {
                                    sb.append("<param>当前您填写的:( name=").append(p.getName());
                                    sb.append(" )是错误的!它只能填写为:forbidWord|");
                                    sb.append("忽略大小写 ;\n");
                                }
                            }
                        }
                    } else if (Validators.ENUM.equalsIgnoreCase(v.getName())) {
                        for (FieldConfigBean f : v.getField()) {
                            for (ParamConfigBean p : f.getParam()) {
                                if (!Validators.ENUM_WORD_PARAM.equalsIgnoreCase(p.getName())) {
                                    sb.append("<param>当前您填写的:( name=").append(p.getName());
                                    sb.append(" )是错误的!它只能填写为:enumWord|");
                                    sb.append("忽略大小写 ;\n");
                                }
                            }
                        }
                    }
                }
                if (!"".equals(v.getClazz())) {
                    try {
                        if (Thread.currentThread().getContextClassLoader().loadClass(v.getClazz()) == null) {
                            sb.append("当前您填写的( class=").append(v.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
                        }
                    } catch (ClassNotFoundException e) {
                        sb.append("当前您填写的( class=").append(v.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
                    }
                }
            }
            if (!"".equals(sb.toString())) {
                error = "\n<br /><b>" + xmlFile + "[bean name=" + beanID + "][validator]:</b>\n" + sb.toString();
            }
        }
    }
    return error;
}
Also used : ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean)

Aggregations

ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)20 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)19 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)11 HashMap (java.util.HashMap)8 Validation (org.eweb4j.mvc.action.Validation)8 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)2 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)1 InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)1 UploadFile (org.eweb4j.mvc.upload.UploadFile)1 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)1 DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)1