Search in sources :

Example 1 with Post

use of com.weicoder.web.annotation.Post in project weicoder by wdcode.

the class BasicServlet method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long curr = System.currentTimeMillis();
    // 获得客户端IP
    String ip = RequestUtil.getIp(request);
    // 获得callback
    String callback = RequestUtil.getParameter(request, "callback");
    // 提交方法
    String m = request.getMethod();
    // 获得path
    String path = request.getPathInfo();
    LOG.debug("request ip={} path={} Method={} scheme={} queryString={}", ip, path, m, request.getScheme(), request.getQueryString());
    if (U.E.isNotEmpty(path)) {
        // 分解提交action 去处开头的/ 并且按/或者_分解出数组
        String actionName = StringUtil.subString(path, 1, path.length());
        String[] actions = StringUtil.contains(actionName, C.S.BACKSLASH) ? StringUtil.split(actionName, C.S.BACKSLASH) : StringUtil.split(actionName, C.S.UNDERLINE);
        if (U.E.isEmpty(actions)) {
            LOG.debug("this path={}", path);
            ResponseUtil.json(response, callback, "action is null path");
            return;
        }
        // 获得Action
        String name = actions[actions.length - 1];
        Object action = null;
        for (int i = actions.length - 2; i >= 0; i--) {
            name = actions[i];
            action = WebCommons.ACTIONS.get(name);
            if (action != null)
                break;
        }
        // action为空
        if (action == null) {
            // 如果使用action[_/]method模式 直接返回
            if (actions.length == 2) {
                LOG.debug("request ip={},path={},no action", ip, path);
                ResponseUtil.json(response, callback, "no action");
                return;
            }
            // 查找方法对应action
            action = WebCommons.METHODS_ACTIONS.get(name);
        }
        // action为空
        if (action == null) {
            // 还是为空
            LOG.warn("request ip={},path={},name={},actionName={},ma={},no action and method", ip, path, name, actionName, WebCommons.METHODS_ACTIONS);
            ResponseUtil.json(response, callback, "no action and method");
            return;
        }
        // 过滤IP
        Action a = action.getClass().getAnnotation(Action.class);
        if (WebParams.IPS || a.ips() || action.getClass().isAnnotationPresent(Ips.class)) {
            // 如果在允许列表继续 否则退出
            if (!IpUtil.contains(ip)) {
                LOG.debug("this ip={}", ip);
                ResponseUtil.json(response, callback, "not exist ip");
                return;
            }
        }
        // 获得方法
        Map<String, Method> methods = WebCommons.ACTIONS_METHODS.get(name);
        if (U.E.isEmpty(methods))
            methods = WebCommons.METHODS;
        Method method = methods.get(actions[actions.length - 1]);
        if (method == null) {
            LOG.debug("request ip={},path={},no method", ip, path);
            ResponseUtil.json(response, callback, "no method");
            return;
        }
        // 校验是否只使用post方法提交
        if (method.isAnnotationPresent(Post.class) && !StringUtil.equals("POST", m.toUpperCase())) {
            ResponseUtil.json(response, callback, "no method is " + m);
            return;
        }
        // 校验是否只使用get方法提交
        if (method.isAnnotationPresent(Get.class) && !StringUtil.equals("GET", m.toUpperCase())) {
            ResponseUtil.json(response, callback, "no method is " + m);
            return;
        }
        // }
        // 设置参数
        Parameter[] pars = WebCommons.METHODS_PARAMES.get(method);
        Object[] params = null;
        // 所有提交的参数
        Map<String, String> ps = RequestUtil.getAll(request);
        LOG.debug("action={} params={}", actionName, ps);
        // 验证
        int code = Validators.validator(method, action, ps, ip);
        if (U.E.isNotEmpty(pars)) {
            // 参数不为空 设置参数
            params = new Object[pars.length];
            // ip没有传 注入当前客户端IP
            if (U.E.isEmpty(ps.get("ip")))
                ps.put("ip", ip);
            // 当前时间time没有传注入time
            if (U.E.isEmpty(ps.get("time")))
                ps.put("time", W.C.toString(DateUtil.getTime()));
            LOG.trace("request all ip={} params={}", ip, params);
            // token验证通过在执行
            // if (code == StateParams.SUCCESS) {
            // action全部参数下标
            int i = 0;
            for (; i < pars.length; i++) {
                // 判断类型并设置
                Parameter p = pars[i];
                // 获得参数值
                String v = ps.get(p.getName());
                // 参数的类型
                Class<?> cs = p.getType();
                if (HttpServletRequest.class.equals(cs))
                    params[i] = request;
                else if (HttpServletResponse.class.equals(cs))
                    params[i] = response;
                else if (TokenBean.class.equals(cs))
                    // 设置Token
                    params[i] = TokenEngine.decrypt(v);
                else if (Map.class.equals(cs))
                    params[i] = ps;
                else if (cs.isArray())
                    params[i] = U.A.array(v, cs);
                else if (ClassUtil.isBaseType(cs)) {
                    // 获得参数
                    params[i] = W.C.to(v, cs);
                    // 验证参数
                    if (code == StateParams.SUCCESS)
                        if ((code = Validators.validator(p, params[i])) != StateParams.SUCCESS)
                            break;
                } else {
                    // 设置属性
                    params[i] = BeanUtil.copy(ps, cs);
                    // 验证参数
                    if (code == StateParams.SUCCESS)
                        if ((code = Validators.validator(params[i])) != StateParams.SUCCESS)
                            break;
                }
            }
        // }
        }
        // try {
        if (code == StateParams.SUCCESS) {
            // 判断是否异步
            if (a.async() || method.isAnnotationPresent(Asyn.class) || action.getClass().isAnnotationPresent(Asyn.class)) {
                // 获得异步全局
                AsyncContext async = request.startAsync();
                Object ac = action;
                Object[] p = params;
                // 异步执行
                async.start(() -> {
                    // 执行方法并返回结果
                    result(method, ac, invoke(ac, method, p, request, response), callback, request, response, ip, actionName, p, pars, curr);
                    // 通知主线程完成
                    async.complete();
                });
            // // 异步处理
            // ExecutorUtil.pool("async").execute(() -> {
            // // 执行方法并返回结果
            // try {
            // result(method, ac, invoke(ac, method, p, request, response), callback, request, response, ip, actionName, p, pars, curr);
            // } catch (Exception e) {
            // Logs.error(e);
            // }
            // // 通知主线程完成
            // async.complete();
            // });
            } else
                result(method, action, invoke(action, method, params, request, response), callback, request, response, ip, actionName, params, pars, curr);
        } else
            result(method, action, StateCode.build(code), callback, request, response, ip, actionName, params, pars, curr);
    }
}
Also used : Action(com.weicoder.web.annotation.Action) Post(com.weicoder.web.annotation.Post) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) Method(java.lang.reflect.Method) Ips(com.weicoder.web.annotation.Ips) Get(com.weicoder.web.annotation.Get) Asyn(com.weicoder.common.annotation.Asyn) Parameter(java.lang.reflect.Parameter) Map(java.util.Map)

Aggregations

Asyn (com.weicoder.common.annotation.Asyn)1 Action (com.weicoder.web.annotation.Action)1 Get (com.weicoder.web.annotation.Get)1 Ips (com.weicoder.web.annotation.Ips)1 Post (com.weicoder.web.annotation.Post)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Map (java.util.Map)1 AsyncContext (javax.servlet.AsyncContext)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1