-
-
Notifications
You must be signed in to change notification settings - Fork 370
#1462 Introduce sns batch template #1574
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
Merged
MatejNedic
merged 5 commits into
awspring:main
from
MatejNedic:Introduce-SnsBatch-template
Feb 11, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
spring-cloud-aws-sns/src/main/java/io/awspring/cloud/sns/core/SnsHeaderConverterUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| * Copyright 2013-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.awspring.cloud.sns.core; | ||
|
|
||
| import static io.awspring.cloud.sns.core.SnsHeaders.MESSAGE_DEDUPLICATION_ID_HEADER; | ||
| import static io.awspring.cloud.sns.core.SnsHeaders.MESSAGE_GROUP_ID_HEADER; | ||
| import static io.awspring.cloud.sns.core.SnsHeaders.NOTIFICATION_SUBJECT_HEADER; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.springframework.messaging.Message; | ||
| import org.springframework.messaging.MessageHeaders; | ||
| import org.springframework.messaging.support.AbstractMessageChannel; | ||
| import org.springframework.util.Assert; | ||
| import org.springframework.util.MimeType; | ||
| import org.springframework.util.NumberUtils; | ||
| import software.amazon.awssdk.core.SdkBytes; | ||
| import software.amazon.awssdk.services.sns.SnsClient; | ||
| import software.amazon.awssdk.services.sns.model.MessageAttributeValue; | ||
|
|
||
| /** | ||
| * Implementation of {@link AbstractMessageChannel} which is used for converting and sending messages via | ||
| * {@link SnsClient} to SNS. | ||
| * | ||
| * @author Agim Emruli | ||
| * @author Alain Sahli | ||
| * @author Gyozo Papp | ||
| * @author Matej Nedic | ||
| * @since 4.0.1 | ||
| */ | ||
| public class SnsHeaderConverterUtil { | ||
|
|
||
| public static JsonStringEncoder jsonStringEncoder = JsonStringEncoder.create(); | ||
|
MatejNedic marked this conversation as resolved.
Outdated
|
||
| private static final Log logger = LogFactory.getLog(SnsHeaderConverterUtil.class); | ||
|
|
||
| public static Map<String, MessageAttributeValue> toSnsMessageAttributes(Message<?> message) { | ||
| HashMap<String, MessageAttributeValue> messageAttributes = new HashMap<>(); | ||
| for (Map.Entry<String, Object> messageHeader : message.getHeaders().entrySet()) { | ||
| String messageHeaderName = messageHeader.getKey(); | ||
| Object messageHeaderValue = messageHeader.getValue(); | ||
|
|
||
| if (isSkipHeader(messageHeaderName)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (MessageHeaders.CONTENT_TYPE.equals(messageHeaderName) && messageHeaderValue != null) { | ||
| messageAttributes.put(messageHeaderName, getContentTypeMessageAttribute(messageHeaderValue)); | ||
| } | ||
| else if (MessageHeaders.ID.equals(messageHeaderName) && messageHeaderValue != null) { | ||
| messageAttributes.put(messageHeaderName, getStringMessageAttribute(messageHeaderValue.toString())); | ||
| } | ||
| else if (MessageHeaders.TIMESTAMP.equals(messageHeaderName) && messageHeaderValue != null) { | ||
| messageAttributes.put(messageHeaderName, getDetailedNumberMessageAttribute(messageHeaderValue)); | ||
| } | ||
| else if (messageHeaderValue instanceof String) { | ||
| messageAttributes.put(messageHeaderName, getStringMessageAttribute((String) messageHeaderValue)); | ||
| } | ||
| else if (messageHeaderValue instanceof Number) { | ||
| messageAttributes.put(messageHeaderName, getDetailedNumberMessageAttribute(messageHeaderValue)); | ||
| } | ||
| else if (messageHeaderValue instanceof ByteBuffer) { | ||
| messageAttributes.put(messageHeaderName, getBinaryMessageAttribute((ByteBuffer) messageHeaderValue)); | ||
| } | ||
| else if (messageHeaderValue instanceof List) { | ||
| messageAttributes.put(messageHeaderName, getStringArrayMessageAttribute((List<?>) messageHeaderValue)); | ||
| } | ||
| else { | ||
| logger.warn(String.format( | ||
| "Message header with name '%s' and type '%s' cannot be sent as" | ||
| + " message attribute because it is not supported by SNS.", | ||
| messageHeaderName, messageHeaderValue != null ? messageHeaderValue.getClass().getName() : "")); | ||
| } | ||
| } | ||
|
|
||
| return messageAttributes; | ||
| } | ||
|
|
||
| private static boolean isSkipHeader(String headerName) { | ||
| return NOTIFICATION_SUBJECT_HEADER.equals(headerName) || MESSAGE_GROUP_ID_HEADER.equals(headerName) | ||
| || MESSAGE_DEDUPLICATION_ID_HEADER.equals(headerName); | ||
| } | ||
|
|
||
| private static <T> MessageAttributeValue getStringArrayMessageAttribute(List<T> messageHeaderValue) { | ||
| String stringValue = messageHeaderValue.stream().map(item -> { | ||
| StringBuilder sb = new StringBuilder(); | ||
| jsonStringEncoder.quoteAsString(item.toString(), sb); | ||
| return "\"" + sb + "\""; | ||
| }).collect(Collectors.joining(", ", "[", "]")); | ||
|
|
||
| return MessageAttributeValue.builder().dataType(MessageAttributeDataTypes.STRING_ARRAY).stringValue(stringValue) | ||
| .build(); | ||
| } | ||
|
|
||
| private static MessageAttributeValue getBinaryMessageAttribute(ByteBuffer messageHeaderValue) { | ||
| return MessageAttributeValue.builder().dataType(MessageAttributeDataTypes.BINARY) | ||
| .binaryValue(SdkBytes.fromByteBuffer(messageHeaderValue)).build(); | ||
| } | ||
|
|
||
| @Nullable | ||
| private static MessageAttributeValue getContentTypeMessageAttribute(Object messageHeaderValue) { | ||
| if (messageHeaderValue instanceof MimeType) { | ||
| return MessageAttributeValue.builder().dataType(MessageAttributeDataTypes.STRING) | ||
| .stringValue(messageHeaderValue.toString()).build(); | ||
| } | ||
| else if (messageHeaderValue instanceof String) { | ||
| return MessageAttributeValue.builder().dataType(MessageAttributeDataTypes.STRING) | ||
| .stringValue((String) messageHeaderValue).build(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static MessageAttributeValue getStringMessageAttribute(String messageHeaderValue) { | ||
| return MessageAttributeValue.builder().dataType(MessageAttributeDataTypes.STRING) | ||
| .stringValue(messageHeaderValue).build(); | ||
| } | ||
|
|
||
| private static MessageAttributeValue getDetailedNumberMessageAttribute(Object messageHeaderValue) { | ||
| Assert.isTrue(NumberUtils.STANDARD_NUMBER_TYPES.contains(messageHeaderValue.getClass()), | ||
| "Only standard number types are accepted as message header."); | ||
|
|
||
| return MessageAttributeValue.builder() | ||
| .dataType(MessageAttributeDataTypes.NUMBER + "." + messageHeaderValue.getClass().getName()) | ||
| .stringValue(messageHeaderValue.toString()).build(); | ||
| } | ||
|
|
||
| private static MessageAttributeValue getNumberMessageAttribute(Object messageHeaderValue) { | ||
| return MessageAttributeValue.builder().dataType(MessageAttributeDataTypes.NUMBER) | ||
| .stringValue(messageHeaderValue.toString()).build(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.