
org.catools.ws.https.CHttpResponse Maven / Gradle / Ivy
package org.catools.ws.https;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.catools.common.collections.CList;
import org.catools.common.io.CStreamUtil;
import org.catools.common.json.CJsonGenerationException;
import org.catools.common.text.CStringUtil;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.util.Locale;
/**
* Response with all information returns from http server after call
*/
public class CHttpResponse {
private final String protocol;
private final Integer statusCode;
private final String reasonPhrase;
private final Long entityLength;
private final Header entityType;
private final Header entityEncoding;
private final byte[] entityContent;
private final Locale locale;
private final CList headers;
public CHttpResponse(CloseableHttpResponse response) {
try {
if (response.getProtocolVersion() != null) {
this.protocol = response.getProtocolVersion().getProtocol();
} else {
this.protocol = null;
}
if (response.getStatusLine() != null) {
this.statusCode = response.getStatusLine().getStatusCode();
this.reasonPhrase = response.getStatusLine().getReasonPhrase();
} else {
this.statusCode = null;
this.reasonPhrase = null;
}
if (response.getEntity() != null) {
this.entityLength = response.getEntity().getContentLength();
this.entityType = response.getEntity().getContentType();
this.entityEncoding = response.getEntity().getContentEncoding();
try {
this.entityContent = CStreamUtil.toByteArray(response.getEntity().getContent());
} catch (Throwable t) {
throw new RuntimeException(t);
}
} else {
this.entityLength = null;
this.entityType = null;
this.entityEncoding = null;
this.entityContent = null;
}
this.locale = response.getLocale();
this.headers = new CList<>(response.getAllHeaders());
} finally {
try {
response.close();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
public String getProtocol() {
return protocol;
}
public Integer getStatusCode() {
return statusCode;
}
public String getReasonPhrase() {
return reasonPhrase;
}
public Long getEntityLength() {
return entityLength;
}
public Header getEntityType() {
return entityType;
}
public Header getEntityEncoding() {
return entityEncoding;
}
public byte[] getEntityContent() {
return entityContent;
}
public String getEntityContentAsString() {
return new String(entityContent);
}
public JSONObject getEntityContentAsJSONObject() {
try {
return new JSONObject(getEntityContentAsString());
} catch (JSONException e) {
throw new CJsonGenerationException("Cannot Convert content to json.\n" + getEntityContentAsString(), e);
}
}
public Locale getLocale() {
return locale;
}
public CList getHeaders() {
return headers;
}
public String getHeaderValue(String headerName) {
Header firstOrNull = headers.getFirstOrNull(h -> CStringUtil.equalsIgnoreCase(headerName, h.getName()));
if (firstOrNull == null) {
return "";
}
return firstOrNull.getValue();
}
@Override
public String toString() {
return "CHttpResponse{" +
"protocol='" +
protocol +
'\'' +
",\nstatusCode=" +
statusCode +
",\nreasonPhrase='" +
reasonPhrase +
'\'' +
",\nentityLength=" +
entityLength +
",\nentityType=" +
entityType +
",\nentityEncoding=" +
entityEncoding +
",\nentityContent=" +
getEntityContentAsString() +
",\nlocale=" +
locale +
",\nheaders=" +
headers.join("\n") +
'}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy