From bb289a69fa4d292378c5c534e10985be65b2e392 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 25 Mar 2018 22:16:09 -0300 Subject: generalization for notifications processing --- src/org/traccar/notification/NotificationMail.java | 62 +++++++++------------- 1 file changed, 26 insertions(+), 36 deletions(-) (limited to 'src/org/traccar/notification/NotificationMail.java') diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 6c81ecc79..3a2a19337 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -32,10 +32,7 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -public final class NotificationMail { - - private NotificationMail() { - } +public final class NotificationMail extends Notificator { private static Properties getProperties(PropertiesProvider provider) { Properties properties = new Properties(); @@ -84,7 +81,8 @@ public final class NotificationMail { return properties; } - public static void sendMailSync(long userId, Event event, Position position) throws MessagingException { + @Override + public void sendSync(long userId, Event event, Position position) throws NotificationException { User user = Context.getPermissionsManager().getUser(userId); Properties properties = null; @@ -103,40 +101,32 @@ public final class NotificationMail { MimeMessage message = new MimeMessage(session); - String from = properties.getProperty("mail.smtp.from"); - if (from != null) { - message.setFrom(new InternetAddress(from)); - } - - message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); - message.setSubject(mailMessage.getSubject()); - message.setSentDate(new Date()); - message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); - - Transport transport = session.getTransport(); try { - Context.getStatisticsManager().registerMail(); - transport.connect( - properties.getProperty("mail.smtp.host"), - properties.getProperty("mail.smtp.username"), - properties.getProperty("mail.smtp.password")); - transport.sendMessage(message, message.getAllRecipients()); - } finally { - transport.close(); - } - } + String from = properties.getProperty("mail.smtp.from"); + if (from != null) { + message.setFrom(new InternetAddress(from)); + } - public static void sendMailAsync(final long userId, final Event event, final Position position) { - new Thread(new Runnable() { - public void run() { - try { - sendMailSync(userId, event, position); - } catch (MessagingException error) { - Log.warning(error); - } + message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); + MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); + message.setSubject(mailMessage.getSubject()); + message.setSentDate(new Date()); + message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); + + Transport transport = session.getTransport(); + try { + Context.getStatisticsManager().registerMail(); + transport.connect( + properties.getProperty("mail.smtp.host"), + properties.getProperty("mail.smtp.username"), + properties.getProperty("mail.smtp.password")); + transport.sendMessage(message, message.getAllRecipients()); + } finally { + transport.close(); } - }).start(); + } catch (MessagingException e) { + throw new NotificationException(e); + } } } -- cgit v1.2.3