aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/eu/ortlepp/notificationsender/service
diff options
context:
space:
mode:
authorThorsten Ortlepp <post@ortlepp.eu>2024-04-26 00:30:46 +0200
committerThorsten Ortlepp <post@ortlepp.eu>2024-04-26 00:30:46 +0200
commite03b55be17261ed13ddf421bcf4a804a083a7614 (patch)
tree8512120756c494efe53c64f33e61a20316c17186 /src/main/java/eu/ortlepp/notificationsender/service
parentb6bdf180c777566bbe908303774e53c7d4e099c4 (diff)
downloadnotification-sender-e03b55be17261ed13ddf421bcf4a804a083a7614.zip
added implementation
Diffstat (limited to 'src/main/java/eu/ortlepp/notificationsender/service')
-rw-r--r--src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java41
-rw-r--r--src/main/java/eu/ortlepp/notificationsender/service/SnsNotificationSender.java63
2 files changed, 104 insertions, 0 deletions
diff --git a/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java b/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java
new file mode 100644
index 0000000..8c39c72
--- /dev/null
+++ b/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java
@@ -0,0 +1,41 @@
+package eu.ortlepp.notificationsender.service;
+
+import java.util.List;
+
+/**
+ * Interface for notification senders.
+ */
+public interface NotificationSender {
+
+ /**
+ * Send a list of notifications.
+ *
+ * @param notifications The notifications to send
+ * @return The result of the sending; true = successful, false = error occurred
+ */
+ boolean sendNotifications(final List<String> notifications);
+
+
+ /**
+ * Format notifications for sending.
+ * If only one notification is passed, it is returned as is.
+ * If multiple notifications are passed, a numbered list of notifications is returned.
+ *
+ * @param notifications The notifications to format
+ * @return The formatted notifications
+ */
+ default String formatNotifications(final List<String> notifications) {
+ if (notifications.size() == 1) {
+ return notifications.getFirst() + "\n\n";
+ }
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < notifications.size(); i++) {
+ builder.append(i + 1)
+ .append(". Notification:\n")
+ .append(notifications.get(i))
+ .append("\n\n");
+ }
+ return builder.toString();
+ }
+
+}
diff --git a/src/main/java/eu/ortlepp/notificationsender/service/SnsNotificationSender.java b/src/main/java/eu/ortlepp/notificationsender/service/SnsNotificationSender.java
new file mode 100644
index 0000000..958365a
--- /dev/null
+++ b/src/main/java/eu/ortlepp/notificationsender/service/SnsNotificationSender.java
@@ -0,0 +1,63 @@
+package eu.ortlepp.notificationsender.service;
+
+import com.amazonaws.services.lambda.runtime.LambdaLogger;
+import eu.ortlepp.notificationsender.util.Config;
+import java.util.List;
+import software.amazon.awssdk.http.HttpStatusCode;
+import software.amazon.awssdk.services.sns.SnsClient;
+import software.amazon.awssdk.services.sns.model.PublishRequest;
+import software.amazon.awssdk.services.sns.model.PublishResponse;
+import software.amazon.awssdk.services.sns.model.SnsException;
+
+/**
+ * A notification sender for Amazon SNS.
+ */
+public class SnsNotificationSender implements NotificationSender {
+
+ private final LambdaLogger logger;
+
+
+ /**
+ * Constructor to initialize the sender.
+ *
+ * @param logger The logger of the AWS Lambda
+ */
+ public SnsNotificationSender(final LambdaLogger logger) {
+ this.logger = logger;
+ }
+
+
+ /**
+ * Send a list of notifications to the configured SNS topic.
+ * All notifications are combined into a single message.
+ *
+ * @param notifications The notifications to send
+ * @return The result of the sending; true = successful, false = error occurred
+ */
+ @Override
+ public boolean sendNotifications(final List<String> notifications) {
+ String notification = formatNotifications(notifications);
+
+ try (SnsClient snsClient = SnsClient.builder().region(Config.REGION).build()) {
+ PublishRequest request =
+ PublishRequest.builder().message(notification).topicArn(Config.ARN).build();
+ PublishResponse result = snsClient.publish(request);
+ int resultStatusCode = result.sdkHttpResponse().statusCode();
+
+ if (resultStatusCode == HttpStatusCode.OK) {
+ logger.log("notification sent successfully");
+ return true;
+ }
+
+ logger.log("sending notification failed with status " + resultStatusCode);
+ logger.log("failed message was " + notification);
+ return false;
+
+ } catch (SnsException ex) {
+ logger.log("sending notification failed: " + ex.getMessage());
+ logger.log("failed message was " + notification);
+ return false;
+ }
+ }
+
+}