Search in sources :

Example 1 with StateCode

use of com.weicoder.common.bean.StateCode 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)

Example 2 with StateCode

use of com.weicoder.common.bean.StateCode in project weicoder by wdcode.

the class BasicServlet method result.

private void result(Method method, Object action, Object res, String callback, HttpServletRequest request, HttpServletResponse response, String ip, String actionName, Object[] params, Parameter[] pars, long curr) {
    try {
        // 判断是否需要写cookie
        boolean cookie = method.isAnnotationPresent(Cookies.class) || action.getClass().isAnnotationPresent(Cookies.class);
        String[] names = null;
        Cookies c = null;
        if (cookie) {
            // 获得Cookies注解
            c = method.getAnnotation(Cookies.class);
            if (c == null)
                c = action.getClass().getAnnotation(Cookies.class);
            names = c.names();
        }
        // 判断是否跳转url
        if (method.isAnnotationPresent(Redirect.class) || action.getClass().isAnnotationPresent(Redirect.class)) {
            String url = W.C.toString(res);
            if (U.E.isEmpty(url))
                ResponseUtil.json(response, callback, "Redirect is null");
            else
                response.sendRedirect(url);
        }
        // 是否Forward跳转
        if (method.isAnnotationPresent(Forward.class) || action.getClass().isAnnotationPresent(Forward.class)) {
            String url = W.C.toString(res);
            if (U.E.isEmpty(url))
                ResponseUtil.json(response, callback, "Forward is null");
            else
                request.getRequestDispatcher(url).forward(request, response);
        }
        // 是否json
        if (method.isAnnotationPresent(Json.class) || action.getClass().isAnnotationPresent(Json.class)) {
        } else if (WebParams.STATE || method.isAnnotationPresent(State.class) || action.getClass().isAnnotationPresent(State.class)) {
            // 状态码对象
            State state = method.getAnnotation(State.class);
            if (state == null)
                state = action.getClass().getAnnotation(State.class);
            // 字段名
            String code = state == null ? "code" : state.code();
            String content = state == null ? "content" : state.content();
            String message = state == null ? "message" : state.message();
            // 如果res为状态码
            if (res == null)
                // 写空信息
                res = Maps.newMap(new String[] { code, message }, StateCode.NULL.to());
            else if (res instanceof StateCode)
                // 写错误信息
                res = Maps.newMap(new String[] { code, message }, ((StateCode) res).to());
            else
                // 写入到前端
                res = Maps.newMap(new String[] { code, content }, new Object[] { StateCode.SUCCESS.getCode(), res });
        } else {
            // 如果结果为空
            if (res == null)
                // 结果设置为空map
                res = Maps.emptyMap();
        }
        // 是否写cookie
        if (cookie)
            CookieUtil.adds(response, c.maxAge(), res, names);
        // 写到前端
        LOG.info("request ip={} name={} time={} params={} pars={} method={} type={} res={} end", ip, actionName, System.currentTimeMillis() - curr, params, pars, request.getMethod(), request.getContentType(), ResponseUtil.json(response, callback, res));
    } catch (Exception e) {
        Logs.error(e);
    }
}
Also used : Cookies(com.weicoder.web.annotation.Cookies) State(com.weicoder.web.annotation.State) StateCode(com.weicoder.common.bean.StateCode) Forward(com.weicoder.web.annotation.Forward) Redirect(com.weicoder.web.annotation.Redirect) Json(com.weicoder.web.annotation.Json) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StateException(com.weicoder.common.exception.StateException)

Aggregations

StateCode (com.weicoder.common.bean.StateCode)2 StateException (com.weicoder.common.exception.StateException)2 Cookies (com.weicoder.web.annotation.Cookies)1 Forward (com.weicoder.web.annotation.Forward)1 Json (com.weicoder.web.annotation.Json)1 Redirect (com.weicoder.web.annotation.Redirect)1 State (com.weicoder.web.annotation.State)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ServletException (javax.servlet.ServletException)1