diff --git a/plugins/webhook/README.md b/plugins/webhook/README.md index a4e5a6a..ff8281b 100644 --- a/plugins/webhook/README.md +++ b/plugins/webhook/README.md @@ -7,11 +7,12 @@ This plugin sends HTTP requests to third-party endpoints to trigger further proc ## Configuration ```yaml -- webhook: - endpoints: - - url: "http://example.localhost/trigger" # URL to send the request to. Required. - method: "POST" # HTTP method to use. Default is POST. - includeDataSet: false # Send the Sauron Data Set as the body of the request. Default is false. +webhook: + endpoints: + example: + url: "http://example.localhost/trigger" # URL to send the request to. Required. + method: "POST" # HTTP method to use. Default is POST. + includeDataSet: false # Send the Sauron Data Set as the body of the request. Default is false. ``` ## Input diff --git a/plugins/webhook/src/main/java/com/freenow/sauron/plugins/Webhook.java b/plugins/webhook/src/main/java/com/freenow/sauron/plugins/Webhook.java index 50157e2..bab181b 100644 --- a/plugins/webhook/src/main/java/com/freenow/sauron/plugins/Webhook.java +++ b/plugins/webhook/src/main/java/com/freenow/sauron/plugins/Webhook.java @@ -58,39 +58,39 @@ public Webhook(RestTemplate restTemplate) */ @Override public DataSet apply(PluginsConfigurationProperties properties, DataSet input) { - properties.getPluginConfigurationProperty(PLUGIN_ID, PROPERTY_ENDPOINTS).ifPresent(endpoints -> { - for (Map endpoint : (List>) endpoints) - { - String url = (String) endpoint.getOrDefault(PROPERTY_URL, ""); - if (Objects.equals(url, "")) - { - logger.error("URL of webhook endpoint not set"); - continue; - } + properties.getPluginConfigurationProperty(PLUGIN_ID, PROPERTY_ENDPOINTS).ifPresent(endpointProperty -> { + Map endpoints = (Map) endpointProperty; + endpoints.forEach((name, data) -> { + Map config = (Map) data; + String url = (String) config.getOrDefault(PROPERTY_URL, ""); + String methodRaw = (String) config.getOrDefault(PROPERTY_METHOD, "POST"); + Boolean includeDataSet = (Boolean) config.getOrDefault(PROPERTY_INCLUDE_DATASET, false); + send(name, url, methodRaw, includeDataSet, input); + }); + }); - String methodRaw = (String) endpoint.getOrDefault(PROPERTY_METHOD, "POST"); - HttpMethod method = HttpMethod.resolve(methodRaw); - if (method == null) - { - logger.error("HTTP method {} of webhook endpoint is invalid", methodRaw); - continue; - } + return input; + } - HttpEntity payload = null; - Boolean includeDataSet = (Boolean) endpoint.getOrDefault(PROPERTY_INCLUDE_DATASET, false); - if (includeDataSet) - { - payload = new HttpEntity<>(input); - } + private void send(String name, String url, String methodRaw, Boolean includeDataSet, DataSet dataSet) + { + HttpMethod method = HttpMethod.resolve(methodRaw); + if (method == null) + { + logger.error("HTTP method {} of webhook endpoint {} is invalid", name, methodRaw); + return; + } - ResponseEntity response = restTemplate.exchange(url, method, payload, Object.class); - if (!response.getStatusCode().is2xxSuccessful()) - { - logger.error("Sending webhook to {} failed with HTTP status code {}", url, response.getStatusCode()); - } - } - }); + HttpEntity payload = null; + if (includeDataSet) + { + payload = new HttpEntity<>(dataSet); + } - return input; + ResponseEntity response = restTemplate.exchange(url, method, payload, Object.class); + if (!response.getStatusCode().is2xxSuccessful()) + { + logger.error("Sending webhook {} to {} failed with HTTP status code {}", name, url, response.getStatusCode()); + } } } diff --git a/plugins/webhook/src/test/java/com/freenow/sauron/plugins/WebhookTest.java b/plugins/webhook/src/test/java/com/freenow/sauron/plugins/WebhookTest.java index 5267291..6c83397 100644 --- a/plugins/webhook/src/test/java/com/freenow/sauron/plugins/WebhookTest.java +++ b/plugins/webhook/src/test/java/com/freenow/sauron/plugins/WebhookTest.java @@ -98,7 +98,10 @@ private PluginsConfigurationProperties createPluginConfigurationProperties(Strin {{ put("webhook", new HashMap<>() {{ - put("endpoints", List.of(endpoint)); + put("endpoints", new HashMap<>() + {{ + put("unittest", endpoint); + }}); }}); }}; }