Alon Bar-Lev has uploaded a new change for review. Change subject: tools: notifier: smtp: collapse JavaMailSender into transport ......................................................................
tools: notifier: smtp: collapse JavaMailSender into transport transport is all about sending smtp, there is no need for cross class access and multiple configuration access. Change-Id: I5082144693689c4feebb9c7e3be9bffaf75b8d72 Signed-off-by: Alon Bar-Lev <alo...@redhat.com> --- D 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 2 files changed, 125 insertions(+), 165 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/30/24530/1 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 deleted file mode 100644 index 5293600..0000000 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/transport/smtp/JavaMailSender.java +++ /dev/null @@ -1,146 +0,0 @@ -package org.ovirt.engine.core.notifier.transport.smtp; - -import java.util.Date; -import java.util.Properties; - -import javax.mail.Address; -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.InternetAddress; -import javax.mail.internet.MimeMessage; - -import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Logger; -import org.ovirt.engine.core.notifier.utils.NotificationProperties; - -/** - * Support email sending by SMTP or SMTP over SSL methods. - */ -public class JavaMailSender { - - private static final Logger log = Logger.getLogger(JavaMailSender.class); - private Session session = null; - private InternetAddress from = null; - private InternetAddress replyTo = null; - private boolean isBodyHtml = false; - private EmailAuthenticator auth; - - /** - * Creates an instance of {@code javax.mail.Session} which could be used for multiple messages dispatch. - * @param aMailProps - * properties required for creating a mail session - */ - public JavaMailSender(NotificationProperties aMailProps) { - Properties mailSessionProps = setCommonProperties(aMailProps); - - mailSessionProps.put("mail.smtp.host", aMailProps.getProperty(Smtp.MAIL_SERVER)); - mailSessionProps.put("mail.smtp.port", aMailProps.getProperty(Smtp.MAIL_PORT)); - // enable SSL - 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(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(Smtp.MAIL_PASSWORD, true); - if (StringUtils.isNotEmpty(password)) { - auth = new EmailAuthenticator(aMailProps.getProperty(Smtp.MAIL_USER, true), - password); - this.session = Session.getDefaultInstance(mailSessionProps, auth); - } else { - this.session = Session.getInstance(mailSessionProps); - } - } - - /** - * Set common properties for both secured and non-secured mail session - * @param aMailProps - * mail configuration properties - * @return a session common properties - */ - private Properties setCommonProperties(NotificationProperties aMailProps) { - Properties mailSessionProps = new Properties(); - if (log.isTraceEnabled()) { - mailSessionProps.put("mail.debug", "true"); - } - - isBodyHtml = aMailProps.getBoolean(Smtp.HTML_MESSAGE_FORMAT, false); - - return mailSessionProps; - } - - /** - * Sends a message to a recipient using pre-configured mail session, either as a plan text message or as a html - * message body - * @param recipient - * a recipient mail address - * @param messageSubject - * the subject of the message - * @param messageBody - * the body of the message - * @throws MessagingException - */ - public void send(String recipient, String messageSubject, String messageBody) throws MessagingException { - try { - Message msg = new MimeMessage(session); - msg.setFrom(from); - InternetAddress address = new InternetAddress(recipient); - msg.setRecipient(Message.RecipientType.TO, address); - if (replyTo != null) { - msg.setReplyTo(new Address[] { replyTo }); - } - msg.setSubject(messageSubject); - if (isBodyHtml){ - msg.setContent(String.format("<html><head><title>%s</title></head><body><p>%s</body></html>", - messageSubject, - messageBody), "text/html"); - } else { - msg.setText(messageBody); - } - msg.setSentDate(new Date()); - Transport.send(msg); - } catch (MessagingException mex) { - StringBuilder errorMsg = new StringBuilder("Failed to send message "); - if (from != null) { - errorMsg.append(" from " + from.toString()); - } - if (StringUtils.isNotBlank(recipient)) { - errorMsg.append(" to " + recipient); - } - if (StringUtils.isNotBlank(messageSubject)) { - errorMsg.append(" with subject " + messageSubject); - } - errorMsg.append(" due to to error: " + mex.getMessage()); - log.error(errorMsg.toString(), mex); - throw mex; - } - } - - /** - * An implementation of the {@link Authenticator}, holds the authentication credentials for a network connection. - */ - private class EmailAuthenticator extends Authenticator { - private String userName; - private String password; - public EmailAuthenticator(String userName, String password) { - this.userName = userName; - this.password = password; - } - - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(userName, password); - } - } -} 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 989d836..bd6ba09 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 @@ -3,8 +3,17 @@ import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.Date; +import java.util.Properties; +import javax.mail.Address; +import javax.mail.Authenticator; +import javax.mail.Message; import javax.mail.MessagingException; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; @@ -29,36 +38,69 @@ */ 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 MAIL_SERVER = "MAIL_SERVER"; + private static final String MAIL_PORT = "MAIL_PORT"; + private static final String MAIL_USER = "MAIL_USER"; + private static final String MAIL_PASSWORD = "MAIL_PASSWORD"; + private static final String MAIL_FROM = "MAIL_FROM"; + private static final String MAIL_REPLY_TO = "MAIL_REPLY_TO"; + private static final String HTML_MESSAGE_FORMAT = "HTML_MESSAGE_FORMAT"; + private static final String MAIL_SMTP_ENCRYPTION = "MAIL_SMTP_ENCRYPTION"; + private static final String MAIL_SMTP_ENCRYPTION_NONE = "none"; + private static final String MAIL_SMTP_ENCRYPTION_SSL = "ssl"; + private 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; private boolean isBodyHtml = false; + private Session session = null; + private InternetAddress from = null; + private InternetAddress replyTo = null; + private EmailAuthenticator auth; public Smtp(NotificationProperties props) { - mailSender = new JavaMailSender(props); - String isBodyHtmlStr = props.getProperty(HTML_MESSAGE_FORMAT); - if (StringUtils.isNotEmpty(isBodyHtmlStr)) { - isBodyHtml = Boolean.valueOf(isBodyHtmlStr); - } + + Properties mailSessionProps = new Properties(); try { hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { Smtp.log.error("Failed to resolve machine name, using localhost instead.", e); hostName = "localhost"; + } + + isBodyHtml = props.getBoolean(HTML_MESSAGE_FORMAT, false); + from = props.validateEmail(MAIL_FROM); + replyTo = props.validateEmail(MAIL_REPLY_TO); + + if (log.isTraceEnabled()) { + mailSessionProps.put("mail.debug", "true"); + } + + mailSessionProps.put("mail.smtp.host", props.getProperty(MAIL_SERVER)); + mailSessionProps.put("mail.smtp.port", props.getProperty(MAIL_PORT)); + // enable SSL + if (MAIL_SMTP_ENCRYPTION_SSL.equals( + props.getProperty(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", props.getProperty(MAIL_PORT)); + } else if (MAIL_SMTP_ENCRYPTION_TLS.equals( + props.getProperty(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 = props.getProperty(MAIL_PASSWORD, true); + if (StringUtils.isNotEmpty(password)) { + auth = new EmailAuthenticator(props.getProperty(Smtp.MAIL_USER, true), + password); + session = Session.getDefaultInstance(mailSessionProps, auth); + } else { + session = Session.getInstance(mailSessionProps); } } @@ -125,7 +167,7 @@ boolean shouldRetry = false; try { - mailSender.send(recipient, message.getMessageSubject(), message.getMessageBody()); + sendMail(recipient, message.getMessageSubject(), message.getMessageBody()); } catch (MessagingException ex) { result.setReason(ex.getMessage()); shouldRetry = true; @@ -137,7 +179,7 @@ try { // hold the next send attempt for 30 seconds in case of a busy mail server Thread.sleep(30000); - mailSender.send(recipient, message.getMessageSubject(), message.getMessageBody()); + sendMail(recipient, message.getMessageSubject(), message.getMessageBody()); } catch (MessagingException ex) { result.setReason(ex.getMessage()); shouldRetry = true; @@ -152,5 +194,69 @@ } return result; } + + /** + * Sends a message to a recipient using pre-configured mail session, either as a plan text message or as a html + * message body + * @param recipient + * a recipient mail address + * @param messageSubject + * the subject of the message + * @param messageBody + * the body of the message + * @throws MessagingException + */ + private void sendMail(String recipient, String messageSubject, String messageBody) throws MessagingException { + try { + Message msg = new MimeMessage(session); + msg.setFrom(from); + InternetAddress address = new InternetAddress(recipient); + msg.setRecipient(Message.RecipientType.TO, address); + if (replyTo != null) { + msg.setReplyTo(new Address[] { replyTo }); + } + msg.setSubject(messageSubject); + if (isBodyHtml){ + msg.setContent(String.format("<html><head><title>%s</title></head><body><p>%s</body></html>", + messageSubject, + messageBody), "text/html"); + } else { + msg.setText(messageBody); + } + msg.setSentDate(new Date()); + javax.mail.Transport.send(msg); + } catch (MessagingException mex) { + StringBuilder errorMsg = new StringBuilder("Failed to send message "); + if (from != null) { + errorMsg.append(" from " + from.toString()); + } + if (StringUtils.isNotBlank(recipient)) { + errorMsg.append(" to " + recipient); + } + if (StringUtils.isNotBlank(messageSubject)) { + errorMsg.append(" with subject " + messageSubject); + } + errorMsg.append(" due to to error: " + mex.getMessage()); + log.error(errorMsg.toString(), mex); + throw mex; + } + } + + /** + * An implementation of the {@link Authenticator}, holds the authentication credentials for a network connection. + */ + private class EmailAuthenticator extends Authenticator { + private String userName; + private String password; + public EmailAuthenticator(String userName, String password) { + this.userName = userName; + this.password = password; + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(userName, password); + } + } } -- To view, visit http://gerrit.ovirt.org/24530 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5082144693689c4feebb9c7e3be9bffaf75b8d72 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