Repository: nifi
Updated Branches:
  refs/heads/master c9f1c9789 -> 4687e0fb3


NIFI-4981 Allow using FF's content as message in PutEmail

Signed-off-by: James Wing <[email protected]>

This closes #2556.


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/4687e0fb
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/4687e0fb
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/4687e0fb

Branch: refs/heads/master
Commit: 4687e0fb353f565efe990933afb547df7484c936
Parents: c9f1c97
Author: Pierre Villard <[email protected]>
Authored: Thu Mar 15 19:28:26 2018 +0100
Committer: James Wing <[email protected]>
Committed: Sun Mar 18 11:35:43 2018 -0700

----------------------------------------------------------------------
 .../nifi/processors/standard/PutEmail.java      | 49 +++++++++++++++++---
 .../nifi/processors/standard/TestPutEmail.java  | 31 +++++++++++++
 2 files changed, 74 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/4687e0fb/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java
index 2965a67..aedf947 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java
@@ -18,6 +18,7 @@ package org.apache.nifi.processors.standard;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -50,6 +51,8 @@ import org.apache.commons.codec.binary.Base64;
 import org.apache.nifi.annotation.behavior.InputRequirement;
 import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
 import org.apache.nifi.annotation.behavior.SupportsBatching;
+import org.apache.nifi.annotation.behavior.SystemResource;
+import org.apache.nifi.annotation.behavior.SystemResourceConsideration;
 import org.apache.nifi.annotation.documentation.CapabilityDescription;
 import org.apache.nifi.annotation.documentation.Tags;
 import org.apache.nifi.components.PropertyDescriptor;
@@ -66,11 +69,14 @@ import org.apache.nifi.processor.Relationship;
 import org.apache.nifi.processor.exception.ProcessException;
 import org.apache.nifi.processor.io.InputStreamCallback;
 import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.stream.io.StreamUtils;
 
 @SupportsBatching
 @Tags({"email", "put", "notify", "smtp"})
 @InputRequirement(Requirement.INPUT_REQUIRED)
 @CapabilityDescription("Sends an e-mail to configured recipients for each 
incoming FlowFile")
+@SystemResourceConsideration(resource = SystemResource.MEMORY, description = 
"The entirety of the FlowFile's content (as a String object) "
+        + "will be read into memory in case the property to use the flow file 
content as the email body is set to true.")
 public class PutEmail extends AbstractProcessor {
 
     public static final PropertyDescriptor SMTP_HOSTNAME = new 
PropertyDescriptor.Builder()
@@ -182,9 +188,8 @@ public class PutEmail extends AbstractProcessor {
     public static final PropertyDescriptor MESSAGE = new 
PropertyDescriptor.Builder()
             .name("Message")
             .description("The body of the email message")
-            .required(true)
+            .required(false)
             .expressionLanguageSupported(true)
-            .defaultValue("")
             .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
             .build();
     public static final PropertyDescriptor ATTACH_FILE = new 
PropertyDescriptor.Builder()
@@ -194,6 +199,15 @@ public class PutEmail extends AbstractProcessor {
             .allowableValues("true", "false")
             .defaultValue("false")
             .build();
+    public static final PropertyDescriptor CONTENT_AS_MESSAGE = new 
PropertyDescriptor.Builder()
+            .name("email-ff-content-as-message")
+            .displayName("Flow file content as message")
+            .description("Specifies whether or not the FlowFile content should 
be the message of the email. If true, the 'Message' property is ignored.")
+            .required(true)
+            .expressionLanguageSupported(true)
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .defaultValue("false")
+            .build();
     public static final PropertyDescriptor INCLUDE_ALL_ATTRIBUTES = new 
PropertyDescriptor.Builder()
             .name("Include All Attributes In Message")
             .description("Specifies whether or not all FlowFile attributes 
should be recorded in the body of the email message")
@@ -248,6 +262,7 @@ public class PutEmail extends AbstractProcessor {
         properties.add(BCC);
         properties.add(SUBJECT);
         properties.add(MESSAGE);
+        properties.add(CONTENT_AS_MESSAGE);
         properties.add(ATTACH_FILE);
         properties.add(INCLUDE_ALL_ATTRIBUTES);
         this.properties = Collections.unmodifiableList(properties);
@@ -305,11 +320,8 @@ public class PutEmail extends AbstractProcessor {
 
             message.setHeader("X-Mailer", 
context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions(flowFile).getValue());
             
message.setSubject(context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue());
-            String messageText = 
context.getProperty(MESSAGE).evaluateAttributeExpressions(flowFile).getValue();
 
-            if (context.getProperty(INCLUDE_ALL_ATTRIBUTES).asBoolean()) {
-                messageText = formatAttributes(flowFile, messageText);
-            }
+            String messageText = getMessage(flowFile, context, session);
 
             String contentType = 
context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions(flowFile).getValue();
             message.setContent(messageText, contentType);
@@ -350,6 +362,31 @@ public class PutEmail extends AbstractProcessor {
         }
     }
 
+    private String getMessage(final FlowFile flowFile, final ProcessContext 
context, final ProcessSession session) {
+        String messageText = "";
+
+        
if(context.getProperty(CONTENT_AS_MESSAGE).evaluateAttributeExpressions(flowFile).asBoolean())
 {
+            // reading all the content of the input flow file
+            final byte[] byteBuffer = new byte[(int) flowFile.getSize()];
+            session.read(flowFile, new InputStreamCallback() {
+                @Override
+                public void process(InputStream in) throws IOException {
+                    StreamUtils.fillBuffer(in, byteBuffer, false);
+                }
+            });
+
+            messageText = new String(byteBuffer, 0, byteBuffer.length, 
Charset.forName("UTF-8"));
+        } else if (context.getProperty(MESSAGE).isSet()) {
+            messageText = 
context.getProperty(MESSAGE).evaluateAttributeExpressions(flowFile).getValue();
+        }
+
+        if (context.getProperty(INCLUDE_ALL_ATTRIBUTES).asBoolean()) {
+            return formatAttributes(flowFile, messageText);
+        }
+
+        return messageText;
+    }
+
     /**
      * Based on the input properties, determine whether an authenticate or 
unauthenticated session should be used. If authenticated, creates a Password 
Authenticator for use in sending the email.
      *

http://git-wip-us.apache.org/repos/asf/nifi/blob/4687e0fb/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java
index 782f61c..e2319d9 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java
@@ -253,4 +253,35 @@ public class TestPutEmail {
         assertNull(message.getRecipients(RecipientType.CC));
     }
 
+    @Test
+    public void testOutgoingMessageWithFlowfileContent() throws Exception {
+        // verifies that are set on the outgoing Message correctly
+        runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+        runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+        runner.setProperty(PutEmail.FROM, "[email protected]");
+        runner.setProperty(PutEmail.MESSAGE, "${body}");
+        runner.setProperty(PutEmail.TO, "[email protected]");
+        runner.setProperty(PutEmail.CONTENT_AS_MESSAGE, "${sendContent}");
+
+        Map<String, String> attributes = new HashMap<String, String>();
+        attributes.put("sendContent", "true");
+        attributes.put("body", "Message Body");
+
+        runner.enqueue("Some Text".getBytes(), attributes);
+        runner.run();
+
+        runner.assertQueueEmpty();
+        runner.assertAllFlowFilesTransferred(PutEmail.REL_SUCCESS);
+
+        // Verify that the Message was populated correctly
+        assertEquals("Expected a single message to be sent", 1, 
processor.getMessages().size());
+        Message message = processor.getMessages().get(0);
+        assertEquals("[email protected]", message.getFrom()[0].toString());
+        assertEquals("X-Mailer Header", "TestingNiFi", 
message.getHeader("X-Mailer")[0]);
+        assertEquals("Some Text", message.getContent());
+        assertEquals("[email protected]", 
message.getRecipients(RecipientType.TO)[0].toString());
+        assertNull(message.getRecipients(RecipientType.BCC));
+        assertNull(message.getRecipients(RecipientType.CC));
+    }
+
 }

Reply via email to