You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/07/11 17:45:40 UTC

[GitHub] [nifi] emiliosetiadarma opened a new pull request, #6193: NIFI-9451 add input character set property for PutEmail and additiona…

emiliosetiadarma opened a new pull request, #6193:
URL: https://github.com/apache/nifi/pull/6193

   …l tests
   
   Also fixed an issue regarding sending non US-ASCII messages in PutEmail. The original version had non US-ASCII messages appear as "?" in the resulting email.
   
   <!-- 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. -->
   
   # Summary
   
   [NIFI-9451](https://issues.apache.org/jira/browse/NIFI-9451)
   
   # Tracking
   
   Please complete the following tracking steps prior to pull request creation.
   
   ### Issue Tracking
   
   - [x] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
   
   ### Pull Request Tracking
   
   - [x] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
   - [x] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
   
   ### Pull Request Formatting
   
   - [x] Pull Request based on current revision of the `main` branch
   - [x] Pull Request refers to a feature branch with one commit containing changes
   
   # Verification
   
   Please indicate the verification steps performed prior to pull request creation.
   
   ### Build
   
   - [x] Build completed using `mvn clean install -P contrib-check`
     - [x] JDK 8
     - [x] JDK 11
     - [] JDK 17
   
   ### Licensing
   
   - [ ] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
   - [ ] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
   
   ### Documentation
   
   - [ ] Documentation formatting appears as expected in rendered files
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] emiliosetiadarma commented on a diff in pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
emiliosetiadarma commented on code in PR #6193:
URL: https://github.com/apache/nifi/pull/6193#discussion_r919305097


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java:
##########
@@ -362,17 +373,26 @@ public void onTrigger(final ProcessContext context, final ProcessSession session
             }
             this.setMessageHeader("X-Mailer", context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions(flowFile).getValue(), message);
             message.setSubject(context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue());
+            message.setSentDate(new Date());
 
             final String messageText = getMessage(flowFile, context, session);
-
             final String contentType = context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions(flowFile).getValue();
-            message.setContent(messageText, contentType);
-            message.setSentDate(new Date());
+            final Charset charset = getCharset(context);
+            final String charsetName = MimeUtility.mimeCharset(charset.name());
+            final DataHandler messageDataHandler = new DataHandler(

Review Comment:
   Yeah it will encode the body text into base64. I figured out that the existing implementation of PutEmail used base64 encoding of body text when the `ATTACH_FILE` property is set to true. 
   
   Just extended it to the non `ATTACH_FILE` case and made sure to set the appropriate header. If I hadn't done that, the email sent would have a `Content-Transfer-Encoding` of `7-bit` which can only represent US-ASCII characters



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] emiliosetiadarma commented on a diff in pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
emiliosetiadarma commented on code in PR #6193:
URL: https://github.com/apache/nifi/pull/6193#discussion_r919316353


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java:
##########
@@ -299,4 +303,55 @@ public void testOutgoingMessageWithFlowfileContent() throws Exception {
         assertEquals("anotherbcc@apache.org", message.getRecipients(RecipientType.BCC)[1].toString());
     }
 
-}
+    @Test
+    public void testUnrecognizedCharset() {
+        runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+        runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+        runner.setProperty(PutEmail.FROM, "test@apache.org");
+        runner.setProperty(PutEmail.MESSAGE, "test message");
+        runner.setProperty(PutEmail.TO, "recipient@apache.org");
+
+        // not one of the recognized charsets
+        runner.setProperty(PutEmail.INPUT_CHARACTER_SET, "NOT A CHARACTER SET");
+
+        runner.assertNotValid();
+    }
+
+    @Test
+    public void testPutEmailWithMismatchedCharset() throws Exception {
+        // String specifically chosen to have characters encoded differently in US_ASCII and UTF_8
+        final String rawString = "SoftwÄrë Ënginëër Ön NiFi";
+        final byte[] rawBytes = rawString.getBytes(StandardCharsets.US_ASCII);
+        final byte[] rawBytesUTF8 = rawString.getBytes(StandardCharsets.UTF_8);
+
+        // verify that the message bytes are different (some messages are not)
+        assertNotEquals(rawBytes, rawBytesUTF8);
+
+        runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+        runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+        runner.setProperty(PutEmail.FROM, "test@apache.org");
+        runner.setProperty(PutEmail.MESSAGE, new String(rawBytes, StandardCharsets.US_ASCII));
+        runner.setProperty(PutEmail.TO, "recipient@apache.org");
+        runner.setProperty(PutEmail.INPUT_CHARACTER_SET, StandardCharsets.UTF_8.name());
+
+        runner.enqueue("Some Text".getBytes());
+
+        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);
+        assertNotEquals(rawString, message.getContent());

Review Comment:
   After looking at the test, I decided to rework it a bit. My goal in this test was to show that the text `"SoftwÄrë Ënginëër Ön NiFi"` which has characters that are not supported with US-ASCII (proven by the first `assertNotEquals(rawBytes, rawBytesUTF8)`). This will result in a corrupted message (`Softw?r? ?ngin??r ?n NiFi`) when improperly setting the `INPUT_CHARACTER_SET`.
   
   I reworked the test to send "SoftwÄrë Ënginëër Ön NiFi" while setting the `INPUT_CHARACTER_SET` to US-ASCII (and making sure the result is a corrupted message) - indicating that we must make sure that we specify the appropriate character set for the message



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] emiliosetiadarma closed pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
emiliosetiadarma closed pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…
URL: https://github.com/apache/nifi/pull/6193


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] greyp9 commented on a diff in pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
greyp9 commented on code in PR #6193:
URL: https://github.com/apache/nifi/pull/6193#discussion_r918369148


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java:
##########
@@ -233,6 +232,17 @@ public class PutEmail extends AbstractProcessor {
             .allowableValues("true", "false")
             .defaultValue("false")
             .build();
+    public static final PropertyDescriptor INPUT_CHARACTER_SET = new PropertyDescriptor.Builder()
+            .name("input-character-set")
+            .displayName("Input Character Set")
+            .description("Specifies the character set of the FlowFile contents "
+                    + "for reading input FlowFile contents to generate the message body "
+                    + "or as an attachment to the message. "
+                    + "If not set, UTF-8 will be the default value.")
+            .required(true)
+            .addValidator(StandardValidators.CHARACTER_SET_VALIDATOR)
+            .defaultValue("UTF-8")

Review Comment:
   My vote is for using a constant here; one option is `StandardCharsets.UTF_8.name()`.



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java:
##########
@@ -362,17 +373,26 @@ public void onTrigger(final ProcessContext context, final ProcessSession session
             }
             this.setMessageHeader("X-Mailer", context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions(flowFile).getValue(), message);
             message.setSubject(context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue());
+            message.setSentDate(new Date());
 
             final String messageText = getMessage(flowFile, context, session);
-
             final String contentType = context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions(flowFile).getValue();
-            message.setContent(messageText, contentType);
-            message.setSentDate(new Date());
+            final Charset charset = getCharset(context);
+            final String charsetName = MimeUtility.mimeCharset(charset.name());
+            final DataHandler messageDataHandler = new DataHandler(

Review Comment:
   If I understand correctly, this would unconditionally encode the body text into base64? 



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java:
##########
@@ -299,4 +303,55 @@ public void testOutgoingMessageWithFlowfileContent() throws Exception {
         assertEquals("anotherbcc@apache.org", message.getRecipients(RecipientType.BCC)[1].toString());
     }
 
-}
+    @Test
+    public void testUnrecognizedCharset() {
+        runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+        runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+        runner.setProperty(PutEmail.FROM, "test@apache.org");
+        runner.setProperty(PutEmail.MESSAGE, "test message");
+        runner.setProperty(PutEmail.TO, "recipient@apache.org");
+
+        // not one of the recognized charsets
+        runner.setProperty(PutEmail.INPUT_CHARACTER_SET, "NOT A CHARACTER SET");
+
+        runner.assertNotValid();
+    }
+
+    @Test
+    public void testPutEmailWithMismatchedCharset() throws Exception {
+        // String specifically chosen to have characters encoded differently in US_ASCII and UTF_8
+        final String rawString = "SoftwÄrë Ënginëër Ön NiFi";
+        final byte[] rawBytes = rawString.getBytes(StandardCharsets.US_ASCII);
+        final byte[] rawBytesUTF8 = rawString.getBytes(StandardCharsets.UTF_8);
+
+        // verify that the message bytes are different (some messages are not)
+        assertNotEquals(rawBytes, rawBytesUTF8);
+
+        runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+        runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+        runner.setProperty(PutEmail.FROM, "test@apache.org");
+        runner.setProperty(PutEmail.MESSAGE, new String(rawBytes, StandardCharsets.US_ASCII));
+        runner.setProperty(PutEmail.TO, "recipient@apache.org");
+        runner.setProperty(PutEmail.INPUT_CHARACTER_SET, StandardCharsets.UTF_8.name());
+
+        runner.enqueue("Some Text".getBytes());
+
+        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);
+        assertNotEquals(rawString, message.getContent());

Review Comment:
   Is it reasonable to tighten up this check?  What exactly are we expecting the differences to be?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
exceptionfactory commented on code in PR #6193:
URL: https://github.com/apache/nifi/pull/6193#discussion_r942881754


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java:
##########
@@ -245,6 +245,17 @@ public class PutEmail extends AbstractProcessor {
             .allowableValues("true", "false")
             .defaultValue("false")
             .build();
+    public static final PropertyDescriptor INPUT_CHARACTER_SET = new PropertyDescriptor.Builder()
+            .name("input-character-set")
+            .displayName("Input Character Set")
+            .description("Specifies the character set of the FlowFile contents "
+                    + "for reading input FlowFile contents to generate the message body "
+                    + "or as an attachment to the message. "
+                    + "If not set, UTF-8 will be the default value.")
+            .required(true)
+            .addValidator(StandardValidators.CHARACTER_SET_VALIDATOR)
+            .defaultValue(StandardCharsets.UTF_8.name())

Review Comment:
   This should probably be `StandardCharsets.UTF_8.toString()` instead of `UTF_8.name()`.



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java:
##########
@@ -386,17 +398,26 @@ public void onTrigger(final ProcessContext context, final ProcessSession session
             }
             this.setMessageHeader("X-Mailer", context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions(flowFile).getValue(), message);
             message.setSubject(context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue());
+            message.setSentDate(new Date());
 
             final String messageText = getMessage(flowFile, context, session);
-
             final String contentType = context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions(flowFile).getValue();
-            message.setContent(messageText, contentType);
-            message.setSentDate(new Date());
+            final Charset charset = getCharset(context);
+            final String charsetName = MimeUtility.mimeCharset(charset.name());
+            final DataHandler messageDataHandler = new DataHandler(
+                    new ByteArrayDataSource(
+                            Base64.encodeBase64(messageText.getBytes(charset)),
+                            contentType + String.format("; charset=\"%s\"", charsetName)
+                    )
+            );
+
+            final MimeMultipart multipart = new MimeMultipart();
+            final MimeBodyPart mimeText = new PreencodedMimeBodyPart("base64");
+            mimeText.setDataHandler(messageDataHandler);
+            mimeText.setHeader("Content-Transfer-Encoding", "base64");
+            multipart.addBodyPart(mimeText);

Review Comment:
   This approach changes the message format to always use multipart formatting, which does not seem the like the ideal solution.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] emiliosetiadarma commented on pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
emiliosetiadarma commented on PR #6193:
URL: https://github.com/apache/nifi/pull/6193#issuecomment-1217204855

   Closing while revising


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] emiliosetiadarma commented on pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
emiliosetiadarma commented on PR #6193:
URL: https://github.com/apache/nifi/pull/6193#issuecomment-1182314566

   Thanks @greyp9 for the comments! I've addressed some of them. As for checking to see the ability for the mail receiver to check the message without decoding it first, I will take a look and post another comment if I find something!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] emiliosetiadarma commented on a diff in pull request #6193: NIFI-9451 add input character set property for PutEmail and additiona…

Posted by GitBox <gi...@apache.org>.
emiliosetiadarma commented on code in PR #6193:
URL: https://github.com/apache/nifi/pull/6193#discussion_r919301727


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutEmail.java:
##########
@@ -233,6 +232,17 @@ public class PutEmail extends AbstractProcessor {
             .allowableValues("true", "false")
             .defaultValue("false")
             .build();
+    public static final PropertyDescriptor INPUT_CHARACTER_SET = new PropertyDescriptor.Builder()
+            .name("input-character-set")
+            .displayName("Input Character Set")
+            .description("Specifies the character set of the FlowFile contents "
+                    + "for reading input FlowFile contents to generate the message body "
+                    + "or as an attachment to the message. "
+                    + "If not set, UTF-8 will be the default value.")
+            .required(true)
+            .addValidator(StandardValidators.CHARACTER_SET_VALIDATOR)
+            .defaultValue("UTF-8")

Review Comment:
   Sure that makes sense, making the changes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org