Search in sources :

Example 1 with ParamsErrorException

use of org.neusoft.neubbs.exception.ParamsErrorException in project neubbs by nuitcoder.

the class ValidationServiceImpl method checkUsername.

@Override
public IValidationService checkUsername(String username) {
    if (username == null) {
        throw new ParamsErrorException(ApiMessage.PARAM_ERROR).log(LogWarnEnum.VS2);
    }
    String paramType = PatternUtil.matchEmail(username) ? ParamConst.EMAIL : ParamConst.USERNAME;
    ParamValidateUtil.check(paramType, username);
    return this;
}
Also used : ParamsErrorException(org.neusoft.neubbs.exception.ParamsErrorException)

Example 2 with ParamsErrorException

use of org.neusoft.neubbs.exception.ParamsErrorException in project neubbs by nuitcoder.

the class ValidationServiceImpl method checkOnlyNotNullParam.

@Override
public void checkOnlyNotNullParam(String... params) {
    int len = params.length;
    if (len == 0 || len % SetConst.LENGTH_TWO != 0) {
        throw new ParamsErrorException(ApiMessage.PARAM_ERROR).log(LogWarnEnum.US4);
    }
    for (int i = 0; i < len; i += SetConst.LENGTH_TWO) {
        String type = params[i];
        String value = params[i + 1];
        // if value != null, check param
        if (value != null) {
            this.check(type, value);
        }
    }
}
Also used : ParamsErrorException(org.neusoft.neubbs.exception.ParamsErrorException)

Example 3 with ParamsErrorException

use of org.neusoft.neubbs.exception.ParamsErrorException in project neubbs by nuitcoder.

the class ParamValidateUtil method checkPattern.

/**
 * 正则检查
 *      - 反射加载 PatternUtil.java 类实例
 *      - 反射执行类中方法(Class<?> - 通配泛型,可代表任何类型,Class<T> 在实例化的时候,T要替换成具体类)
 *      - method.invoke(null, param) 静态方法不需要借助实例运行,所以为 null
 *
 * @param type 参数类型
 * @param param 参数值
 */
private static void checkPattern(String type, String param) {
    Pattern pattern = typePatternMap.get(type);
    if (pattern == null) {
        return;
    }
    try {
        Class<?> clazz = Class.forName("org.neusoft.neubbs.utils.PatternUtil");
        Method method = clazz.getDeclaredMethod(pattern.methodName, String.class);
        if (!((boolean) method.invoke(null, param))) {
            log.warn(param + " ( " + type + pattern.logMessage);
            throw new ParamsErrorException(ApiMessage.PARAM_ERROR);
        }
    } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
        log.warn("反射加载 PatternUtils.java 失败,异常出现!");
        throw new ParamsErrorException(ApiMessage.PARAM_ERROR);
    }
}
Also used : ParamsErrorException(org.neusoft.neubbs.exception.ParamsErrorException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ParamsErrorException (org.neusoft.neubbs.exception.ParamsErrorException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1