Fixed endpoint not assigned in thread and added connection and read timeout to the HTTP connection
This commit is contained in:
parent
9eb4ad8bf7
commit
9b888a796b
|
@ -44,10 +44,14 @@ public class HTTPEventSender implements EventSender {
|
||||||
|
|
||||||
public class HTTPost implements Runnable {
|
public class HTTPost implements Runnable {
|
||||||
|
|
||||||
|
private static final int CONNECTION_TIMEOUT = 10000;
|
||||||
|
private static final int READ_TIMEOUT = 5000;
|
||||||
|
|
||||||
private URL endpoint;
|
private URL endpoint;
|
||||||
private String jsonString;
|
private String jsonString;
|
||||||
|
|
||||||
public HTTPost(URL endpoint, String jsonString) {
|
public HTTPost(URL endpoint, String jsonString) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
this.jsonString = jsonString;
|
this.jsonString = jsonString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,14 +65,22 @@ public class HTTPEventSender implements EventSender {
|
||||||
if (log.isTraceEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
log.trace("Performing HTTP POST");
|
log.trace("Performing HTTP POST");
|
||||||
}
|
}
|
||||||
HttpURLConnection con = (HttpURLConnection) endpoint.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
|
||||||
con.setDoOutput(true);
|
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||||
con.setDoInput(true);
|
if (log.isTraceEnabled()) {
|
||||||
con.setRequestProperty("Content-Type", "application/json");
|
log.trace("HTTP connection timeout set to: " + connection.getConnectTimeout());
|
||||||
con.setRequestProperty("Accept", "application/json");
|
}
|
||||||
con.setRequestProperty("Method", "POST");
|
connection.setReadTimeout(READ_TIMEOUT);
|
||||||
con.setRequestProperty("Authorization", token.getAsBearer());
|
if (log.isTraceEnabled()) {
|
||||||
OutputStream os = con.getOutputStream();
|
log.trace("HTTP connection Read timeout set to: " + connection.getReadTimeout());
|
||||||
|
}
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.setRequestProperty("Accept", "application/json");
|
||||||
|
connection.setRequestProperty("Method", "POST");
|
||||||
|
connection.setRequestProperty("Authorization", token.getAsBearer());
|
||||||
|
OutputStream os = connection.getOutputStream();
|
||||||
if (log.isTraceEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
log.trace("Sending event JSON");
|
log.trace("Sending event JSON");
|
||||||
}
|
}
|
||||||
|
@ -76,7 +88,7 @@ public class HTTPEventSender implements EventSender {
|
||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
int httpResultCode = con.getResponseCode();
|
int httpResultCode = connection.getResponseCode();
|
||||||
if (log.isTraceEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
log.trace("HTTP Response code: " + httpResultCode);
|
log.trace("HTTP Response code: " + httpResultCode);
|
||||||
}
|
}
|
||||||
|
@ -86,10 +98,10 @@ public class HTTPEventSender implements EventSender {
|
||||||
boolean ok = true;
|
boolean ok = true;
|
||||||
InputStreamReader isr = null;
|
InputStreamReader isr = null;
|
||||||
if (httpResultCode == HttpURLConnection.HTTP_OK) {
|
if (httpResultCode == HttpURLConnection.HTTP_OK) {
|
||||||
isr = new InputStreamReader(con.getInputStream(), "UTF-8");
|
isr = new InputStreamReader(connection.getInputStream(), "UTF-8");
|
||||||
} else {
|
} else {
|
||||||
ok = false;
|
ok = false;
|
||||||
isr = new InputStreamReader(con.getErrorStream(), "UTF-8");
|
isr = new InputStreamReader(connection.getErrorStream(), "UTF-8");
|
||||||
}
|
}
|
||||||
BufferedReader br = new BufferedReader(isr);
|
BufferedReader br = new BufferedReader(isr);
|
||||||
String line = null;
|
String line = null;
|
||||||
|
|
Loading…
Reference in New Issue