aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/notificators
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-10 08:19:27 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-10 08:19:27 -0700
commitfe61a79b37a2de3bc435890e5cca43ac05fc598c (patch)
treefc77ed01798ed38ba68effe49a1e2dd3dea099dc /src/main/java/org/traccar/notificators
parentbbe84d6a751fdc840e4201ef9027a96527006049 (diff)
downloadtrackermap-server-fe61a79b37a2de3bc435890e5cca43ac05fc598c.tar.gz
trackermap-server-fe61a79b37a2de3bc435890e5cca43ac05fc598c.tar.bz2
trackermap-server-fe61a79b37a2de3bc435890e5cca43ac05fc598c.zip
Full injection for notificators
Diffstat (limited to 'src/main/java/org/traccar/notificators')
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorFirebase.java39
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorPushover.java50
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTelegram.java51
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTraccar.java15
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorWeb.java20
5 files changed, 72 insertions, 103 deletions
diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
index cafd2237d..3f1667568 100644
--- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java
+++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
@@ -17,24 +17,22 @@
package org.traccar.notificators;
import com.fasterxml.jackson.annotation.JsonProperty;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
-import org.traccar.Main;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.model.User;
-import org.traccar.notification.NotificationMessage;
import org.traccar.notification.NotificationFormatter;
import org.traccar.session.cache.CacheManager;
+import javax.inject.Inject;
+import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
public class NotificatorFirebase implements Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorFirebase.class);
+ private final CacheManager cacheManager;
+ private final Client client;
private final String url;
private final String key;
@@ -55,13 +53,16 @@ public class NotificatorFirebase implements Notificator {
private Notification notification;
}
- public NotificatorFirebase() {
+ @Inject
+ public NotificatorFirebase(Config config, CacheManager cacheManager, Client client) {
this(
- "https://fcm.googleapis.com/fcm/send",
- Context.getConfig().getString(Keys.NOTIFICATOR_FIREBASE_KEY));
+ cacheManager, client, "https://fcm.googleapis.com/fcm/send",
+ config.getString(Keys.NOTIFICATOR_FIREBASE_KEY));
}
- protected NotificatorFirebase(String url, String key) {
+ protected NotificatorFirebase(CacheManager cacheManager, Client client, String url, String key) {
+ this.cacheManager = cacheManager;
+ this.client = client;
this.url = url;
this.key = key;
}
@@ -70,8 +71,7 @@ public class NotificatorFirebase implements Notificator {
public void send(User user, Event event, Position position) {
if (user.getAttributes().containsKey("notificationTokens")) {
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(
- Main.getInjector().getInstance(CacheManager.class), user, event, position, "short");
+ var shortMessage = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
Notification notification = new Notification();
notification.title = shortMessage.getSubject();
@@ -82,18 +82,7 @@ public class NotificatorFirebase implements Notificator {
message.tokens = user.getString("notificationTokens").split("[, ]");
message.notification = notification;
- Context.getClient().target(url).request()
- .header("Authorization", "key=" + key)
- .async().post(Entity.json(message), new InvocationCallback<Object>() {
- @Override
- public void completed(Object o) {
- }
-
- @Override
- public void failed(Throwable throwable) {
- LOGGER.warn("Firebase notification error", throwable);
- }
- });
+ client.target(url).request().header("Authorization", "key=" + key).post(Entity.json(message)).close();
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java
index 70c1a6cdc..2ac489dd6 100644
--- a/src/main/java/org/traccar/notificators/NotificatorPushover.java
+++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java
@@ -16,24 +16,22 @@
package org.traccar.notificators;
import com.fasterxml.jackson.annotation.JsonProperty;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
-import org.traccar.Main;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
import org.traccar.session.cache.CacheManager;
+import javax.inject.Inject;
+import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
public class NotificatorPushover implements Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorPushover.class);
+ private final CacheManager cacheManager;
+ private final Client client;
private final String url;
private final String token;
@@ -52,33 +50,27 @@ public class NotificatorPushover implements Notificator {
private String message;
}
- public NotificatorPushover() {
+ @Inject
+ public NotificatorPushover(Config config, CacheManager cacheManager, Client client) {
+ this.cacheManager = cacheManager;
+ this.client = client;
url = "https://api.pushover.net/1/messages.json";
- token = Context.getConfig().getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN);
- user = Context.getConfig().getString(Keys.NOTIFICATOR_PUSHOVER_USER);
+ token = config.getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN);
+ user = config.getString(Keys.NOTIFICATOR_PUSHOVER_USER);
+ if (token == null || user == null) {
+ throw new RuntimeException("Pushover token or user missing");
+ }
}
@Override
public void send(User user, Event event, Position position) {
String device = "";
-
if (user.getAttributes().containsKey("notificator.pushover.device")) {
device = user.getString("notificator.pushover.device").replaceAll(" *, *", ",");
}
- if (token == null) {
- LOGGER.warn("Pushover token not found");
- return;
- }
-
- if (this.user == null) {
- LOGGER.warn("Pushover user not found");
- return;
- }
-
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(
- Main.getInjector().getInstance(CacheManager.class), user, event, position, "short");
+ var shortMessage = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
Message message = new Message();
message.token = token;
@@ -87,17 +79,7 @@ public class NotificatorPushover implements Notificator {
message.title = shortMessage.getSubject();
message.message = shortMessage.getBody();
- Context.getClient().target(url).request()
- .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);
- }
- });
+ client.target(url).request().post(Entity.json(message)).close();
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
index 0ed53ac4c..1dccf2c04 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
@@ -17,24 +17,22 @@
package org.traccar.notificators;
import com.fasterxml.jackson.annotation.JsonProperty;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
-import org.traccar.Main;
-import org.traccar.model.User;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
import org.traccar.session.cache.CacheManager;
+import javax.inject.Inject;
+import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
public class NotificatorTelegram implements Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTelegram.class);
+ private final CacheManager cacheManager;
+ private final Client client;
private final String urlSendText;
private final String urlSendLocation;
@@ -63,29 +61,16 @@ public class NotificatorTelegram implements Notificator {
private int bearing;
}
- public NotificatorTelegram() {
+ @Inject
+ public NotificatorTelegram(Config config, CacheManager cacheManager, Client client) {
+ this.cacheManager = cacheManager;
+ this.client = client;
urlSendText = String.format(
- "https://api.telegram.org/bot%s/sendMessage",
- Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
+ "https://api.telegram.org/bot%s/sendMessage", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
urlSendLocation = String.format(
- "https://api.telegram.org/bot%s/sendLocation",
- Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
- chatId = Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID);
- sendLocation = Context.getConfig().getBoolean(Keys.NOTIFICATOR_TELEGRAM_SEND_LOCATION);
- }
-
- 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 failed(Throwable throwable) {
- LOGGER.warn("Telegram API error", throwable);
- }
- });
+ "https://api.telegram.org/bot%s/sendLocation", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
+ chatId = config.getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID);
+ sendLocation = config.getBoolean(Keys.NOTIFICATOR_TELEGRAM_SEND_LOCATION);
}
private LocationMessage createLocationMessage(String messageChatId, Position position) {
@@ -100,8 +85,7 @@ public class NotificatorTelegram implements Notificator {
@Override
public void send(User user, Event event, Position position) {
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(
- Main.getInjector().getInstance(CacheManager.class), user, event, position, "short");
+ var shortMessage = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
TextMessage message = new TextMessage();
message.chatId = user.getString("telegramChatId");
@@ -109,9 +93,10 @@ public class NotificatorTelegram implements Notificator {
message.chatId = chatId;
}
message.text = shortMessage.getBody();
- executeRequest(urlSendText, message);
+ client.target(urlSendText).request().post(Entity.json(message)).close();
if (sendLocation && position != null) {
- executeRequest(urlSendLocation, createLocationMessage(message.chatId, position));
+ client.target(urlSendLocation).request().post(
+ Entity.json(createLocationMessage(message.chatId, position))).close();
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorTraccar.java b/src/main/java/org/traccar/notificators/NotificatorTraccar.java
index 5bcd18b5e..0827567ae 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTraccar.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTraccar.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,15 +15,20 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
+import org.traccar.session.cache.CacheManager;
+
+import javax.inject.Inject;
+import javax.ws.rs.client.Client;
public class NotificatorTraccar extends NotificatorFirebase {
- public NotificatorTraccar() {
+ @Inject
+ public NotificatorTraccar(Config config, CacheManager cacheManager, Client client) {
super(
- "https://www.traccar.org/push/",
- Context.getConfig().getString(Keys.NOTIFICATOR_TRACCAR_KEY));
+ cacheManager, client, "https://www.traccar.org/push/",
+ config.getString(Keys.NOTIFICATOR_TRACCAR_KEY));
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorWeb.java b/src/main/java/org/traccar/notificators/NotificatorWeb.java
index e19a94c1d..402f7a9f0 100644
--- a/src/main/java/org/traccar/notificators/NotificatorWeb.java
+++ b/src/main/java/org/traccar/notificators/NotificatorWeb.java
@@ -16,17 +16,26 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
-import org.traccar.Main;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
+import org.traccar.session.ConnectionManager;
import org.traccar.session.cache.CacheManager;
+import javax.inject.Inject;
+
public final class NotificatorWeb implements Notificator {
+ private final ConnectionManager connectionManager;
+ private final CacheManager cacheManager;
+
+ @Inject
+ public NotificatorWeb(ConnectionManager connectionManager, CacheManager cacheManager) {
+ this.connectionManager = connectionManager;
+ this.cacheManager = cacheManager;
+ }
+
@Override
public void send(User user, Event event, Position position) {
@@ -40,11 +49,10 @@ public final class NotificatorWeb implements Notificator {
copy.setMaintenanceId(event.getMaintenanceId());
copy.getAttributes().putAll(event.getAttributes());
- NotificationMessage message = NotificationFormatter.formatMessage(
- Main.getInjector().getInstance(CacheManager.class), user, event, position, "short");
+ var message = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
copy.set("message", message.getBody());
- Context.getConnectionManager().updateEvent(user.getId(), copy);
+ connectionManager.updateEvent(user.getId(), copy);
}
}