From 6be8d86f14011d6437b138f16b2a44062af10139 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Thu, 7 Jan 2021 18:43:44 +0100 Subject: add a second Apache Http-Client response-handler as default implementation --- .../at/gv/egiz/eaaf/core/impl/http/HttpUtils.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'eaaf_core_utils/src/main') 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 10555822..365e969d 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 @@ -18,7 +18,9 @@ package at.gv.egiz.eaaf.core.impl.http; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; @@ -37,6 +39,7 @@ import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.conn.ssl.TrustAllStrategy; +import org.apache.http.entity.ContentType; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.TrustStrategy; @@ -46,6 +49,7 @@ import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider; import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; import at.gv.egiz.eaaf.core.exceptions.EaafFactoryException; import at.gv.egiz.eaaf.core.impl.data.Pair; +import at.gv.egiz.eaaf.core.impl.data.Triple; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -70,6 +74,27 @@ 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 ResponseHandler> + bodyStatusCodeResponseHandler() { + return new ResponseHandler>() { + @Override + public Triple handleResponse(HttpResponse response) + throws ClientProtocolException, IOException { + byte[] bodyBytes = EntityUtils.toByteArray(response.getEntity()); + return Triple.newInstance(response.getStatusLine(), new ByteArrayInputStream(bodyBytes), + ContentType.getOrDefault(response.getEntity())); + + } + }; + } + /** * Helper method to retrieve server URL including context path. * -- cgit v1.2.3