From e0349ae2e7460bb679c114a54d9be053199aaeae Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Wed, 20 Dec 2023 11:34:24 +0100 Subject: feat(core): add one more default implementation of HttpClientResponseHandler --- .../at/gv/egiz/eaaf/core/impl/http/HttpUtils.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'eaaf_core_utils') 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; @@ -101,6 +102,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> stringBodyStatusCodeResponseHandler() { + return new HttpClientResponseHandler>() { + @Override + public Triple 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. * -- cgit v1.2.3