aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-02-20 11:30:26 -0800
committerAnton Tananaev <anton@traccar.org>2023-02-20 11:30:26 -0800
commit1439422c6f4ce947f6719743b37fa49f251fc97f (patch)
tree45bb38bba8ac882a0267ce419c565bc0b2b02303 /src/main/java
parent8ae0436e5edb76243e59ee6e9b2c1a6132fd9464 (diff)
downloadtrackermap-server-1439422c6f4ce947f6719743b37fa49f251fc97f.tar.gz
trackermap-server-1439422c6f4ce947f6719743b37fa49f251fc97f.tar.bz2
trackermap-server-1439422c6f4ce947f6719743b37fa49f251fc97f.zip
Refactor notificator classes
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/traccar/api/resource/NotificationResource.java4
-rw-r--r--src/main/java/org/traccar/database/NotificationManager.java4
-rw-r--r--src/main/java/org/traccar/notification/NotificatorManager.java12
-rw-r--r--src/main/java/org/traccar/notificators/Notificator.java5
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorFirebase.java8
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorMail.java5
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorNull.java34
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorPushover.java5
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorSms.java5
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTelegram.java5
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTraccar.java18
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorWeb.java8
-rw-r--r--src/main/java/org/traccar/sms/HttpSmsClient.java4
-rw-r--r--src/main/java/org/traccar/sms/SmsManager.java3
14 files changed, 43 insertions, 77 deletions
diff --git a/src/main/java/org/traccar/api/resource/NotificationResource.java b/src/main/java/org/traccar/api/resource/NotificationResource.java
index 2e4ad12f3..d58557601 100644
--- a/src/main/java/org/traccar/api/resource/NotificationResource.java
+++ b/src/main/java/org/traccar/api/resource/NotificationResource.java
@@ -83,7 +83,7 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
public Response testMessage() throws MessageException, InterruptedException, StorageException {
User user = permissionsService.getUser(getUserId());
for (Typed method : notificatorManager.getAllNotificatorTypes()) {
- notificatorManager.getNotificator(method.getType()).send(user, new Event("test", 0), null);
+ notificatorManager.getNotificator(method.getType()).send(null, user, new Event("test", 0), null);
}
return Response.noContent().build();
}
@@ -93,7 +93,7 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
public Response testMessage(@PathParam("notificator") String notificator)
throws MessageException, InterruptedException, StorageException {
User user = permissionsService.getUser(getUserId());
- notificatorManager.getNotificator(notificator).send(user, new Event("test", 0), null);
+ notificatorManager.getNotificator(notificator).send(null, user, new Event("test", 0), null);
return Response.noContent().build();
}
diff --git a/src/main/java/org/traccar/database/NotificationManager.java b/src/main/java/org/traccar/database/NotificationManager.java
index cb971b082..32216dfc6 100644
--- a/src/main/java/org/traccar/database/NotificationManager.java
+++ b/src/main/java/org/traccar/database/NotificationManager.java
@@ -107,8 +107,8 @@ public class NotificationManager {
cacheManager.getNotificationUsers(notification.getId(), event.getDeviceId()).forEach(user -> {
for (String notificator : notification.getNotificatorsTypes()) {
try {
- notificatorManager.getNotificator(notificator).send(user, event, position);
- } catch (MessageException | InterruptedException exception) {
+ notificatorManager.getNotificator(notificator).send(notification, user, event, position);
+ } catch (MessageException exception) {
LOGGER.warn("Notification failed", exception);
}
}
diff --git a/src/main/java/org/traccar/notification/NotificatorManager.java b/src/main/java/org/traccar/notification/NotificatorManager.java
index 1d9f4f423..c3ee7b480 100644
--- a/src/main/java/org/traccar/notification/NotificatorManager.java
+++ b/src/main/java/org/traccar/notification/NotificatorManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,15 +17,13 @@
package org.traccar.notification;
import com.google.inject.Injector;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Typed;
import org.traccar.notificators.Notificator;
+import org.traccar.notificators.NotificatorCommand;
import org.traccar.notificators.NotificatorFirebase;
import org.traccar.notificators.NotificatorMail;
-import org.traccar.notificators.NotificatorNull;
import org.traccar.notificators.NotificatorPushover;
import org.traccar.notificators.NotificatorSms;
import org.traccar.notificators.NotificatorTelegram;
@@ -43,9 +41,8 @@ import java.util.stream.Collectors;
@Singleton
public class NotificatorManager {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorManager.class);
-
private static final Map<String, Class<? extends Notificator>> NOTIFICATORS_ALL = Map.of(
+ "command", NotificatorCommand.class,
"web", NotificatorWeb.class,
"mail", NotificatorMail.class,
"sms", NotificatorSms.class,
@@ -75,8 +72,7 @@ public class NotificatorManager {
return notificator;
}
}
- LOGGER.warn("Failed to get notificator {}", type);
- return new NotificatorNull();
+ throw new RuntimeException("Failed to get notificator " + type);
}
public Set<Typed> getAllNotificatorTypes() {
diff --git a/src/main/java/org/traccar/notificators/Notificator.java b/src/main/java/org/traccar/notificators/Notificator.java
index 052365c7a..cf71141c0 100644
--- a/src/main/java/org/traccar/notificators/Notificator.java
+++ b/src/main/java/org/traccar/notificators/Notificator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,12 +17,13 @@
package org.traccar.notificators;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
public interface Notificator {
- void send(User user, Event event, Position position) throws MessageException, InterruptedException;
+ void send(Notification notification, User user, Event event, Position position) throws MessageException;
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
index 5ce2cbc0b..9fa2ebaf3 100644
--- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java
+++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,10 +26,10 @@ import com.google.firebase.messaging.Aps;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.MulticastMessage;
-import com.google.firebase.messaging.Notification;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
@@ -64,7 +64,7 @@ public class NotificatorFirebase implements Notificator {
}
@Override
- public void send(User user, Event event, Position position) throws MessageException {
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
if (user.hasAttribute("notificationTokens")) {
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
@@ -72,7 +72,7 @@ public class NotificatorFirebase implements Notificator {
List<String> registrationTokens = Arrays.asList(user.getString("notificationTokens").split("[, ]"));
MulticastMessage message = MulticastMessage.builder()
- .setNotification(Notification.builder()
+ .setNotification(com.google.firebase.messaging.Notification.builder()
.setTitle(shortMessage.getSubject())
.setBody(shortMessage.getBody())
.build())
diff --git a/src/main/java/org/traccar/notificators/NotificatorMail.java b/src/main/java/org/traccar/notificators/NotificatorMail.java
index 19fde6756..7f755f170 100644
--- a/src/main/java/org/traccar/notificators/NotificatorMail.java
+++ b/src/main/java/org/traccar/notificators/NotificatorMail.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +18,7 @@ package org.traccar.notificators;
import org.traccar.mail.MailManager;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
@@ -40,7 +41,7 @@ public class NotificatorMail implements Notificator {
}
@Override
- public void send(User user, Event event, Position position) throws MessageException {
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
try {
var fullMessage = notificationFormatter.formatMessage(user, event, position, "full");
mailManager.sendMessage(user, fullMessage.getSubject(), fullMessage.getBody());
diff --git a/src/main/java/org/traccar/notificators/NotificatorNull.java b/src/main/java/org/traccar/notificators/NotificatorNull.java
deleted file mode 100644
index ba9ade9c6..000000000
--- a/src/main/java/org/traccar/notificators/NotificatorNull.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright 2018 - 2022 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.notificators;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.model.Event;
-import org.traccar.model.Position;
-import org.traccar.model.User;
-
-public class NotificatorNull implements Notificator {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorNull.class);
-
- @Override
- public void send(User user, Event event, Position position) {
- LOGGER.warn("You are using null notificatior, please check your configuration, notification not sent");
- }
-
-}
diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java
index e00db0579..703d86876 100644
--- a/src/main/java/org/traccar/notificators/NotificatorPushover.java
+++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2023 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.
@@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
@@ -61,7 +62,7 @@ public class NotificatorPushover implements Notificator {
}
@Override
- public void send(User user, Event event, Position position) {
+ public void send(Notification notification, User user, Event event, Position position) {
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
Message message = new Message();
diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java
index e37d10888..7ec47b156 100644
--- a/src/main/java/org/traccar/notificators/NotificatorSms.java
+++ b/src/main/java/org/traccar/notificators/NotificatorSms.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +18,7 @@ package org.traccar.notificators;
import org.traccar.database.StatisticsManager;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
@@ -43,7 +44,7 @@ public class NotificatorSms implements Notificator {
}
@Override
- public void send(User user, Event event, Position position) throws MessageException, InterruptedException {
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
if (user.getPhone() != null) {
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
statisticsManager.registerSms();
diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
index 38e87c222..63d2bb717 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2019 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2019 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2021 Rafael Miquelino (rafaelmiquelino@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +20,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
@@ -85,7 +86,7 @@ public class NotificatorTelegram implements Notificator {
}
@Override
- public void send(User user, Event event, Position position) {
+ public void send(Notification notification, User user, Event event, Position position) {
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
TextMessage message = new TextMessage();
diff --git a/src/main/java/org/traccar/notificators/NotificatorTraccar.java b/src/main/java/org/traccar/notificators/NotificatorTraccar.java
index 9ae39f975..e32229506 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 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2023 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.
@@ -37,7 +37,7 @@ public class NotificatorTraccar implements Notificator {
private final String url;
private final String key;
- public static class Notification {
+ public static class NotificationObject {
@JsonProperty("title")
private String title;
@JsonProperty("body")
@@ -50,7 +50,7 @@ public class NotificatorTraccar implements Notificator {
@JsonProperty("registration_ids")
private String[] tokens;
@JsonProperty("notification")
- private Notification notification;
+ private NotificationObject notification;
}
@Inject
@@ -62,19 +62,19 @@ public class NotificatorTraccar implements Notificator {
}
@Override
- public void send(User user, Event event, Position position) {
+ public void send(org.traccar.model.Notification notification, User user, Event event, Position position) {
if (user.hasAttribute("notificationTokens")) {
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
- Notification notification = new Notification();
- notification.title = shortMessage.getSubject();
- notification.body = shortMessage.getBody();
- notification.sound = "default";
+ NotificationObject item = new NotificationObject();
+ item.title = shortMessage.getSubject();
+ item.body = shortMessage.getBody();
+ item.sound = "default";
Message message = new Message();
message.tokens = user.getString("notificationTokens").split("[, ]");
- message.notification = notification;
+ message.notification = item;
client.target(url).request().header("Authorization", "key=" + key).post(Entity.json(message)).close();
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorWeb.java b/src/main/java/org/traccar/notificators/NotificatorWeb.java
index deabeade1..51884074e 100644
--- a/src/main/java/org/traccar/notificators/NotificatorWeb.java
+++ b/src/main/java/org/traccar/notificators/NotificatorWeb.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +17,7 @@
package org.traccar.notificators;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
@@ -32,14 +33,13 @@ public final class NotificatorWeb implements Notificator {
private final NotificationFormatter notificationFormatter;
@Inject
- public NotificatorWeb(
- ConnectionManager connectionManager, NotificationFormatter notificationFormatter) {
+ public NotificatorWeb(ConnectionManager connectionManager, NotificationFormatter notificationFormatter) {
this.connectionManager = connectionManager;
this.notificationFormatter = notificationFormatter;
}
@Override
- public void send(User user, Event event, Position position) {
+ public void send(Notification notification, User user, Event event, Position position) {
Event copy = new Event();
copy.setId(event.getId());
diff --git a/src/main/java/org/traccar/sms/HttpSmsClient.java b/src/main/java/org/traccar/sms/HttpSmsClient.java
index 51b161594..683022967 100644
--- a/src/main/java/org/traccar/sms/HttpSmsClient.java
+++ b/src/main/java/org/traccar/sms/HttpSmsClient.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -67,7 +67,7 @@ public class HttpSmsClient implements SmsManager {
}
private String prepareValue(String value) throws UnsupportedEncodingException {
- return encode ? URLEncoder.encode(value, StandardCharsets.UTF_8.name()) : value;
+ return encode ? URLEncoder.encode(value, StandardCharsets.UTF_8) : value;
}
private String preparePayload(String destAddress, String message) {
diff --git a/src/main/java/org/traccar/sms/SmsManager.java b/src/main/java/org/traccar/sms/SmsManager.java
index 9ab25d9cb..8cf99c9e8 100644
--- a/src/main/java/org/traccar/sms/SmsManager.java
+++ b/src/main/java/org/traccar/sms/SmsManager.java
@@ -20,7 +20,6 @@ import org.traccar.notification.MessageException;
public interface SmsManager {
- void sendMessage(
- String destAddress, String message, boolean command) throws InterruptedException, MessageException;
+ void sendMessage(String destAddress, String message, boolean command) throws MessageException;
}