summaryrefslogtreecommitdiff
path: root/eaaf_core_utils
diff options
context:
space:
mode:
authorThomas <>2023-12-20 11:34:24 +0100
committerThomas <>2023-12-20 11:34:24 +0100
commite0349ae2e7460bb679c114a54d9be053199aaeae (patch)
tree58be8a6febec8be446a9988f496ecb88e2451df2 /eaaf_core_utils
parent96e0c0799e653e832f356b8a465a872cdc02c2c1 (diff)
downloadEAAF-Components-e0349ae2e7460bb679c114a54d9be053199aaeae.tar.gz
EAAF-Components-e0349ae2e7460bb679c114a54d9be053199aaeae.tar.bz2
EAAF-Components-e0349ae2e7460bb679c114a54d9be053199aaeae.zip
feat(core): add one more default implementation of HttpClientResponseHandler
Diffstat (limited to 'eaaf_core_utils')
-rw-r--r--eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java28
1 files changed, 28 insertions, 0 deletions
diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java
index 1b6df3de..4b358adf 100644
--- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java
+++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java
@@ -41,6 +41,7 @@ import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.StatusLine;
@@ -102,6 +103,33 @@ public class HttpUtils {
}
/**
+ * Http response-handler that gives a pair of http status-code, a copy of the
+ * full http-body as {@link InputStream} and the response {@link ContentType}.
+ *
+ * @return {@link Triple} of http response {@link StatusLine}, http body as
+ * {@link InputStream}, and {@link ContentType}
+ */
+ public static HttpClientResponseHandler<Triple<StatusLine, String, ContentType>> stringBodyStatusCodeResponseHandler() {
+ return new HttpClientResponseHandler<Triple<StatusLine, String, ContentType>>() {
+ @Override
+ public Triple<StatusLine, String, ContentType> handleResponse(
+ ClassicHttpResponse response)
+ throws ClientProtocolException, IOException {
+ try {
+ String body = EntityUtils.toString(response.getEntity());
+ return Triple.newInstance(new StatusLine(response), body,
+ ContentType.parse(response.getEntity().getContentType()));
+
+ } catch (ParseException | IOException e) {
+ log.warn("Can not process HTTP response. Return empty body content!", e);
+ return Triple.newInstance(new StatusLine(response), StringUtils.EMPTY,
+ ContentType.parse(response.getEntity().getContentType()));
+ }
+ }
+ };
+ }
+
+ /**
* Helper method to retrieve server URL including context path.
*
* @param request HttpServletRequest