aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/sms
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-11 15:04:51 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-11 15:04:51 -0700
commit7c08991f12b4958135fdffc26f272677c03630ad (patch)
treec1111ec78bad1dd8b0e4eb8f0c9194095d154d57 /src/main/java/org/traccar/sms
parent4025a42c42e34bb620f4263de05781a10ddc7a9d (diff)
downloadtrackermap-server-7c08991f12b4958135fdffc26f272677c03630ad.tar.gz
trackermap-server-7c08991f12b4958135fdffc26f272677c03630ad.tar.bz2
trackermap-server-7c08991f12b4958135fdffc26f272677c03630ad.zip
Inject network client
Diffstat (limited to 'src/main/java/org/traccar/sms')
-rw-r--r--src/main/java/org/traccar/sms/HttpSmsClient.java23
-rw-r--r--src/main/java/org/traccar/sms/SnsSmsClient.java24
2 files changed, 22 insertions, 25 deletions
diff --git a/src/main/java/org/traccar/sms/HttpSmsClient.java b/src/main/java/org/traccar/sms/HttpSmsClient.java
index 5c3cef747..51b161594 100644
--- a/src/main/java/org/traccar/sms/HttpSmsClient.java
+++ b/src/main/java/org/traccar/sms/HttpSmsClient.java
@@ -16,11 +16,12 @@
*/
package org.traccar.sms;
-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.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.MediaType;
@@ -31,6 +32,7 @@ import java.nio.charset.StandardCharsets;
public class HttpSmsClient implements SmsManager {
+ private final Client client;
private final String url;
private final String authorizationHeader;
private final String authorization;
@@ -38,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));
@@ -53,7 +56,7 @@ public class HttpSmsClient implements SmsManager {
authorization = null;
}
}
- template = Context.getConfig().getString(Keys.SMS_HTTP_TEMPLATE).trim();
+ template = config.getString(Keys.SMS_HTTP_TEMPLATE).trim();
if (template.charAt(0) == '{' || template.charAt(0) == '[') {
encode = false;
mediaType = MediaType.APPLICATION_JSON_TYPE;
@@ -78,7 +81,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);
}
diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java
index 49889ac89..ed5a325cc 100644
--- a/src/main/java/org/traccar/sms/SnsSmsClient.java
+++ b/src/main/java/org/traccar/sms/SnsSmsClient.java
@@ -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,19 +38,12 @@ 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
@@ -64,11 +57,12 @@ 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) {
}