From b74d71b64738545c9e02e601b028de19c137af3b Mon Sep 17 00:00:00 2001 From: Matthew Burgess Date: Tue, 12 Feb 2019 12:33:15 -0500 Subject: [PATCH] NIFI-4886: Add EL support for Webhook URL in PutSlack Signed-off-by: Pierre Villard This closes #3303. --- .../apache/nifi/processors/slack/PutSlack.java | 5 +++-- .../nifi/processors/slack/PutSlackTest.java | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/main/java/org/apache/nifi/processors/slack/PutSlack.java b/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/main/java/org/apache/nifi/processors/slack/PutSlack.java index fd4a3023df..6951ad531d 100644 --- a/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/main/java/org/apache/nifi/processors/slack/PutSlack.java +++ b/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/main/java/org/apache/nifi/processors/slack/PutSlack.java @@ -74,6 +74,7 @@ public class PutSlack extends AbstractProcessor { .required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) .sensitive(true) .build(); @@ -254,7 +255,7 @@ public class PutSlack extends AbstractProcessor { jsonWriter.writeObject(jsonObject); jsonWriter.close(); - URL url = new URL(context.getProperty(WEBHOOK_URL).getValue()); + URL url = new URL(context.getProperty(WEBHOOK_URL).evaluateAttributeExpressions(flowFile).getValue()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); @@ -267,7 +268,7 @@ public class PutSlack extends AbstractProcessor { if (responseCode >= 200 && responseCode < 300) { getLogger().info("Successfully posted message to Slack"); session.transfer(flowFile, REL_SUCCESS); - session.getProvenanceReporter().send(flowFile, context.getProperty(WEBHOOK_URL).getValue()); + session.getProvenanceReporter().send(flowFile, url.toString()); } else { getLogger().error("Failed to post message to Slack with response code {}", new Object[]{responseCode}); flowFile = session.penalize(flowFile); diff --git a/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/test/java/org/apache/nifi/processors/slack/PutSlackTest.java b/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/test/java/org/apache/nifi/processors/slack/PutSlackTest.java index f6b09dca0a..7e98c500c9 100644 --- a/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/test/java/org/apache/nifi/processors/slack/PutSlackTest.java +++ b/nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/test/java/org/apache/nifi/processors/slack/PutSlackTest.java @@ -241,4 +241,19 @@ public class PutSlackTest { + "integration-test-webhook%22%2C%22icon_url%22%3A%22http%3A%2F%2Florempixel.com%2F48%2F48%2F%22%7D"; assertTrue(Arrays.equals(expected.getBytes(), servlet.getLastPost())); } + + @Test + public void testSimplePutWithEL() { + testRunner.setProperty(PutSlack.WEBHOOK_URL, "${slack.url}"); + testRunner.setProperty(PutSlack.WEBHOOK_TEXT, PutSlackTest.WEBHOOK_TEST_TEXT); + + testRunner.enqueue(new byte[0], new HashMap(){{ + put("slack.url", server.getUrl()); + }}); + testRunner.run(1); + testRunner.assertAllFlowFilesTransferred(PutSlack.REL_SUCCESS, 1); + + byte[] expected = "payload=%7B%22text%22%3A%22Hello+From+Apache+NiFi%22%7D".getBytes(); + assertTrue(Arrays.equals(expected, servlet.getLastPost())); + } }