Lehel44 commented on a change in pull request #5471:
URL: https://github.com/apache/nifi/pull/5471#discussion_r735576033



##########
File path: 
nifi-nar-bundles/nifi-standard-services/nifi-record-sink-service-bundle/nifi-record-sink-service/src/main/java/org/apache/nifi/record/sink/EmailRecordSink.java
##########
@@ -0,0 +1,360 @@
+/*
+ * 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.record.sink;
+
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.serialization.RecordSetWriter;
+import org.apache.nifi.serialization.RecordSetWriterFactory;
+import org.apache.nifi.serialization.WriteResult;
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordSet;
+
+import javax.mail.Authenticator;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+public class EmailRecordSink extends AbstractControllerService implements 
RecordSinkService {
+
+    public static final PropertyDescriptor SMTP_HOSTNAME = new 
PropertyDescriptor.Builder()
+            .name("SMTP Hostname")
+            .description("The hostname of the SMTP Server that is used to send 
Email Notifications")
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .required(true)
+            .build();
+    public static final PropertyDescriptor SMTP_PORT = new 
PropertyDescriptor.Builder()
+            .name("SMTP Port")
+            .description("The Port used for SMTP communications")
+            .required(true)
+            .defaultValue("25")
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.PORT_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor SMTP_USERNAME = new 
PropertyDescriptor.Builder()
+            .name("SMTP Username")
+            .description("Username for the SMTP account")
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .required(false)
+            .build();
+    public static final PropertyDescriptor SMTP_PASSWORD = new 
PropertyDescriptor.Builder()
+            .name("SMTP Password")
+            .description("Password for the SMTP account")
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .required(false)
+            .sensitive(true)
+            .build();
+    public static final PropertyDescriptor SMTP_AUTH = new 
PropertyDescriptor.Builder()
+            .name("SMTP Auth")
+            .description("Flag indicating whether authentication should be 
used")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .defaultValue("true")
+            .build();
+    public static final PropertyDescriptor SMTP_TLS = new 
PropertyDescriptor.Builder()
+            .name("SMTP TLS")
+            .description("Flag indicating whether TLS should be enabled")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .defaultValue("false")
+            .build();
+    public static final PropertyDescriptor SMTP_SOCKET_FACTORY = new 
PropertyDescriptor.Builder()
+            .name("SMTP Socket Factory")
+            .description("Socket Factory to use for SMTP Connection")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .defaultValue("javax.net.ssl.SSLSocketFactory")
+            .build();
+    public static final PropertyDescriptor HEADER_XMAILER = new 
PropertyDescriptor.Builder()
+            .name("SMTP X-Mailer Header")
+            .description("X-Mailer used in the header of the outgoing email")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .defaultValue("NiFi")
+            .build();
+    public static final PropertyDescriptor CONTENT_TYPE = new 
PropertyDescriptor.Builder()
+            .name("Content Type")
+            .description("Mime Type used to interpret the contents of the 
email, such as text/plain or text/html")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .defaultValue("text/plain")
+            .build();
+    public static final PropertyDescriptor FROM = new 
PropertyDescriptor.Builder()
+            .name("From")
+            .description("Specifies the Email address to use as the sender")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor TO = new 
PropertyDescriptor.Builder()
+            .name("To")
+            .description("The recipients to include in the To-Line of the 
email")
+            .required(false)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor CC = new 
PropertyDescriptor.Builder()
+            .name("CC")
+            .description("The recipients to include in the CC-Line of the 
email")
+            .required(false)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor BCC = new 
PropertyDescriptor.Builder()
+            .name("BCC")
+            .description("The recipients to include in the BCC-Line of the 
email")
+            .required(false)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor SUBJECT = new 
PropertyDescriptor.Builder()
+            .name("Subject")
+            .description("The email subject")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .defaultValue("Message from NiFi")
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+
+    private volatile RecordSetWriterFactory writerFactory;
+
+    /**
+     * Mapping of the mail properties to the NiFi PropertyDescriptors that 
will be evaluated at runtime
+     */
+    private static final Map<String, PropertyDescriptor> propertyToContext = 
new HashMap<>();
+
+    static {
+        propertyToContext.put("mail.smtp.host", SMTP_HOSTNAME);
+        propertyToContext.put("mail.smtp.port", SMTP_PORT);
+        propertyToContext.put("mail.smtp.socketFactory.port", SMTP_PORT);
+        propertyToContext.put("mail.smtp.socketFactory.class", 
SMTP_SOCKET_FACTORY);
+        propertyToContext.put("mail.smtp.auth", SMTP_AUTH);
+        propertyToContext.put("mail.smtp.starttls.enable", SMTP_TLS);
+        propertyToContext.put("mail.smtp.user", SMTP_USERNAME);
+        propertyToContext.put("mail.smtp.password", SMTP_PASSWORD);
+    }
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return Collections.unmodifiableList(Arrays.asList(
+                RecordSinkService.RECORD_WRITER_FACTORY,
+                SMTP_HOSTNAME,
+                SMTP_PORT,
+                SMTP_USERNAME,
+                SMTP_PASSWORD,
+                SMTP_AUTH,
+                SMTP_TLS,
+                SMTP_SOCKET_FACTORY,
+                HEADER_XMAILER,
+                CONTENT_TYPE,
+                FROM,
+                TO,
+                CC,
+                BCC,
+                SUBJECT
+        ));
+    }
+
+    @Override
+    protected Collection<ValidationResult> customValidate(final 
ValidationContext context) {
+        final List<ValidationResult> errors = new 
ArrayList<>(super.customValidate(context));
+
+        final String to = context.getProperty(TO).getValue();
+        final String cc = context.getProperty(CC).getValue();
+        final String bcc = context.getProperty(BCC).getValue();
+
+        if (to == null && cc == null && bcc == null) {
+            errors.add(new ValidationResult.Builder().subject("To, CC, 
BCC").valid(false).explanation("Must specify at least one To/CC/BCC 
address").build());
+        }
+
+        return errors;
+    }
+
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) {
+        writerFactory = 
context.getProperty(RECORD_WRITER_FACTORY).asControllerService(RecordSetWriterFactory.class);
+    }
+
+    public void sendMessage(final ConfigurationContext context, final String 
messageText) {
+        final Properties properties = getMailProperties(context);
+        final Session mailSession = createMailSession(properties);
+        final Message message = new MimeMessage(mailSession);
+
+        try {
+            
message.setFrom(InternetAddress.parse(context.getProperty(FROM).evaluateAttributeExpressions().getValue())[0]);
+
+            final InternetAddress[] toAddresses = toInetAddresses(context, TO);
+            message.setRecipients(Message.RecipientType.TO, toAddresses);
+
+            final InternetAddress[] ccAddresses = toInetAddresses(context, CC);
+            message.setRecipients(Message.RecipientType.CC, ccAddresses);
+
+            final InternetAddress[] bccAddresses = toInetAddresses(context, 
BCC);
+            message.setRecipients(Message.RecipientType.BCC, bccAddresses);
+
+            message.setHeader("X-Mailer", 
context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions().getValue());
+            
message.setSubject(context.getProperty(SUBJECT).evaluateAttributeExpressions().getValue());
+
+            final String contentType = 
context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions().getValue();
+            message.setContent(messageText, contentType);
+            message.setSentDate(new Date());
+
+            send(message);
+        } catch (final ProcessException | MessagingException e) {
+            throw new RuntimeException("Failed to send E-mail Notification", 
e);
+        }
+    }
+
+    @Override
+    public WriteResult sendData(RecordSet recordSet, Map<String, String> 
attributes, boolean sendZeroResults) throws IOException {
+        WriteResult writeResult;
+        try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            try (final RecordSetWriter writer = 
writerFactory.createWriter(getLogger(), recordSet.getSchema(), out, 
attributes)) {
+                writer.beginRecordSet();
+                Record r;
+                while ((r = recordSet.next()) != null) {
+                    writer.write(r);
+                    writer.flush();
+                }
+                writeResult = writer.finishRecordSet();
+                writer.flush();
+                sendMessage(getConfigurationContext(), out.toString());
+            }
+        } catch (SchemaNotFoundException e) {
+            throw new IOException(e);
+        }
+        return writeResult;
+    }
+
+    @Override
+    public void reset() {
+        RecordSinkService.super.reset();
+    }
+
+    /**
+     * Wrapper for static method {@link Transport#send(Message)} to add 
testability of this class.
+     *
+     * @param msg the message to send
+     * @throws MessagingException on error
+     */
+    protected void send(final Message msg) throws MessagingException {
+        Transport.send(msg);
+    }
+
+    /**
+     * Uses the mapping of javax.mail properties to NiFi PropertyDescriptors 
to build the required Properties object to be used for sending this email
+     *
+     * @param context context
+     * @return mail properties
+     */
+    private Properties getMailProperties(final ConfigurationContext context) {
+        final Properties properties = new Properties();
+
+        for (Map.Entry<String, PropertyDescriptor> entry : 
propertyToContext.entrySet()) {
+            // Evaluate the property descriptor against the variable registry
+            String property = entry.getKey();
+            String propValue = 
context.getProperty(entry.getValue()).evaluateAttributeExpressions().getValue();
+
+            // Nullable values are not allowed, so filter out
+            if (null != propValue) {

Review comment:
       I think so, though all the properties are put in the `propertyToContext` 
map are required attributes with default values. I think we shouldn't 
attributes without value there. What do you think of leaving it there for 
safety?




-- 
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]


Reply via email to