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 +++++++++++++++++++++ .../eaaf/core/test/http/HttpClientFactoryTest.java | 26 ++++++++++++++++------ 2 files changed, 44 insertions(+), 7 deletions(-) (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 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. * diff --git a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java index c88e05d5..baedadc8 100644 --- a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java +++ b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java @@ -1,5 +1,6 @@ package at.gv.egiz.eaaf.core.test.http; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetAddress; @@ -10,11 +11,13 @@ import java.security.Provider; import java.security.cert.X509Certificate; import org.apache.commons.lang3.RandomStringUtils; +import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.junit.After; import org.junit.Assert; @@ -30,8 +33,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory; import at.gv.egiz.eaaf.core.impl.data.Pair; +import at.gv.egiz.eaaf.core.impl.data.Triple; import at.gv.egiz.eaaf.core.impl.http.HttpClientConfiguration; +import at.gv.egiz.eaaf.core.impl.http.HttpUtils; import at.gv.egiz.eaaf.core.impl.http.IHttpClientFactory; +import at.gv.egiz.eaaf.core.impl.utils.StreamUtils; import okhttp3.HttpUrl; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; @@ -238,8 +244,9 @@ public class HttpClientFactoryTest { //request webservice final HttpUriRequest httpGet1 = new HttpPost(mockServerUrl.url().toString()); - final CloseableHttpResponse httpResp1 = client.execute(httpGet1); - Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); + final StatusLine httpResp1 = client.execute(httpGet1, + HttpUtils.simpleStatusCodeResponseHandler()); + Assert.assertEquals("http statusCode", 200, httpResp1.getStatusCode()); } @@ -252,21 +259,26 @@ public class HttpClientFactoryTest { final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + + String bodyData = RandomStringUtils.randomAlphanumeric(10); mockWebServer.enqueue(new MockResponse().setResponseCode(200) - .setBody("GetData")); + .setBody(bodyData)); //request webservice final HttpUriRequest httpGet1 = new HttpGet(mockServerUrl.url().toString()); - final CloseableHttpResponse httpResp1 = client.execute(httpGet1); - Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); - + final Triple httpResp1 = client.execute(httpGet1, + HttpUtils.bodyStatusCodeResponseHandler()); + Assert.assertEquals("http statusCode", 200, httpResp1.getFirst().getStatusCode()); + Assert.assertEquals("http statusCode", bodyData, new String(StreamUtils.readStream(httpResp1.getSecond()))); + + } @Test -- cgit v1.2.3