aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/Context.java9
-rw-r--r--src/org/traccar/database/MailManager.java83
-rw-r--r--src/org/traccar/notification/NotificatorManager.java18
-rw-r--r--src/org/traccar/notificators/NotificatorMail.java40
4 files changed, 103 insertions, 47 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java
index b310d6740..2607fd107 100644
--- a/src/org/traccar/Context.java
+++ b/src/org/traccar/Context.java
@@ -41,6 +41,7 @@ import org.traccar.database.DeviceManager;
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;
@@ -141,6 +142,12 @@ public final class Context {
return ldapProvider;
}
+ private static MailManager mailManager;
+
+ public static MailManager getMailManager() {
+ return mailManager;
+ }
+
private static MediaManager mediaManager;
public static MediaManager getMediaManager() {
@@ -378,6 +385,8 @@ public final class Context {
ldapProvider = new LdapProvider(config);
}
+ mailManager = new MailManager();
+
mediaManager = new MediaManager(config.getString("media.path"));
if (dataManager != null) {
diff --git a/src/org/traccar/database/MailManager.java b/src/org/traccar/database/MailManager.java
index 5dd2c97b7..e64aa638a 100644
--- a/src/org/traccar/database/MailManager.java
+++ b/src/org/traccar/database/MailManager.java
@@ -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 {
@@ -88,8 +86,13 @@ public final class MailManager {
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;
@@ -108,31 +111,35 @@ public final class MailManager {
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());
}
}
diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java
index 41678d65c..a4080a38d 100644
--- a/src/org/traccar/notification/NotificatorManager.java
+++ b/src/org/traccar/notification/NotificatorManager.java
@@ -25,20 +25,20 @@ import org.slf4j.Logger;
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(",");
@@ -46,16 +46,16 @@ public final class NotificatorManager {
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;
diff --git a/src/org/traccar/notificators/NotificatorMail.java b/src/org/traccar/notificators/NotificatorMail.java
new file mode 100644
index 000000000..6b9774c58
--- /dev/null
+++ b/src/org/traccar/notificators/NotificatorMail.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org)
+ *
+ * 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);
+ }
+ }
+
+}