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("[email protected]",
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, "[email protected]");
+ runner.setProperty(PutEmail.MESSAGE, "test message");
+ runner.setProperty(PutEmail.TO, "[email protected]");
+
+ // 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, "[email protected]");
+ runner.setProperty(PutEmail.MESSAGE, new String(rawBytes,
StandardCharsets.US_ASCII));
+ runner.setProperty(PutEmail.TO, "[email protected]");
+ 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]