Skip to content

Commit

Permalink
fix: use configured SMTP hostname and port
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Mar 31, 2024
1 parent f120a85 commit 300188a
Showing 1 changed file with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,35 @@ public class EmailProvenanceReporter extends AbstractProvenanceReporter {
.name("SMTP Hostname")
.description("The hostname of the SMTP host")
.required(true)
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();

public static final PropertyDescriptor SMTP_PORT = new PropertyDescriptor.Builder()
.name("SMTP Port")
.description("The Port used for SMTP communications")
.description("The port used for SMTP communications")
.required(true)
.defaultValue("25")
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.PORT_VALIDATOR)
.build();

public static final PropertyDescriptor SMTP_AUTH = new PropertyDescriptor.Builder()
.name("SMTP Auth")
.description("Flag indicating whether authentication should be used")
.required(true)
.addValidator(StandardValidators.BOOLEAN_VALIDATOR)
.defaultValue("true")
.build();

public static final PropertyDescriptor SMTP_USERNAME = new PropertyDescriptor.Builder()
.name("SMTP Username")
.description("Username for the SMTP account")
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.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.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.required(false)
.sensitive(true)
Expand All @@ -62,31 +65,21 @@ public class EmailProvenanceReporter extends AbstractProvenanceReporter {
.displayName("SMTP STARTTLS")
.description("Flag indicating whether Opportunistic TLS should be enabled using STARTTLS command")
.required(true)
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.BOOLEAN_VALIDATOR)
.defaultValue("false")
.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.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.BOOLEAN_VALIDATOR)
.defaultValue("true")
.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.FLOWFILE_ATTRIBUTES)
.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.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.defaultValue("NiFi")
.build();
Expand All @@ -95,7 +88,6 @@ public class EmailProvenanceReporter extends AbstractProvenanceReporter {
.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.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.defaultValue("text/plain")
.build();
Expand All @@ -104,7 +96,6 @@ public class EmailProvenanceReporter extends AbstractProvenanceReporter {
.description("Specifies the Email address to use as the sender. "
+ "Comma separated sequence of addresses following RFC822 syntax.")
.required(true)
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();
public static final PropertyDescriptor TO = new PropertyDescriptor.Builder()
Expand Down Expand Up @@ -148,6 +139,7 @@ public List<PropertyDescriptor> getSupportedPropertyDescriptors() {
descriptors = super.getSupportedPropertyDescriptors();
descriptors.add(SMTP_HOSTNAME);
descriptors.add(SMTP_PORT);
descriptors.add(SMTP_AUTH);
descriptors.add(SMTP_USERNAME);
descriptors.add(SMTP_PASSWORD);
descriptors.add(SMTP_TLS);
Expand All @@ -163,6 +155,22 @@ public List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return descriptors;
}

/**
* 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 Collection<ValidationResult> customValidate(final ValidationContext context) {
final List<ValidationResult> errors = new ArrayList<>(super.customValidate(context));
Expand All @@ -186,6 +194,23 @@ private void setMessageHeader(final String header, final String value, final Mes
}
}

private Properties getEmailProperties(ReportingContext context) {
final Properties properties = new Properties();

for (final Map.Entry<String, PropertyDescriptor> entry : propertyToContext.entrySet()) {
// Evaluate the property descriptor against the flow file
final String propertyValue = context.getProperty(entry.getValue()).getValue();
final String property = entry.getKey();

// Nullable values are not allowed, so filter out
if (null != propertyValue) {
properties.setProperty(property, propertyValue);
}
}

return properties;
}

/**
* 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.
Expand Down Expand Up @@ -301,7 +326,7 @@ public void sendErrorEmail(Map<String, Object> event, ReportingContext context)
String emailSubject = "Error occurred in processor " + event.get("component_name") + " "
+ "in process group " + event.get("process_group_name");

final Properties properties = new Properties();
final Properties properties = this.getEmailProperties(context);
final Session mailSession = this.createMailSession(properties, context);
final Message message = new MimeMessage(mailSession);

Expand Down

0 comments on commit 300188a

Please sign in to comment.