cn.net.wanmo.common.send.BaseReq Maven / Gradle / Ivy
package cn.net.wanmo.common.send;
import cn.net.wanmo.common.util.DateUtil;
import java.io.File;
import java.io.Serializable;
import java.util.Map;
/**
* 请求体
*/
public abstract class BaseReq implements Serializable {
/**
* 是否需要加密
*/
protected boolean encrypt;
/**
* 加密所需的参数
*/
protected Map encryptParams;
/**
* 请求体
*/
protected String reqBody;
/**
* 文件
*/
protected ReqFile reqFile;
/**
* 请求时间(单位:毫秒)
*/
protected Long reqTime;
/**
* 默认请求体无需加密
*/
public BaseReq() {
this.encrypt = false;
reqTime = DateUtil.nowLong();
}
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;
}
public String getReqBody() {
return reqBody;
}
/**
* 设置请求体,如果需要加密,需重写 encryptBody(body) 进行加密处理
*
* @param reqBody
*/
public void setReqBody(String reqBody) {
this.reqBody = reqBody;
if (isEncrypt()) {
this.reqBody = encryptBody(reqBody);
}
}
public ReqFile getReqFile() {
return reqFile;
}
public void setReqFile(ReqFile reqFile) {
this.reqFile = reqFile;
}
/**
* 将请求体加密
* 构造上加密后的可用的请求体
*
* @param body 待加密数据
* @return 加密后的数据
*/
public String encryptBody(String body) {
this.reqBody = body;
return body;
}
public Long getReqTime() {
return reqTime;
}
public void setReqTime(Long reqTime) {
this.reqTime = reqTime;
}
@Override
public String toString() {
return toJSONString();
}
/**
* 将 请求数据转为 json 字符串
*/
public abstract String toJSONString();
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;
}
}
}