Skip to content

Commit

Permalink
Migrate SMS config keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Dec 30, 2020
1 parent 7f06921 commit 8fe1e67
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
2 changes: 1 addition & 1 deletion setup/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-->

<entry key='web.enable'>true</entry>
<entry key='web.port'>8082</entry>
<entry key='web.path'>./web</entry>
<entry key='web.cacheControl'>max-age=3600,public</entry>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/traccar/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public static void init(String configFile) throws Exception {

identityManager = deviceManager;

if (config.getBoolean("web.enable")) {
if (config.hasKey(Keys.WEB_PORT)) {
webServer = new WebServer(config);
}

Expand All @@ -315,7 +315,7 @@ public static void init(String configFile) throws Exception {

tripsConfig = initTripsConfig();

if (config.getBoolean("sms.enable")) {
if (config.hasKey(Keys.SMS_HTTP_URL)) {
smsManager = new HttpSmsClient();
}

Expand Down
47 changes: 45 additions & 2 deletions src/main/java/org/traccar/config/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -544,6 +543,50 @@ public final class Keys {
"forward.retry.limit",
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.
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/org/traccar/sms/HttpSmsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 8fe1e67

Please sign in to comment.