blob: 6a93992f9b9c4fa9d92efa3b5c3a593e2b48a8a5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
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;
/**
* AWS Lambda implementation.
*/
public class NotificationHandler implements RequestHandler<Map<String, Object>, Response> {
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. Omit empty notifications.
*
* @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);
var messages = new ArrayList<>(Arrays.asList(notifications.messages()));
messages.removeIf(message -> message.trim().isEmpty());
return messages;
} catch (JsonProcessingException ex) {
logger.log("parsing input JSON failed : " + ex.getMessage());
return new ArrayList<>();
}
}
}
|