From 9cc97bf816e166f8d5c43d4aadee24105a9e317d 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 | 5 ++ src/main/java/org/traccar/config/Keys.java | 30 +++++++++++ src/main/java/org/traccar/sms/SnsSmsClient.java | 68 +++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/main/java/org/traccar/sms/SnsSmsClient.java (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 3ba8843f0..0dce70b93 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -60,6 +60,7 @@ import org.traccar.reports.model.TripsConfig; import org.traccar.schedule.ScheduleManager; import org.traccar.sms.HttpSmsClient; import org.traccar.sms.SmsManager; +import org.traccar.sms.SnsSmsClient; import org.traccar.web.WebServer; import javax.ws.rs.client.Client; @@ -319,6 +320,10 @@ public final class Context { smsManager = new HttpSmsClient(); } + if (config.hasKey(Keys.AWS_SNS_ENABLED)) { + smsManager = new SnsSmsClient(); + } + initEventsModule(); serverManager = new ServerManager(); diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 5f8b36c6d..423c6a31b 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -687,6 +687,36 @@ public final class Keys { "sms.http.template", Collections.singletonList(KeyType.GLOBAL)); + /** + * AWS Access Key with SNS permission. + */ + public static final ConfigKey AWS_ACCESS_KEY = new ConfigKey<>( + "aws.access_key", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * AWS Secret Access Key with SNS permission. + */ + public static final ConfigKey AWS_SECRET_KEY = new ConfigKey<>( + "aws.secret_key", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * SNS Enable switch. + * Use 'enable' to send message with SNS or 'disable' to stop using SNS + */ + public static final ConfigKey AWS_SNS_ENABLED = new ConfigKey<>( + "aws.sns_enabled", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * AWS Region for SNS service. + * Make sure to use regions that are supported for messaging. + */ + public static final ConfigKey AWS_REGION = new ConfigKey<>( + "aws.region", + Collections.singletonList(KeyType.GLOBAL)); + /** * Traccar notification API key. */ diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java new file mode 100644 index 000000000..07814efa5 --- /dev/null +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -0,0 +1,68 @@ +package org.traccar.sms; + +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +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 com.amazonaws.services.sns.model.PublishResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.traccar.Context; +import org.traccar.config.Keys; +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 String access_key; + private final String secret_key; + private final String region; + + private final String sns_status; + private final AmazonSNS snsClient; + + public SnsSmsClient() { + access_key = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); + secret_key = Context.getConfig().getString(Keys.AWS_SECRET_KEY); + sns_status = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); + region = Context.getConfig().getString(Keys.AWS_REGION); + snsClient = awsSNSClient(access_key, secret_key, region); + + if (!sns_status.equals("true") || access_key == null || secret_key == null || region == null) { + LOGGER.error("SNS Not Configured Properly. Please provide valid config."); + } + } + + public AmazonSNS awsSNSClient(String access_key, String secret_key, String region) { + BasicAWSCredentials awsCreds = new BasicAWSCredentials(access_key, secret_key); + return AmazonSNSClientBuilder.standard().withRegion(region) + .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); + } + + public void sendSNSMessage(String message, String destAddress) { + Map smsAttributes = new HashMap<>(); + smsAttributes.put("AWS.SNS.SMS.SenderID", + new MessageAttributeValue().withStringValue("VegitOne").withDataType("String")); + smsAttributes.put("AWS.SNS.SMS.SMSType", + new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); + + PublishResult result = this.snsClient.publish(new PublishRequest().withMessage(message) + .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); + } + + @java.lang.Override + public void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, MessageException { + sendSNSMessage(message, destAddress); + } + + @java.lang.Override + public void sendMessageAsync(String destAddress, String message, boolean command) { + sendSNSMessage(message, destAddress); + } +} \ No newline at end of file -- cgit v1.2.3 From 1705812639b9ca5e16ac1a0a96a7384b581e41e3 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 14:54:52 +0530 Subject: end of file --- src/main/java/org/traccar/sms/SnsSmsClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 07814efa5..cacf08811 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -65,4 +65,4 @@ public class SnsSmsClient implements SmsManager{ public void sendMessageAsync(String destAddress, String message, boolean command) { sendSNSMessage(message, destAddress); } -} \ No newline at end of file +} -- cgit v1.2.3 From d65f6d379cbda4f9d81ffe36b141bd370085f889 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 15:15:46 +0530 Subject: linting fixed --- src/main/java/org/traccar/config/Keys.java | 6 +++--- src/main/java/org/traccar/sms/SnsSmsClient.java | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 423c6a31b..937510f10 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -691,14 +691,14 @@ public final class Keys { * AWS Access Key with SNS permission. */ public static final ConfigKey AWS_ACCESS_KEY = new ConfigKey<>( - "aws.access_key", + "aws.access", Collections.singletonList(KeyType.GLOBAL)); /** * AWS Secret Access Key with SNS permission. */ public static final ConfigKey AWS_SECRET_KEY = new ConfigKey<>( - "aws.secret_key", + "aws.secret", Collections.singletonList(KeyType.GLOBAL)); /** @@ -706,7 +706,7 @@ public final class Keys { * Use 'enable' to send message with SNS or 'disable' to stop using SNS */ public static final ConfigKey AWS_SNS_ENABLED = new ConfigKey<>( - "aws.sns_enabled", + "aws.sns.enabled", 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 cacf08811..dcfe5b536 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -17,7 +17,7 @@ import java.util.HashMap; import java.util.Map; -public class SnsSmsClient implements SmsManager{ +public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); private final String access_key; @@ -57,7 +57,8 @@ public class SnsSmsClient implements SmsManager{ } @java.lang.Override - public void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, MessageException { + public void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException { sendSNSMessage(message, destAddress); } -- cgit v1.2.3 From 85da571cc6bf2f5012f0476a504c60360e2adcb4 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 15:22:42 +0530 Subject: linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index dcfe5b536..0e77cea01 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -20,21 +20,21 @@ import java.util.Map; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); - private final String access_key; - private final String secret_key; + private final String accessKey; + private final String secretKey; private final String region; - private final String sns_status; + private final String snsStatus; private final AmazonSNS snsClient; public SnsSmsClient() { - access_key = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); - secret_key = Context.getConfig().getString(Keys.AWS_SECRET_KEY); - sns_status = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); + accessKey = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); + secretKey = Context.getConfig().getString(Keys.AWS_SECRET_KEY); + snsStatus = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); region = Context.getConfig().getString(Keys.AWS_REGION); - snsClient = awsSNSClient(access_key, secret_key, region); + snsClient = awsSNSClient(accessKey, secretKey, region); - if (!sns_status.equals("true") || access_key == null || secret_key == null || region == null) { + if (!snsStatus.equals("true") || accessKey == null || secretKey == null || region == null) { LOGGER.error("SNS Not Configured Properly. Please provide valid config."); } } @@ -48,7 +48,7 @@ public class SnsSmsClient implements SmsManager { public void sendSNSMessage(String message, String destAddress) { Map smsAttributes = new HashMap<>(); smsAttributes.put("AWS.SNS.SMS.SenderID", - new MessageAttributeValue().withStringValue("VegitOne").withDataType("String")); + new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); -- cgit v1.2.3 From 0ef761fbbb0299651368f729f962bf38c013350a Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 15:29:17 +0530 Subject: linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 0e77cea01..8ac234605 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -39,8 +39,8 @@ public class SnsSmsClient implements SmsManager { } } - public AmazonSNS awsSNSClient(String access_key, String secret_key, String region) { - BasicAWSCredentials awsCreds = new BasicAWSCredentials(access_key, secret_key); + public AmazonSNS awsSNSClient(String accessKey, String secretKey, String region) { + BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); return AmazonSNSClientBuilder.standard().withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); } -- cgit v1.2.3 From b98f3333d83795b8034257469e4f34fe46050372 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Fri, 5 Mar 2021 15:31:32 +0530 Subject: Review resolution --- src/main/java/org/traccar/Context.java | 4 +- src/main/java/org/traccar/config/Keys.java | 8 ++-- src/main/java/org/traccar/sms/SnsSmsClient.java | 50 ++++++++++++++----------- 3 files changed, 34 insertions(+), 28 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 0dce70b93..40340b1f0 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -318,9 +318,7 @@ public final class Context { if (config.hasKey(Keys.SMS_HTTP_URL)) { smsManager = new HttpSmsClient(); - } - - if (config.hasKey(Keys.AWS_SNS_ENABLED)) { + } else if (config.hasKey(Keys.AWS_SNS_ENABLED)) { smsManager = new SnsSmsClient(); } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 937510f10..ac105b99c 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -691,14 +691,14 @@ public final class Keys { * AWS Access Key with SNS permission. */ public static final ConfigKey AWS_ACCESS_KEY = new ConfigKey<>( - "aws.access", + "sms.aws.access", Collections.singletonList(KeyType.GLOBAL)); /** * AWS Secret Access Key with SNS permission. */ public static final ConfigKey AWS_SECRET_KEY = new ConfigKey<>( - "aws.secret", + "sms.aws.secret", Collections.singletonList(KeyType.GLOBAL)); /** @@ -706,7 +706,7 @@ public final class Keys { * Use 'enable' to send message with SNS or 'disable' to stop using SNS */ public static final ConfigKey AWS_SNS_ENABLED = new ConfigKey<>( - "aws.sns.enabled", + "sms.sns.enabled", Collections.singletonList(KeyType.GLOBAL)); /** @@ -714,7 +714,7 @@ public final class Keys { * Make sure to use regions that are supported for messaging. */ public static final ConfigKey AWS_REGION = new ConfigKey<>( - "aws.region", + "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 8ac234605..f8b05b1ce 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -1,3 +1,18 @@ +/* + * Copyright 2021 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.sms; import com.amazonaws.auth.AWSStaticCredentialsProvider; @@ -20,29 +35,23 @@ import java.util.Map; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); - private final String accessKey; - private final String secretKey; - private final String region; - - private final String snsStatus; private final AmazonSNS snsClient; public SnsSmsClient() { - accessKey = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); - secretKey = Context.getConfig().getString(Keys.AWS_SECRET_KEY); - snsStatus = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); - region = Context.getConfig().getString(Keys.AWS_REGION); - snsClient = awsSNSClient(accessKey, secretKey, region); - - if (!snsStatus.equals("true") || accessKey == null || secretKey == null || region == null) { - LOGGER.error("SNS Not Configured Properly. Please provide valid config."); + if (!Context.getConfig().getString(Keys.AWS_SNS_ENABLED).equals("true") || + Context.getConfig().getString(Keys.AWS_ACCESS_KEY) == null || + Context.getConfig().getString(Keys.AWS_SECRET_KEY) == null || + Context.getConfig().getString(Keys.AWS_REGION) == null) { + throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); } + snsClient = awsSNSClient(); } - public AmazonSNS awsSNSClient(String accessKey, String secretKey, String region) { - BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); - return AmazonSNSClientBuilder.standard().withRegion(region) - .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); + 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)) + .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build(); } public void sendSNSMessage(String message, String destAddress) { @@ -51,18 +60,17 @@ public class SnsSmsClient implements SmsManager { new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - - PublishResult result = this.snsClient.publish(new PublishRequest().withMessage(message) + snsClient.publish(new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); } - @java.lang.Override + @Override public void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, MessageException { sendSNSMessage(message, destAddress); } - @java.lang.Override + @Override public void sendMessageAsync(String destAddress, String message, boolean command) { sendSNSMessage(message, destAddress); } -- cgit v1.2.3 From b01ac16f670861b1fb5ffe8747674d0c298d1f81 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Fri, 5 Mar 2021 15:35:46 +0530 Subject: Linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index f8b05b1ce..d79079f7a 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -21,7 +21,6 @@ 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 com.amazonaws.services.sns.model.PublishResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; @@ -38,10 +37,10 @@ public class SnsSmsClient implements SmsManager { private final AmazonSNS snsClient; public SnsSmsClient() { - if (!Context.getConfig().getString(Keys.AWS_SNS_ENABLED).equals("true") || - Context.getConfig().getString(Keys.AWS_ACCESS_KEY) == null || - Context.getConfig().getString(Keys.AWS_SECRET_KEY) == null || - Context.getConfig().getString(Keys.AWS_REGION) == null) { + if (!Context.getConfig().getString(Keys.AWS_SNS_ENABLED).equals("true") + || Context.getConfig().getString(Keys.AWS_ACCESS_KEY) == null + || Context.getConfig().getString(Keys.AWS_SECRET_KEY) == null + || Context.getConfig().getString(Keys.AWS_REGION) == null) { throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); } snsClient = awsSNSClient(); -- cgit v1.2.3 From d2dc1f983a9b5fc579b24c406587260e726dc35d Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Sun, 7 Mar 2021 09:58:11 +0530 Subject: Minor fixes --- src/main/java/org/traccar/Context.java | 2 +- src/main/java/org/traccar/config/Keys.java | 8 -------- src/main/java/org/traccar/sms/SnsSmsClient.java | 23 ++++++++++++----------- 3 files changed, 13 insertions(+), 20 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 40340b1f0..934006347 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_SNS_ENABLED)) { + } else if (config.hasKey(Keys.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 ac105b99c..c3dce88c4 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -701,14 +701,6 @@ public final class Keys { "sms.aws.secret", Collections.singletonList(KeyType.GLOBAL)); - /** - * SNS Enable switch. - * Use 'enable' to send message with SNS or 'disable' to stop using SNS - */ - public static final ConfigKey AWS_SNS_ENABLED = new ConfigKey<>( - "sms.sns.enabled", - Collections.singletonList(KeyType.GLOBAL)); - /** * AWS Region for SNS service. * Make sure to use regions that are supported for messaging. diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index d79079f7a..81114e5f5 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -37,10 +37,9 @@ public class SnsSmsClient implements SmsManager { private final AmazonSNS snsClient; public SnsSmsClient() { - if (!Context.getConfig().getString(Keys.AWS_SNS_ENABLED).equals("true") + if (Context.getConfig().getString(Keys.AWS_REGION) == null || Context.getConfig().getString(Keys.AWS_ACCESS_KEY) == null - || Context.getConfig().getString(Keys.AWS_SECRET_KEY) == null - || Context.getConfig().getString(Keys.AWS_REGION) == null) { + || Context.getConfig().getString(Keys.AWS_SECRET_KEY) == null) { throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); } snsClient = awsSNSClient(); @@ -53,7 +52,9 @@ public class SnsSmsClient implements SmsManager { .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build(); } - public void sendSNSMessage(String message, String destAddress) { + @Override + public void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException { Map smsAttributes = new HashMap<>(); smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue("SNS").withDataType("String")); @@ -63,14 +64,14 @@ public class SnsSmsClient implements SmsManager { .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); } - @Override - public void sendMessageSync(String destAddress, String message, boolean command) - throws InterruptedException, MessageException { - sendSNSMessage(message, destAddress); - } - @Override public void sendMessageAsync(String destAddress, String message, boolean command) { - sendSNSMessage(message, destAddress); + try { + sendMessageSync(destAddress, message, command); + } catch (InterruptedException interruptedException) { + LOGGER.warn("SMS send failed", interruptedException.getMessage()); + } catch (MessageException messageException) { + LOGGER.warn("SMS send failed", messageException.getMessage()); + } } } -- cgit v1.2.3 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/java/org/traccar') 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 From e290a9f460b5e364aa7cf020aaadc874c5fc282a Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 15:15:46 +0530 Subject: linting fixed --- src/main/java/org/traccar/config/Keys.java | 20 ++++--- src/main/java/org/traccar/sms/SnsSmsClient.java | 71 +++++++++++-------------- 2 files changed, 45 insertions(+), 46 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index d2e5bbd99..937510f10 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -690,23 +690,31 @@ public final class Keys { /** * AWS Access Key with SNS permission. */ - public static final ConfigKey SMS_AWS_ACCESS = new ConfigKey<>( - "sms.aws.access", + public static final ConfigKey AWS_ACCESS_KEY = new ConfigKey<>( + "aws.access", Collections.singletonList(KeyType.GLOBAL)); /** * AWS Secret Access Key with SNS permission. */ - public static final ConfigKey SMS_AWS_SECRET = new ConfigKey<>( - "sms.aws.secret", + public static final ConfigKey AWS_SECRET_KEY = new ConfigKey<>( + "aws.secret", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * SNS Enable switch. + * Use 'enable' to send message with SNS or 'disable' to stop using SNS + */ + public static final ConfigKey AWS_SNS_ENABLED = new ConfigKey<>( + "aws.sns.enabled", Collections.singletonList(KeyType.GLOBAL)); /** * AWS Region for SNS service. * Make sure to use regions that are supported for messaging. */ - public static final ConfigKey SMS_AWS_REGION = new ConfigKey<>( - "sms.aws.region", + public static final ConfigKey AWS_REGION = new ConfigKey<>( + "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 0c2be0bd2..dcfe5b536 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -1,18 +1,3 @@ -/* - * Copyright 2021 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package org.traccar.sms; import com.amazonaws.auth.AWSStaticCredentialsProvider; @@ -21,7 +6,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 com.amazonaws.services.sns.model.PublishResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; @@ -31,48 +16,54 @@ 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 String access_key; + private final String secret_key; + private final String region; + + private final String sns_status; private final AmazonSNS 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)) { - snsClient = awsSNSClient(); - } else { - throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); + access_key = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); + secret_key = Context.getConfig().getString(Keys.AWS_SECRET_KEY); + sns_status = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); + region = Context.getConfig().getString(Keys.AWS_REGION); + snsClient = awsSNSClient(access_key, secret_key, region); + + if (!sns_status.equals("true") || access_key == null || secret_key == null || region == null) { + LOGGER.error("SNS Not Configured Properly. Please provide valid config."); } } - public AmazonSNS awsSNSClient() { - 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(); + public AmazonSNS awsSNSClient(String access_key, String secret_key, String region) { + BasicAWSCredentials awsCreds = new BasicAWSCredentials(access_key, secret_key); + return AmazonSNSClientBuilder.standard().withRegion(region) + .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); } - @Override - public void sendMessageSync(String destAddress, String message, boolean command) - throws InterruptedException, MessageException { + public void sendSNSMessage(String message, String destAddress) { Map smsAttributes = new HashMap<>(); smsAttributes.put("AWS.SNS.SMS.SenderID", - new MessageAttributeValue().withStringValue("SNS").withDataType("String")); + new MessageAttributeValue().withStringValue("VegitOne").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - snsClient.publish(new PublishRequest().withMessage(message) + + PublishResult result = this.snsClient.publish(new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); } - @Override + @java.lang.Override + public void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException { + sendSNSMessage(message, destAddress); + } + + @java.lang.Override public void sendMessageAsync(String destAddress, String message, boolean command) { - try { - sendMessageSync(destAddress, message, command); - } catch (InterruptedException interruptedException) { - LOGGER.warn("SMS send failed", interruptedException.getMessage()); - } catch (MessageException messageException) { - LOGGER.warn("SMS send failed", messageException.getMessage()); - } + sendSNSMessage(message, destAddress); } } -- cgit v1.2.3 From d874d35a097f0a9a4547f8722a76a5dcda40c7e3 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 15:22:42 +0530 Subject: linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index dcfe5b536..0e77cea01 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -20,21 +20,21 @@ import java.util.Map; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); - private final String access_key; - private final String secret_key; + private final String accessKey; + private final String secretKey; private final String region; - private final String sns_status; + private final String snsStatus; private final AmazonSNS snsClient; public SnsSmsClient() { - access_key = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); - secret_key = Context.getConfig().getString(Keys.AWS_SECRET_KEY); - sns_status = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); + accessKey = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); + secretKey = Context.getConfig().getString(Keys.AWS_SECRET_KEY); + snsStatus = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); region = Context.getConfig().getString(Keys.AWS_REGION); - snsClient = awsSNSClient(access_key, secret_key, region); + snsClient = awsSNSClient(accessKey, secretKey, region); - if (!sns_status.equals("true") || access_key == null || secret_key == null || region == null) { + if (!snsStatus.equals("true") || accessKey == null || secretKey == null || region == null) { LOGGER.error("SNS Not Configured Properly. Please provide valid config."); } } @@ -48,7 +48,7 @@ public class SnsSmsClient implements SmsManager { public void sendSNSMessage(String message, String destAddress) { Map smsAttributes = new HashMap<>(); smsAttributes.put("AWS.SNS.SMS.SenderID", - new MessageAttributeValue().withStringValue("VegitOne").withDataType("String")); + new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); -- cgit v1.2.3 From d241a198bad5c877f542b7aa38afc3f328c14c47 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Thu, 4 Mar 2021 15:29:17 +0530 Subject: linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 0e77cea01..8ac234605 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -39,8 +39,8 @@ public class SnsSmsClient implements SmsManager { } } - public AmazonSNS awsSNSClient(String access_key, String secret_key, String region) { - BasicAWSCredentials awsCreds = new BasicAWSCredentials(access_key, secret_key); + public AmazonSNS awsSNSClient(String accessKey, String secretKey, String region) { + BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); return AmazonSNSClientBuilder.standard().withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); } -- cgit v1.2.3 From cab0b9d1e4dab5d17259cd33fd85fa2f0462a961 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Fri, 5 Mar 2021 15:31:32 +0530 Subject: Review resolution --- src/main/java/org/traccar/config/Keys.java | 20 +++---- src/main/java/org/traccar/sms/SnsSmsClient.java | 69 ++++++++++++++----------- 2 files changed, 45 insertions(+), 44 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 937510f10..d2e5bbd99 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -690,31 +690,23 @@ public final class Keys { /** * AWS Access Key with SNS permission. */ - public static final ConfigKey AWS_ACCESS_KEY = new ConfigKey<>( - "aws.access", + 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<>( - "aws.secret", - Collections.singletonList(KeyType.GLOBAL)); - - /** - * SNS Enable switch. - * Use 'enable' to send message with SNS or 'disable' to stop using SNS - */ - public static final ConfigKey AWS_SNS_ENABLED = new ConfigKey<>( - "aws.sns.enabled", + public static final ConfigKey SMS_AWS_SECRET = new ConfigKey<>( + "sms.aws.secret", Collections.singletonList(KeyType.GLOBAL)); /** * AWS Region for SNS service. * Make sure to use regions that are supported for messaging. */ - public static final ConfigKey AWS_REGION = new ConfigKey<>( - "aws.region", + 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 8ac234605..0c2be0bd2 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -1,3 +1,18 @@ +/* + * Copyright 2021 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.sms; import com.amazonaws.auth.AWSStaticCredentialsProvider; @@ -6,7 +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 com.amazonaws.services.sns.model.PublishResult; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; @@ -16,54 +31,48 @@ 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 String accessKey; - private final String secretKey; - private final String region; - - private final String snsStatus; private final AmazonSNS snsClient; public SnsSmsClient() { - accessKey = Context.getConfig().getString(Keys.AWS_ACCESS_KEY); - secretKey = Context.getConfig().getString(Keys.AWS_SECRET_KEY); - snsStatus = Context.getConfig().getString(Keys.AWS_SNS_ENABLED); - region = Context.getConfig().getString(Keys.AWS_REGION); - snsClient = awsSNSClient(accessKey, secretKey, region); - - if (!snsStatus.equals("true") || accessKey == null || secretKey == null || region == null) { - LOGGER.error("SNS Not Configured Properly. Please provide valid config."); + 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."); } } - public AmazonSNS awsSNSClient(String accessKey, String secretKey, String region) { - BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); - return AmazonSNSClientBuilder.standard().withRegion(region) - .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); + public AmazonSNS awsSNSClient() { + 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(); } - public void sendSNSMessage(String message, String destAddress) { + @Override + public void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException { Map smsAttributes = new HashMap<>(); smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - - PublishResult result = this.snsClient.publish(new PublishRequest().withMessage(message) + snsClient.publish(new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); } - @java.lang.Override - public void sendMessageSync(String destAddress, String message, boolean command) - throws InterruptedException, MessageException { - sendSNSMessage(message, destAddress); - } - - @java.lang.Override + @Override public void sendMessageAsync(String destAddress, String message, boolean command) { - sendSNSMessage(message, destAddress); + try { + sendMessageSync(destAddress, message, command); + } catch (InterruptedException interruptedException) { + LOGGER.warn("SMS send failed", interruptedException.getMessage()); + } catch (MessageException messageException) { + LOGGER.warn("SMS send failed", messageException.getMessage()); + } } } -- cgit v1.2.3 From 5d43235b3d1fb967c8df269d2bdedb2164e726b2 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Mon, 8 Mar 2021 13:08:16 +0530 Subject: Resloved requested changes --- src/main/java/org/traccar/sms/SnsSmsClient.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 0c2be0bd2..c2d568aa8 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -69,10 +69,8 @@ public class SnsSmsClient implements SmsManager { public void sendMessageAsync(String destAddress, String message, boolean command) { try { sendMessageSync(destAddress, message, command); - } catch (InterruptedException interruptedException) { - LOGGER.warn("SMS send failed", interruptedException.getMessage()); - } catch (MessageException messageException) { - LOGGER.warn("SMS send failed", messageException.getMessage()); + } catch (InterruptedException | MessageException exception) { + LOGGER.warn("SMS send failed", exception.getMessage()); } } } -- cgit v1.2.3 From 382237464a4c80bb4b5f05e05e77fedd61ab9f84 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Tue, 9 Mar 2021 12:53:52 +0530 Subject: Added Async call --- src/main/java/org/traccar/sms/SnsSmsClient.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index c2d568aa8..b4fbb8205 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -30,6 +30,7 @@ import org.traccar.notification.MessageException; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); @@ -54,8 +55,7 @@ public class SnsSmsClient implements SmsManager { } @Override - public void sendMessageSync(String destAddress, String message, boolean command) - throws InterruptedException, MessageException { + public void sendMessageSync(String destAddress, String message, boolean command) { Map smsAttributes = new HashMap<>(); smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue("SNS").withDataType("String")); @@ -67,10 +67,6 @@ public class SnsSmsClient implements SmsManager { @Override public void sendMessageAsync(String destAddress, String message, boolean command) { - try { - sendMessageSync(destAddress, message, command); - } catch (InterruptedException | MessageException exception) { - LOGGER.warn("SMS send failed", exception.getMessage()); - } + new Thread(() -> sendMessageSync(destAddress, message, command)); } } -- cgit v1.2.3 From d3a4ce4c83da1062b4cfc2f983150a827b3db389 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Tue, 9 Mar 2021 12:56:40 +0530 Subject: Removed unused imports --- src/main/java/org/traccar/sms/SnsSmsClient.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index b4fbb8205..af52315e0 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -26,11 +26,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; import org.traccar.config.Keys; -import org.traccar.notification.MessageException; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.CompletableFuture; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); -- cgit v1.2.3 From 8f6c0ca2d62f608c5acf370f460c553974aa29d4 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Wed, 17 Mar 2021 12:58:37 +0530 Subject: Async changes --- src/main/java/org/traccar/sms/SnsSmsClient.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index af52315e0..e59f42753 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -17,11 +17,11 @@ package org.traccar.sms; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; -import com.amazonaws.services.sns.AmazonSNS; -import com.amazonaws.services.sns.AmazonSNSClientBuilder; +import com.amazonaws.services.sns.*; import com.amazonaws.services.sns.model.MessageAttributeValue; 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; @@ -29,11 +29,12 @@ import org.traccar.config.Keys; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.Future; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); - private final AmazonSNS snsClient; + private final AmazonSNSAsync snsClient; public SnsSmsClient() { if (Context.getConfig().hasKey(Keys.SMS_AWS_REGION) @@ -45,10 +46,10 @@ public class SnsSmsClient implements SmsManager { } } - public AmazonSNS awsSNSClient() { + public AmazonSNSAsync awsSNSClient() { 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)) + return AmazonSNSAsyncClientBuilder.standard().withRegion(Context.getConfig().getString(Keys.SMS_AWS_REGION)) .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build(); } @@ -59,12 +60,12 @@ public class SnsSmsClient implements SmsManager { new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - snsClient.publish(new PublishRequest().withMessage(message) + snsClient.publishAsync(new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); } @Override public void sendMessageAsync(String destAddress, String message, boolean command) { - new Thread(() -> sendMessageSync(destAddress, message, command)); + sendMessageSync(destAddress, message, command); } } -- cgit v1.2.3 From a7c8ab0d0408518805dc3937b954ecc628b1e845 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Wed, 17 Mar 2021 13:05:47 +0530 Subject: Linting fix --- src/main/java/org/traccar/sms/SnsSmsClient.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index e59f42753..d13fb3c34 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -17,11 +17,11 @@ package org.traccar.sms; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; -import com.amazonaws.services.sns.*; +import com.amazonaws.services.sns.AmazonSNSAsync; +import com.amazonaws.services.sns.AmazonSNSAsyncClientBuilder; import com.amazonaws.services.sns.model.MessageAttributeValue; 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; @@ -29,7 +29,6 @@ import org.traccar.config.Keys; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Future; public class SnsSmsClient implements SmsManager { private static final Logger LOGGER = LoggerFactory.getLogger(SnsSmsClient.class); -- cgit v1.2.3 From fb37051a51c9ceb643ac4e5374fafaaad2196d95 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Fri, 19 Mar 2021 12:20:15 +0530 Subject: Review resolution --- src/main/java/org/traccar/sms/SnsSmsClient.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index d13fb3c34..8a3710ec8 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -1,5 +1,6 @@ /* * Copyright 2021 Anton Tananaev (anton@traccar.org) + * Copyright 2021 Subodh Ranadive (subodhranadive3103@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,9 +41,7 @@ public class SnsSmsClient implements SmsManager { && 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."); - } + } else { throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); } } public AmazonSNSAsync awsSNSClient() { @@ -59,8 +58,10 @@ public class SnsSmsClient implements SmsManager { new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - snsClient.publishAsync(new PublishRequest().withMessage(message) - .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); + try { + snsClient.publishAsync(new PublishRequest().withMessage(message) + .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); + } catch (Exception exception) { LOGGER.warn("SMS send failed", exception); } } @Override -- cgit v1.2.3 From 31753a17d83d0db41e645f292f2a1cf9d7374694 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Fri, 19 Mar 2021 12:30:16 +0530 Subject: Linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 8a3710ec8..cc7d09778 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -41,7 +41,9 @@ public class SnsSmsClient implements SmsManager { && 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."); } + } else { + throw new RuntimeException("SNS Not Configured Properly. Please provide valid config."); + } } public AmazonSNSAsync awsSNSClient() { @@ -61,7 +63,9 @@ public class SnsSmsClient implements SmsManager { try { snsClient.publishAsync(new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); - } catch (Exception exception) { LOGGER.warn("SMS send failed", exception); } + } catch (Exception exception) { + LOGGER.warn("SMS send failed", exception); + } } @Override -- cgit v1.2.3 From 94f88fa74e6cdd198511eb0dbf574030869638d7 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Fri, 19 Mar 2021 23:45:56 +0530 Subject: Review resolution --- src/main/java/org/traccar/sms/SnsSmsClient.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index cc7d09778..3b64852b5 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -40,19 +40,17 @@ public class SnsSmsClient implements SmsManager { if (Context.getConfig().hasKey(Keys.SMS_AWS_REGION) && Context.getConfig().hasKey(Keys.SMS_AWS_ACCESS) && Context.getConfig().hasKey(Keys.SMS_AWS_SECRET)) { - snsClient = awsSNSClient(); + 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 AmazonSNSAsync awsSNSClient() { - BasicAWSCredentials awsCredentials = new BasicAWSCredentials(Context.getConfig().getString(Keys.SMS_AWS_ACCESS), - Context.getConfig().getString(Keys.SMS_AWS_SECRET)); - return AmazonSNSAsyncClientBuilder.standard().withRegion(Context.getConfig().getString(Keys.SMS_AWS_REGION)) - .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build(); - } - @Override public void sendMessageSync(String destAddress, String message, boolean command) { Map smsAttributes = new HashMap<>(); -- cgit v1.2.3 From 6209400006ef3500adf64096dac84aa18dbf84e1 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Mon, 22 Mar 2021 17:48:32 +0530 Subject: Review resolution --- src/main/java/org/traccar/sms/SnsSmsClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 3b64852b5..6089d4f38 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -61,7 +61,7 @@ public class SnsSmsClient implements SmsManager { try { snsClient.publishAsync(new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); - } catch (Exception exception) { + } catch (RuntimeException exception) { LOGGER.warn("SMS send failed", exception); } } -- cgit v1.2.3 From 6b6fde98cec480405010eae7a9b07db7ccd4ae82 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Wed, 24 Mar 2021 15:29:17 +0530 Subject: Async Exception handling --- src/main/java/org/traccar/sms/SnsSmsClient.java | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 6089d4f38..1741b5559 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -18,11 +18,13 @@ package org.traccar.sms; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.handlers.AsyncHandler; import com.amazonaws.services.sns.AmazonSNSAsync; import com.amazonaws.services.sns.AmazonSNSAsyncClientBuilder; import com.amazonaws.services.sns.model.MessageAttributeValue; 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; @@ -58,12 +60,20 @@ public class SnsSmsClient implements SmsManager { new MessageAttributeValue().withStringValue("SNS").withDataType("String")); smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - try { - snsClient.publishAsync(new PublishRequest().withMessage(message) - .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes)); - } catch (RuntimeException exception) { - LOGGER.warn("SMS send failed", exception); - } + + PublishRequest pubReq = new PublishRequest().withMessage(message) + .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes); + + snsClient.publishAsync( pubReq, new AsyncHandler() { + @Override + public void onError( Exception exception) { + LOGGER.error("SMS send failed", exception); + } + @Override + public void onSuccess( PublishRequest request, PublishResult result ) { + LOGGER.info("SMS sent successfully"); + } + }); } @Override -- cgit v1.2.3 From 5c102511b5b29d89713018a790ed91404c805db4 Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Wed, 24 Mar 2021 15:38:57 +0530 Subject: Linting fixed --- src/main/java/org/traccar/sms/SnsSmsClient.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 1741b5559..44526f655 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -61,16 +61,16 @@ public class SnsSmsClient implements SmsManager { smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String")); - PublishRequest pubReq = new PublishRequest().withMessage(message) + PublishRequest publishRequest = new PublishRequest().withMessage(message) .withPhoneNumber(destAddress).withMessageAttributes(smsAttributes); - snsClient.publishAsync( pubReq, new AsyncHandler() { + snsClient.publishAsync(publishRequest, new AsyncHandler() { @Override - public void onError( Exception exception) { + public void onError(Exception exception) { LOGGER.error("SMS send failed", exception); } @Override - public void onSuccess( PublishRequest request, PublishResult result ) { + public void onSuccess(PublishRequest request, PublishResult result) { LOGGER.info("SMS sent successfully"); } }); -- cgit v1.2.3 From f10dabf49aa78c8277680d787837cfe74b9e653c Mon Sep 17 00:00:00 2001 From: Subodh Ranadive Date: Wed, 24 Mar 2021 22:47:43 +0530 Subject: Review resolution --- src/main/java/org/traccar/sms/SnsSmsClient.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/sms/SnsSmsClient.java b/src/main/java/org/traccar/sms/SnsSmsClient.java index 44526f655..bdd4104f5 100644 --- a/src/main/java/org/traccar/sms/SnsSmsClient.java +++ b/src/main/java/org/traccar/sms/SnsSmsClient.java @@ -71,7 +71,6 @@ public class SnsSmsClient implements SmsManager { } @Override public void onSuccess(PublishRequest request, PublishResult result) { - LOGGER.info("SMS sent successfully"); } }); } -- cgit v1.2.3