NIFI-4886: Add EL support for Webhook URL in PutSlack

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #3303.
This commit is contained in:
Matthew Burgess 2019-02-12 12:33:15 -05:00 committed by Pierre Villard
parent 82a0434901
commit b74d71b647
No known key found for this signature in database
GPG Key ID: BEE1599F0726E9CD
2 changed files with 18 additions and 2 deletions

View File

@ -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);

View File

@ -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<String,String>(){{
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()));
}
}