2020-06-18 12:55:34 +02:00
|
|
|
package org.gcube.event.publisher;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
2020-06-24 09:52:51 +02:00
|
|
|
import java.net.MalformedURLException;
|
2020-06-18 12:55:34 +02:00
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import org.gcube.oidc.rest.JWTToken;
|
|
|
|
import org.gcube.oidc.rest.OpenIdConnectRESTHelper;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
public class HTTPEventSender implements EventSender {
|
|
|
|
|
|
|
|
protected static final Logger log = LoggerFactory.getLogger(HTTPEventSender.class);
|
|
|
|
|
|
|
|
private URL endpoint;
|
|
|
|
private String clientId;
|
|
|
|
private String clientSecret;
|
|
|
|
private URL tokenURL;
|
|
|
|
|
|
|
|
public HTTPEventSender(URL endpointURL, String clientId, String clientSecret, URL tokenURL) {
|
|
|
|
this.endpoint = endpointURL;
|
|
|
|
this.clientId = clientId;
|
|
|
|
this.clientSecret = clientSecret;
|
|
|
|
this.tokenURL = tokenURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-06-23 13:09:44 +02:00
|
|
|
public void send(Event event) {
|
2020-06-18 12:55:34 +02:00
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Starting HTTP POST thread to: " + endpoint);
|
|
|
|
}
|
2020-06-24 09:52:51 +02:00
|
|
|
try {
|
|
|
|
URL eventEndpoint = new URL(endpoint, event.getName());
|
|
|
|
new Thread(new HTTPost(eventEndpoint, event.toJSONString())).start();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
log.error("Cannot compute event endpoint URL. Event name: " + event.getName() + ", base endpoint: "
|
|
|
|
+ endpoint, e);
|
|
|
|
}
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class HTTPost implements Runnable {
|
|
|
|
|
2020-06-25 11:41:04 +02:00
|
|
|
private static final int CONNECTION_TIMEOUT = 10000;
|
|
|
|
private static final int READ_TIMEOUT = 5000;
|
|
|
|
|
2020-06-24 09:52:51 +02:00
|
|
|
private URL endpoint;
|
|
|
|
private String jsonString;
|
2020-06-18 12:55:34 +02:00
|
|
|
|
2020-06-24 09:52:51 +02:00
|
|
|
public HTTPost(URL endpoint, String jsonString) {
|
2020-06-25 11:41:04 +02:00
|
|
|
this.endpoint = endpoint;
|
2020-06-24 09:52:51 +02:00
|
|
|
this.jsonString = jsonString;
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Getting token for client: " + clientId);
|
|
|
|
}
|
|
|
|
JWTToken token = OpenIdConnectRESTHelper.queryClientToken(clientId, clientSecret, tokenURL);
|
|
|
|
if (log.isTraceEnabled()) {
|
|
|
|
log.trace("Performing HTTP POST");
|
|
|
|
}
|
2020-06-25 11:41:04 +02:00
|
|
|
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
|
|
|
|
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
|
|
|
if (log.isTraceEnabled()) {
|
|
|
|
log.trace("HTTP connection timeout set to: " + connection.getConnectTimeout());
|
|
|
|
}
|
|
|
|
connection.setReadTimeout(READ_TIMEOUT);
|
|
|
|
if (log.isTraceEnabled()) {
|
|
|
|
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();
|
2020-06-18 12:55:34 +02:00
|
|
|
if (log.isTraceEnabled()) {
|
|
|
|
log.trace("Sending event JSON");
|
|
|
|
}
|
2020-06-24 09:52:51 +02:00
|
|
|
os.write(jsonString.getBytes("UTF-8"));
|
2020-06-18 12:55:34 +02:00
|
|
|
os.close();
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2020-06-25 11:41:04 +02:00
|
|
|
int httpResultCode = connection.getResponseCode();
|
2020-06-18 12:55:34 +02:00
|
|
|
if (log.isTraceEnabled()) {
|
|
|
|
log.trace("HTTP Response code: " + httpResultCode);
|
|
|
|
}
|
|
|
|
if (log.isTraceEnabled()) {
|
|
|
|
log.trace("Reading response");
|
|
|
|
}
|
|
|
|
boolean ok = true;
|
|
|
|
InputStreamReader isr = null;
|
|
|
|
if (httpResultCode == HttpURLConnection.HTTP_OK) {
|
2020-06-25 11:41:04 +02:00
|
|
|
isr = new InputStreamReader(connection.getInputStream(), "UTF-8");
|
2020-06-18 12:55:34 +02:00
|
|
|
} else {
|
|
|
|
ok = false;
|
2020-06-25 11:41:04 +02:00
|
|
|
isr = new InputStreamReader(connection.getErrorStream(), "UTF-8");
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
String line = null;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
sb.append(line + "\n");
|
|
|
|
}
|
|
|
|
br.close();
|
|
|
|
if (ok) {
|
|
|
|
if (log.isDebugEnabled()) {
|
2020-06-24 18:49:50 +02:00
|
|
|
log.debug("[" + httpResultCode + "] Event publish OK. Results: " + sb.toString());
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-24 18:49:50 +02:00
|
|
|
log.warn("[" + httpResultCode + "] Event publish is not OK. Results: " + sb.toString());
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("POSTing JSON to: " + endpoint, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 09:52:51 +02:00
|
|
|
|
|
|
|
}
|