[
https://issues.apache.org/jira/browse/NIFI-1578?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15248154#comment-15248154
]
ASF GitHub Bot commented on NIFI-1578:
--------------------------------------
Github user mattyb149 commented on a diff in the pull request:
https://github.com/apache/nifi/pull/256#discussion_r60267217
--- Diff:
nifi-nar-bundles/nifi-slack-bundle/nifi-slack-processors/src/main/java/org/apache/nifi/processors/slack/PutSlack.java
---
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.slack;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.components.Validator;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.stream.io.DataOutputStream;
+
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+@Tags({"put", "slack", "notify"})
+@CapabilityDescription("Sends a message to your team on slack.com")
+public class PutSlack extends AbstractProcessor {
+
+ public static final PropertyDescriptor WEBHOOK_URL = new
PropertyDescriptor
+ .Builder()
+ .name("webhook-url")
+ .displayName("Webhook URL")
+ .description("The POST URL provided by Slack to send messages
into a channel.")
+ .required(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .addValidator(StandardValidators.URL_VALIDATOR)
+ .sensitive(true)
+ .build();
+
+ public static final PropertyDescriptor WEBHOOK_TEXT = new
PropertyDescriptor
+ .Builder()
+ .name("webhook-text")
+ .displayName("Webhook Text")
+ .description("The text sent in the webhook message")
+ .required(true)
+ .expressionLanguageSupported(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ public static final PropertyDescriptor CHANNEL = new PropertyDescriptor
+ .Builder()
+ .name("channel")
+ .displayName("Channel")
+ .description("A public channel using #channel or direct
message using @username")
+ .required(false)
+ .expressionLanguageSupported(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .addValidator(new ChannelValidator())
+ .build();
+
+ public static final PropertyDescriptor USERNAME = new
PropertyDescriptor
+ .Builder()
+ .name("username")
+ .displayName("Username")
+ .description("The displayed Slack username")
+ .required(false)
+ .expressionLanguageSupported(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+
+ public static final PropertyDescriptor ICON_URL = new
PropertyDescriptor
+ .Builder()
+ .name("icon-url")
+ .displayName("Icon URL")
+ .description("Icon URL to be used for the message")
+ .required(false)
+ .expressionLanguageSupported(true)
+ .addValidator(StandardValidators.URL_VALIDATOR)
+ .build();
+
+ public static final PropertyDescriptor ICON_EMOJI = new
PropertyDescriptor
+ .Builder()
+ .name("icon-emoji")
+ .displayName("Icon Emoji")
+ .description("Icon Emoji to be used for the message. Must
begin and end with a colon, e.g. :ghost:")
+ .required(false)
+ .expressionLanguageSupported(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .addValidator(new EmojiValidator())
+ .build();
+
+ public static final Relationship REL_SUCCESS = new
Relationship.Builder()
+ .name("success")
+ .description("FlowFiles are routed to success after being
successfully sent to Slack")
+ .build();
+
+ public static final Relationship REL_FAILURE = new
Relationship.Builder()
+ .name("failure")
+ .description("FlowFiles are routed to failure if unable to be
sent to Slack")
+ .build();
+
+ public static final List<PropertyDescriptor> descriptors =
Collections.unmodifiableList(
+ Arrays.asList(WEBHOOK_URL, WEBHOOK_TEXT, CHANNEL, USERNAME,
ICON_URL, ICON_EMOJI));
+
+ public static final Set<Relationship> relationships =
Collections.unmodifiableSet(
+ new HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));
+
+ @Override
+ public Set<Relationship> getRelationships() {
+ return this.relationships;
+ }
+
+ @Override
+ public final List<PropertyDescriptor>
getSupportedPropertyDescriptors() {
+ return descriptors;
+ }
+
+ @Override
+ public void onTrigger(final ProcessContext context, final
ProcessSession session) throws ProcessException {
+ FlowFile flowFile = session.get();
+ if ( flowFile == null ) {
+ return;
+ }
+
+ try {
+ URL url = new URL(context.getProperty(WEBHOOK_URL).getValue());
+ HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
+ conn.setRequestMethod("POST");
+ conn.setDoOutput(true);
+ DataOutputStream outputStream = new
DataOutputStream(conn.getOutputStream());
+
+ JsonObjectBuilder builder = Json.createObjectBuilder();
+ String text =
context.getProperty(WEBHOOK_TEXT).evaluateAttributeExpressions(flowFile).getValue();
+ if (text != null && !text.isEmpty()) {
+ builder.add("text", text);
+ } else if (text == null || (text != null && !text.isEmpty())) {
--- End diff --
I think there's a negative logic bug here. The else clause will
short-circuit on the text==null check so you can safely assume text != null in
the second clause. However then it should check if text is empty, right now it
checks to see if it's NOT empty, which would've satisfied the if-clause. So if
you have empty text, it will bypass this clause and send an empty body,
resulting in a 500 HTTP Response.
> Create PutSlack processor
> -------------------------
>
> Key: NIFI-1578
> URL: https://issues.apache.org/jira/browse/NIFI-1578
> Project: Apache NiFi
> Issue Type: Improvement
> Components: Extensions
> Reporter: Adam Lamar
> Assignee: Adam Lamar
> Fix For: 0.7.0
>
>
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)