Alon Bar-Lev has uploaded a new change for review.

Change subject: tools: notifier: move mail specific validations into smtp 
transport
......................................................................

tools: notifier: move mail specific validations into smtp transport

the static usage will be altered once we reorder transport creation.

Change-Id: I5c956198a2bb41deff7259607feb83dfe1a7ad53
Signed-off-by: Alon Bar-Lev <alo...@redhat.com>
---
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/JavaMailSender.java
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/Smtp.java
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
4 files changed, 84 insertions(+), 105 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/20/24520/1

diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
index 20cc35c..f188e0b 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
@@ -15,6 +15,7 @@
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.apache.log4j.xml.DOMConfigurator;
+import org.ovirt.engine.core.notifier.transport.smtp.Smtp;
 import org.ovirt.engine.core.notifier.utils.NotificationProperties;
 
 /**
@@ -59,6 +60,7 @@
         try {
             prop = NotificationProperties.getInstance();
             prop.validate();
+            Smtp.validate(prop);
         } catch (Exception ex) {
             log.error("Failed to parse configuration.", ex);
             // print error also to stderr to be seen in console during service 
startup
diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/JavaMailSender.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/JavaMailSender.java
index d9bacda..5293600 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/JavaMailSender.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/JavaMailSender.java
@@ -37,25 +37,25 @@
     public JavaMailSender(NotificationProperties aMailProps) {
         Properties mailSessionProps = setCommonProperties(aMailProps);
 
-        mailSessionProps.put("mail.smtp.host", 
aMailProps.getProperty(NotificationProperties.MAIL_SERVER));
-        mailSessionProps.put("mail.smtp.port", 
aMailProps.getProperty(NotificationProperties.MAIL_PORT));
+        mailSessionProps.put("mail.smtp.host", 
aMailProps.getProperty(Smtp.MAIL_SERVER));
+        mailSessionProps.put("mail.smtp.port", 
aMailProps.getProperty(Smtp.MAIL_PORT));
         // enable SSL
-        if (NotificationProperties.MAIL_SMTP_ENCRYPTION_SSL.equals(
-                
aMailProps.getProperty(NotificationProperties.MAIL_SMTP_ENCRYPTION, true))) {
+        if (Smtp.MAIL_SMTP_ENCRYPTION_SSL.equals(
+                aMailProps.getProperty(Smtp.MAIL_SMTP_ENCRYPTION, true))) {
             mailSessionProps.put("mail.smtp.auth", "true");
             mailSessionProps.put("mail.smtp.socketFactory.class", 
"javax.net.ssl.SSLSocketFactory");
             mailSessionProps.put("mail.smtp.socketFactory.fallback", false);
-            mailSessionProps.put("mail.smtp.socketFactory.port", 
aMailProps.getProperty(NotificationProperties.MAIL_PORT));
-        } else if (NotificationProperties.MAIL_SMTP_ENCRYPTION_TLS.equals(
-                
aMailProps.getProperty(NotificationProperties.MAIL_SMTP_ENCRYPTION, true))) {
+            mailSessionProps.put("mail.smtp.socketFactory.port", 
aMailProps.getProperty(Smtp.MAIL_PORT));
+        } else if (Smtp.MAIL_SMTP_ENCRYPTION_TLS.equals(
+                aMailProps.getProperty(Smtp.MAIL_SMTP_ENCRYPTION, true))) {
             mailSessionProps.put("mail.smtp.auth", "true");
             mailSessionProps.put("mail.smtp.starttls.enable", "true");
             mailSessionProps.put("mail.smtp.starttls.required", "true");
         }
 
-        String password = 
aMailProps.getProperty(NotificationProperties.MAIL_PASSWORD, true);
+        String password = aMailProps.getProperty(Smtp.MAIL_PASSWORD, true);
         if (StringUtils.isNotEmpty(password)) {
-            auth = new 
EmailAuthenticator(aMailProps.getProperty(NotificationProperties.MAIL_USER, 
true),
+            auth = new 
EmailAuthenticator(aMailProps.getProperty(Smtp.MAIL_USER, true),
                     password);
             this.session = Session.getDefaultInstance(mailSessionProps, auth);
         } else {
@@ -75,7 +75,7 @@
             mailSessionProps.put("mail.debug", "true");
         }
 
-        isBodyHtml = 
aMailProps.getBoolean(NotificationProperties.HTML_MESSAGE_FORMAT, false);
+        isBodyHtml = aMailProps.getBoolean(Smtp.HTML_MESSAGE_FORMAT, false);
 
         return mailSessionProps;
     }
diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/Smtp.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/Smtp.java
index 504d432..98d653b 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/Smtp.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/Smtp.java
@@ -29,6 +29,19 @@
  */
 public class Smtp implements Transport {
 
+    public static final String MAIL_SERVER = "MAIL_SERVER";
+    public static final String MAIL_PORT = "MAIL_PORT";
+    public static final String MAIL_USER = "MAIL_USER";
+    public static final String MAIL_PASSWORD = "MAIL_PASSWORD";
+    public static final String MAIL_FROM = "MAIL_FROM";
+    public static final String MAIL_REPLY_TO = "MAIL_REPLY_TO";
+    public static final String HTML_MESSAGE_FORMAT = "HTML_MESSAGE_FORMAT";
+    public static final String MAIL_SMTP_ENCRYPTION = "MAIL_SMTP_ENCRYPTION";
+    public static final String MAIL_SMTP_ENCRYPTION_NONE = "none";
+    public static final String MAIL_SMTP_ENCRYPTION_SSL = "ssl";
+    public static final String MAIL_SMTP_ENCRYPTION_TLS = "tls";
+    private static final String GENERIC_VALIDATION_MESSAGE = "Check 
configuration file, ";
+
     private static final Logger log = Logger.getLogger(Smtp.class);
     private JavaMailSender mailSender;
     private String hostName;
@@ -36,7 +49,7 @@
 
     public Smtp(NotificationProperties mailProp) {
         mailSender = new JavaMailSender(mailProp);
-        String isBodyHtmlStr = 
mailProp.getProperty(NotificationProperties.HTML_MESSAGE_FORMAT);
+        String isBodyHtmlStr = mailProp.getProperty(HTML_MESSAGE_FORMAT);
         if (StringUtils.isNotEmpty(isBodyHtmlStr)) {
             isBodyHtml = Boolean.valueOf(isBodyHtmlStr);
         }
@@ -49,6 +62,61 @@
         }
     }
 
+    public static void validate(NotificationProperties props) {
+        // validate mandatory and non empty properties
+        props.requireOne(MAIL_SERVER);
+        // validate MAIL_PORT
+        props.requireAll(MAIL_PORT);
+        boolean mailPortValid = false;
+        try {
+            int port = new Integer(props.getProperty(MAIL_PORT));
+            if (port > 0 && port < 65536) {
+                mailPortValid = true;
+            }
+        } catch (NumberFormatException ex) {
+        }
+        if (!mailPortValid) {
+            throw new IllegalArgumentException(
+                    String.format("Check configuration file, MAIL_PORT value 
has to be in range from 1 to 65535,"
+                            + " currently '%s'",
+                            props.getProperty(MAIL_PORT)));
+        }
+
+        // validate MAIL_USER value
+        String emailUser = props.getProperty(MAIL_USER, true);
+        if (StringUtils.isEmpty(emailUser)
+                && 
(MAIL_SMTP_ENCRYPTION_SSL.equals(props.getProperty(MAIL_SMTP_ENCRYPTION, true))
+                        || 
MAIL_SMTP_ENCRYPTION_TLS.equals(props.getProperty(MAIL_SMTP_ENCRYPTION, true))
+                        || 
StringUtils.isNotEmpty(props.getProperty(MAIL_PASSWORD, true)))) {
+            throw new IllegalArgumentException(
+                    String.format(
+                            "'%s' must be set when SSL or TLS is enabled or 
when password is set",
+                            MAIL_USER));
+        }
+
+        if 
(!(MAIL_SMTP_ENCRYPTION_NONE.equals(props.getProperty(MAIL_SMTP_ENCRYPTION, 
true))
+                || 
MAIL_SMTP_ENCRYPTION_SSL.equals(props.getProperty(MAIL_SMTP_ENCRYPTION, true))
+                || 
MAIL_SMTP_ENCRYPTION_TLS.equals(props.getProperty(MAIL_SMTP_ENCRYPTION, 
true)))) {
+            throw new IllegalArgumentException(
+                    String.format(
+                            GENERIC_VALIDATION_MESSAGE + "'%s' value has to be 
one of: '%s', '%s', '%s'.",
+                            MAIL_SMTP_ENCRYPTION,
+                            MAIL_SMTP_ENCRYPTION_NONE,
+                            MAIL_SMTP_ENCRYPTION_SSL,
+                            MAIL_SMTP_ENCRYPTION_TLS
+                            ));
+        }
+
+        // validate email addresses
+        for (String property : new String[] {
+                MAIL_USER,
+                MAIL_FROM,
+                MAIL_REPLY_TO }) {
+            String candidate = props.getProperty(property);
+            props.validateEmail(property, candidate);
+        }
+    }
+
     public EventSenderResult send(AuditLogEvent event, AuditLogEventSubscriber 
subscriber) {
         EventSenderResult result = new EventSenderResult();
         EventMessageContent message = new EventMessageContent();
diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
index 43c2cf3..5989a64 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
@@ -43,33 +43,6 @@
      */
     public static final String DAYS_TO_SEND_ON_STARTUP = 
"DAYS_TO_SEND_ON_STARTUP";
 
-    /**
-     * Email parameters
-     */
-    public static final String MAIL_SERVER = "MAIL_SERVER";
-    public static final String MAIL_PORT = "MAIL_PORT";
-    public static final String MAIL_USER = "MAIL_USER";
-    public static final String MAIL_PASSWORD = "MAIL_PASSWORD";
-    public static final String MAIL_SMTP_ENCRYPTION = "MAIL_SMTP_ENCRYPTION";
-    public static final String MAIL_FROM = "MAIL_FROM";
-    public static final String MAIL_REPLY_TO = "MAIL_REPLY_TO";
-    public static final String HTML_MESSAGE_FORMAT = "HTML_MESSAGE_FORMAT";
-
-    /**
-     * No SMTP transport encryption (plain SMTP)
-     */
-    public static final String MAIL_SMTP_ENCRYPTION_NONE = "none";
-
-    /**
-     * SMTP transport encryption using SSL (SMTPS)
-     */
-    public static final String MAIL_SMTP_ENCRYPTION_SSL = "ssl";
-
-    /**
-     * SMTP transport encryption using TLS (SMTP with STARTTLS)
-     */
-    public static final String MAIL_SMTP_ENCRYPTION_TLS = "tls";
-
     private static final String GENERIC_MESSAGE = "Check configuration file, ";
 
     // Default files for defaults and overridden values:
@@ -119,8 +92,6 @@
      */
     public void validate() {
         validateCommon();
-
-        validateSmtp();
     }
 
     private void validateCommon() {
@@ -132,8 +103,6 @@
                 NotificationProperties.INTERVAL_IN_SECONDS,
                 NotificationProperties.IS_HTTPS_PROTOCOL,
                 NotificationProperties.REPEAT_NON_RESPONSIVE_NOTIFICATION);
-        // validate mandatory and non empty properties
-        requireOne(NotificationProperties.MAIL_SERVER);
 
         // validate non negative args
         for (String property : new String[] {
@@ -155,62 +124,11 @@
         }
     }
 
-    private void validateSmtp() {
-        // validate MAIL_PORT
-        requireAll(MAIL_PORT);
-        boolean mailPortValid = false;
-        try {
-            int port = new Integer(getProperty(MAIL_PORT));
-            if (port > 0 && port < 65536) {
-                mailPortValid = true;
-            }
-        } catch (NumberFormatException ex) {
-        }
-        if (!mailPortValid) {
-            throw new IllegalArgumentException(
-                    String.format("Check configuration file, MAIL_PORT value 
has to be in range from 1 to 65535,"
-                            + " currently '%s'",
-                            getProperty(MAIL_PORT)));
-        }
-
-        // validate MAIL_USER value
-        String emailUser = getProperty(NotificationProperties.MAIL_USER, true);
-        if (StringUtils.isEmpty(emailUser)
-                && 
(MAIL_SMTP_ENCRYPTION_SSL.equals(getProperty(MAIL_SMTP_ENCRYPTION, true))
-                        || 
MAIL_SMTP_ENCRYPTION_TLS.equals(getProperty(MAIL_SMTP_ENCRYPTION, true))
-                        || 
StringUtils.isNotEmpty(getProperty(NotificationProperties.MAIL_PASSWORD, 
true)))) {
-            throw new IllegalArgumentException(
-                    String.format(
-                            "'%s' must be set when SSL or TLS is enabled or 
when password is set",
-                            NotificationProperties.MAIL_USER));
-        }
-
-        if (!isSmtpEncryptionOptionValid()) {
-            throw new IllegalArgumentException(
-                    String.format(
-                            GENERIC_MESSAGE + "'%s' value has to be one of: 
'%s', '%s', '%s'.",
-                            NotificationProperties.MAIL_SMTP_ENCRYPTION,
-                            NotificationProperties.MAIL_SMTP_ENCRYPTION_NONE,
-                            NotificationProperties.MAIL_SMTP_ENCRYPTION_SSL,
-                            NotificationProperties.MAIL_SMTP_ENCRYPTION_TLS
-                            ));
-        }
-
-        // validate email addresses
-        for (String property : new String[] {
-                NotificationProperties.MAIL_USER,
-                NotificationProperties.MAIL_FROM,
-                NotificationProperties.MAIL_REPLY_TO }) {
-            String candidate = getProperty(property);
-            validateEmail(property, candidate);
-        }
-    }
-
     public boolean isConfigured(String property) {
         return !StringUtils.isEmpty(getProperty(property, true));
     }
 
-    private void requireAll(String... mandatoryProperties) {
+    public void requireAll(String... mandatoryProperties) {
         for (String property : mandatoryProperties) {
             if (StringUtils.isEmpty(getProperty(property, true))) {
                 throw new IllegalArgumentException(
@@ -221,7 +139,7 @@
         }
     }
 
-    private void requireOne(String... mandatoryProperties) {
+    public void requireOne(String... mandatoryProperties) {
         boolean provided = false;
         for (String property : mandatoryProperties) {
             if (isConfigured(property)) {
@@ -242,7 +160,7 @@
         }
     }
 
-    private void validateEmail(String propName, String propVal) {
+    public void validateEmail(String propName, String propVal) {
         if (!StringUtils.isEmpty(propVal)) {
             try {
                 new InternetAddress(propVal);
@@ -254,14 +172,5 @@
                         ex);
             }
         }
-    }
-
-    /**
-     * Returns {@code true} if mail transport encryption type is correctly 
specified, otherwise {@code false}
-     */
-    public boolean isSmtpEncryptionOptionValid() {
-        return 
MAIL_SMTP_ENCRYPTION_NONE.equals(getProperty(MAIL_SMTP_ENCRYPTION, true))
-                || 
MAIL_SMTP_ENCRYPTION_SSL.equals(getProperty(MAIL_SMTP_ENCRYPTION, true))
-                || 
MAIL_SMTP_ENCRYPTION_TLS.equals(getProperty(MAIL_SMTP_ENCRYPTION, true));
     }
 }


-- 
To view, visit http://gerrit.ovirt.org/24520
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5c956198a2bb41deff7259607feb83dfe1a7ad53
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alon Bar-Lev <alo...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to