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

cn.net.wanmo.common.http.pojo.ResData Maven / Gradle / Ivy

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

import cn.net.wanmo.common.util.CollectionUtil;
import cn.net.wanmo.common.util.MapUtil;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HTTP 请求的响应数据
 */
public class ResData implements Serializable {

    public static ResData build(String body, Map> headers) {
        return new ResData(body, headers);
    }

    public static ResData build(File file, Map> headers) {
        return new ResData(file, headers);
    }


    // -----------------------------------------------------------
    /**
     * 如果响应的是字符串
     */
    private String body;
    /**
     * 将响应的字符串解析为对象
     */
    private Obj obj;
    /**
     * 如果响应的是文件
     */
    private File file;
    /**
     * 响应头信息
     */
    private Map> headers = new HashMap<>();

    private ResData() {
    }

    private ResData(String body) {
        this();
        this.body = body;
    }

    public ResData(File file) {
        this();
        this.file = file;
    }

    public ResData(String body, Map> headers) {
        this(body);
        this.headers = headers;
    }

    public ResData(File file, Map> headers) {
        this(file);
        this.headers = headers;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Obj getObj() {
        return obj;
    }

    public ResData setObj(Obj obj) {
        this.obj = obj;
        return this;
    }

    public File getFile() {
        return file;
    }

    public ResData setFile(File file) {
        this.file = file;
        return this;
    }

    public Map> getHeaders() {
        return headers;
    }

    public void setHeaders(Map> headers) {
        this.headers = headers;
    }


    /**
     * 从响应头中获取信息
     *
     * @param key 响应头 key
     * @return 响应头 value
     */
    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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy