Search in sources :

Example 1 with StateException

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;
    }
}
Also used : Validators(com.weicoder.web.validator.Validators) Arrays(java.util.Arrays) StringUtil(com.weicoder.common.util.StringUtil) Maps(com.weicoder.common.lang.Maps) RequestUtil(com.weicoder.web.util.RequestUtil) ServletException(javax.servlet.ServletException) Ips(com.weicoder.web.annotation.Ips) Aops(com.weicoder.web.aop.Aops) W(com.weicoder.common.W) U(com.weicoder.common.U) Redirect(com.weicoder.web.annotation.Redirect) DateUtil(com.weicoder.common.util.DateUtil) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) Get(com.weicoder.web.annotation.Get) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) LogFactory(com.weicoder.common.log.LogFactory) IpUtil(com.weicoder.common.util.IpUtil) BeanUtil(com.weicoder.common.util.BeanUtil) TokenEngine(com.weicoder.common.token.TokenEngine) Method(java.lang.reflect.Method) Asyn(com.weicoder.common.annotation.Asyn) Lists(com.weicoder.common.lang.Lists) Log(com.weicoder.common.log.Log) CookieUtil(com.weicoder.web.util.CookieUtil) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Cookies(com.weicoder.web.annotation.Cookies) ResponseUtil(com.weicoder.web.util.ResponseUtil) Json(com.weicoder.web.annotation.Json) WebServlet(javax.servlet.annotation.WebServlet) C(com.weicoder.common.C) Action(com.weicoder.web.annotation.Action) List(java.util.List) ClassUtil(com.weicoder.common.util.ClassUtil) StateCode(com.weicoder.common.bean.StateCode) WebParams(com.weicoder.web.params.WebParams) Post(com.weicoder.web.annotation.Post) Aop(com.weicoder.web.aop.Aop) StateParams(com.weicoder.common.params.StateParams) Forward(com.weicoder.web.annotation.Forward) StateException(com.weicoder.common.exception.StateException) TokenBean(com.weicoder.common.token.TokenBean) Logs(com.weicoder.common.log.Logs) WebCommons(com.weicoder.web.common.WebCommons) State(com.weicoder.web.annotation.State) StateException(com.weicoder.common.exception.StateException) Aops(com.weicoder.web.aop.Aops) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StateException(com.weicoder.common.exception.StateException)

Example 2 with StateException

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;
}
Also used : StateCode(com.weicoder.common.bean.StateCode) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) StateException(com.weicoder.common.exception.StateException) StateException(com.weicoder.common.exception.StateException)

Aggregations

StateCode (com.weicoder.common.bean.StateCode)2 StateException (com.weicoder.common.exception.StateException)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 C (com.weicoder.common.C)1 U (com.weicoder.common.U)1 W (com.weicoder.common.W)1 Asyn (com.weicoder.common.annotation.Asyn)1 Lists (com.weicoder.common.lang.Lists)1 Maps (com.weicoder.common.lang.Maps)1 Log (com.weicoder.common.log.Log)1 LogFactory (com.weicoder.common.log.LogFactory)1 Logs (com.weicoder.common.log.Logs)1 StateParams (com.weicoder.common.params.StateParams)1 TokenBean (com.weicoder.common.token.TokenBean)1 TokenEngine (com.weicoder.common.token.TokenEngine)1 BeanUtil (com.weicoder.common.util.BeanUtil)1 ClassUtil (com.weicoder.common.util.ClassUtil)1 DateUtil (com.weicoder.common.util.DateUtil)1 IpUtil (com.weicoder.common.util.IpUtil)1