From 49b8cdc68d4ea2ab2e93e6d9d1622f3e828a7776 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 14:53:05 +0530 Subject: feature : added support of AWS SNS messaging --- src/main/java/org/traccar/Context.java | 2 +- src/main/java/org/traccar/config/Keys.java | 6 +++--- src/main/java/org/traccar/sms/SnsSmsClient.java | 17 +++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 934006347..fe494dabf 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -318,7 +318,7 @@ public final class Context { if (config.hasKey(Keys.SMS_HTTP_URL)) { smsManager = new HttpSmsClient(); - } else if (config.hasKey(Keys.AWS_REGION)) { + } else if (config.hasKey(Keys.SMS_AWS_REGION)) { smsManager = new SnsSmsClient(); } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index c3dce88c4..d2e5bbd99 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -690,14 +690,14 @@ public final class Keys { /** * AWS Access Key with SNS permission. */ - public static final ConfigKey AWS_ACCESS_KEY = new ConfigKey<>( + public static final ConfigKey SMS_AWS_ACCESS = new ConfigKey<>( "sms.aws.access", Collections.singletonList(KeyType.GLOBAL)); /** * AWS Secret Access Key with SNS permission. */ - public static final ConfigKey AWS_SECRET_KEY = new ConfigKey<>( + public static final ConfigKey SMS_AWS_SECRET = new ConfigKey<>( "sms.aws.secret", Collections.singletonList(KeyType.GLOBAL)); @@ -705,7 +705,7 @@ public final class Keys { * AWS Region for SNS service. * Make sure to use regions that are supported for messaging. */ - public static final ConfigKey AWS_REGION = new ConfigKey<>( + public static final ConfigKey SMS_AWS_REGION = new ConfigKey<>( "sms.aws.region", Collections.singletonList(KeyType.GLOBAL)); diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 81114e5f5..0c2be0bd2 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -21,6 +21,7 @@ import com.amazonaws.services.sns.AmazonSNS; import com.amazonaws.services.sns.AmazonSNSClientBuilder; import com.amazonaws.services.sns.model.MessageAttributeValue; import com.amazonaws.services.sns.model.PublishRequest; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; @@ -30,25 +31,25 @@ import org.traccar.notification.MessageException; import java.util.HashMap; import java.util.Map; - public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); private final AmazonSNS snsClient; public SnsSmsClient() { - if (Context.getConfig().getString(Keys.AWS_REGION) == null - || Context.getConfig().getString(Keys.AWS_ACCESS_KEY) == null - || Context.getConfig().getString(Keys.AWS_SECRET_KEY) == null) { + if (Context.getConfig().hasKey(Keys.SMS_AWS_REGION) + && Context.getConfig().hasKey(Keys.SMS_AWS_ACCESS) + && Context.getConfig().hasKey(Keys.SMS_AWS_SECRET)) { + snsClient = awsSNSClient(); + } else { throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); } - snsClient = awsSNSClient(); } public AmazonSNS awsSNSClient() { - BasicAWSCredentials awsCredentials = new BasicAWSCredentials(Context.getConfig().getString(Keys.AWS_ACCESS_KEY), - Context.getConfig().getString(Keys.AWS_SECRET_KEY)); - return AmazonSNSClientBuilder.standard().withRegion(Context.getConfig().getString(Keys.AWS_REGION)) + BasicAWSCredentials awsCredentials = new BasicAWSCredentials(Context.getConfig().getString(Keys.SMS_AWS_ACCESS), + Context.getConfig().getString(Keys.SMS_AWS_SECRET)); + return AmazonSNSClientBuilder.standard().withRegion(Context.getConfig().getString(Keys.SMS_AWS_REGION)) .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build(); } -- cgit v1.2.3