Skip to content

Commit

Permalink
Merge pull request #376 from europeana/EA-3700-http-response-logging
Browse files Browse the repository at this point in the history
logging http responses
  • Loading branch information
gsergiu authored Apr 25, 2024
2 parents cb053c7 + bd80883 commit 3c2c603
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
Expand All @@ -27,6 +30,7 @@ public class HttpConnection {
private HttpClient httpClient;
private int connectionRetries;
private int connectionTimeout;
Logger logger = LogManager.getLogger(getClass().getName());

/**
* Constructor with explicit indication of retries and timeout
Expand Down Expand Up @@ -69,7 +73,11 @@ public String getURLContentAsString(String url, String headerName, String header
if (get.getStatusCode() >= STATUS_OK_START && get.getStatusCode() <= STATUS_OK_END) {
return get.getResponseBodyAsString();
} else {
return null;
if(logger.isWarnEnabled()) {
logger.warn("Received Status Code: {}, and Response Body: {}, for the url: {}",
get.getStatusCode(), get.getResponseBodyAsString(), url);
}
return null;
}
}

Expand All @@ -88,7 +96,11 @@ public InputStream getURLContentAsStream(String url) throws IOException {
if (get.getStatusCode() >= STATUS_OK_START && get.getStatusCode() <= STATUS_OK_END) {
return get.getResponseBodyAsStream();
} else {
return null;
if(logger.isWarnEnabled()) {
logger.warn("Received Status Code: {}, and Response Body: {}, for the url: {}",
get.getStatusCode(), get.getResponseBodyAsString(), url);
}
return null;
}
}

Expand All @@ -111,7 +123,11 @@ public InputStream postRequest(String url, String body) throws IOException {
if (post.getStatusCode() >= STATUS_OK_START && post.getStatusCode() <= STATUS_OK_END) {
return post.getResponseBodyAsStream();
} else {
return null;
if(logger.isWarnEnabled()) {
logger.warn("Received Status Code: {}, and Response Body: {}, for the url: {}",
post.getStatusCode(), post.getResponseBodyAsString(), url);
}
return null;
}
}

Expand Down

0 comments on commit 3c2c603

Please sign in to comment.