aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
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/NotificationHandler.java
parentb6bdf180c777566bbe908303774e53c7d4e099c4 (diff)
downloadnotification-sender-e03b55be17261ed13ddf421bcf4a804a083a7614.zip
added implementation
Diffstat (limited to 'src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java')
-rw-r--r--src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java93
1 files changed, 89 insertions, 4 deletions
diff --git a/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java b/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
index 43e1aa9..17b4a46 100644
--- a/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
+++ b/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
@@ -1,12 +1,97 @@
package eu.ortlepp.notificationsender;
import com.amazonaws.services.lambda.runtime.Context;
+import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import eu.ortlepp.notificationsender.model.Notifications;
+import eu.ortlepp.notificationsender.model.Response;
+import eu.ortlepp.notificationsender.model.Status;
+import eu.ortlepp.notificationsender.service.NotificationSender;
+import eu.ortlepp.notificationsender.service.SnsNotificationSender;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
-public class NotificationHandler implements RequestHandler<String, String> {
+/**
+ * AWS Lambda implementation.
+ */
+public class NotificationHandler implements RequestHandler<Map<String, Object>, Response> {
- public String handleRequest(String event, Context context) {
- return event.toLowerCase();
+ private LambdaLogger logger;
+
+ private NotificationSender sender;
+
+
+ /**
+ * Standard constructor for AWS runtime. Uses default NotificationSender implementation.
+ */
+ public NotificationHandler() {
+ this.sender = null;
+ }
+
+
+ /**
+ * Constructor with custom NotificationSender implementation. Mainly for testing purposes.
+ *
+ * @param sender A custom NotificationSender
+ */
+ public NotificationHandler(final NotificationSender sender) {
+ this.sender = sender;
+ }
+
+
+ /**
+ * Constructor with custom NotificationSender and Context implementation.
+ * Mainly for testing purposes.
+ *
+ * @param sender A custom NotificationSender
+ * @param context A custom Context (to initialize the build-in logger)
+ */
+ public NotificationHandler(final NotificationSender sender, final Context context) {
+ this.sender = sender;
+ this.logger = context.getLogger();
+ }
+
+
+ /**
+ * The AWS Lambda handler; executes the Lambda functionality.
+ *
+ * @param event The incoming event which triggered the Lambda
+ * @param context The context of the Lambda execution
+ * @return The final status of the execution
+ */
+ public Response handleRequest(final Map<String, Object> event, final Context context) {
+ logger = context.getLogger();
+ if (sender == null) {
+ sender = new SnsNotificationSender(logger);
+ }
+
+ List<String> notifications = extractNotifications(event.get("body"));
+ if (!notifications.isEmpty() && sender.sendNotifications(notifications)) {
+ return new Response(Status.SUCCESS);
+ }
+ return new Response(Status.FAILED);
+ }
+
+
+ /**
+ * Extract the notifications from the input JSON.
+ *
+ * @param body The input JSON
+ * @return The extracted messages; empty list if an error occurred while parsing the JSON
+ */
+ protected List<String> extractNotifications(final Object body) {
+ try {
+ Notifications notifications =
+ new ObjectMapper().readValue(body.toString(), Notifications.class);
+ return Arrays.asList(notifications.messages());
+ } catch (JsonProcessingException ex) {
+ logger.log("parsing input JSON failed : " + ex.getMessage());
+ return new ArrayList<>();
+ }
}
-} \ No newline at end of file
+}