1818import com .fasterxml .jackson .databind .ObjectReader ;
1919import org .slf4j .Logger ;
2020import org .slf4j .LoggerFactory ;
21- import software .amazon .lambda .powertools .utilities .eventpart .factory .EventPartResolver ;
22- import software .amazon .lambda .powertools .utilities .eventpart .factory .EventPartResolverFactory ;
23- import software .amazon .lambda .powertools .utilities .eventpart .resolvers .APIGatewayProxyRequestEventPartResolver ;
2421
2522import java .io .IOException ;
2623import java .util .List ;
2724import java .util .Map ;
2825import java .util .stream .Collectors ;
2926
27+ import static java .nio .charset .StandardCharsets .UTF_8 ;
3028import static software .amazon .lambda .powertools .utilities .jmespath .Base64Function .decode ;
29+ import static software .amazon .lambda .powertools .utilities .jmespath .Base64GZipFunction .decompress ;
3130
3231/**
3332 * Class that can be used to extract the meaningful part of an event and deserialize it into a Java object.<br/>
@@ -38,16 +37,106 @@ public class EventDeserializer {
3837 private static final Logger LOG = LoggerFactory .getLogger (EventDeserializer .class );
3938
4039 /**
40+ * Extract the meaningful part of a Lambda Event object. Main events are built-in:
41+ * <ul>
42+ * <li>{@link APIGatewayProxyRequestEvent} -> body</li>
43+ * <li>{@link APIGatewayV2HTTPEvent} -> body</li>
44+ * <li>{@link SNSEvent} -> Records[0].Sns.Message</li>
45+ * <li>{@link SQSEvent} -> Records[*].body <i>(list)</i></li>
46+ * <li>{@link ScheduledEvent} -> detail</li>
47+ * <li>{@link ApplicationLoadBalancerRequestEvent} -> body</li>
48+ * <li>{@link CloudWatchLogsEvent} -> powertools_base64_gzip(data)</li>
49+ * <li>{@link CloudFormationCustomResourceEvent} -> resourceProperties</li>
50+ * <li>{@link KinesisEvent} -> Records[*].kinesis.powertools_base64(data) <i>(list)</i></li>
51+ * <li>{@link KinesisFirehoseEvent} -> Records[*].powertools_base64(data) <i>(list)</i></li>
52+ * <li>{@link KafkaEvent} -> records[*].values[*].powertools_base64(value) <i>(list)</i></li>
53+ * <li>{@link ActiveMQEvent} -> messages[*].powertools_base64(data) <i>(list)</i></li>
54+ * <li>{@link RabbitMQEvent} -> rmqMessagesByQueue[*].values[*].powertools_base64(data) <i>(list)</i></li>
55+ * <li>{@link KinesisAnalyticsFirehoseInputPreprocessingEvent} -> Records[*].kinesis.powertools_base64(data) <i>(list)</i></li>
56+ * <li>{@link KinesisAnalyticsStreamsInputPreprocessingEvent} > Records[*].kinesis.powertools_base64(data) <i>(list)</i></li>
57+ * <li>{@link String}</li>
58+ * <li>{@link Map}</li>
59+ * </ul>
4160 * To be used in conjunction with {@link EventPart#as(Class)} or {@link EventPart#asListOf(Class)}
4261 * for the deserialization.
4362 *
44- * @param event the event of your Lambda function handler method
63+ * @param object the event of your Lambda function handler method
4564 * @return the part of the event which is meaningful (ex: body of the API Gateway).<br/>
4665 */
47- public static EventPart extractDataFrom (Object event ) {
48- EventPartResolverFactory factory = new EventPartResolverFactory ();
49- EventPartResolver generator = factory .resolveEventType (event );
50- return generator .createEventPart (event );
66+ public static EventPart extractDataFrom (Object object ) {
67+ if (object instanceof String ) {
68+ return new EventPart ((String ) object );
69+ } else if (object instanceof Map ) {
70+ return new EventPart ((Map <String , Object >) object );
71+ } else if (object instanceof APIGatewayProxyRequestEvent ) {
72+ APIGatewayProxyRequestEvent event = (APIGatewayProxyRequestEvent ) object ;
73+ return new EventPart (event .getBody ());
74+ } else if (object instanceof APIGatewayV2HTTPEvent ) {
75+ APIGatewayV2HTTPEvent event = (APIGatewayV2HTTPEvent ) object ;
76+ return new EventPart (event .getBody ());
77+ } else if (object instanceof SNSEvent ) {
78+ SNSEvent event = (SNSEvent ) object ;
79+ return new EventPart (event .getRecords ().get (0 ).getSNS ().getMessage ());
80+ } else if (object instanceof SQSEvent ) {
81+ SQSEvent event = (SQSEvent ) object ;
82+ return new EventPart (event .getRecords ().stream ()
83+ .map (SQSEvent .SQSMessage ::getBody )
84+ .collect (Collectors .toList ()));
85+ } else if (object instanceof ScheduledEvent ) {
86+ ScheduledEvent event = (ScheduledEvent ) object ;
87+ return new EventPart (event .getDetail ());
88+ } else if (object instanceof ApplicationLoadBalancerRequestEvent ) {
89+ ApplicationLoadBalancerRequestEvent event = (ApplicationLoadBalancerRequestEvent ) object ;
90+ return new EventPart (event .getBody ());
91+ } else if (object instanceof CloudWatchLogsEvent ) {
92+ CloudWatchLogsEvent event = (CloudWatchLogsEvent ) object ;
93+ return new EventPart (decompress (decode (event .getAwsLogs ().getData ().getBytes (UTF_8 ))));
94+ } else if (object instanceof CloudFormationCustomResourceEvent ) {
95+ CloudFormationCustomResourceEvent event = (CloudFormationCustomResourceEvent ) object ;
96+ return new EventPart (event .getResourceProperties ());
97+ } else if (object instanceof KinesisEvent ) {
98+ KinesisEvent event = (KinesisEvent ) object ;
99+ return new EventPart (event .getRecords ().stream ()
100+ .map (r -> decode (r .getKinesis ().getData ()))
101+ .collect (Collectors .toList ()));
102+ } else if (object instanceof KinesisFirehoseEvent ) {
103+ KinesisFirehoseEvent event = (KinesisFirehoseEvent ) object ;
104+ return new EventPart (event .getRecords ().stream ()
105+ .map (r -> decode (r .getData ()))
106+ .collect (Collectors .toList ()));
107+ } else if (object instanceof KafkaEvent ) {
108+ KafkaEvent event = (KafkaEvent ) object ;
109+ return new EventPart (event .getRecords ().values ().stream ()
110+ .flatMap (List ::stream )
111+ .map (r -> decode (r .getValue ()))
112+ .collect (Collectors .toList ()));
113+ } else if (object instanceof ActiveMQEvent ) {
114+ ActiveMQEvent event = (ActiveMQEvent ) object ;
115+ return new EventPart (event .getMessages ().stream ()
116+ .map (m -> decode (m .getData ()))
117+ .collect (Collectors .toList ()));
118+ } else if (object instanceof RabbitMQEvent ) {
119+ RabbitMQEvent event = (RabbitMQEvent ) object ;
120+ return new EventPart (event .getRmqMessagesByQueue ().values ().stream ()
121+ .flatMap (List ::stream )
122+ .map (r -> decode (r .getData ()))
123+ .collect (Collectors .toList ()));
124+ } else if (object instanceof KinesisAnalyticsFirehoseInputPreprocessingEvent ) {
125+ KinesisAnalyticsFirehoseInputPreprocessingEvent event = (KinesisAnalyticsFirehoseInputPreprocessingEvent ) object ;
126+ return new EventPart (event .getRecords ().stream ()
127+ .map (r -> decode (r .getData ()))
128+ .collect (Collectors .toList ()));
129+ } else if (object instanceof KinesisAnalyticsStreamsInputPreprocessingEvent ) {
130+ KinesisAnalyticsStreamsInputPreprocessingEvent event = (KinesisAnalyticsStreamsInputPreprocessingEvent ) object ;
131+ return new EventPart (event .getRecords ().stream ()
132+ .map (r -> decode (r .getData ()))
133+ .collect (Collectors .toList ()));
134+ } else {
135+ // does not really make sense to use this EventDeserializer when you already have a typed object
136+ // just not to throw an exception
137+ LOG .warn ("Consider using your object directly instead of using EventDeserializer" );
138+ return new EventPart (object );
139+ }
51140 }
52141
53142 /**
@@ -60,19 +149,19 @@ public static class EventPart {
60149 private List <String > contentList ;
61150 private Object contentObject ;
62151
63- public EventPart (List <String > contentList ) {
152+ private EventPart (List <String > contentList ) {
64153 this .contentList = contentList ;
65154 }
66155
67- public EventPart (String content ) {
156+ private EventPart (String content ) {
68157 this .content = content ;
69158 }
70159
71- public EventPart (Map <String , Object > contentMap ) {
160+ private EventPart (Map <String , Object > contentMap ) {
72161 this .contentMap = contentMap ;
73162 }
74163
75- public EventPart (Object content ) {
164+ private EventPart (Object content ) {
76165 this .contentObject = content ;
77166 }
78167
0 commit comments