diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-12-29 22:20:03 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-12-29 22:20:03 -0800 |
commit | 8fe1e679935688676f7139bb9ed6dc61838d0e2f (patch) | |
tree | 75704a42322d85d7245658b8d06aea9def6132a1 /src/main/java | |
parent | 7f0692101a017f33ffb984e6df92e5dbef6ce71b (diff) | |
download | traccar-server-8fe1e679935688676f7139bb9ed6dc61838d0e2f.tar.gz traccar-server-8fe1e679935688676f7139bb9ed6dc61838d0e2f.tar.bz2 traccar-server-8fe1e679935688676f7139bb9ed6dc61838d0e2f.zip |
Migrate SMS config keys
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/traccar/Context.java | 4 | ||||
-rw-r--r-- | src/main/java/org/traccar/config/Keys.java | 47 | ||||
-rw-r--r-- | src/main/java/org/traccar/sms/HttpSmsClient.java | 33 |
3 files changed, 64 insertions, 20 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 438c99a6d..9a382adb4 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -305,7 +305,7 @@ public final class Context { identityManager = deviceManager; - if (config.getBoolean("web.enable")) { + if (config.hasKey(Keys.WEB_PORT)) { webServer = new WebServer(config); } @@ -315,7 +315,7 @@ public final class Context { tripsConfig = initTripsConfig(); - if (config.getBoolean("sms.enable")) { + if (config.hasKey(Keys.SMS_HTTP_URL)) { smsManager = new HttpSmsClient(); } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index ebe496c4f..96ed7773b 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -439,8 +439,7 @@ public final class Keys { */ public static final ConfigKey<Integer> WEB_PORT = new ConfigKey<>( "web.port", - Collections.singletonList(KeyType.GLOBAL), - 8082); + Collections.singletonList(KeyType.GLOBAL)); /** * WebSocket connection timeout in milliseconds. Default timeout is 10 minutes. @@ -545,6 +544,50 @@ public final class Keys { Collections.singletonList(KeyType.GLOBAL)); /** + * SMS API service full URL. Enables SMS commands and notifications. + */ + public static final ConfigKey<String> SMS_HTTP_URL = new ConfigKey<>( + "sms.http.url", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * SMS API authorization header name. Default value is 'Authorization'. + */ + public static final ConfigKey<String> SMS_HTTP_AUTHORIZATION_HEADER = new ConfigKey<>( + "sms.http.authorizationHeader", + Collections.singletonList(KeyType.GLOBAL), + "Authorization"); + + /** + * SMS API authorization header value. This value takes precedence over user and password. + */ + public static final ConfigKey<String> SMS_HTTP_AUTHORIZATION = new ConfigKey<>( + "sms.http.authorization", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * SMS API basic authentication user. + */ + public static final ConfigKey<String> SMS_HTTP_USER = new ConfigKey<>( + "sms.http.user", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * SMS API basic authentication password. + */ + public static final ConfigKey<String> SMS_HTTP_PASSWORD = new ConfigKey<>( + "sms.http.password", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * SMS API body template. Placeholders {phone} and {message} can be used in the template. + * If value starts with '{' or '[', server automatically assumes JSON format. + */ + public static final ConfigKey<String> SMS_HTTP_TEMPLATE = new ConfigKey<>( + "sms.http.template", + 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/sms/HttpSmsClient.java b/src/main/java/org/traccar/sms/HttpSmsClient.java index f65c9e83e..6234eabb8 100644 --- a/src/main/java/org/traccar/sms/HttpSmsClient.java +++ b/src/main/java/org/traccar/sms/HttpSmsClient.java @@ -16,19 +16,18 @@ */ package org.traccar.sms; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.client.InvocationCallback; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; -import org.traccar.api.SecurityRequestFilter; +import org.traccar.config.Keys; import org.traccar.helper.DataConverter; import org.traccar.notification.MessageException; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.InvocationCallback; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -39,25 +38,27 @@ public class HttpSmsClient implements SmsManager { private final String url; private final String authorizationHeader; - private String authorization; + private final String authorization; private final String template; private final boolean encode; private final MediaType mediaType; public HttpSmsClient() { - url = Context.getConfig().getString("sms.http.url"); - authorizationHeader = Context.getConfig().getString("sms.http.authorizationHeader", - SecurityRequestFilter.AUTHORIZATION_HEADER); - authorization = Context.getConfig().getString("sms.http.authorization"); - if (authorization == null) { - String user = Context.getConfig().getString("sms.http.user"); - String password = Context.getConfig().getString("sms.http.password"); + url = Context.getConfig().getString(Keys.SMS_HTTP_URL); + authorizationHeader = Context.getConfig().getString(Keys.SMS_HTTP_AUTHORIZATION_HEADER); + if (Context.getConfig().hasKey(Keys.SMS_HTTP_AUTHORIZATION)) { + authorization = Context.getConfig().getString(Keys.SMS_HTTP_AUTHORIZATION); + } else { + String user = Context.getConfig().getString(Keys.SMS_HTTP_USER); + String password = Context.getConfig().getString(Keys.SMS_HTTP_PASSWORD); if (user != null && password != null) { authorization = "Basic " + DataConverter.printBase64((user + ":" + password).getBytes(StandardCharsets.UTF_8)); + } else { + authorization = null; } } - template = Context.getConfig().getString("sms.http.template").trim(); + template = Context.getConfig().getString(Keys.SMS_HTTP_TEMPLATE).trim(); if (template.charAt(0) == '{' || template.charAt(0) == '[') { encode = false; mediaType = MediaType.APPLICATION_JSON_TYPE; |