From 86afd4c162c4c9df1452e23b483baf95773f8cc3 Mon Sep 17 00:00:00 2001 From: Mauro Mugnaini Date: Mon, 29 Jun 2020 17:00:57 +0200 Subject: [PATCH] Event name now is the end part of the endpoint URL, removed Accept header to avoid Conductor issue and client identy (to construct authorization header) now is optional --- .../event/publisher/HTTPEventSender.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/gcube/event/publisher/HTTPEventSender.java b/src/main/java/org/gcube/event/publisher/HTTPEventSender.java index 8396d55..81b5896 100644 --- a/src/main/java/org/gcube/event/publisher/HTTPEventSender.java +++ b/src/main/java/org/gcube/event/publisher/HTTPEventSender.java @@ -16,13 +16,13 @@ public class HTTPEventSender implements EventSender { protected static final Logger log = LoggerFactory.getLogger(HTTPEventSender.class); - private URL endpoint; + private URL baseEnndpointURL; private String clientId; private String clientSecret; private URL tokenURL; - public HTTPEventSender(URL endpointURL, String clientId, String clientSecret, URL tokenURL) { - this.endpoint = endpointURL; + public HTTPEventSender(URL baseEnndpointURL, String clientId, String clientSecret, URL tokenURL) { + this.baseEnndpointURL = baseEnndpointURL; this.clientId = clientId; this.clientSecret = clientSecret; this.tokenURL = tokenURL; @@ -31,14 +31,14 @@ public class HTTPEventSender implements EventSender { @Override public void send(Event event) { if (log.isDebugEnabled()) { - log.debug("Starting HTTP POST thread to: " + endpoint); + log.debug("Starting HTTP POST thread to: " + baseEnndpointURL); } try { - URL eventEndpoint = new URL(endpoint, event.getName()); + URL eventEndpoint = new URL(baseEnndpointURL, 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); + + baseEnndpointURL, e); } } @@ -61,11 +61,18 @@ public class HTTPEventSender implements EventSender { if (log.isDebugEnabled()) { log.debug("Getting OIDC token for client: " + clientId); } - JWTToken token = OpenIdConnectRESTHelper.queryClientToken(clientId, clientSecret, tokenURL); + JWTToken token = null; + if (clientId != null && clientSecret != null && tokenURL != null) { + if (log.isDebugEnabled()) { + log.debug("Getting OIDC token for clientId '" + clientId + "' from: " + tokenURL); + } + token = OpenIdConnectRESTHelper.queryClientToken(clientId, clientSecret, tokenURL); + } if (log.isDebugEnabled()) { log.debug("Performing HTTP POST to: " + endpoint); } HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection(); + connection.setRequestMethod("POST"); connection.setConnectTimeout(CONNECTION_TIMEOUT); if (log.isTraceEnabled()) { log.trace("HTTP connection timeout set to: " + connection.getConnectTimeout()); @@ -74,17 +81,26 @@ public class HTTPEventSender implements EventSender { 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()); + // Commented out as per the Conductor issue: https://github.com/Netflix/conductor/issues/376 + // connection.setRequestProperty("Accept", "application/json"); + connection.setDoOutput(true); + if (token != null) { + if (log.isDebugEnabled()) { + log.debug("Setting authorization header as: " + token.getAsBearer()); + } + connection.setRequestProperty("Authorization", token.getAsBearer()); + } else { + if (log.isDebugEnabled()) { + log.debug("Sending request without authorization header"); + } + } OutputStream os = connection.getOutputStream(); if (log.isTraceEnabled()) { log.trace("Sending event JSON: " + jsonString); } os.write(jsonString.getBytes("UTF-8")); + os.flush(); os.close(); StringBuilder sb = new StringBuilder(); @@ -109,6 +125,7 @@ public class HTTPEventSender implements EventSender { sb.append(line + "\n"); } br.close(); + isr.close(); if (ok) { if (log.isDebugEnabled()) { log.debug("[" + httpResultCode + "] Event publish OK. Results: " + sb.toString());