Java tutorial
/* * Copyrights 2002-2011 Webb Fontaine * Developer: Sargis Harutyunyan * Date: 23/11/11 * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. */ package com.webbfontaine.valuewebb.irms.action.mail; import com.google.common.eventbus.Subscribe; import com.webbfontaine.valuewebb.irms.action.ActionDef; import com.webbfontaine.valuewebb.irms.action.ActionHandler; import com.webbfontaine.valuewebb.irms.action.logger.ActionLogger; import com.webbfontaine.valuewebb.props.ApplicationProperties; import org.apache.commons.lang3.StringUtils; import org.apache.commons.mail.*; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.activation.DataSource; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; @Name("mailActionHandler") @Scope(ScopeType.APPLICATION) public class MailActionHandler implements ActionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(MailActionHandler.class); private static final Pattern COMPILE = Pattern.compile(","); private ActionLogger actionLogger; private ApplicationProperties applicationProperties; @Subscribe public void handle(TextMailActionDef actionDef) { doHandle(actionDef, new SimpleMailCreator(this)); } @Subscribe public void handle(HtmlMailActionDef actionDef) { doHandle(actionDef, new HtmlMailCreator(this)); } private void doHandle(final MailActionDef actionDef, final MailCreator mailCreator) { try { doWork(actionDef, mailCreator); } catch (Exception e) { LOGGER.error("", e); } } private Void doWork(MailActionDef actionDef, MailCreator mailCreator) throws EmailException { LOGGER.debug("Creating/sending mail from definition: {}", actionDef); if (validate(actionDef)) { Email email = mailCreator.createMail(actionDef); doSend(email); doLog(actionDef); } else { LOGGER.warn("Message body is blank for: {}, will not send email alert.", actionDef); } return null; } private static boolean validate(MailActionDef actionDef) { return StringUtils.isNotBlank(actionDef.getMessage()); } public void doSend(Email email) { try { email.send(); } catch (EmailException e) { LOGGER.error("", e); } } private void doLog(ActionDef actionDef) { try { actionLogger.log(actionDef); } catch (Exception e) { LOGGER.error("", e); } } private String getSmtpServer() { return applicationProperties.getSmtpServer(); } private int getSmtpServerPort() { return applicationProperties.getSmtpServerPort(); } private String getFrom(MailActionDef event) { return StringUtils.defaultIfBlank(event.getFrom(), applicationProperties.getEmailFrom()); } @In(create = true) public void setApplicationProperties(ApplicationProperties applicationProperties) { this.applicationProperties = applicationProperties; } @In(create = true) public void setActionLogger(ActionLogger actionLogger) { this.actionLogger = actionLogger; } private static class SimpleMailCreator implements MailCreator { private final MailActionHandler mailActionHandler; private SimpleMailCreator(MailActionHandler mailActionHandler) { this.mailActionHandler = mailActionHandler; } @Override public Email createMail(MailActionDef actionDef) throws EmailException { MultiPartEmail email = new MultiPartEmail(); setBaseParameters(mailActionHandler, email, actionDef); email.setMsg(actionDef.getMessage()); return email; } } private static class HtmlMailCreator implements MailCreator { private final MailActionHandler mailActionHandler; private HtmlMailCreator(MailActionHandler mailActionHandler) { this.mailActionHandler = mailActionHandler; } @Override public Email createMail(MailActionDef actionDef) throws EmailException { HtmlEmail email = new HtmlEmail(); setBaseParameters(mailActionHandler, email, actionDef); email.setHtmlMsg(actionDef.getMessage()); return email; } } private static void setBaseParameters(MailActionHandler actionHandler, MultiPartEmail email, MailActionDef actionDef) throws EmailException { email.setHostName(actionHandler.getSmtpServer()); email.setSmtpPort(actionHandler.getSmtpServerPort()); email.setFrom(actionHandler.getFrom(actionDef)); email.setSubject(actionDef.getSubject()); for (Attachable attachment : actionDef.getAttachments()) { attach(email, attachment); } addRecipients(email, actionDef.getRecipients()); } private static void addRecipients(Email email, List<String> recipients) throws EmailException { for (String recipient : recipients) { List<String> newRcpts = Arrays.asList(COMPILE.split(recipient)); if (newRcpts.size() == 1) { email.addTo(newRcpts.get(0).trim()); } else { addRecipients(email, newRcpts); } } } private static void attach(MultiPartEmail email, Attachable attachment) throws EmailException { try { DataSource source = attachment.getDataSource(); InputStream inputStream = source.getInputStream(); if (inputStream != null) { email.attach(dataSource(inputStream, source.getContentType()), attachment.getName(), "", EmailAttachment.ATTACHMENT); } else { LOGGER.warn("Attached doc: {} content is empty, nothing to attach...", attachment.getName()); } } catch (IOException e) { throw new EmailException(e); } } private static ByteArrayDataSource dataSource(InputStream inputStream, String contentType) throws IOException { return new ByteArrayDataSource(inputStream, contentType); } }