diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/traccar/api/resource/PasswordResource.java | 6 | ||||
-rw-r--r-- | src/main/java/org/traccar/notification/NotificationFormatter.java | 3 | ||||
-rw-r--r-- | src/main/java/org/traccar/notification/NotificationMessage.java (renamed from src/main/java/org/traccar/notification/Message.java) | 4 | ||||
-rw-r--r-- | src/main/java/org/traccar/notification/TextTemplateFormatter.java | 12 | ||||
-rw-r--r-- | src/main/java/org/traccar/notificators/NotificatorFirebase.java | 16 | ||||
-rw-r--r-- | src/main/java/org/traccar/notificators/NotificatorMail.java | 6 | ||||
-rw-r--r-- | src/main/java/org/traccar/notificators/NotificatorPushover.java | 38 | ||||
-rw-r--r-- | src/main/java/org/traccar/notificators/NotificatorSms.java | 6 | ||||
-rw-r--r-- | src/main/java/org/traccar/notificators/NotificatorTelegram.java | 20 |
9 files changed, 53 insertions, 58 deletions
diff --git a/src/main/java/org/traccar/api/resource/PasswordResource.java b/src/main/java/org/traccar/api/resource/PasswordResource.java index 16b0afc12..74cb7ca34 100644 --- a/src/main/java/org/traccar/api/resource/PasswordResource.java +++ b/src/main/java/org/traccar/api/resource/PasswordResource.java @@ -19,7 +19,7 @@ import org.apache.velocity.VelocityContext; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.User; -import org.traccar.notification.Message; +import org.traccar.notification.NotificationMessage; import org.traccar.notification.TextTemplateFormatter; import javax.annotation.security.PermitAll; @@ -53,8 +53,8 @@ public class PasswordResource extends BaseResource { Context.getUsersManager().updateItem(user); VelocityContext velocityContext = TextTemplateFormatter.prepareContext(null); velocityContext.put("token", token); - Message message = TextTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full"); - Context.getMailManager().sendMessage(userId, message.getSubject(), message.getBody()); + NotificationMessage fullMessage = TextTemplateFormatter.formatMessage(velocityContext, "passwordReset","full"); + Context.getMailManager().sendMessage(userId, fullMessage.getSubject(), fullMessage.getBody()); break; } } diff --git a/src/main/java/org/traccar/notification/NotificationFormatter.java b/src/main/java/org/traccar/notification/NotificationFormatter.java index 3170d1204..9a6723a71 100644 --- a/src/main/java/org/traccar/notification/NotificationFormatter.java +++ b/src/main/java/org/traccar/notification/NotificationFormatter.java @@ -58,8 +58,9 @@ public final class NotificationFormatter { return velocityContext; } - public static Message formatMessage(long userId, Event event, Position position, String templatePath) { + public static NotificationMessage formatMessage(long userId, Event event, Position position, String templatePath) { VelocityContext velocityContext = prepareContext(userId, event, position); return TextTemplateFormatter.formatMessage(velocityContext, event.getType(), templatePath); } + } diff --git a/src/main/java/org/traccar/notification/Message.java b/src/main/java/org/traccar/notification/NotificationMessage.java index 33f38007c..0fb8d7654 100644 --- a/src/main/java/org/traccar/notification/Message.java +++ b/src/main/java/org/traccar/notification/NotificationMessage.java @@ -16,12 +16,12 @@ */ package org.traccar.notification; -public class Message { +public class NotificationMessage { private String subject; private String body; - public Message(String subject, String body) { + public NotificationMessage(String subject, String body) { this.subject = subject; this.body = body; } diff --git a/src/main/java/org/traccar/notification/TextTemplateFormatter.java b/src/main/java/org/traccar/notification/TextTemplateFormatter.java index 77a2fea05..6a95628f0 100644 --- a/src/main/java/org/traccar/notification/TextTemplateFormatter.java +++ b/src/main/java/org/traccar/notification/TextTemplateFormatter.java @@ -71,17 +71,11 @@ public final class TextTemplateFormatter { return template; } - public static Message formatMessage(VelocityContext velocityContext, String name, String templatePath) { - String formattedMessage = format(velocityContext, name, templatePath); - return new Message((String) velocityContext.get("subject"), formattedMessage); - } - - private static String format( - VelocityContext velocityContext, String name, String templatePath) { - + public static NotificationMessage formatMessage(VelocityContext velocityContext, String name, String templatePath) { StringWriter writer = new StringWriter(); getTemplate(name, templatePath).merge(velocityContext, writer); - return writer.toString(); + String formattedMessage = writer.toString(); + return new NotificationMessage((String) velocityContext.get("subject"), formattedMessage); } } diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java index 0f189a330..37d0cb25a 100644 --- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java +++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java @@ -24,7 +24,7 @@ import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -import org.traccar.notification.Message; +import org.traccar.notification.NotificationMessage; import org.traccar.notification.NotificationFormatter; import javax.ws.rs.client.Entity; @@ -46,7 +46,7 @@ public class NotificatorFirebase extends Notificator { private String sound; } - public static class Payload { + public static class Message { @JsonProperty("registration_ids") private String[] tokens; @JsonProperty("notification") @@ -69,20 +69,20 @@ public class NotificatorFirebase extends Notificator { final User user = Context.getPermissionsManager().getUser(userId); if (user.getAttributes().containsKey("notificationTokens")) { - Message shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position,"short"); Notification notification = new Notification(); - notification.title = shortMessage.getSubject(); + notification.title= shortMessage.getSubject(); notification.body = shortMessage.getBody(); notification.sound = "default"; - Payload payload = new Payload(); - payload.tokens = user.getString("notificationTokens").split("[, ]"); - payload.notification = notification; + Message message = new Message(); + message.tokens = user.getString("notificationTokens").split("[, ]"); + message.notification = notification; Context.getClient().target(url).request() .header("Authorization", "key=" + key) - .async().post(Entity.json(payload), new InvocationCallback<Object>() { + .async().post(Entity.json(message), new InvocationCallback<Object>() { @Override public void completed(Object o) { } diff --git a/src/main/java/org/traccar/notificators/NotificatorMail.java b/src/main/java/org/traccar/notificators/NotificatorMail.java index 339445622..48e9d2311 100644 --- a/src/main/java/org/traccar/notificators/NotificatorMail.java +++ b/src/main/java/org/traccar/notificators/NotificatorMail.java @@ -19,7 +19,7 @@ package org.traccar.notificators; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.notification.Message; +import org.traccar.notification.NotificationMessage; import org.traccar.notification.MessageException; import org.traccar.notification.NotificationFormatter; @@ -30,8 +30,8 @@ public final class NotificatorMail extends Notificator { @Override public void sendSync(long userId, Event event, Position position) throws MessageException { try { - Message message = NotificationFormatter.formatMessage(userId, event, position, "full"); - Context.getMailManager().sendMessage(userId, message.getSubject(), message.getBody()); + NotificationMessage fullMessage = NotificationFormatter.formatMessage(userId, event, position,"full"); + Context.getMailManager().sendMessage(userId, fullMessage.getSubject(), fullMessage.getBody()); } catch (MessagingException e) { throw new MessageException(e); } diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 4a3099d7c..b62be7654 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -23,8 +23,8 @@ import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -import org.traccar.notification.Message; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -37,7 +37,7 @@ public class NotificatorPushover extends Notificator { private final String token; private final String user; - public static class Payload { + public static class Message { @JsonProperty("token") private String token; @JsonProperty("user") @@ -77,26 +77,26 @@ public class NotificatorPushover extends Notificator { return; } - Message shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position,"short"); - Payload payload = new Payload(); - payload.token = token; - payload.user = this.user; - payload.device = device; - payload.title = shortMessage.getSubject(); - payload.message = shortMessage.getBody(); + Message message = new Message(); + message.token = token; + message.user = this.user; + message.device = device; + message.title= shortMessage.getSubject(); + message.message = shortMessage.getBody(); Context.getClient().target(url).request() - .async().post(Entity.json(payload), new InvocationCallback<Object>() { - @Override - public void completed(Object o) { - } - - @Override - public void failed(Throwable throwable) { - LOGGER.warn("Pushover API error", throwable); - } - }); + .async().post(Entity.json(message), new InvocationCallback<Object>() { + @Override + public void completed(Object o) { + } + + @Override + public void failed(Throwable throwable) { + LOGGER.warn("Pushover API error", throwable); + } + }); } @Override diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java index 1a4739c1a..c99b555ce 100644 --- a/src/main/java/org/traccar/notificators/NotificatorSms.java +++ b/src/main/java/org/traccar/notificators/NotificatorSms.java @@ -22,9 +22,9 @@ import org.traccar.database.StatisticsManager; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -import org.traccar.notification.Message; import org.traccar.notification.MessageException; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; public final class NotificatorSms extends Notificator { @@ -32,7 +32,7 @@ public final class NotificatorSms extends Notificator { public void sendAsync(long userId, Event event, Position position) { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { - Message shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position,"short"); Main.getInjector().getInstance(StatisticsManager.class).registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), shortMessage.getBody(), false); @@ -43,7 +43,7 @@ public final class NotificatorSms extends Notificator { public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { - Message shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position,"short"); Main.getInjector().getInstance(StatisticsManager.class).registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), shortMessage.getBody(), false); diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index c85e2be3d..62a5a14ff 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java +++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java @@ -24,8 +24,8 @@ import org.traccar.model.User; import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.notification.Message; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -75,15 +75,15 @@ public class NotificatorTelegram extends Notificator { private void executeRequest(String url, Object message) { Context.getClient().target(url).request() .async().post(Entity.json(message), new InvocationCallback<Object>() { - @Override - public void completed(Object o) { - } + @Override + public void completed(Object o) { + } - @Override - public void failed(Throwable throwable) { - LOGGER.warn("Telegram API error", throwable); - } - }); + @Override + public void failed(Throwable throwable) { + LOGGER.warn("Telegram API error", throwable); + } + }); } private LocationMessage createLocationMessage(String messageChatId, Position position) { @@ -99,7 +99,7 @@ public class NotificatorTelegram extends Notificator { @Override public void sendSync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - Message shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position,"short"); TextMessage message = new TextMessage(); message.chatId = user.getString("telegramChatId"); |