You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by pv...@apache.org on 2022/01/20 09:28:35 UTC
[nifi] branch main updated: NIFI-9601 Upgraded nifi-bootstrap to Jakarta Mail 2
This is an automated email from the ASF dual-hosted git repository.
pvillard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new c42cc61 NIFI-9601 Upgraded nifi-bootstrap to Jakarta Mail 2
c42cc61 is described below
commit c42cc61f71478e6d80434787b11521c5c12c8c18
Author: exceptionfactory <ex...@apache.org>
AuthorDate: Wed Jan 19 15:10:47 2022 -0600
NIFI-9601 Upgraded nifi-bootstrap to Jakarta Mail 2
Signed-off-by: Pierre Villard <pi...@gmail.com>
This closes #5686.
---
nifi-bootstrap/pom.xml | 17 ++-
.../email/EmailNotificationService.java | 26 +++--
.../TestCustomNotificationService.java | 84 ---------------
.../email/EmailNotificationServiceTest.java | 118 +++++++++++++++++++++
.../src/test/resources/notification-services.xml | 24 -----
5 files changed, 144 insertions(+), 125 deletions(-)
diff --git a/nifi-bootstrap/pom.xml b/nifi-bootstrap/pom.xml
index 5fd23db..bc012f8 100644
--- a/nifi-bootstrap/pom.xml
+++ b/nifi-bootstrap/pom.xml
@@ -58,9 +58,14 @@ language governing permissions and limitations under the License. -->
<scope>runtime</scope>
</dependency>
<dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.7</version>
+ <groupId>jakarta.mail</groupId>
+ <artifactId>jakarta.mail-api</artifactId>
+ <version>2.0.1</version>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.mail</groupId>
+ <artifactId>jakarta.mail</artifactId>
+ <version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
@@ -85,5 +90,11 @@ language governing permissions and limitations under the License. -->
<artifactId>nifi-properties-loader</artifactId>
<version>1.16.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.nifi</groupId>
+ <artifactId>nifi-mock</artifactId>
+ <version>1.16.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/notification/email/EmailNotificationService.java b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/notification/email/EmailNotificationService.java
index 588b069..b9caf8d 100644
--- a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/notification/email/EmailNotificationService.java
+++ b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/notification/email/EmailNotificationService.java
@@ -25,16 +25,16 @@ import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
-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.Message.RecipientType;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeMessage;
+import jakarta.mail.Authenticator;
+import jakarta.mail.Message;
+import jakarta.mail.MessagingException;
+import jakarta.mail.PasswordAuthentication;
+import jakarta.mail.Session;
+import jakarta.mail.Transport;
+import jakarta.mail.Message.RecipientType;
+import jakarta.mail.internet.AddressException;
+import jakarta.mail.internet.InternetAddress;
+import jakarta.mail.internet.MimeMessage;
import org.apache.nifi.bootstrap.notification.AbstractNotificationService;
import org.apache.nifi.bootstrap.notification.NotificationContext;
import org.apache.nifi.bootstrap.notification.NotificationFailedException;
@@ -272,20 +272,18 @@ public class EmailNotificationService extends AbstractNotificationService {
*/
private Session createMailSession(final Properties properties) {
String authValue = properties.getProperty("mail.smtp.auth");
- Boolean auth = Boolean.valueOf(authValue);
+ final boolean auth = Boolean.parseBoolean(authValue);
/*
* Conditionally create a password authenticator if the 'auth' parameter is set.
*/
- final Session mailSession = auth ? Session.getInstance(properties, new Authenticator() {
+ return auth ? Session.getInstance(properties, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = properties.getProperty("mail.smtp.user"), password = properties.getProperty("mail.smtp.password");
return new PasswordAuthentication(username, password);
}
}) : Session.getInstance(properties); // without auth
-
- return mailSession;
}
}
diff --git a/nifi-bootstrap/src/test/groovy/org/apache/nifi/bootstrap/notification/TestCustomNotificationService.java b/nifi-bootstrap/src/test/groovy/org/apache/nifi/bootstrap/notification/TestCustomNotificationService.java
deleted file mode 100644
index 515d2ac..0000000
--- a/nifi-bootstrap/src/test/groovy/org/apache/nifi/bootstrap/notification/TestCustomNotificationService.java
+++ /dev/null
@@ -1,84 +0,0 @@
-package org.apache.nifi.bootstrap.notification;
-
-/*
- * 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.
- */
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.nifi.components.PropertyDescriptor;
-import org.apache.nifi.expression.ExpressionLanguageScope;
-import org.apache.nifi.processor.util.StandardValidators;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class TestCustomNotificationService extends AbstractNotificationService {
-
- private static Logger logger = LoggerFactory.getLogger(TestCustomNotificationService.class);
-
- public static final PropertyDescriptor CUSTOM_HOSTNAME = new PropertyDescriptor.Builder()
- .name("Custom Hostname")
- .description("The hostname of the Custom Server that is used to send notifications")
- .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
- .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
- .required(true)
- .build();
- public static final PropertyDescriptor CUSTOM_USERNAME = new PropertyDescriptor.Builder()
- .name("Custom Username")
- .description("Username for the account")
- .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
- .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
- .required(false)
- .build();
- public static final PropertyDescriptor CUSTOM_PASSWORD = new PropertyDescriptor.Builder()
- .name("Custom Password")
- .description("Password for the account")
- .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
- .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
- .required(false)
- .sensitive(true)
- .build();
-
- /**
- * 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("custom.host", CUSTOM_HOSTNAME);
- propertyToContext.put("custom.user", CUSTOM_USERNAME);
- propertyToContext.put("custom.password", CUSTOM_PASSWORD);
- }
-
- @Override
- protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
- final List<PropertyDescriptor> properties = new ArrayList<>();
- properties.add(CUSTOM_HOSTNAME);
- properties.add(CUSTOM_USERNAME);
- properties.add(CUSTOM_PASSWORD);
- return properties;
- }
-
- @Override
- public void notify(NotificationContext context, NotificationType type, String subject, String message) throws NotificationFailedException {
- logger.info(context.getProperty(CUSTOM_HOSTNAME).evaluateAttributeExpressions().getValue());
- logger.info(context.getProperty(CUSTOM_USERNAME).evaluateAttributeExpressions().getValue());
- logger.info(context.getProperty(CUSTOM_PASSWORD).evaluateAttributeExpressions().getValue());
- }
-}
diff --git a/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/email/EmailNotificationServiceTest.java b/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/email/EmailNotificationServiceTest.java
new file mode 100644
index 0000000..10e08f3
--- /dev/null
+++ b/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/email/EmailNotificationServiceTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.bootstrap.email;
+
+import jakarta.mail.MessagingException;
+import org.apache.nifi.bootstrap.notification.NotificationContext;
+import org.apache.nifi.bootstrap.notification.NotificationFailedException;
+import org.apache.nifi.bootstrap.notification.NotificationInitializationContext;
+import org.apache.nifi.bootstrap.notification.NotificationType;
+import org.apache.nifi.bootstrap.notification.email.EmailNotificationService;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.PropertyValue;
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.util.MockPropertyValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class EmailNotificationServiceTest {
+
+ private static final String SUBJECT = "Subject";
+
+ private static final String MESSAGE = "Message";
+
+ private static final String LOCALHOST_ADDRESS = "127.0.0.1";
+
+ private static final String ADDRESS = "username@localhost.localdomain";
+
+ @Test
+ public void testNotifyMessagingException() {
+ final Map<PropertyDescriptor, PropertyValue> properties = getProperties();
+ final EmailNotificationService service = getNotificationService(properties);
+
+ final NotificationFailedException exception = assertThrows(NotificationFailedException.class, () -> service.notify(getNotificationContext(), NotificationType.NIFI_STARTED, SUBJECT, MESSAGE));
+ final Throwable cause = exception.getCause();
+ assertTrue(cause instanceof MessagingException);
+ }
+
+ private EmailNotificationService getNotificationService(final Map<PropertyDescriptor, PropertyValue> properties) {
+ final EmailNotificationService service = new EmailNotificationService();
+ final NotificationInitializationContext context = new NotificationInitializationContext() {
+ @Override
+ public String getIdentifier() {
+ return NotificationInitializationContext.class.getName();
+ }
+
+ @Override
+ public PropertyValue getProperty(final PropertyDescriptor descriptor) {
+ final PropertyValue propertyValue = properties.get(descriptor);
+ return propertyValue == null ? new MockPropertyValue(descriptor.getDefaultValue()) : propertyValue;
+ }
+
+ @Override
+ public Map<String, String> getAllProperties() {
+ final Map<String, String> allProperties = new HashMap<>();
+ for (final Map.Entry<PropertyDescriptor, PropertyValue> entry : properties.entrySet()) {
+ allProperties.put(entry.getKey().getName(), entry.getValue().getValue());
+ }
+ return allProperties;
+ }
+ };
+
+ service.initialize(context);
+ return service;
+ }
+
+ private NotificationContext getNotificationContext() {
+ final Map<PropertyDescriptor, PropertyValue> properties = getProperties();
+ return new NotificationContext() {
+ @Override
+ public PropertyValue getProperty(final PropertyDescriptor descriptor) {
+ final PropertyValue propertyValue = properties.get(descriptor);
+ return propertyValue == null ? new MockPropertyValue(descriptor.getDefaultValue()) : propertyValue;
+ }
+
+ @Override
+ public Map<PropertyDescriptor, String> getProperties() {
+ final Map<PropertyDescriptor, String> propertyValues = new HashMap<>();
+ for (final Map.Entry<PropertyDescriptor, PropertyValue> entry : properties.entrySet()) {
+ propertyValues.put(entry.getKey(), entry.getValue().getValue());
+ }
+ return propertyValues;
+ }
+ };
+ }
+
+ private Map<PropertyDescriptor, PropertyValue> getProperties() {
+ final Map<PropertyDescriptor, PropertyValue> properties = new HashMap<>();
+
+ properties.put(EmailNotificationService.SMTP_HOSTNAME, new MockPropertyValue(LOCALHOST_ADDRESS));
+
+ final int port = NetworkUtils.getAvailableTcpPort();
+ properties.put(EmailNotificationService.SMTP_PORT, new MockPropertyValue(Integer.toString(port)));
+ properties.put(EmailNotificationService.SMTP_AUTH, new MockPropertyValue(Boolean.FALSE.toString()));
+ properties.put(EmailNotificationService.FROM, new MockPropertyValue(ADDRESS));
+ properties.put(EmailNotificationService.TO, new MockPropertyValue(ADDRESS));
+
+ return properties;
+ }
+}
diff --git a/nifi-bootstrap/src/test/resources/notification-services.xml b/nifi-bootstrap/src/test/resources/notification-services.xml
deleted file mode 100644
index 5f02a3b..0000000
--- a/nifi-bootstrap/src/test/resources/notification-services.xml
+++ /dev/null
@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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.
--->
-<services>
- <service>
- <id>custom-notification</id>
- <class>org.apache.nifi.bootstrap.notification.TestCustomNotificationService</class>
- <property name="Custom Hostname">${test.server}</property>
- <property name="Custom Username">${test.username}</property>
- <property name="Custom Password">${test.password}</property>
- </service>
-</services>
\ No newline at end of file