diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-06-28 17:07:25 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-28 17:07:25 +1200 |
commit | e849d3a56f55c83dc0ebed02165a124e30e22dbd (patch) | |
tree | 730e542145652ca1d06c64a6944dfa70e0b31b6b /src/org/traccar/notification | |
parent | 4d6229c7daa4a7b8f28337c5eca9a422876c98b1 (diff) | |
parent | 14840af2abd9976b8f5634af8e77f4a7126dfac1 (diff) | |
download | trackermap-server-e849d3a56f55c83dc0ebed02165a124e30e22dbd.tar.gz trackermap-server-e849d3a56f55c83dc0ebed02165a124e30e22dbd.tar.bz2 trackermap-server-e849d3a56f55c83dc0ebed02165a124e30e22dbd.zip |
Merge pull request #3950 from Abyss777/notification_refactor
Notification refactor to allow custom "notificators"
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r-- | src/org/traccar/notification/FullMessage.java (renamed from src/org/traccar/notification/MailMessage.java) | 4 | ||||
-rw-r--r-- | src/org/traccar/notification/MessageException.java | 25 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 13 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 66 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationNull.java | 34 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationSms.java | 43 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationWeb.java | 30 | ||||
-rw-r--r-- | src/org/traccar/notification/Notificator.java | 40 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificatorManager.java | 81 |
9 files changed, 269 insertions, 67 deletions
diff --git a/src/org/traccar/notification/MailMessage.java b/src/org/traccar/notification/FullMessage.java index 0fce43740..f66537c6e 100644 --- a/src/org/traccar/notification/MailMessage.java +++ b/src/org/traccar/notification/FullMessage.java @@ -16,12 +16,12 @@ */ package org.traccar.notification; -public class MailMessage { +public class FullMessage { private String subject; private String body; - public MailMessage(String subject, String body) { + public FullMessage(String subject, String body) { this.subject = subject; this.body = body; } diff --git a/src/org/traccar/notification/MessageException.java b/src/org/traccar/notification/MessageException.java new file mode 100644 index 000000000..ce4b9f6ee --- /dev/null +++ b/src/org/traccar/notification/MessageException.java @@ -0,0 +1,25 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 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.notification; + +public class MessageException extends Exception { + + public MessageException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 524153721..c011403c5 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -88,18 +88,15 @@ 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 FullMessage formatFullMessage(long userId, Event event, Position position) { VelocityContext velocityContext = prepareContext(userId, event, position); - String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath); + String formattedMessage = formatMessage(velocityContext, userId, event, position, "full"); - return new MailMessage((String) velocityContext.get("subject"), formattedMessage); + return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } - public static String formatSmsMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("sms.templatesPath", "sms"); - - return formatMessage(null, userId, event, position, templatePath); + public static String formatShortMessage(long userId, Event event, Position position) { + return formatMessage(null, userId, event, position, "short"); } private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position, diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 6c81ecc79..c2ee67299 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. @@ -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 MessageException { 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())); + 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(); } - }).start(); + } catch (MessagingException e) { + throw new MessageException(e); + } } } diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java new file mode 100644 index 000000000..3ee954c24 --- /dev/null +++ b/src/org/traccar/notification/NotificationNull.java @@ -0,0 +1,34 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 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.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..ed651ac11 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2017 - 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. @@ -20,33 +20,38 @@ 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 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 smsManager; - private NotificationSms() { + public NotificationSms() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + final String smsClass = Context.getConfig().getString("notificator.sms.manager.class", ""); + if (smsClass.length() > 0) { + smsManager = (SmsManager) Class.forName(smsClass).newInstance(); + } else { + smsManager = 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); + smsManager.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 MessageException, 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); + smsManager.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..afc401d24 --- /dev/null +++ b/src/org/traccar/notification/NotificationWeb.java @@ -0,0 +1,30 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 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.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) { + 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..d912b445d --- /dev/null +++ b/src/org/traccar/notification/Notificator.java @@ -0,0 +1,40 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 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.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 (MessageException | InterruptedException error) { + Log.warning(error); + } + } + }).start(); + } + + public abstract void sendSync(long userId, Event event, Position position) + throws MessageException, InterruptedException; + +} diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java new file mode 100644 index 000000000..147de47d3 --- /dev/null +++ b/src/org/traccar/notification/NotificatorManager.java @@ -0,0 +1,81 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 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.notification; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Typed; + +public final class NotificatorManager { + + private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notification.NotificationWeb"; + private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notification.NotificationMail"; + private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notification.NotificationSms"; + + private final Map<String, Notificator> notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + + 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; + break; + case "mail": + defaultNotificator = DEFAULT_MAIL_NOTIFICATOR; + break; + case "sms": + defaultNotificator = DEFAULT_SMS_NOTIFICATOR; + break; + default: + break; + } + final String className = Context.getConfig() + .getString("notificator." + type + ".class", defaultNotificator); + try { + notificators.put(type, (Notificator) Class.forName(className).newInstance()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); + } + } + } + + public Notificator getNotificator(String type) { + final Notificator notificator = notificators.get(type); + if (notificator == null) { + Log.error("No notificator configured for type : " + type); + return NULL_NOTIFICATOR; + } + return notificator; + } + + public Set<Typed> getAllNotificatorTypes() { + Set<Typed> result = new HashSet<>(); + for (String notificator : notificators.keySet()) { + result.add(new Typed(notificator)); + } + return result; + } + +} |