use of com.weicoder.common.exception.StateException in project weicoder by wdcode.
the class BasicServlet method invoke.
private Object invoke(Object action, Method method, Object[] params, HttpServletRequest request, HttpServletResponse response) {
// 获得所有aop
List<Aops> aops = aops(action, method);
try {
// 前置执行
aops.forEach(aop -> aop.before(action, params, request, response));
// 执行方法返回结果
Object result = method.invoke(action, params);
// Object result = method.invoke(action, U.E.isEmpty(params) ? null : params);
if (result == null && void.class.equals(method.getReturnType()))
result = StateCode.SUCCESS;
// 后置执行
for (Aops aop : aops) aop.after(action, params, result, request, response);
// 返回结果
return result;
} catch (StateException e) {
return e.state();
} catch (Exception e) {
Logs.error(e, "action invoke method={} args={} params={}", method.getName(), Arrays.toString(params), Arrays.toString(method.getParameters()));
// 异常执行
aops.forEach(aop -> aop.exception(e, action, params, request, response));
// 返回错误
return StateCode.ERROR;
}
}
use of com.weicoder.common.exception.StateException in project weicoder by wdcode.
the class Validators method validator.
/**
* 调用验证方法
*
* @param vali 验证类
* @param ps 提交参数
* @return 是否成功
*/
public static int validator(Validator vali, Map<String, String> ps) {
// 返回错误码
int res = 0;
try {
// 获得验证类名
String name = vali.name();
// 获得验证方法
for (String val : vali.value()) {
// 获得验证类
Object obj = U.E.isEmpty(name) ? WebCommons.METHOD_VALIDATOR.get(val) : WebCommons.VALIDATORS.get(name);
// 获得验证方法
Method method = U.E.isEmpty(name) ? WebCommons.METHODS_VALIDATORS.get(val) : WebCommons.VALIDATORS_METHODS.get(name).get(val);
// 获得所有参数类型
Parameter[] pars = WebCommons.VALIDATORS_METHODS_PARAMES.get(method);
Object[] params = new Object[pars.length];
for (int i = 0; i < pars.length; i++) {
// 判断类型并设置
Parameter p = pars[i];
// 参数的类型
Class<?> cs = p.getType();
if (Map.class.equals(cs))
params[i] = ps;
else if (ClassUtil.isBaseType(cs))
// 获得参数
params[i] = W.C.to(ps.get(p.getName()), cs);
else
// 设置属性
params[i] = BeanUtil.copy(ps, cs);
LOG.debug("validator Parameter index={},name={},type={},value={}", i, p.getName(), cs, params[i]);
}
// 调用并返回验证结果
Object rs = null;
try {
// BeanUtil.invoke(obj, method, params);
rs = method.invoke(obj, params);
} catch (StateException e) {
rs = e.state();
}
// 判断状态码对象
if (rs instanceof StateCode && (res = ((StateCode) rs).getCode()) != StateParams.SUCCESS)
break;
// 判断状态码 int 类型
if (rs instanceof Integer && (res = W.C.toInt(rs)) != StateParams.SUCCESS)
break;
}
} catch (Exception e) {
LOG.error(e);
res = StateParams.ERROR;
}
// 返回验证码
return res;
}
Aggregations