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 --- .../notification/NotificationException.java | 25 +++++++++ src/org/traccar/notification/NotificationMail.java | 62 +++++++++------------- src/org/traccar/notification/NotificationNull.java | 31 +++++++++++ src/org/traccar/notification/NotificationSms.java | 27 ++++------ src/org/traccar/notification/NotificationWeb.java | 31 +++++++++++ src/org/traccar/notification/Notificator.java | 40 ++++++++++++++ .../traccar/notification/NotificatorManager.java | 51 ++++++++++++++++++ 7 files changed, 215 insertions(+), 52 deletions(-) create mode 100644 src/org/traccar/notification/NotificationException.java create mode 100644 src/org/traccar/notification/NotificationNull.java create mode 100644 src/org/traccar/notification/NotificationWeb.java create mode 100644 src/org/traccar/notification/Notificator.java create mode 100644 src/org/traccar/notification/NotificatorManager.java (limited to 'src/org/traccar/notification') 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/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); + } } } diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java new file mode 100644 index 000000000..afe160561 --- /dev/null +++ b/src/org/traccar/notification/NotificationNull.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.model.Event; +import org.traccar.model.Position; + +public final class NotificationNull extends Notificator { + + @Override + public void sendAsync(long userId, Event event, Position position) { + } + + @Override + public void sendSync(long userId, Event event, Position position) { + } +} diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 8c0265af4..34d68e174 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,32 +20,27 @@ import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; +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 NotificationSms() { - } - - public static void sendSmsAsync(long userId, Event event, Position position) { + @Override + public void sendAsync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + if (Context.getSmsManager() != null && user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageAsync(user.getPhone(), + Context.getSmsManager().sendMessageAsync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); } } - public static void sendSmsSync(long userId, Event event, Position position) throws RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { + @Override + public void sendSync(long userId, Event event, Position position) throws SMSException, + InterruptedException { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + if (Context.getSmsManager() != null && user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageSync(user.getPhone(), + Context.getSmsManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(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..814c8dc1a --- /dev/null +++ b/src/org/traccar/notification/NotificatorManager.java @@ -0,0 +1,51 @@ +/* + * 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.util.HashMap; +import java.util.Map; + +public class NotificatorManager { + + protected NotificatorManager() { + //not called + } + + private static final Map NOTIFICATORS = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + + static { + NOTIFICATORS.put("sms", new NotificationSms()); + NOTIFICATORS.put("mail", new NotificationMail()); + NOTIFICATORS.put("web", new NotificationWeb()); + } + + public static Notificator getNotificator(String type) { + return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + } + + public static Notificator getSms() { + return getNotificator("sms"); + } + + public static Notificator getMail() { + return getNotificator("mail"); + } + + +} + -- cgit v1.2.3 From 2891a1a4eee21e6c80b65915d35558c033503047 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 1 Apr 2018 18:22:29 -0300 Subject: generalization for notifications processing --- src/org/traccar/notification/NotificationSms.java | 4 ++-- src/org/traccar/notification/NotificatorManager.java | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 34d68e174..a8eaff798 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -27,7 +27,7 @@ public final class NotificationSms extends Notificator { @Override public void sendAsync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmsManager() != null && user.getPhone() != null) { + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); @@ -38,7 +38,7 @@ public final class NotificationSms extends Notificator { public void sendSync(long userId, Event event, Position position) throws SMSException, InterruptedException { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmsManager() != null && user.getPhone() != null) { + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 814c8dc1a..1aa76beff 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -19,6 +19,8 @@ package org.traccar.notification; import java.util.HashMap; import java.util.Map; +import org.traccar.Context; + public class NotificatorManager { protected NotificatorManager() { @@ -29,7 +31,9 @@ public class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); static { - NOTIFICATORS.put("sms", new NotificationSms()); + if (Context.getSmsManager() != null) { + NOTIFICATORS.put("sms", new NotificationSms()); + } NOTIFICATORS.put("mail", new NotificationMail()); NOTIFICATORS.put("web", new NotificationWeb()); } -- cgit v1.2.3 From 9339c709e5a6d521fd0bdd4b1221b909c0a888d6 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 9 Apr 2018 21:20:15 -0300 Subject: configuration support for notificators --- setup/default.xml | 5 +++ .../traccar/notification/NotificatorManager.java | 41 +++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) (limited to 'src/org/traccar/notification') diff --git a/setup/default.xml b/setup/default.xml index 1788cb901..123f67572 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -27,6 +27,11 @@ ./media + web,mail + org.traccar.notification.NotificationWeb + org.traccar.notification.NotificationSms + org.traccar.notification.NotificationMail + https://www.traccar.org/analytics/ diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 1aa76beff..4353b7914 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -16,30 +16,45 @@ */ package org.traccar.notification; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.traccar.Context; +import org.traccar.helper.Log; -public class NotificatorManager { +public final class NotificatorManager { - protected NotificatorManager() { - //not called - } - - private static final Map NOTIFICATORS = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + private static final NotificatorManager INSTANCE = new NotificatorManager(); - static { - if (Context.getSmsManager() != null) { - NOTIFICATORS.put("sms", new NotificationSms()); + private 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 c = (Class) Class.forName(className); + try { + final Constructor 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()); + } + } } - NOTIFICATORS.put("mail", new NotificationMail()); - NOTIFICATORS.put("web", new NotificationWeb()); } + private final Map notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + public static Notificator getNotificator(String type) { - return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + return INSTANCE.notificators.getOrDefault(type, NULL_NOTIFICATOR); } public static Notificator getSms() { -- cgit v1.2.3 From 50a80d68516890aa3ec5c2826f929474927def30 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:34:35 -0300 Subject: generic template names --- src/org/traccar/notification/NotificationFormatter.java | 8 ++++---- src/org/traccar/notification/NotificationMail.java | 2 +- src/org/traccar/notification/NotificationSms.java | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 114825a83..d1f6c3903 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -85,16 +85,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 3a2a19337..fb89bf5bd 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -108,7 +108,7 @@ public final class NotificationMail extends Notificator { } message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); + MailMessage mailMessage = NotificationFormatter.formatFullMessage(userId, event, position); message.setSubject(mailMessage.getSubject()); message.setSentDate(new Date()); message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index a8eaff798..804fa8527 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -30,7 +30,7 @@ public final class NotificationSms extends Notificator { if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); + NotificationFormatter.formatShortMessage(userId, event, position), false); } } @@ -41,7 +41,7 @@ public final class NotificationSms extends Notificator { if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); + NotificationFormatter.formatShortMessage(userId, event, position), false); } } } -- cgit v1.2.3 From 633835e8e28a70fddc252e0ee92dcf7131ba167b Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:38:54 -0300 Subject: added warning when using the null notificator --- src/org/traccar/notification/NotificationNull.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java index afe160561..fd7950a80 100644 --- a/src/org/traccar/notification/NotificationNull.java +++ b/src/org/traccar/notification/NotificationNull.java @@ -16,6 +16,7 @@ */ package org.traccar.notification; +import org.traccar.helper.Log; import org.traccar.model.Event; import org.traccar.model.Position; @@ -23,9 +24,11 @@ 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"); } } -- cgit v1.2.3 From 8ca7b2d0d9b5e18dd7e96a2cf5b1b8d8d751051a Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:50:10 -0300 Subject: make compatible with java 7 --- src/org/traccar/notification/NotificatorManager.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 4353b7914..0ffef5d05 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -54,7 +54,12 @@ public final class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); public static Notificator getNotificator(String type) { - return INSTANCE.notificators.getOrDefault(type, NULL_NOTIFICATOR); + final Notificator n = INSTANCE.notificators.get(type); + if (n == null) { + Log.error("No notificator configured for type : " + type); + return NULL_NOTIFICATOR; + } + return n; } public static Notificator getSms() { -- cgit v1.2.3 From 4fc750b585dd6b2953b16408dd57a8ef93fdeee9 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 09:48:07 -0300 Subject: move NotificatorManager instance to Context --- src/org/traccar/Context.java | 8 ++++++++ src/org/traccar/api/resource/NotificationResource.java | 5 ++--- src/org/traccar/database/NotificationManager.java | 3 +-- src/org/traccar/notification/NotificatorManager.java | 12 +++++------- 4 files changed, 16 insertions(+), 12 deletions(-) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index c9ac4ec12..a7c1817cf 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -73,6 +73,7 @@ import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.MultiPartEventForwarder; +import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; import org.traccar.sms.SMSManager; import org.traccar.web.WebServer; @@ -196,6 +197,12 @@ public final class Context { return notificationManager; } + private static NotificatorManager notificatorManager; + + public static NotificatorManager getNotificatorManager() { + return notificatorManager; + } + private static VelocityEngine velocityEngine; public static VelocityEngine getVelocityEngine() { @@ -425,6 +432,7 @@ public final class Context { geofenceManager = new GeofenceManager(dataManager); calendarManager = new CalendarManager(dataManager); notificationManager = new NotificationManager(dataManager); + notificatorManager = new NotificatorManager(); Properties velocityProperties = new Properties(); velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/"); diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 2a398f40d..830e34fc0 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -31,7 +31,6 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; import org.traccar.notification.NotificationException; -import org.traccar.notification.NotificatorManager; @Path("notifications") @@ -52,8 +51,8 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - NotificatorManager.getMail().sendSync(getUserId(), new Event("test", 0), null); - NotificatorManager.getSms().sendSync(getUserId(), new Event("test", 0), null); + Context.getNotificatorManager().getMail().sendSync(getUserId(), new Event("test", 0), null); + Context.getNotificatorManager().getSms().sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 10c76181a..a10fbf69a 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -32,7 +32,6 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Typed; -import org.traccar.notification.NotificatorManager; public class NotificationManager extends ExtendedObjectManager { @@ -90,7 +89,7 @@ public class NotificationManager extends ExtendedObjectManager { } } for (String nm : notificationMethods) { - NotificatorManager.getNotificator(nm).sendAsync(userId, event, position); + Context.getNotificatorManager().getNotificator(nm).sendAsync(userId, event, position); } } } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 0ffef5d05..c58149847 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -26,9 +26,7 @@ import org.traccar.helper.Log; public final class NotificatorManager { - private static final NotificatorManager INSTANCE = new NotificatorManager(); - - private 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", ""); @@ -53,8 +51,8 @@ public final class NotificatorManager { private final Map notificators = new HashMap<>(); private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); - public static Notificator getNotificator(String type) { - final Notificator n = INSTANCE.notificators.get(type); + 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; @@ -62,11 +60,11 @@ public final class NotificatorManager { return n; } - public static Notificator getSms() { + public Notificator getSms() { return getNotificator("sms"); } - public static Notificator getMail() { + public Notificator getMail() { return getNotificator("mail"); } -- cgit v1.2.3 From 294c399e5b260313a2d49a0fed1516116a23fbd5 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 10:43:16 -0300 Subject: update api to support notificators --- .../traccar/api/resource/NotificationResource.java | 21 +++++++++++++++++++-- .../traccar/notification/NotificatorManager.java | 8 +++----- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 830e34fc0..2347b43fa 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -21,6 +21,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -51,9 +52,25 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - Context.getNotificatorManager().getMail().sendSync(getUserId(), new Event("test", 0), null); - Context.getNotificatorManager().getSms().sendSync(getUserId(), new Event("test", 0), null); + for (String method : Context.getNotificatorManager().getNotificatorTypes()) { + Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + } return Response.noContent().build(); } + @POST + @Path("test/{method}") + public Response testMessage(@PathParam("method") String method) throws NotificationException, InterruptedException { + Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + return Response.noContent().build(); + } + + + @GET + @Path("notificators") + public Collection getNotificators() { + return Context.getNotificatorManager().getNotificatorTypes(); + } + + } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index c58149847..20c7749d2 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -20,6 +20,7 @@ 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; @@ -60,12 +61,9 @@ public final class NotificatorManager { return n; } - public Notificator getSms() { - return getNotificator("sms"); - } - public Notificator getMail() { - return getNotificator("mail"); + public Set getNotificatorTypes() { + return notificators.keySet(); } -- cgit v1.2.3 From 575deac9e2df1cbd0601328f7ea7a9f22029fa43 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 11:20:39 -0300 Subject: option to instantiate a new SMSNotifier using a different SMSManager --- src/org/traccar/Context.java | 18 +++++++++--------- src/org/traccar/notification/NotificationSms.java | 22 ++++++++++++++++++---- 2 files changed, 27 insertions(+), 13 deletions(-) (limited to 'src/org/traccar/notification') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index a7c1817cf..abf9f7a8a 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -372,6 +372,15 @@ public final class Context { tripsConfig = initTripsConfig(); + if (config.getBoolean("sms.smpp.enable")) { + final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); + try { + smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); + } catch (ClassNotFoundException e) { + Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); + } + } + if (config.getBoolean("event.enable")) { initEventsModule(); } @@ -394,15 +403,6 @@ public final class Context { statisticsManager = new StatisticsManager(); - if (config.getBoolean("sms.smpp.enable")) { - final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); - try { - smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); - } catch (ClassNotFoundException e) { - Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); - } - } - } private static void initGeolocationModule() { diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 804fa8527..63fd57895 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,16 +20,30 @@ 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; public final class NotificationSms extends Notificator { + private final SMSManager sms; + + 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(); + } + } + + @Override public void sendAsync(long userId, Event event, Position position) { - User user = Context.getPermissionsManager().getUser(userId); + final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmsManager().sendMessageAsync(user.getPhone(), + sms.sendMessageAsync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } @@ -37,10 +51,10 @@ public final class NotificationSms extends Notificator { @Override public void sendSync(long userId, Event event, Position position) throws SMSException, InterruptedException { - User user = Context.getPermissionsManager().getUser(userId); + final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmsManager().sendMessageSync(user.getPhone(), + sms.sendMessageSync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } -- cgit v1.2.3