diff options
author | Anton Tananaev <atananaev@lyft.com> | 2020-12-30 10:54:14 -0800 |
---|---|---|
committer | Anton Tananaev <atananaev@lyft.com> | 2020-12-30 10:54:14 -0800 |
commit | b7677cbcc610c775ba57fa82486e88f42a30d5bd (patch) | |
tree | 6546ba6801d219bab6639c292b5bed07d7bd24f8 /src/main/java/org | |
parent | fa3d072b47be36ba011d6f48a1e3fe39bb5f2f6d (diff) | |
download | traccar-server-b7677cbcc610c775ba57fa82486e88f42a30d5bd.tar.gz traccar-server-b7677cbcc610c775ba57fa82486e88f42a30d5bd.tar.bz2 traccar-server-b7677cbcc610c775ba57fa82486e88f42a30d5bd.zip |
Migrate string keys
Diffstat (limited to 'src/main/java/org')
7 files changed, 92 insertions, 16 deletions
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 6b779baea..52f1894cd 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -45,6 +45,13 @@ public final class Keys { Collections.singletonList(KeyType.GLOBAL)); /** + * Device password. Commonly used in some protocol for sending commands. + */ + public static final ConfigSuffix<String> PROTOCOL_DEVICE_PASSWORD = new ConfigSuffix<>( + ".devicePassword", + Collections.singletonList(KeyType.GLOBAL)); + + /** * Default protocol mask to use. Currently used only by Skypatrol protocol. */ public static final ConfigSuffix<Integer> PROTOCOL_MASK = new ConfigSuffix<>( @@ -493,6 +500,13 @@ public final class Keys { Collections.singletonList(KeyType.GLOBAL)); /** + * Path to the web app folder. + */ + public static final ConfigKey<String> WEB_PATH = new ConfigKey<>( + "web.path", + Collections.singletonList(KeyType.GLOBAL)); + + /** * WebSocket connection timeout in milliseconds. Default timeout is 10 minutes. */ public static final ConfigKey<Long> WEB_TIMEOUT = new ConfigKey<>( @@ -671,6 +685,48 @@ public final class Keys { Collections.singletonList(KeyType.GLOBAL)); /** + * Traccar notification API key. + */ + public static final ConfigKey<String> NOTIFICATOR_TRACCAR_KEY = new ConfigKey<>( + "notificator.traccar.key", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Firebase server API key for push notifications. + */ + public static final ConfigKey<String> NOTIFICATOR_FIREBASE_KEY = new ConfigKey<>( + "notificator.firebase.key", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Pushover notification user name. + */ + public static final ConfigKey<String> NOTIFICATOR_PUSHOVER_USER = new ConfigKey<>( + "notificator.pushover.user", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Pushover notification user token. + */ + public static final ConfigKey<String> NOTIFICATOR_PUSHOVER_TOKEN = new ConfigKey<>( + "notificator.pushover.token", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Telegram notification API key. + */ + public static final ConfigKey<String> NOTIFICATOR_TELEGRAM_KEY = new ConfigKey<>( + "notificator.telegram.key", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Telegram notification chat id to post messages to. + */ + public static final ConfigKey<String> NOTIFICATOR_TELEGRAM_CHAT_ID = new ConfigKey<>( + "notificator.telegram.chatId", + Collections.singletonList(KeyType.GLOBAL)); + + /** * Maximum time period for reports in seconds. Can be useful to prevent users to request unreasonably long reports. * By default there is no limit. */ diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java index 8db694d73..c8a99274c 100644 --- a/src/main/java/org/traccar/database/DeviceManager.java +++ b/src/main/java/org/traccar/database/DeviceManager.java @@ -135,7 +135,7 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity } if (protocol != null) { - password = Context.getConfig().getString(protocol + "." + Command.KEY_DEVICE_PASSWORD); + password = Context.getConfig().getString(Keys.PROTOCOL_DEVICE_PASSWORD.withPrefix(protocol)); if (password != null) { return password; } diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java index 8a3deee92..89cdbcb14 100644 --- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java +++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; +import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; @@ -50,7 +51,7 @@ public class NotificatorFirebase extends Notificator { public NotificatorFirebase() { this( "https://fcm.googleapis.com/fcm/send", - Context.getConfig().getString("notificator.firebase.key")); + Context.getConfig().getString(Keys.NOTIFICATOR_FIREBASE_KEY)); } protected NotificatorFirebase(String url, String key) { diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 141d652ca..189af7834 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; +import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; @@ -33,7 +34,7 @@ public class NotificatorPushover extends Notificator { private final String url; private final String token; - private final String puser; + private final String user; public static class Message { @JsonProperty("token") @@ -48,8 +49,8 @@ public class NotificatorPushover extends Notificator { public NotificatorPushover() { url = "https://api.pushover.net/1/messages.json"; - token = Context.getConfig().getString("notificator.pushover.token"); - puser = Context.getConfig().getString("notificator.pushover.user"); + token = Context.getConfig().getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN); + user = Context.getConfig().getString(Keys.NOTIFICATOR_PUSHOVER_USER); } @Override @@ -68,14 +69,14 @@ public class NotificatorPushover extends Notificator { return; } - if (puser == null) { + if (this.user == null) { LOGGER.warn("Pushover user not found"); return; } Message message = new Message(); message.token = token; - message.user = puser; + message.user = this.user; message.device = device; message.message = NotificationFormatter.formatShortMessage(userId, event, position); diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index c0b51d043..6b8bc426d 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 Anton Tananaev (anton@traccar.org) + * Copyright 2019 - 2020 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; +import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.notification.NotificationFormatter; @@ -30,8 +31,8 @@ public class NotificatorTelegram extends Notificator { private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTelegram.class); - private String url; - private String chatId; + private final String url; + private final String chatId; public static class Message { @JsonProperty("chat_id") @@ -45,8 +46,8 @@ public class NotificatorTelegram extends Notificator { public NotificatorTelegram() { url = String.format( "https://api.telegram.org/bot%s/sendMessage", - Context.getConfig().getString("notificator.telegram.key")); - chatId = Context.getConfig().getString("notificator.telegram.chatId"); + Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY)); + chatId = Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID); } @Override diff --git a/src/main/java/org/traccar/notificators/NotificatorTraccar.java b/src/main/java/org/traccar/notificators/NotificatorTraccar.java index 2b75d60e4..5bcd18b5e 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTraccar.java +++ b/src/main/java/org/traccar/notificators/NotificatorTraccar.java @@ -1,13 +1,29 @@ +/* + * Copyright 2020 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. + * 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.traccar.Context; +import org.traccar.config.Keys; public class NotificatorTraccar extends NotificatorFirebase { public NotificatorTraccar() { super( "https://www.traccar.org/push/", - Context.getConfig().getString("notificator.traccar.key")); + Context.getConfig().getString(Keys.NOTIFICATOR_TRACCAR_KEY)); } } diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 92e0ca958..82e9397e4 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -134,7 +134,7 @@ public class WebServer { private void initWebApp(Config config, ServletContextHandler servletHandler) { ServletHolder servletHolder = new ServletHolder(DefaultServlet.class); - servletHolder.setInitParameter("resourceBase", new File(config.getString("web.path")).getAbsolutePath()); + servletHolder.setInitParameter("resourceBase", new File(config.getString(Keys.WEB_PATH)).getAbsolutePath()); if (config.getBoolean(Keys.WEB_DEBUG)) { servletHandler.setWelcomeFiles(new String[] {"debug.html", "index.html"}); } else { @@ -150,9 +150,10 @@ public class WebServer { private void initApi(Config config, ServletContextHandler servletHandler) { servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/api/socket"); - if (config.hasKey("media.path")) { + String mediaPath = config.getString(Keys.MEDIA_PATH); + if (mediaPath != null) { ServletHolder servletHolder = new ServletHolder(DefaultServlet.class); - servletHolder.setInitParameter("resourceBase", new File(config.getString("media.path")).getAbsolutePath()); + servletHolder.setInitParameter("resourceBase", new File(mediaPath).getAbsolutePath()); servletHolder.setInitParameter("dirAllowed", "false"); servletHolder.setInitParameter("pathInfoOnly", "true"); servletHandler.addServlet(servletHolder, "/api/media/*"); |