Skip to content

Commit

Permalink
Support receiving message attributes with types of the form Binary or…
Browse files Browse the repository at this point in the history
… Binary.
  • Loading branch information
ziyanli-amazon committed Dec 3, 2024
1 parent 1aa7bab commit a30c8b1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
.idea/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ for communicating with Amazon Simple Queue Service. This project builds on top o
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-messaging-lib</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<type>jar</type>
</dependency>
```
* **Further information** - Read the [API documentation](http://aws.amazon.com/documentation/sqs/).

## Feedback
* Give us feedback [here](https://github.com/awslabs/amazon-sqs-java-messaging-lib/issues).
* If you'd like to contribute a new feature or bug fix, we'd love to see Github pull requests from you.
* If you'd like to contribute a new feature or bug fix, we'd love to see GitHub pull requests from you.



10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-messaging-lib</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>Amazon SQS Java Messaging Library</name>
<description>The Amazon SQS Java Messaging Library holds the Java Message Service compatible classes, that are used
for communicating with Amazon Simple Queue Service.
Expand All @@ -29,7 +29,7 @@
</developer>
</developers>
<properties>
<aws-java-sdk.version>1.12.415</aws-java-sdk.version>
<aws-java-sdk.version>1.12.778</aws-java-sdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand All @@ -47,19 +47,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.1.1</version>
<version>5.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class SQSMessagingClientConstants {

public static final String SHORT = "Number.short";

public static final String BINARY = "Binary";

public static final String INT_FALSE = "0";

public static final String INT_TRUE = "1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.amazon.sqs.javamessaging.SQSQueueDestination;
import com.amazon.sqs.javamessaging.acknowledge.Acknowledger;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import static com.amazon.sqs.javamessaging.SQSMessagingClientConstants.*;

Expand All @@ -60,7 +62,8 @@
* approximate Receive Count observed on the SQS side.
*/
public class SQSMessage implements Message {


private static final Log LOG = LogFactory.getLog(SQSMessage.class);
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

// Define constant message types.
Expand Down Expand Up @@ -1150,9 +1153,11 @@ private static Object getObjectValue(String value, String type) throws JMSExcept
return Float.valueOf(value);
} else if (SHORT.equals(type)) {
return Short.valueOf(value);
} else if (type != null && (type.startsWith(STRING) || type.startsWith(NUMBER))) {
} else if (type != null && (type.startsWith(STRING) || type.startsWith(NUMBER) || type.startsWith(BINARY))) {
return value;
} else {
LOG.debug("Caught exception while constructing message attribute. " +
"Unknown or yet supported JMS property type - " + type);
throw new JMSException(type + " is not a supported JMS property type");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -33,6 +35,7 @@
import java.util.Set;

import static com.amazon.sqs.javamessaging.SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT;
import static com.amazon.sqs.javamessaging.SQSMessagingClientConstants.BINARY;
import static com.amazon.sqs.javamessaging.SQSMessagingClientConstants.JMSX_DELIVERY_COUNT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -59,6 +62,8 @@ public class SQSMessageTest {
final String myString = "myString";
final String myCustomString = "myCustomString";
final String myNumber = "myNumber";
final String myBinary = "myBinary";
final String myCustomBinary = "myCustomBinary";

@BeforeEach
public void setup() {
Expand Down Expand Up @@ -296,6 +301,18 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
.withDataType(SQSMessagingClientConstants.NUMBER)
.withStringValue("500"));

messageAttributes.put(myBinary, new MessageAttributeValue()
.withDataType(SQSMessagingClientConstants.BINARY)
.withStringValue("BQUZTR1ZzYkc4PQ=="));

messageAttributes.put(myCustomBinary, new MessageAttributeValue()
.withDataType(SQSMessagingClientConstants.BINARY + ".custom")
.withStringValue("data:image/gif;base64,QABAAACAkQBADs=")
.withBinaryValue(ByteBuffer.wrap("data:image/gif;base64,QABAAACAkQBADs="
.getBytes(StandardCharsets.UTF_8))
)
);

com.amazonaws.services.sqs.model.Message sqsMessage = new com.amazonaws.services.sqs.model.Message()
.withMessageAttributes(messageAttributes)
.withAttributes(systemAttributes)
Expand Down Expand Up @@ -353,6 +370,12 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
assertEquals(message.getFloatProperty(myNumber), 500f);
assertEquals(message.getDoubleProperty(myNumber), 500d);

assertTrue(message.propertyExists(myBinary));
assertEquals(message.getObjectProperty(myBinary), "BQUZTR1ZzYkc4PQ==");
assertEquals(message.getStringProperty(myBinary), "BQUZTR1ZzYkc4PQ==");
assertTrue(message.propertyExists(myCustomBinary));
assertEquals(message.getObjectProperty(myCustomBinary), "data:image/gif;base64,QABAAACAkQBADs=");
assertEquals(message.getStringProperty(myCustomBinary), "data:image/gif;base64,QABAAACAkQBADs=");

// Validate property names
Set<String> propertyNamesSet = Set.of(
Expand All @@ -367,6 +390,8 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
myString,
myCustomString,
myNumber,
myBinary,
myCustomBinary,
JMSX_DELIVERY_COUNT
);

Expand All @@ -388,6 +413,8 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
assertFalse(message.propertyExists("myByteProperty"));
assertFalse(message.propertyExists("myString"));
assertFalse(message.propertyExists("myNumber"));
assertFalse(message.propertyExists("myBinary"));
assertFalse(message.propertyExists("myCustomBinary"));

propertyNames = message.getPropertyNames();
assertFalse(propertyNames.hasMoreElements());
Expand Down

0 comments on commit a30c8b1

Please sign in to comment.