aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/sms
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/sms')
-rw-r--r--src/main/java/org/traccar/sms/HttpSmsClient.java70
-rw-r--r--src/main/java/org/traccar/sms/SmsManager.java8
-rw-r--r--src/main/java/org/traccar/sms/SnsSmsClient.java33
3 files changed, 42 insertions, 69 deletions
diff --git a/src/main/java/org/traccar/sms/HttpSmsClient.java b/src/main/java/org/traccar/sms/HttpSmsClient.java
index 6234eabb8..a2a0dd57f 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 - 2020 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");
@@ -16,26 +16,23 @@
*/
package org.traccar.sms;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
+import org.traccar.config.Config;
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 jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.client.Invocation;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class HttpSmsClient implements SmsManager {
- private static final Logger LOGGER = LoggerFactory.getLogger(HttpSmsClient.class);
-
+ private final Client client;
private final String url;
private final String authorizationHeader;
private final String authorization;
@@ -43,14 +40,15 @@ public class HttpSmsClient implements SmsManager {
private final boolean encode;
private final MediaType mediaType;
- public HttpSmsClient() {
- 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);
+ public HttpSmsClient(Config config, Client client) {
+ this.client = client;
+ url = config.getString(Keys.SMS_HTTP_URL);
+ authorizationHeader = config.getString(Keys.SMS_HTTP_AUTHORIZATION_HEADER);
+ if (config.hasKey(Keys.SMS_HTTP_AUTHORIZATION)) {
+ authorization = config.getString(Keys.SMS_HTTP_AUTHORIZATION);
} else {
- String user = Context.getConfig().getString(Keys.SMS_HTTP_USER);
- String password = Context.getConfig().getString(Keys.SMS_HTTP_PASSWORD);
+ String user = config.getString(Keys.SMS_HTTP_USER);
+ String password = config.getString(Keys.SMS_HTTP_PASSWORD);
if (user != null && password != null) {
authorization = "Basic "
+ DataConverter.printBase64((user + ":" + password).getBytes(StandardCharsets.UTF_8));
@@ -58,8 +56,11 @@ public class HttpSmsClient implements SmsManager {
authorization = null;
}
}
- template = Context.getConfig().getString(Keys.SMS_HTTP_TEMPLATE).trim();
- if (template.charAt(0) == '{' || template.charAt(0) == '[') {
+ template = config.getString(Keys.SMS_HTTP_TEMPLATE).trim();
+ if (template.charAt(0) == '<') {
+ encode = false;
+ mediaType = MediaType.APPLICATION_XML_TYPE;
+ } else if (template.charAt(0) == '{' || template.charAt(0) == '[') {
encode = false;
mediaType = MediaType.APPLICATION_JSON_TYPE;
} else {
@@ -69,7 +70,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) {
@@ -83,7 +84,7 @@ public class HttpSmsClient implements SmsManager {
}
private Invocation.Builder getRequestBuilder() {
- Invocation.Builder builder = Context.getClient().target(url).request();
+ Invocation.Builder builder = client.target(url).request();
if (authorization != null) {
builder = builder.header(authorizationHeader, authorization);
}
@@ -91,26 +92,13 @@ public class HttpSmsClient implements SmsManager {
}
@Override
- public void sendMessageSync(String destAddress, String message, boolean command) throws MessageException {
- Response response = getRequestBuilder().post(Entity.entity(preparePayload(destAddress, message), mediaType));
- if (response.getStatus() / 100 != 2) {
- throw new MessageException(response.readEntity(String.class));
- }
- }
-
- @Override
- public void sendMessageAsync(final String destAddress, final String message, final boolean command) {
- getRequestBuilder().async().post(
- Entity.entity(preparePayload(destAddress, message), mediaType), new InvocationCallback<String>() {
- @Override
- public void completed(String s) {
- }
-
- @Override
- public void failed(Throwable throwable) {
- LOGGER.warn("SMS send failed", throwable);
+ public void sendMessage(String destAddress, String message, boolean command) throws MessageException {
+ try (Response response = getRequestBuilder()
+ .post(Entity.entity(preparePayload(destAddress, message), mediaType))) {
+ if (response.getStatus() / 100 != 2) {
+ throw new MessageException(response.readEntity(String.class));
}
- });
+ }
}
}
diff --git a/src/main/java/org/traccar/sms/SmsManager.java b/src/main/java/org/traccar/sms/SmsManager.java
index 3b0cbda7f..8cf99c9e8 100644
--- a/src/main/java/org/traccar/sms/SmsManager.java
+++ b/src/main/java/org/traccar/sms/SmsManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 Anton Tananaev (anton@traccar.org)
+ * 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");
@@ -20,10 +20,6 @@ import org.traccar.notification.MessageException;
public interface SmsManager {
- void sendMessageSync(
- String destAddress, String message, boolean command) throws InterruptedException, MessageException;
-
- void sendMessageAsync(
- String destAddress, String message, boolean command);
+ void sendMessage(String destAddress, String message, boolean command) throws MessageException;
}
diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java
index bdd4104f5..ed5a325cc 100644
--- a/src/main/java/org/traccar/sms/SnsSmsClient.java
+++ b/src/main/java/org/traccar/sms/SnsSmsClient.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2021 Anton Tananaev (anton@traccar.org)
+ * Copyright 2021 - 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2021 Subodh Ranadive (subodhranadive3103@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,7 +27,7 @@ import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Context;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import java.util.HashMap;
@@ -38,23 +38,16 @@ public class SnsSmsClient implements SmsManager {
private final AmazonSNSAsync snsClient;
- public SnsSmsClient() {
- if (Context.getConfig().hasKey(Keys.SMS_AWS_REGION)
- && Context.getConfig().hasKey(Keys.SMS_AWS_ACCESS)
- && Context.getConfig().hasKey(Keys.SMS_AWS_SECRET)) {
- BasicAWSCredentials awsCredentials =
- new BasicAWSCredentials(Context.getConfig().getString(Keys.SMS_AWS_ACCESS),
- Context.getConfig().getString(Keys.SMS_AWS_SECRET));
- snsClient = AmazonSNSAsyncClientBuilder.standard()
- .withRegion(Context.getConfig().getString(Keys.SMS_AWS_REGION))
- .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
- } else {
- throw new RuntimeException("SNS Not Configured Properly. Please provide valid config.");
- }
+ public SnsSmsClient(Config config) {
+ BasicAWSCredentials awsCredentials = new BasicAWSCredentials(
+ config.getString(Keys.SMS_AWS_ACCESS), config.getString(Keys.SMS_AWS_SECRET));
+ snsClient = AmazonSNSAsyncClientBuilder.standard()
+ .withRegion(config.getString(Keys.SMS_AWS_REGION))
+ .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
}
@Override
- public void sendMessageSync(String destAddress, String message, boolean command) {
+ public void sendMessage(String destAddress, String message, boolean command) {
Map<String, MessageAttributeValue> smsAttributes = new HashMap<>();
smsAttributes.put("AWS.SNS.SMS.SenderID",
new MessageAttributeValue().withStringValue("SNS").withDataType("String"));
@@ -64,19 +57,15 @@ public class SnsSmsClient implements SmsManager {
PublishRequest publishRequest = new PublishRequest().withMessage(message)
.withPhoneNumber(destAddress).withMessageAttributes(smsAttributes);
- snsClient.publishAsync(publishRequest, new AsyncHandler<PublishRequest, PublishResult>() {
+ snsClient.publishAsync(publishRequest, new AsyncHandler<>() {
@Override
public void onError(Exception exception) {
LOGGER.error("SMS send failed", exception);
}
+
@Override
public void onSuccess(PublishRequest request, PublishResult result) {
}
});
}
-
- @Override
- public void sendMessageAsync(String destAddress, String message, boolean command) {
- sendMessageSync(destAddress, message, command);
- }
}