|
| 1 | +/* |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | +package software.amazon.lambda.powertools.utilities; |
| 15 | + |
| 16 | +import com.amazonaws.services.lambda.runtime.events.*; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 26 | +import static software.amazon.lambda.powertools.utilities.jmespath.Base64Function.decode; |
| 27 | +import static software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction.decompress; |
| 28 | + |
| 29 | +public class EventDeserializer { |
| 30 | + |
| 31 | + private static final Logger LOG = LoggerFactory.getLogger(EventDeserializer.class); |
| 32 | + |
| 33 | + public static EventPart from(Object obj) { |
| 34 | + if (obj instanceof String) { |
| 35 | + return new EventPart((String) obj); |
| 36 | + } else if (obj instanceof Map) { |
| 37 | + return new EventPart((Map<String, Object>) obj); |
| 38 | + } else if (obj instanceof APIGatewayProxyRequestEvent) { |
| 39 | + APIGatewayProxyRequestEvent event = (APIGatewayProxyRequestEvent) obj; |
| 40 | + return new EventPart(event.getBody()); |
| 41 | + } else if (obj instanceof APIGatewayV2HTTPEvent) { |
| 42 | + APIGatewayV2HTTPEvent event = (APIGatewayV2HTTPEvent) obj; |
| 43 | + return new EventPart(event.getBody()); |
| 44 | + } else if (obj instanceof SNSEvent) { |
| 45 | + SNSEvent event = (SNSEvent) obj; |
| 46 | + return new EventPart(event.getRecords().get(0).getSNS().getMessage()); |
| 47 | + } else if (obj instanceof SQSEvent) { |
| 48 | + SQSEvent event = (SQSEvent) obj; |
| 49 | + return new EventPart(event.getRecords().stream().map(SQSEvent.SQSMessage::getBody).collect(Collectors.toList())); |
| 50 | + } else if (obj instanceof ScheduledEvent) { |
| 51 | + ScheduledEvent event = (ScheduledEvent) obj; |
| 52 | + return new EventPart(event.getDetail()); |
| 53 | + } else if (obj instanceof ApplicationLoadBalancerRequestEvent) { |
| 54 | + ApplicationLoadBalancerRequestEvent event = (ApplicationLoadBalancerRequestEvent) obj; |
| 55 | + return new EventPart(event.getBody()); |
| 56 | + } else if (obj instanceof CloudWatchLogsEvent) { |
| 57 | + CloudWatchLogsEvent event = (CloudWatchLogsEvent) obj; |
| 58 | + return new EventPart(decompress(decode(event.getAwsLogs().getData().getBytes(UTF_8)))); |
| 59 | + } else if (obj instanceof CloudFormationCustomResourceEvent) { |
| 60 | + CloudFormationCustomResourceEvent event = (CloudFormationCustomResourceEvent) obj; |
| 61 | + return new EventPart(event.getResourceProperties()); |
| 62 | + } else if (obj instanceof KinesisEvent) { |
| 63 | + KinesisEvent event = (KinesisEvent) obj; |
| 64 | + return new EventPart(event.getRecords().stream().map(r -> decode(r.getKinesis().getData())).collect(Collectors.toList())); |
| 65 | + } else if (obj instanceof KinesisFirehoseEvent) { |
| 66 | + KinesisFirehoseEvent event = (KinesisFirehoseEvent) obj; |
| 67 | + return new EventPart(event.getRecords().stream().map(r -> decode(r.getData())).collect(Collectors.toList())); |
| 68 | + } else if (obj instanceof KafkaEvent) { |
| 69 | + KafkaEvent event = (KafkaEvent) obj; |
| 70 | + return new EventPart(event.getRecords().values().stream().flatMap(List::stream).map(r -> decode(r.getValue())).collect(Collectors.toList())); |
| 71 | + } else if (obj instanceof ActiveMQEvent) { |
| 72 | + ActiveMQEvent event = (ActiveMQEvent) obj; |
| 73 | + return new EventPart(event.getMessages().stream().map(m -> decode(m.getData())).collect(Collectors.toList())); |
| 74 | + } else if (obj instanceof RabbitMQEvent) { |
| 75 | + RabbitMQEvent event = (RabbitMQEvent) obj; |
| 76 | + return new EventPart(event.getRmqMessagesByQueue().values().stream().flatMap(List::stream).map(r -> decode(r.getData())).collect(Collectors.toList())); |
| 77 | + } else if (obj instanceof KinesisAnalyticsFirehoseInputPreprocessingEvent) { |
| 78 | + KinesisAnalyticsFirehoseInputPreprocessingEvent event = (KinesisAnalyticsFirehoseInputPreprocessingEvent) obj; |
| 79 | + return new EventPart(event.getRecords().stream().map(r -> decode(r.getData())).collect(Collectors.toList())); |
| 80 | + } else if (obj instanceof KinesisAnalyticsStreamsInputPreprocessingEvent) { |
| 81 | + KinesisAnalyticsStreamsInputPreprocessingEvent event = (KinesisAnalyticsStreamsInputPreprocessingEvent) obj; |
| 82 | + return new EventPart(event.getRecords().stream().map(r -> decode(r.getData())).collect(Collectors.toList())); |
| 83 | + } else { |
| 84 | + // does not really make sense to use this EventLoader when you already have a typed object |
| 85 | + // just not to throw an exception |
| 86 | + LOG.warn("Consider using your object directly instead of using EventDeserializer"); |
| 87 | + return new EventPart(obj); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static class EventPart { |
| 92 | + private Map<String, Object> contentMap; |
| 93 | + private String content; |
| 94 | + private List<String> contentList; |
| 95 | + private Object contentObject; |
| 96 | + |
| 97 | + public EventPart(List<String> contentList) { |
| 98 | + this.contentList = contentList; |
| 99 | + } |
| 100 | + |
| 101 | + public EventPart(String content) { |
| 102 | + this.content = content; |
| 103 | + } |
| 104 | + |
| 105 | + public EventPart(Map<String, Object> contentMap) { |
| 106 | + this.contentMap = contentMap; |
| 107 | + } |
| 108 | + |
| 109 | + public EventPart(Object content) { |
| 110 | + this.contentObject = content; |
| 111 | + } |
| 112 | + |
| 113 | + public <T> T extractDataAs(Class<T> clazz) { |
| 114 | + try { |
| 115 | + if (content != null) { |
| 116 | + if (content.getClass().equals(clazz)) { |
| 117 | + // do not read json when returning String, just return the String |
| 118 | + return (T) content; |
| 119 | + } |
| 120 | + return JsonConfig.get().getObjectMapper().reader().readValue(content, clazz); |
| 121 | + } |
| 122 | + if (contentMap != null) { |
| 123 | + return JsonConfig.get().getObjectMapper().convertValue(contentMap, clazz); |
| 124 | + } |
| 125 | + if (contentObject != null) { |
| 126 | + return (T) contentObject; |
| 127 | + } |
| 128 | + if (contentList != null) { |
| 129 | + throw new EventDeserializationException("The content of this event is a list, consider using 'extractDataAsListOf' instead"); |
| 130 | + } |
| 131 | + throw new EventDeserializationException("Event content is null"); |
| 132 | + } catch (IOException e) { |
| 133 | + throw new EventDeserializationException("Cannot load the event as " + clazz.getSimpleName(), e); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public <T> List<T> extractDataAsListOf(Class<T> clazz) { |
| 138 | + if (contentList == null) { |
| 139 | + throw new EventDeserializationException("Event content is null"); |
| 140 | + } |
| 141 | + return contentList.stream().map(s -> { |
| 142 | + try { |
| 143 | + return JsonConfig.get().getObjectMapper().reader().readValue(s, clazz); |
| 144 | + } catch (IOException e) { |
| 145 | + throw new EventDeserializationException("Cannot load the event as " + clazz.getSimpleName(), e); |
| 146 | + } |
| 147 | + }).collect(Collectors.toList()); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments