fix(sqs): honor ReceiveMessage message attribute selectors (#4699)

* fix(sqs): honor ReceiveMessage message attribute selectors

* fix(sqs): match receive attribute checksum to selected fields

* test(sns): use the SQS Query message attribute selector

* fix(sqs): match message attribute prefix selectors
This commit is contained in:
XuJian authored and GitHub committed 2026-10-01 07:17:20 -04:00
1 parent 812e9f8269
commit 75b56d49e2
8 files changed
+257 -15

No files matched your search

@@ -259,6 +259,7 @@ public class SqsJsonHandler {
Set<String> requestedAttrs = new LinkedHashSet<>();
requestedAttrs.addAll(jsonNodeToList(request.path("AttributeNames")));
requestedAttrs.addAll(jsonNodeToList(request.path("MessageSystemAttributeNames")));
Set<String> requestedMessageAttrs = new LinkedHashSet<>(jsonNodeToList(request.path("MessageAttributeNames")));
List<Message> messages = sqsService.receiveMessage(queueUrl, maxMessages,
visibilityTimeout, waitTimeSeconds, region);
@@ -273,20 +274,23 @@ public class SqsJsonHandler {
}
ArrayNode messagesArray = response.putArray("Messages");
for (Message msg : messages) {
Map<String, MessageAttributeValue> selectedMessageAttrs = SqsMessageAttributeSelector.select(
msg.getMessageAttributes(), requestedMessageAttrs);
ObjectNode msgNode = objectMapper.createObjectNode();
msgNode.put("MessageId", msg.getMessageId());
msgNode.put("ReceiptHandle", msg.getReceiptHandle());
msgNode.put("MD5OfBody", msg.getMd5OfBody());
if (msg.getMd5OfMessageAttributes() != null) {
msgNode.put("MD5OfMessageAttributes", msg.getMd5OfMessageAttributes());
String messageAttrsMd5 = Message.computeMessageAttributesMd5(selectedMessageAttrs);
if (messageAttrsMd5 != null) {
msgNode.put("MD5OfMessageAttributes", messageAttrsMd5);
}
msgNode.put("Body", msg.getBody());
writeSystemAttributesJson(msgNode, msg, requestedAttrs, senderId);
if (msg.getMessageAttributes() != null && !msg.getMessageAttributes().isEmpty()) {
ObjectNode msgAttrs = msgNode.putObject("MessageAttributes");
for (Map.Entry<String, MessageAttributeValue> entry : msg.getMessageAttributes().entrySet()) {
if (!selectedMessageAttrs.isEmpty()) {
ObjectNode msgAttrs = objectMapper.createObjectNode();
for (Map.Entry<String, MessageAttributeValue> entry : selectedMessageAttrs.entrySet()) {
ObjectNode valNode = msgAttrs.putObject(entry.getKey());
valNode.put("DataType", entry.getValue().getDataType());
if (entry.getValue().getBinaryValue() != null) {
@@ -295,6 +299,7 @@ public class SqsJsonHandler {
valNode.put("StringValue", entry.getValue().getStringValue());
}
}
msgNode.set("MessageAttributes", msgAttrs);
}
messagesArray.add(msgNode);
@@ -0,0 +1,39 @@
package io.github.hectorvent.floci.services.sqs;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
final class SqsMessageAttributeSelector {
private SqsMessageAttributeSelector() {
}
static Map<String, MessageAttributeValue> select(Map<String, MessageAttributeValue> attributes,
Set<String> requestedNames) {
if (attributes == null || attributes.isEmpty() || requestedNames.isEmpty()) {
return Map.of();
}
Map<String, MessageAttributeValue> selected = new LinkedHashMap<>();
for (Map.Entry<String, MessageAttributeValue> entry : attributes.entrySet()) {
if (includes(requestedNames, entry.getKey())) {
selected.put(entry.getKey(), entry.getValue());
}
}
return selected;
}
private static boolean includes(Set<String> requestedNames, String name) {
if (requestedNames.contains("All") || requestedNames.contains(".*") || requestedNames.contains(name)) {
return true;
}
for (String requestedName : requestedNames) {
if (requestedName.endsWith(".*") && name.startsWith(requestedName.substring(0, requestedName.length() - 2))) {
return true;
}
}
return false;
}
}
@@ -273,23 +273,27 @@ public class SqsQueryHandler {
Set<String> requestedAttrs = new LinkedHashSet<>();
requestedAttrs.addAll(collectIndexed(params, "AttributeName."));
requestedAttrs.addAll(collectIndexed(params, "MessageSystemAttributeName."));
Set<String> requestedMessageAttrs = new LinkedHashSet<>(collectIndexed(params, "MessageAttributeName."));
List<Message> messages = sqsService.receiveMessage(queueUrl, maxMessages, visibilityTimeout, waitTimeSeconds, region);
String senderId = sqsService.senderIdFor(queueUrl);
XmlBuilder xml = new XmlBuilder();
for (Message msg : messages) {
Map<String, MessageAttributeValue> selectedMessageAttrs = SqsMessageAttributeSelector.select(
msg.getMessageAttributes(), requestedMessageAttrs);
xml.start("Message")
.elem("MessageId", msg.getMessageId())
.elem("ReceiptHandle", msg.getReceiptHandle())
.elem("MD5OfBody", msg.getMd5OfBody());
if (msg.getMd5OfMessageAttributes() != null) {
xml.elem("MD5OfMessageAttributes", msg.getMd5OfMessageAttributes());
String messageAttrsMd5 = Message.computeMessageAttributesMd5(selectedMessageAttrs);
if (messageAttrsMd5 != null) {
xml.elem("MD5OfMessageAttributes", messageAttrsMd5);
}
xml.elem("Body", msg.getBody());
writeSystemAttributesXml(xml, msg, requestedAttrs, senderId);
if (msg.getMessageAttributes() != null && !msg.getMessageAttributes().isEmpty()) {
for (Map.Entry<String, MessageAttributeValue> entry : msg.getMessageAttributes().entrySet()) {
if (!selectedMessageAttrs.isEmpty()) {
for (Map.Entry<String, MessageAttributeValue> entry : selectedMessageAttrs.entrySet()) {
xml.start("MessageAttribute")
.elem("Name", entry.getKey())
.start("Value")
@@ -151,9 +151,12 @@ public class Message {
}
public void updateMd5OfMessageAttributes() {
this.md5OfMessageAttributes = computeMessageAttributesMd5(messageAttributes);
}
public static String computeMessageAttributesMd5(Map<String, MessageAttributeValue> messageAttributes) {
if (messageAttributes == null || messageAttributes.isEmpty()) {
this.md5OfMessageAttributes = null;
return;
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
@@ -190,10 +193,10 @@ public class Message {
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
this.md5OfMessageAttributes = sb.toString();
return sb.toString();
} catch (Exception ignored) {
// Attribute serialization failure falls back to null MD5
this.md5OfMessageAttributes = null;
return null;
}
}
@@ -211,4 +214,4 @@ public class Message {
return "";
}
}
}
}