All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.wanmo.common.restful.body.Res Maven / Gradle / Ivy

There is a newer version: 1.3.9
Show newest version
package cn.net.wanmo.common.restful.body;

import cn.net.wanmo.common.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * 响应体
 */
public abstract class Res {
    /**
     * 错误代码
     */
    protected Code errCode;
    /**
     * 错误内容
     */
    protected String errMsg;
    /**
     * 是否需要解密
     */
    protected boolean decrypt;
    /**
     * 解密所需的参数
     */
    protected Map decryptParams;
    /**
     * 待解构的响应体 类型(字符串/对象/数组)
     */
    protected BodyType type;
    /**
     * 待解构的响应体
     */
    protected String body;
    /**
     * 响应头
     */
    protected Map> headers;
    /**
     * 响应时间(单位:毫秒)
     */
    protected Long resTime;
    /**
     * 耗时(单位:毫秒)
     */
    protected Long consumeTime;


    /**
     * 默认响应体无需解密
     */
    public Res() {
        this.type = BodyType.str;
        this.decrypt = false;
        this.resTime = DateUtil.nowLong();
    }

    public Res(Code errCode, String errMsg) {
        this();

        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public Code getErrCode() {
        return ObjectUtil.cast(errCode);
    }

    public void setErrCode(Code errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public Long getConsumeTime() {
        return consumeTime;
    }

    public void setConsumeTime(Long consumeTime) {
        this.consumeTime = consumeTime;
    }

    public boolean isDecrypt() {
        return decrypt;
    }

    public void setDecrypt(boolean decrypt) {
        this.decrypt = decrypt;
    }

    public Map getDecryptParams() {
        return decryptParams;
    }

    public void setDecryptParams(Map decryptParams) {
        this.decryptParams = decryptParams;
    }

    public BodyType getType() {
        return type;
    }

    public void setType(BodyType type) {
        this.type = type;
    }

    public String getBody() {
        return body;
    }

    /**
     * 设置响应体,并处理响应体
* 如果需要解密,则重写 decryptBody(body) 方法 * * @param body */ public void setBody(String body) { this.body = body; if (StringUtil.isBlank(body)) { return; } if (isDecrypt()) { this.body = decryptBody(body); } parse(this.body); } public Map> getHeaders() { return headers; } public void setHeaders(Map> headers) { this.headers = headers; } public String getHeaderField(String key) { if (MapUtil.isEmpty(headers)) { return null; } List list = headers.get(key); if (CollectionUtil.isEmpty(list)) { return null; } return list.get(0); } public Long getResTime() { return resTime; } public void setResTime(Long resTime) { this.resTime = resTime; } /** * 对响应数据进行解密 * * @param body 完整的响应体 * @return 解密后的数据 */ public String decryptBody(String body) { this.body = body; return this.body; } /** * 从接口响应数据中解析 * * @param resBody 接口响应的数据 * @return 解析后的响应 */ public Res parse(String resBody) { switch (type) { case str: this.body = resBody; break; case obj: parse(JSON.parseObject(resBody)); // this.body = null; // 将响应体转为 JSONObject 置为 null break; case arr: parse(JSON.parseArray(resBody)); // this.body = null; // 将响应体转为 JSONObject 置为 null break; default: this.body = body; } return this; } /** * 从接口响应数据中解析 * * @param object 接口响应的数据 * @return 解析后的响应 */ public Res parse(JSONObject object) { return this; } /** * 从接口响应数据中解析 * * @param array 接口响应的数据 * @return 解析后的响应 */ public Res parse(JSONArray array) { return this; } /** * 获取默认的错误码 * * @return 默认错误码 */ public abstract Code defaultErrCode(); /** * 响应是否成功 */ public abstract boolean isSuccess(); public String toJSONString() { return JSON.toJSONString(this); } @Override public String toString() { return toJSONString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy