|
| 1 | +package com.amazonaws.services.sqs; |
| 2 | + |
| 3 | +import java.util.Map.Entry; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +import com.amazonaws.services.lambda.runtime.Context; |
| 7 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 8 | +import com.amazonaws.services.lambda.runtime.events.SQSEvent; |
| 9 | +import com.amazonaws.services.lambda.runtime.events.SQSEvent.MessageAttribute; |
| 10 | +import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage; |
| 11 | +import com.amazonaws.services.sqs.model.MessageAttributeValue; |
| 12 | + |
| 13 | +/** |
| 14 | + * An SQS-triggered Lambda function that just returns the body of the |
| 15 | + * SQS message. If the message is a request sent by a Requester client, |
| 16 | + * it sends the response through SQS as well. |
| 17 | + */ |
| 18 | +public class SQSIdentityFunction implements RequestHandler<SQSEvent, String> { |
| 19 | + |
| 20 | + private static final AmazonSQSResponder sqs = |
| 21 | + AmazonSQSResponderClientBuilder.defaultClient(); |
| 22 | + |
| 23 | + @Override |
| 24 | + public String handleRequest(SQSEvent input, Context context) { |
| 25 | + SQSMessage message = input.getRecords().get(0); |
| 26 | + String messageBody = message.getBody(); |
| 27 | + |
| 28 | + MessageContent requestMessage = new MessageContent(messageBody, |
| 29 | + message.getMessageAttributes().entrySet().stream().collect( |
| 30 | + Collectors.toMap(Entry::getKey, e -> toMessageAttributeValue(e.getValue())))); |
| 31 | + if (sqs.isResponseMessageRequested(requestMessage)) { |
| 32 | + MessageContent response = new MessageContent(messageBody); |
| 33 | + sqs.sendResponseMessage(requestMessage, response); |
| 34 | + } |
| 35 | + return messageBody; |
| 36 | + } |
| 37 | + |
| 38 | + private MessageAttributeValue toMessageAttributeValue(MessageAttribute attribute) { |
| 39 | + return new MessageAttributeValue().withDataType(attribute.getDataType()) |
| 40 | + .withStringValue(attribute.getStringValue()) |
| 41 | + .withStringListValues(attribute.getStringListValues()) |
| 42 | + .withBinaryValue(attribute.getBinaryValue()) |
| 43 | + .withBinaryListValues(attribute.getBinaryListValues()); |
| 44 | + } |
| 45 | +} |
0 commit comments