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

cn.net.wanmo.common.send.BaseRes Maven / Gradle / Ivy

The newest version!
package cn.net.wanmo.common.send;

import cn.net.wanmo.common.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 响应体
 */
public abstract class BaseRes implements Serializable {
    private Logger logger = LoggerFactory.getLogger(BaseRes.class);

    /**
     * 接口标题
     */
    protected String title;
    /**
     * 错误代码
     */
    protected Code errCode;
    /**
     * 错误内容
     */
    protected String errMsg;
    /**
     * 响应头
     */
    protected Map> headers;
    /**
     * 待解构的响应体 类型(字符串/对象/数组)
     */
    protected BodyType bodyType;
    /**
     * 待解构的响应体
     */
    protected String body;
    /**
     * 解析后的数据对象
     */
    protected Data data;
    /**
     * 解析后的数据对象
     */
    protected List dataList = new ArrayList<>();
    /**
     * 是否成功(HTTP)
     */
    protected boolean successHttp;
    /**
     * 是否需要解密
     */
    protected boolean decrypt;
    /**
     * 解密所需的参数
     */
    protected Map decryptParams;
    /**
     * 响应时间(单位:毫秒)
     */
    protected Long resTime;
    /**
     * 耗时(单位:毫秒)
     */
    protected Long consumeTime;

    /**
     * 初始化 data 对象 
* this.data = obj */ protected abstract void initData(); /** * 默认响应体无需解密 */ public BaseRes() { this.bodyType = BodyType.str; this.decrypt = false; this.resTime = DateUtil.nowLong(); initData(); } public BaseRes(Code errCode, String errMsg) { this(); this.errCode = errCode; this.errMsg = errMsg; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } 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 boolean isSuccessHttp() { return successHttp; } public void setSuccessHttp(boolean successHttp) { this.successHttp = successHttp; } 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 getBodyType() { return bodyType; } public void setBodyType(BodyType bodyType) { this.bodyType = bodyType; } 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 Data getData() { return data; } public void setData(Data data) { this.data = data; } public List getDataList() { return dataList; } public void setDataList(List dataList) { this.dataList = dataList; } 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 BaseRes parse(String resBody) { parseCodeMsg(resBody); // 优先解析出错误码和错误描述 if (isSuccessData() == false) { // 只有数据成功响应,才继续解析数据 return this; } switch (bodyType) { case str: parseStr(resBody); break; case obj: parseObj(resBody); break; case arr: parseArr(resBody); break; case file: parseFile(resBody); break; default: logger.warn("未知的响应体类型:{}", bodyType); } return this; } /** * 从接口响应数据中解析 错误码和错误描述 * * @param resBody 接口响应的数据 * @return 解析后的响应 */ public abstract BaseRes parseCodeMsg(String resBody); /** * 从接口响应数据中解析 * * @param resBody 接口响应的数据 * @return 解析后的响应 */ public BaseRes parseStr(String resBody) { return this; } /** * 从接口响应数据中解析 * * @param resBody 接口响应的数据 * @return 解析后的响应 */ public BaseRes parseObj(String resBody) { return this; } /** * 从接口响应数据中解析 * * @param resBody 接口响应的数据 * @return 解析后的响应 */ public BaseRes parseArr(String resBody) { return this; } /** * 从接口响应数据中解析 * * @param resBody 接口响应的数据 * @return 解析后的响应 */ public BaseRes parseFile(String resBody) { return this; } /** * 获取成功码 * * @return 成功码 */ public abstract Code successDataCode(); /** * 数据是否成功 */ public abstract boolean isSuccessData(); /** * 响应是否成功 */ public boolean isSuccess() { return isSuccessHttp() && isSuccessData(); } public abstract String toJSONString(); @Override public String toString() { return super.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy