Skip to content

Commit

Permalink
Re-implement mail notificator
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Sep 29, 2018
1 parent 83764da commit 6a218f0
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 47 deletions.
9 changes: 9 additions & 0 deletions src/org/traccar/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.traccar.database.DriversManager;
import org.traccar.database.IdentityManager;
import org.traccar.database.LdapProvider;
import org.traccar.database.MailManager;
import org.traccar.database.MaintenancesManager;
import org.traccar.database.MediaManager;
import org.traccar.database.NotificationManager;
Expand Down Expand Up @@ -141,6 +142,12 @@ public static LdapProvider getLdapProvider() {
return ldapProvider;
}

private static MailManager mailManager;

public static MailManager getMailManager() {
return mailManager;
}

private static MediaManager mediaManager;

public static MediaManager getMediaManager() {
Expand Down Expand Up @@ -378,6 +385,8 @@ public static void init(String configFile) throws Exception {
ldapProvider = new LdapProvider(config);
}

mailManager = new MailManager();

mediaManager = new MediaManager(config.getString("media.path"));

if (dataManager != null) {
Expand Down
83 changes: 45 additions & 38 deletions src/org/traccar/database/MailManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@
*/
package org.traccar.database;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
import org.traccar.model.User;
import org.traccar.notification.PropertiesProvider;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.FullMessage;
import org.traccar.notification.MessageException;
import org.traccar.notification.NotificationFormatter;
import org.traccar.notification.PropertiesProvider;
import java.util.Properties;

public final class MailManager {

Expand Down Expand Up @@ -88,8 +86,13 @@ private static Properties getProperties(PropertiesProvider provider) {
return properties;
}

@Override
public void sendSync(long userId, Event event, Position position) throws MessageException {
public void sendMessage(
long userId, String subject, String body) throws MessagingException {
sendMessage(userId, subject, body, null);
}

public void sendMessage(
long userId, String subject, String body, MimeBodyPart attachment) throws MessagingException {
User user = Context.getPermissionsManager().getUser(userId);

Properties properties = null;
Expand All @@ -108,31 +111,35 @@ public void sendSync(long userId, Event event, Position position) throws Message

MimeMessage message = new MimeMessage(session);

try {
String from = properties.getProperty("mail.smtp.from");
if (from != null) {
message.setFrom(new InternetAddress(from));
}
String from = properties.getProperty("mail.smtp.from");
if (from != null) {
message.setFrom(new InternetAddress(from));
}

message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
FullMessage fullMessage = NotificationFormatter.formatFullMessage(userId, event, position);
message.setSubject(fullMessage.getSubject());
message.setSentDate(new Date());
message.setContent(fullMessage.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();
}
} catch (MessagingException e) {
throw new MessageException(e);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject(subject);
message.setSentDate(new Date());

if (attachment != null) {
Multipart multipart = new MimeMultipart();

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachment);

message.setContent(multipart);
} else {
message.setContent(body, "text/html; charset=utf-8");
}

try (Transport transport = session.getTransport()) {
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());
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/org/traccar/notification/NotificatorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@
import org.slf4j.LoggerFactory;
import org.traccar.Context;
import org.traccar.model.Typed;
import org.traccar.notificators.NotificatorFirebase;
import org.traccar.notificators.NotificatorMail;
import org.traccar.notificators.NotificatorNull;
import org.traccar.notificators.Notificator;
import org.traccar.notificators.NotificatorSms;
import org.traccar.notificators.NotificatorWeb;

public final class NotificatorManager {

private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorManager.class);

private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notificators.NotificatorWeb";
private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notificators.NotificatorMail";
private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notificators.NotificatorSms";
private static final String DEFAULT_FIREBASE_NOTIFICATOR = "org.traccar.notificators.NotificatorFirebase";
private static final Notificator NULL_NOTIFICATOR = new NotificatorNull();

private final Map<String, Notificator> notificators = new HashMap<>();
private static final Notificator NULL_NOTIFICATOR = new NotificatorNull();

public NotificatorManager() {
final String[] types = Context.getConfig().getString("notificator.types", "").split(",");
for (String type : types) {
String defaultNotificator = "";
switch (type) {
case "web":
defaultNotificator = DEFAULT_WEB_NOTIFICATOR;
defaultNotificator = NotificatorWeb.class.getCanonicalName();
break;
case "mail":
defaultNotificator = DEFAULT_MAIL_NOTIFICATOR;
defaultNotificator = NotificatorMail.class.getCanonicalName();
break;
case "sms":
defaultNotificator = DEFAULT_SMS_NOTIFICATOR;
defaultNotificator = NotificatorSms.class.getCanonicalName();
break;
case "firebase":
defaultNotificator = DEFAULT_FIREBASE_NOTIFICATOR;
defaultNotificator = NotificatorFirebase.class.getCanonicalName();
break;
default:
break;
Expand Down
40 changes: 40 additions & 0 deletions src/org/traccar/notificators/NotificatorMail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016 - 2018 Anton Tananaev ([email protected])
* Copyright 2017 - 2018 Andrey Kunitsyn ([email protected])
*
* Licensed 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.traccar.notificators;

import org.traccar.Context;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.notification.FullMessage;
import org.traccar.notification.MessageException;
import org.traccar.notification.NotificationFormatter;

import javax.mail.MessagingException;

public final class NotificatorMail extends Notificator {

@Override
public void sendSync(long userId, Event event, Position position) throws MessageException {
try {
FullMessage message = NotificationFormatter.formatFullMessage(userId, event, position);
Context.getMailManager().sendMessage(userId, message.getSubject(), message.getBody());
} catch (MessagingException e) {
throw new MessageException(e);
}
}

}

0 comments on commit 6a218f0

Please sign in to comment.