Skip to content

Commit

Permalink
Use EntityUtils over IoUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Nov 4, 2024
1 parent 2614172 commit e75b924
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,7 +57,7 @@ public static HttpResponse retrievePostResponse(String uri, String token) {

public static <T> List<T> readListFromResponse(HttpResponse response, Class<T> 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<List<T>>() {});
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -66,7 +67,7 @@ public static <T> List<T> readListFromResponse(HttpResponse response, Class<T> e

public static <T> List<T> readDataObjectsFromResponse(HttpResponse response, Class<T> 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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -131,7 +132,7 @@ public HttpResponse retrievePostResponse(String uri, List<Header> headers, Strin
public <T> List<T> readListFromResponse(HttpResponse response, Class<T> 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<List<T>>() {
});
} catch (IOException e) {
Expand All @@ -142,7 +143,7 @@ public <T> List<T> readListFromResponse(HttpResponse response, Class<T> 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) {
Expand All @@ -154,9 +155,9 @@ public static <T> T readObjectFromResponse(HttpResponse response, Class<T> 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);

Expand Down

0 comments on commit e75b924

Please sign in to comment.