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

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

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

import cn.net.wanmo.common.util.DateUtil;

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

/**
 * 请求体
 */
public abstract class BaseReq implements Serializable {
    /**
     * 请求头
     */
    protected Map headers = new HashMap<>();
    /**
     * 请求数据
     */
    protected Map params = new HashMap<>();
    /**
     * 请求体
     */
    protected String body;
    /**
     * 请求文件
     */
    protected ReqFile file;
    /**
     * 请求时间(单位:毫秒)
     */
    protected Long reqTime;
    /**
     * 是否需要加密
     */
    protected boolean encrypt;
    /**
     * 加密所需的参数
     */
    protected Map encryptParams;
    /**
     * 是否 URL 编码
     */
    protected Boolean isUrlEncode;
    /**
     * 是否下载
     */
    protected boolean download;

    /**
     * 默认请求体无需加密
     */
    public BaseReq() {
        this.encrypt = false;
        reqTime = DateUtil.nowLong();
    }

    public Map getHeaders() {
        return headers;
    }

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

    public Map getParams() {
        return params;
    }

    public void setParams(Map params) {
        this.params = params;
    }

    public String getBody() {
        return body;
    }

    /**
     * 设置请求体,如果需要加密,需重写 encryptBody(body) 进行加密处理
     *
     * @param body
     */
    public void setBody(String body) {
        this.body = body;

        if (isEncrypt()) {
            this.body = encryptBody(body);
        }
    }

    public ReqFile getFile() {
        return file;
    }

    public void setFile(ReqFile file) {
        this.file = file;
    }


    public Long getReqTime() {
        return reqTime;
    }

    public void setReqTime(Long reqTime) {
        this.reqTime = reqTime;
    }

    public boolean isEncrypt() {
        return encrypt;
    }

    public void setEncrypt(boolean encrypt) {
        this.encrypt = encrypt;
    }

    public Map getEncryptParams() {
        return encryptParams;
    }

    public void setEncryptParams(Map encryptParams) {
        this.encryptParams = encryptParams;
    }

    /**
     * 将请求体加密
* 构造上加密后的可用的请求体 * * @param body 待加密数据 * @return 加密后的数据 */ public String encryptBody(String body) { this.body = body; return body; } public Boolean getUrlEncode() { return isUrlEncode; } public void setUrlEncode(Boolean urlEncode) { isUrlEncode = urlEncode; } public boolean isDownload() { return download; } public void setDownload(boolean download) { this.download = download; } @Override public String toString() { return super.toString(); } /** * 将请求数据转为请求体 */ public abstract String toBody(); public ReqFile newReqFile(String name, File file) { return new ReqFile(name, file); } public ReqFile newReqFile(String name, File file, String filename) { return new ReqFile(name, file, filename); } public class ReqFile { private String name; private File file; private String filename; public ReqFile(String name, File file) { this.name = name; this.file = file; this.filename = file.getName(); } public ReqFile(String name, File file, String filename) { this(name, file); this.filename = filename; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy