From e75b9242e1c684343b3115d0b527ef0ef8073573 Mon Sep 17 00:00:00 2001 From: Ryan Amari Date: Mon, 4 Nov 2024 11:22:45 -0500 Subject: [PATCH] Use EntityUtils over IoUtils --- .../edu/harvard/dbmi/avillach/AggregateResourceIT.java | 2 +- .../java/edu/harvard/dbmi/avillach/IRCTResourceIT.java | 2 +- .../edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java | 2 +- .../java/edu/harvard/dbmi/avillach/SystemServiceIT.java | 2 +- .../harvard/hms/dbmi/avillach/PicSureInitializer.java | 4 ++-- .../edu/harvard/hms/dbmi/avillach/GA4GHResourceRS.java | 3 ++- .../edu/harvard/hms/dbmi/avillach/HttpClientUtil.java | 5 +++-- .../dbmi/avillach/service/ResourceWebClientTest.java | 2 ++ .../edu/harvard/dbmi/avillach/util/HttpClientUtil.java | 9 +++++---- 9 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java index 1138fc44..7730c14d 100644 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java +++ b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java @@ -145,7 +145,7 @@ public void testQuery() throws IOException { //Should throw an error if credentials missing or wrong System.out.println("401 URL: " + endpointUrl+"/query" + "|headers: " + headers + "|body: " + body); HttpResponse response = retrievePostResponse(endpointUrl+"/query", headers, body); -// System.out.println("Test Response: " + IOUtils.toString(response.getEntity().getContent(), "UTF-8")); +// System.out.println("Test Response: " + EntityUtils.toString(response.getEntity().getContent(), "UTF-8")); assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); assertNotNull("Response message should not be null", responseMessage); diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java index 97f85611..3cd9dd09 100644 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java +++ b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java @@ -408,7 +408,7 @@ public void testQueryResult() throws UnsupportedOperationException, IOException body = objectMapper.writeValueAsString(queryRequest); response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/result", headers, body); assertEquals("Correct request should return a 200",200, response.getStatusLine().getStatusCode()); - String responseBody = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + String responseBody = EntityUtils.toString(response.getEntity().getContent(), "UTF-8"); assertFalse("Response content should not be empty", responseBody.isEmpty()); } diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java index cae7641e..82977041 100644 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java +++ b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java @@ -150,7 +150,7 @@ public void testResult() throws Exception { //This should return some kind of result response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(resultRequest)); assertEquals("Correct request should return a 200", 200, response.getStatusLine().getStatusCode()); - String result = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + String result = EntityUtils.toString(response.getEntity().getContent(), "UTF-8"); assertNotNull("Result should not be null, result"); //Nonexistent resultId diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java index eb021d7e..3a578622 100755 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java +++ b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java @@ -22,7 +22,7 @@ public void testStatusIsAccessibleToSystemUser() throws Exception { get.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ jwt); HttpResponse response = client.execute(get); assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - assertEquals("System status should be RUNNING'", "RUNNING", IOUtils.toString(response.getEntity().getContent(), "UTF-8")); + assertEquals("System status should be RUNNING'", "RUNNING", EntityUtils.toString(response.getEntity().getContent(), "UTF-8")); }catch(Exception e) { fail("Exception: " + e.getMessage()); diff --git a/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java b/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java index a9e1dfa7..61be7f17 100644 --- a/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java +++ b/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java @@ -75,8 +75,8 @@ private static void parseCommandLine(String[] args) throws ParseException, FileN printHelpAndExit(); } url = cmd.getOptionValue("p"); - users = IOUtils.toString(new FileInputStream(cmd.getOptionValue("u")), "UTF-8"); - resources = IOUtils.toString(new FileInputStream(cmd.getOptionValue("r")), "UTF-8"); + users = EntityUtils.toString(new FileInputStream(cmd.getOptionValue("u")), "UTF-8"); + resources = EntityUtils.toString(new FileInputStream(cmd.getOptionValue("r")), "UTF-8"); token = cmd.getOptionValue("t"); } diff --git a/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/GA4GHResourceRS.java b/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/GA4GHResourceRS.java index d83935c5..b7d6aef0 100644 --- a/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/GA4GHResourceRS.java +++ b/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/GA4GHResourceRS.java @@ -22,6 +22,7 @@ import edu.harvard.dbmi.avillach.service.IResourceRS; import org.apache.http.message.BasicHeader; +import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -278,7 +279,7 @@ public QueryStatus query(QueryRequest queryJson) { // If the HTTP Response is a success, then returns an object like so: {"resultId":230464} //TODO later Add things like duration and expiration try { - String responseBody = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); JsonNode responseNode = json.readTree(responseBody); long endtime = new Date().getTime(); diff --git a/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java b/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java index e6320fb4..2353ad26 100644 --- a/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java +++ b/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import edu.harvard.dbmi.avillach.util.exception.ResourceCommunicationException; +import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +57,7 @@ public static HttpResponse retrievePostResponse(String uri, String token) { public static List readListFromResponse(HttpResponse response, Class expectedElementType) { try { - String responseBody = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); return json.readValue(responseBody, new TypeReference>() {}); } catch (IOException e) { e.printStackTrace(); @@ -66,7 +67,7 @@ public static List readListFromResponse(HttpResponse response, Class e public static List readDataObjectsFromResponse(HttpResponse response, Class expectedElementType) { try { - String responseBody = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); // Get only the data_objects field from the returned structure. Ugly, but has to de- and then re-serialize JsonNode jn = json.readTree(responseBody); if (null == jn.get("data_objects")) { diff --git a/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java b/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java index bac8e462..be0f51d6 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java +++ b/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java @@ -8,6 +8,8 @@ import edu.harvard.dbmi.avillach.util.exception.ApplicationException; import edu.harvard.dbmi.avillach.util.exception.ProtocolException; import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.util.EntityUtils; import org.glassfish.jersey.internal.RuntimeDelegateImpl; import org.junit.BeforeClass; import org.junit.Rule; diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java index cc381759..0f5cad7e 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java @@ -33,6 +33,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.ssl.SSLContexts; +import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -131,7 +132,7 @@ public HttpResponse retrievePostResponse(String uri, List
headers, Strin public List readListFromResponse(HttpResponse response, Class expectedElementType) { logger.debug("HttpClientUtil readListFromResponse()"); try { - String responseBody = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); return json.readValue(responseBody, new TypeReference>() { }); } catch (IOException e) { @@ -142,7 +143,7 @@ public List readListFromResponse(HttpResponse response, Class expected public String readObjectFromResponse(HttpResponse response) { logger.debug("HttpClientUtil readObjectFromResponse(HttpResponse response)"); try { - String responseBody = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); logger.debug("readObjectFromResponse() responseBody {}", responseBody); return responseBody; } catch (IOException e) { @@ -154,9 +155,9 @@ public static T readObjectFromResponse(HttpResponse response, Class expec logger.debug("HttpClientUtil readObjectFromResponse()"); try { long startTime = System.nanoTime(); - String responseBody = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); logger.debug( - "readObjectFromResponse() line: IOUtils.toString(response.getEntity().getContent(), \"UTF-8\"), took {}", + "readObjectFromResponse() line: EntityUtils.toString(response.getEntity().getContent(), \"UTF-8\"), took {}", (System.nanoTime() - startTime)); logger.trace("readObjectFromResponse() responseBody {}", responseBody);