Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

add optional timeout parameter to sendHttp*Request #2565

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* for sending HTTP requests
*
* @author Kai Kreuzer - Initial contribution and API
* @author Jan N. Klug - add timeout methods
*
*/
public class HTTP {
Expand All @@ -29,15 +30,26 @@ public class HTTP {
private static Logger logger = LoggerFactory.getLogger(HTTP.class);

/**
* Send out a GET-HTTP request. Errors will be logged, returned values just ignored.
* Send out a GET-HTTP request. Errors will be logged, success returns response
*
* @param url the URL to be used for the GET request.
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpGetRequest(String url) {
return sendHttpGetRequest(url, 5000);
}

/**
* Send out a GET-HTTP request. Errors will be logged, success returns response
*
* @param url the URL to be used for the GET request.
* @param timeout timeout in ms
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpGetRequest(String url, int timeout) {
String response = null;
try {
return HttpUtil.executeUrl("GET", url, 5000);
return HttpUtil.executeUrl("GET", url, timeout);
} catch (IOException e) {
logger.error("Fatal transport error: {}", e);
}
Expand All @@ -51,9 +63,20 @@ static public String sendHttpGetRequest(String url) {
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPutRequest(String url) {
return sendHttpPutRequest(url, 1000);
}

/**
* Send out a PUT-HTTP request. Errors will be logged, returned values just ignored.
*
* @param url the URL to be used for the PUT request.
* @param timeout timeout in ms
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPutRequest(String url, int timeout) {
String response = null;
try {
response = HttpUtil.executeUrl("PUT", url, 1000);
response = HttpUtil.executeUrl("PUT", url, timeout);
} catch (IOException e) {
logger.error("Fatal transport error: {}", e);
}
Expand All @@ -70,9 +93,23 @@ static public String sendHttpPutRequest(String url) {
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPutRequest(String url, String contentType, String content) {
return sendHttpPutRequest(url, contentType, content, 1000);
}

/**
* Send out a PUT-HTTP request. Errors will be logged, returned values just ignored.
*
* @param url the URL to be used for the PUT request.
* @param contentType the content type of the given <code>content</code>
* @param content the content to be send to the given <code>url</code> or <code>null</code> if no content should be
* send.
* @param timeout timeout in ms
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPutRequest(String url, String contentType, String content, int timeout) {
String response = null;
try {
response = HttpUtil.executeUrl("PUT", url, IOUtils.toInputStream(content), contentType, 1000);
response = HttpUtil.executeUrl("PUT", url, IOUtils.toInputStream(content), contentType, timeout);
} catch (IOException e) {
logger.error("Fatal transport error: {}", e);
}
Expand All @@ -86,9 +123,20 @@ static public String sendHttpPutRequest(String url, String contentType, String c
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPostRequest(String url) {
return sendHttpPostRequest(url, 1000);
}

/**
* Send out a POST-HTTP request. Errors will be logged, returned values just ignored.
*
* @param url the URL to be used for the POST request.
* @param timeout timeout in ms
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPostRequest(String url, int timeout) {
String response = null;
try {
response = HttpUtil.executeUrl("POST", url, 1000);
response = HttpUtil.executeUrl("POST", url, timeout);
} catch (IOException e) {
logger.error("Fatal transport error: {}", e);
}
Expand All @@ -105,9 +153,23 @@ static public String sendHttpPostRequest(String url) {
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPostRequest(String url, String contentType, String content) {
return sendHttpPostRequest(url, contentType, content, 1000);
}

/**
* Send out a POST-HTTP request. Errors will be logged, returned values just ignored.
*
* @param url the URL to be used for the POST request.
* @param contentType the content type of the given <code>content</code>
* @param content the content to be send to the given <code>url</code> or <code>null</code> if no content should be
* send.
* @param timeout timeout in ms
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpPostRequest(String url, String contentType, String content, int timeout) {
String response = null;
try {
response = HttpUtil.executeUrl("POST", url, IOUtils.toInputStream(content), contentType, 1000);
response = HttpUtil.executeUrl("POST", url, IOUtils.toInputStream(content), contentType, timeout);
} catch (IOException e) {
logger.error("Fatal transport error: {}", e);
}
Expand All @@ -121,9 +183,20 @@ static public String sendHttpPostRequest(String url, String contentType, String
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpDeleteRequest(String url) {
return sendHttpDeleteRequest(url, 1000);
}

/**
* Send out a DELETE-HTTP request. Errors will be logged, returned values just ignored.
*
* @param url the URL to be used for the DELETE request.
* @param timeout timeout in ms
* @return the response body or <code>NULL</code> when the request went wrong
*/
static public String sendHttpDeleteRequest(String url, int timeout) {
String response = null;
try {
response = HttpUtil.executeUrl("DELETE", url, 1000);
response = HttpUtil.executeUrl("DELETE", url, timeout);
} catch (IOException e) {
logger.error("Fatal transport error: {}", e);
}
Expand Down