aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/notification
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2018-06-26 09:22:22 +0500
committerAbyss777 <abyss@fox5.ru>2018-06-26 09:22:22 +0500
commitaba402226403f8b3eb58cc01e8f536eb4104d35a (patch)
treeec884994ad90365fa9b385465a80391606f6d970 /src/org/traccar/notification
parentc700bfdb66071ba224ddf01e9827e721562fed7a (diff)
parent575deac9e2df1cbd0601328f7ea7a9f22029fa43 (diff)
downloadtrackermap-server-aba402226403f8b3eb58cc01e8f536eb4104d35a.tar.gz
trackermap-server-aba402226403f8b3eb58cc01e8f536eb4104d35a.tar.bz2
trackermap-server-aba402226403f8b3eb58cc01e8f536eb4104d35a.zip
Merge remote-tracking branch 'ivanfmartinez/notifications'
# Conflicts: # src/org/traccar/Context.java
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r--src/org/traccar/notification/NotificationException.java25
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java8
-rw-r--r--src/org/traccar/notification/NotificationMail.java62
-rw-r--r--src/org/traccar/notification/NotificationNull.java34
-rw-r--r--src/org/traccar/notification/NotificationSms.java43
-rw-r--r--src/org/traccar/notification/NotificationWeb.java31
-rw-r--r--src/org/traccar/notification/Notificator.java40
-rw-r--r--src/org/traccar/notification/NotificatorManager.java71
8 files changed, 257 insertions, 57 deletions
diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/NotificationException.java
new file mode 100644
index 000000000..34d5a80f7
--- /dev/null
+++ b/src/org/traccar/notification/NotificationException.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 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.notification;
+
+public class NotificationException extends Exception {
+
+ public NotificationException(Throwable cause) {
+ super(cause);
+ }
+
+}
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 524153721..ddc35227e 100644
--- a/src/org/traccar/notification/NotificationFormatter.java
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -88,16 +88,16 @@ public final class NotificationFormatter {
return template;
}
- public static MailMessage formatMailMessage(long userId, Event event, Position position) {
- String templatePath = Context.getConfig().getString("mail.templatesPath", "mail");
+ public static MailMessage formatFullMessage(long userId, Event event, Position position) {
+ String templatePath = Context.getConfig().getString("message.full.templatesPath", "full");
VelocityContext velocityContext = prepareContext(userId, event, position);
String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath);
return new MailMessage((String) velocityContext.get("subject"), formattedMessage);
}
- public static String formatSmsMessage(long userId, Event event, Position position) {
- String templatePath = Context.getConfig().getString("sms.templatesPath", "sms");
+ public static String formatShortMessage(long userId, Event event, Position position) {
+ String templatePath = Context.getConfig().getString("message.short.templatesPath", "short");
return formatMessage(null, userId, event, position, templatePath);
}
diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java
index 6c81ecc79..fb89bf5bd 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.formatFullMessage(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);
+ }
}
}
diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java
new file mode 100644
index 000000000..fd7950a80
--- /dev/null
+++ b/src/org/traccar/notification/NotificationNull.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 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.notification;
+
+import org.traccar.helper.Log;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public final class NotificationNull extends Notificator {
+
+ @Override
+ public void sendAsync(long userId, Event event, Position position) {
+ Log.warning("You are using null notificatior, please check your configuration, notification not sent");
+ }
+
+ @Override
+ public void sendSync(long userId, Event event, Position position) {
+ Log.warning("You are using null notificatior, please check your configuration, notification not sent");
+ }
+}
diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java
index 8c0265af4..63fd57895 100644
--- a/src/org/traccar/notification/NotificationSms.java
+++ b/src/org/traccar/notification/NotificationSms.java
@@ -20,33 +20,42 @@ import org.traccar.Context;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.model.User;
+import org.traccar.sms.SMSManager;
+import org.traccar.sms.SMSException;
-import com.cloudhopper.smpp.type.RecoverablePduException;
-import com.cloudhopper.smpp.type.SmppChannelException;
-import com.cloudhopper.smpp.type.SmppTimeoutException;
-import com.cloudhopper.smpp.type.UnrecoverablePduException;
+public final class NotificationSms extends Notificator {
-public final class NotificationSms {
+ private final SMSManager sms;
- private NotificationSms() {
+ public NotificationSms(String methodId) throws ClassNotFoundException,
+ InstantiationException, IllegalAccessException {
+ final String smsClass = Context.getConfig().getString("notificator." + methodId + ".sms.class", "");
+ if (smsClass.length() > 0) {
+ sms = (SMSManager) Class.forName(smsClass).newInstance();
+ } else {
+ sms = Context.getSmsManager();
+ }
}
- public static void sendSmsAsync(long userId, Event event, Position position) {
- User user = Context.getPermissionsManager().getUser(userId);
- if (Context.getSmppManager() != null && user.getPhone() != null) {
+
+ @Override
+ public void sendAsync(long userId, Event event, Position position) {
+ final User user = Context.getPermissionsManager().getUser(userId);
+ if (user.getPhone() != null) {
Context.getStatisticsManager().registerSms();
- Context.getSmppManager().sendMessageAsync(user.getPhone(),
- NotificationFormatter.formatSmsMessage(userId, event, position), false);
+ sms.sendMessageAsync(user.getPhone(),
+ NotificationFormatter.formatShortMessage(userId, event, position), false);
}
}
- public static void sendSmsSync(long userId, Event event, Position position) throws RecoverablePduException,
- UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException {
- User user = Context.getPermissionsManager().getUser(userId);
- if (Context.getSmppManager() != null && user.getPhone() != null) {
+ @Override
+ public void sendSync(long userId, Event event, Position position) throws SMSException,
+ InterruptedException {
+ final User user = Context.getPermissionsManager().getUser(userId);
+ if (user.getPhone() != null) {
Context.getStatisticsManager().registerSms();
- Context.getSmppManager().sendMessageSync(user.getPhone(),
- NotificationFormatter.formatSmsMessage(userId, event, position), false);
+ sms.sendMessageSync(user.getPhone(),
+ NotificationFormatter.formatShortMessage(userId, event, position), false);
}
}
}
diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java
new file mode 100644
index 000000000..c6a1ae281
--- /dev/null
+++ b/src/org/traccar/notification/NotificationWeb.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 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.notification;
+
+import org.traccar.Context;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public final class NotificationWeb extends Notificator {
+
+ @Override
+ public void sendSync(long userId, Event event, Position position) throws NotificationException,
+ InterruptedException {
+ Context.getConnectionManager().updateEvent(userId, event);
+ }
+
+}
diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java
new file mode 100644
index 000000000..7192d25c0
--- /dev/null
+++ b/src/org/traccar/notification/Notificator.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 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.notification;
+
+import org.traccar.helper.Log;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public abstract class Notificator {
+
+ public void sendAsync(final long userId, final Event event, final Position position) {
+ new Thread(new Runnable() {
+ public void run() {
+ try {
+ sendSync(userId, event, position);
+ } catch (NotificationException | InterruptedException error) {
+ Log.warning(error);
+ }
+ }
+ }).start();
+ }
+
+
+ public abstract void sendSync(long userId, Event event, Position position)
+ throws NotificationException, InterruptedException;
+}
diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java
new file mode 100644
index 000000000..20c7749d2
--- /dev/null
+++ b/src/org/traccar/notification/NotificatorManager.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 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.notification;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.traccar.Context;
+import org.traccar.helper.Log;
+
+public final class NotificatorManager {
+
+ public NotificatorManager() {
+ final String[] types = Context.getConfig().getString("notificator.types", "").split(",");
+ for (String type : types) {
+ final String className = Context.getConfig().getString("notificator." + type + ".class", "");
+ if (className.length() > 0) {
+ try {
+ final Class<Notificator> c = (Class<Notificator>) Class.forName(className);
+ try {
+ final Constructor<Notificator> constructor = c.getConstructor(new Class[]{String.class});
+ notificators.put(type, constructor.newInstance(type));
+ } catch (NoSuchMethodException e) {
+ // No constructor with String argument
+ notificators.put(type, c.newInstance());
+ }
+ } catch (ClassNotFoundException | InstantiationException
+ | IllegalAccessException | InvocationTargetException e) {
+ Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage());
+ }
+ }
+ }
+ }
+
+ private final Map<String, Notificator> notificators = new HashMap<>();
+ private static final Notificator NULL_NOTIFICATOR = new NotificationNull();
+
+ public Notificator getNotificator(String type) {
+ final Notificator n = notificators.get(type);
+ if (n == null) {
+ Log.error("No notificator configured for type : " + type);
+ return NULL_NOTIFICATOR;
+ }
+ return n;
+ }
+
+
+ public Set<String> getNotificatorTypes() {
+ return notificators.keySet();
+ }
+
+
+}
+