
org.catools.ws.model.CHttpResponse Maven / Gradle / Ivy
package org.catools.ws.model;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.catools.common.collections.CHashMap;
import org.catools.common.collections.interfaces.CMap;
import org.catools.common.extensions.types.CNumberExtension;
import org.catools.common.extensions.types.CObjectExtension;
import org.catools.common.extensions.types.CStringExtension;
import org.catools.common.io.CStreamUtil;
import org.catools.common.json.CJsonUtil;
import org.catools.ws.enums.CHttpStatusCode;
import org.catools.ws.session.CSession;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Locale;
/**
* Response with all information returns from http server after call
*/
public class CHttpResponse {
private String protocol;
private CHttpStatusCode statusCode;
private String reasonPhrase;
private Locale locale;
private byte[] content;
private long contentLength;
public CHttpResponse(CSession session, CloseableHttpResponse response) {
try {
this.Session = session;
if (response != null) {
if (response.getProtocolVersion() != null) {
this.protocol = response.getProtocolVersion().getProtocol();
}
if (response.getStatusLine() != null) {
this.statusCode = CHttpStatusCode.getByCode(response.getStatusLine().getStatusCode());
this.reasonPhrase = response.getStatusLine().getReasonPhrase();
}
this.locale = response.getLocale();
for (Header h : response.getAllHeaders()) {
this.Headers.add(new CResponseHeader(h));
}
}
if (response != null && response.getEntity() != null) {
HttpEntity entity = response.getEntity();
this.ContentType = new CResponseHeader(entity.getContentType());
this.EntityEncoding = new CResponseHeader(entity.getContentEncoding());
this.ContentMap = new CHashMap<>();
this.contentLength = entity.getContentLength();
this.content = CStreamUtil.toByteArray(entity.getContent());
try {
ContentMap.putAll(new JSONObject(new String(content)).toMap());
} catch (JSONException e) {
ContentMap.put("content", new String(content));
}
} else {
this.content = new byte[0];
this.contentLength = 0;
this.ContentType = new CResponseHeader();
this.EntityEncoding = new CResponseHeader();
this.ContentMap = new CHashMap<>();
}
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
try {
response.close();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
//Extensions
public final CStringExtension Protocol = new CStringExtension() {
@Override
public String getBaseValue() {
return protocol;
}
};
public final CObjectExtension StatusCode = new CObjectExtension<>() {
@Override
public CHttpStatusCode getBaseValue() {
return statusCode;
}
};
public final CStringExtension ReasonPhrase = new CStringExtension() {
@Override
public String getBaseValue() {
return reasonPhrase;
}
};
public final CSession Session;
public final CResponseHeaders Headers = new CResponseHeaders();
public final CResponseHeader ContentType;
public final CResponseHeader EntityEncoding;
public final CStringExtension Content = new CStringExtension() {
@Override
public String getBaseValue() {
return new String(content);
}
};
public final CMap ContentMap;
public final CNumberExtension Length = new CNumberExtension<>() {
@Override
public Long getBaseValue() {
return contentLength;
}
};
//Methods
public Locale getLocale() {
return locale;
}
public JSONObject getJsonContent() {
return new JSONObject(Content.getBaseValue());
}
public JSONArray getJsonArrayContent() {
return new JSONArray(Content.getBaseValue());
}
public String getContentString() {
return Content.getBaseValue();
}
public T getContent(Class clazz) {
return CJsonUtil.read(Content.getBaseValue(), clazz);
}
public T getContent(TypeReference clazz) {
return CJsonUtil.read(Content.getBaseValue(), clazz);
}
@Override
public String toString() {
return "CHttpResponse{" +
"protocol='" + protocol + '\'' +
", statusCode=" + statusCode +
", reasonPhrase='" + reasonPhrase + '\'' +
", locale=" + locale +
", contentLength=" + contentLength +
", Headers=" + Headers +
", content=" + getContentString() +
'}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy