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;
|
|
|
|
|
2020-07-09 14:33:54 +02:00
|
|
|
public class HTTPWithUMAAuthEventSender implements EventSender {
|
2020-06-18 12:55:34 +02:00
|
|
|
|
2020-07-09 14:33:54 +02:00
|
|
|
protected static final Logger log = LoggerFactory.getLogger(HTTPWithUMAAuthEventSender.class);
|
2020-06-18 12:55:34 +02:00
|
|
|
|
2020-06-29 17:00:57 +02:00
|
|
|
private URL baseEnndpointURL;
|
2020-06-18 12:55:34 +02:00
|
|
|
private String clientId;
|
|
|
|
private String clientSecret;
|
|
|
|
private URL tokenURL;
|
2020-07-09 14:33:54 +02:00
|
|
|
private String umaAudience;
|
|
|
|
|
|
|
|
public HTTPWithUMAAuthEventSender(URL baseEnndpointURL, String clientId, String clientSecret, URL tokenURL,
|
|
|
|
String umaAudience) {
|
2020-06-18 12:55:34 +02:00
|
|
|
|
2020-06-29 17:00:57 +02:00
|
|
|
this.baseEnndpointURL = baseEnndpointURL;
|
2020-06-18 12:55:34 +02:00
|
|
|
this.clientId = clientId;
|
|
|
|
this.clientSecret = clientSecret;
|
|
|
|
this.tokenURL = tokenURL;
|
2020-07-09 14:33:54 +02:00
|
|
|
this.umaAudience = umaAudience;
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-06-23 13:09:44 +02:00
|
|
|
public void send(Event event) {
|
2020-07-03 17:33:57 +02:00
|
|
|
log.debug("Starting HTTP POST thread to: {}", baseEnndpointURL);
|
2020-06-24 09:52:51 +02:00
|
|
|
try {
|
2020-06-29 17:00:57 +02:00
|
|
|
URL eventEndpoint = new URL(baseEnndpointURL, event.getName());
|
2020-11-17 11:44:20 +01:00
|
|
|
new Thread(new HTTPost(eventEndpoint, event)).start();
|
2020-06-24 09:52:51 +02:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
log.error("Cannot compute event endpoint URL. Event name: " + event.getName() + ", base endpoint: "
|
2020-06-29 17:00:57 +02:00
|
|
|
+ baseEnndpointURL, e);
|
2020-06-24 09:52:51 +02:00
|
|
|
}
|
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;
|
2020-11-17 11:44:20 +01:00
|
|
|
private Event event;
|
2020-06-18 12:55:34 +02:00
|
|
|
|
2020-11-17 11:44:20 +01:00
|
|
|
public HTTPost(URL endpoint, Event event) {
|
2020-06-25 11:41:04 +02:00
|
|
|
this.endpoint = endpoint;
|
2020-11-17 11:44:20 +01:00
|
|
|
this.event = event;
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2020-07-03 17:33:57 +02:00
|
|
|
log.debug("Getting OIDC token for client: {}", clientId);
|
2020-07-09 14:33:54 +02:00
|
|
|
JWTToken umaToken = null;
|
|
|
|
if (clientId != null && clientSecret != null && tokenURL != null && umaAudience != null) {
|
2020-07-03 17:33:57 +02:00
|
|
|
log.debug("Getting OIDC token for clientId '{}' from: {}", clientId, tokenURL);
|
2020-07-09 14:33:54 +02:00
|
|
|
JWTToken oidcToken = OpenIdConnectRESTHelper.queryClientToken(clientId, clientSecret, tokenURL);
|
|
|
|
log.debug("Getting UMA token for audience '{}' from: {}", umaAudience, tokenURL);
|
2020-11-17 11:44:20 +01:00
|
|
|
umaToken = OpenIdConnectRESTHelper.queryUMAToken(tokenURL, oidcToken.getAccessTokenAsBearer(),
|
|
|
|
umaAudience, null);
|
2020-07-09 14:33:54 +02:00
|
|
|
} else {
|
|
|
|
log.debug("Can't get UMA token since not all the required params was provied");
|
2020-06-29 17:00:57 +02:00
|
|
|
}
|
2020-07-03 17:33:57 +02:00
|
|
|
log.debug("Performing HTTP POST to: {}", endpoint);
|
2020-06-25 11:41:04 +02:00
|
|
|
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
|
2020-06-29 17:00:57 +02:00
|
|
|
connection.setRequestMethod("POST");
|
2020-06-25 11:41:04 +02:00
|
|
|
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
2020-07-03 17:33:57 +02:00
|
|
|
log.trace("HTTP connection timeout set to: {}", connection.getConnectTimeout());
|
2020-06-25 11:41:04 +02:00
|
|
|
connection.setReadTimeout(READ_TIMEOUT);
|
2020-07-03 17:33:57 +02:00
|
|
|
log.trace("HTTP connection Read timeout set to: {}", connection.getReadTimeout());
|
2020-06-25 11:41:04 +02:00
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
2020-06-29 17:00:57 +02:00
|
|
|
// Commented out as per the Conductor issue: https://github.com/Netflix/conductor/issues/376
|
|
|
|
// connection.setRequestProperty("Accept", "application/json");
|
|
|
|
connection.setDoOutput(true);
|
2020-07-09 14:33:54 +02:00
|
|
|
if (umaToken != null) {
|
2020-11-17 11:44:20 +01:00
|
|
|
log.debug("Setting authorization header as: {}", umaToken.getAccessTokenAsBearer());
|
|
|
|
connection.setRequestProperty("Authorization", umaToken.getAccessTokenAsBearer());
|
2020-06-29 17:00:57 +02:00
|
|
|
} else {
|
2020-07-03 17:33:57 +02:00
|
|
|
log.debug("Sending request without authorization header");
|
2020-06-29 17:00:57 +02:00
|
|
|
}
|
2020-06-25 11:41:04 +02:00
|
|
|
OutputStream os = connection.getOutputStream();
|
2020-11-17 11:44:20 +01:00
|
|
|
String jsonString = event.toJSONString();
|
2020-07-03 17:33:57 +02:00
|
|
|
log.trace("Sending event JSON: {}", jsonString);
|
2020-06-24 09:52:51 +02:00
|
|
|
os.write(jsonString.getBytes("UTF-8"));
|
2020-06-29 17:00:57 +02:00
|
|
|
os.flush();
|
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-07-03 17:33:57 +02:00
|
|
|
log.trace("HTTP Response code: {}", httpResultCode);
|
|
|
|
|
|
|
|
log.trace("Reading response");
|
2020-06-18 12:55:34 +02:00
|
|
|
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();
|
2020-06-29 17:00:57 +02:00
|
|
|
isr.close();
|
2020-06-18 12:55:34 +02:00
|
|
|
if (ok) {
|
2020-11-17 11:44:20 +01:00
|
|
|
log.info("[{}] Event publish for {} is OK", httpResultCode, event.getName());
|
2020-06-18 12:55:34 +02:00
|
|
|
} else {
|
2020-11-17 11:44:20 +01:00
|
|
|
log.debug("[{}] Event publish for {} is not OK", httpResultCode, event.getName());
|
2020-06-18 12:55:34 +02:00
|
|
|
}
|
2020-11-17 11:44:20 +01:00
|
|
|
log.trace("Response message from server: {}", 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
|
|
|
|
|
|
|
}
|