-
-
Notifications
You must be signed in to change notification settings - Fork 387
Map SNS message attributes to SQS listener headers #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import io.awspring.cloud.sqs.support.converter.legacy.LegacyJackson2SqsMessagingMessageConverter; | ||
| import java.nio.ByteBuffer; | ||
| import java.time.Instant; | ||
| import java.util.Base64; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
@@ -42,6 +43,10 @@ | |
| import software.amazon.awssdk.services.sqs.model.Message; | ||
| import software.amazon.awssdk.services.sqs.model.MessageAttributeValue; | ||
| import software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName; | ||
| import tools.jackson.core.JacksonException; | ||
| import tools.jackson.core.type.TypeReference; | ||
| import tools.jackson.databind.JsonNode; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
|
|
||
| /** | ||
| * A {@link HeaderMapper} implementation for SQS {@link Message}s. Enables creating additional SQS related headers from | ||
|
|
@@ -59,6 +64,11 @@ public class SqsHeaderMapper implements ContextAwareHeaderMapper<Message> { | |
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(SqsHeaderMapper.class); | ||
|
|
||
| private static final TypeReference<Map<String, SnsNotification.MessageAttribute>> SNS_MESSAGE_ATTRIBUTES_TYPE = new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final JsonMapper jsonMapper = new JsonMapper(); | ||
|
|
||
| private BiFunction<Message, MessageHeaderAccessor, MessageHeaders> additionalHeadersFunction = ((message, | ||
| accessor) -> accessor.toMessageHeaders()); | ||
|
|
||
|
|
@@ -163,6 +173,7 @@ public MessageHeaders toHeaders(Message source) { | |
| accessor.copyHeadersIfAbsent(getMessageAttributesAsHeaders(source)); | ||
| accessor.copyHeadersIfAbsent(createDefaultHeaders(source)); | ||
| accessor.copyHeadersIfAbsent(createAdditionalHeaders(source)); | ||
| accessor.copyHeadersIfAbsent(getSnsMessageAttributesAsHeaders(source)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This runs on every SQS message, whether it's an SNS notification or not. Custom Container options can be used then to configure this custom mapper for Queues which will have messages sent via |
||
|
|
||
| MessageHeaders messageHeaders = accessor.toMessageHeaders(); | ||
| logger.trace("Mapped headers {} for message {}", messageHeaders, source.messageId()); | ||
|
|
@@ -191,6 +202,36 @@ private Map<String, Object> getMessageAttributesAsHeaders(Message source) { | |
| .collect(Collectors.toMap(Map.Entry::getKey, this::getValue)); | ||
| } | ||
|
|
||
| private Map<String, Object> getSnsMessageAttributesAsHeaders(Message source) { | ||
| String body = source.body(); | ||
| if (body == null || !body.contains("\"MessageAttributes\"")) { | ||
| return Map.of(); | ||
| } | ||
| try { | ||
| JsonNode jsonNode = jsonMapper.readTree(body); | ||
| if (!isSnsNotification(jsonNode)) { | ||
| return Map.of(); | ||
| } | ||
| JsonNode messageAttributes = jsonNode.get("MessageAttributes"); | ||
| if (messageAttributes == null || !messageAttributes.isObject()) { | ||
| return Map.of(); | ||
| } | ||
| Map<String, SnsNotification.MessageAttribute> attributes = jsonMapper.convertValue(messageAttributes, | ||
| SNS_MESSAGE_ATTRIBUTES_TYPE); | ||
| return attributes.entrySet().stream() | ||
| .collect(Collectors.toMap(Map.Entry::getKey, entry -> getValue(entry.getValue()))); | ||
| } | ||
| catch (JacksonException | IllegalArgumentException e) { | ||
| logger.trace("Could not map SNS message attributes for message " + source.messageId(), e); | ||
| return Map.of(); | ||
| } | ||
| } | ||
|
|
||
| private boolean isSnsNotification(JsonNode jsonNode) { | ||
| JsonNode type = jsonNode.get("Type"); | ||
| return type != null && "Notification".equals(type.asString()) && jsonNode.has("Message"); | ||
| } | ||
|
|
||
| private Object getValue(Map.Entry<String, MessageAttributeValue> entry) { | ||
| MessageAttributeValue value = entry.getValue(); | ||
| String dataType = value.dataType(); | ||
|
|
@@ -207,6 +248,19 @@ private Object getValue(Map.Entry<String, MessageAttributeValue> entry) { | |
| }; | ||
| } | ||
|
|
||
| private Object getValue(SnsNotification.MessageAttribute value) { | ||
| String dataType = value.getType(); | ||
| Assert.notNull(dataType, "dataType must not be null"); | ||
| String baseDataType = dataType.contains(".") ? dataType.substring(0, dataType.indexOf('.')) : dataType; | ||
|
|
||
| return switch (baseDataType) { | ||
| case MessageAttributeDataTypes.NUMBER -> getNumberValue(value.getValue(), dataType); | ||
| case MessageAttributeDataTypes.BINARY -> SdkBytes | ||
| .fromByteArray(Base64.getDecoder().decode(value.getValue())); | ||
| default -> value.getValue(); | ||
| }; | ||
| } | ||
|
|
||
| private Map<String, String> getMessageSystemAttributesAsHeaders(Message source) { | ||
| return source | ||
| .attributes() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JsonMapper should be the same one that is passed in autoconfiguration.