
net.wicp.tams.common.http.HttpResult Maven / Gradle / Ivy
The newest version!
/*
* **********************************************************************
* Copyright (c) 2022 .
* All rights reserved.
* 项目名称:common
* 项目描述:公共的工具集
* 版权说明:本软件属andy.zhou([email protected])所有。
* ***********************************************************************
*/
package net.wicp.tams.common.http;
import java.io.Serializable;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.Header;
import org.apache.http.StatusLine;
import org.apache.http.protocol.HttpContext;
import net.wicp.tams.common.Result;
public class HttpResult implements Serializable {
private static final long serialVersionUID = 1L;
private HttpContext context;
private byte[] body;
private Header contentEncoding;
private long contentLength;
private Header contentType;
private StatusLine statusLine;
private String errMsg;
private boolean isError;
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.isError = true;
this.errMsg = errMsg;
}
public int getHttpStatus() {
return this.statusLine == null ? 0 : this.statusLine.getStatusCode();
}
public StatusLine getStatusLine() {
return statusLine;
}
public void setStatusLine(StatusLine statusLine) {
this.statusLine = statusLine;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public Header getContentType() {
return contentType;
}
public void setContentType(Header contentType) {
this.contentType = contentType;
}
public HttpContext getContext() {
return context;
}
public void setContext(HttpContext context) {
this.context = context;
}
public byte[] getBody() {
return body;
}
public String getBodyStr() {
try {
return new String(this.body, "UTF-8");
} catch (Exception e) {
return new String(this.body);
}
}
public void setBody(byte[] body) {
this.body = body;
}
public Header getContentEncoding() {
return contentEncoding;
}
public void setContentEncoding(Header contentEncoding) {
this.contentEncoding = contentEncoding;
}
/***
* 通过结构解析器得到结果
*
* @param httpResultParser
* @return
*/
public Result getResult(IHttpResultParser httpResultParser) {
if (isError) {
return Result.getError(errMsg);
} else {
Result suc = httpResultParser != null ? httpResultParser.getObj(getBodyStr()) : Result.getSuc();// 没有解析器,全部设置为成功
if (ArrayUtils.isEmpty(suc.retObjs())) {// httpResultParser也可能有返回值,不能冲掉。不能相加,因为类型不一致
suc.setRetObjs(this);
}
return suc;
}
}
public Result getResult() {
return getResult(null);
}
// 适合于结果是Result.retJsonObj得到的字符串
public Result getResultFromJson() {
return getResult(new IHttpResultParser() {
@Override
public Result getObj(String value) {
return Result.getFromResultJson(value);
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy