From ac4c843ecf12b72c5e13b10f5a95cd1fe021ba69 Mon Sep 17 00:00:00 2001 From: Gabriel Hall Date: Wed, 28 Oct 2026 16:09:40 -0700 Subject: [PATCH] fix(amqp): read cloudEvents_ application properties The AMQP binding allows the "cloudEvents_" prefix as well as "cloudEvents:" (section 3.1.3.1), and says consumers should understand both. JMS 2.0 clients can only use "cloudEvents_". The reader now accepts either prefix. The writer still uses "cloudEvents:". Fixes #756 Signed-off-by: Gabriel Hall --- .../cloudevents/amqp/ProtonAmqpMessageFactory.java | 11 ++++++++++- .../java/io/cloudevents/amqp/impl/AmqpConstants.java | 8 ++++++++ .../amqp/impl/ProtonAmqpBinaryMessageReader.java | 8 +++++--- .../amqp/ProtonAmqpMessageFactoryTest.java | 12 ++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/amqp/src/main/java/io/cloudevents/amqp/ProtonAmqpMessageFactory.java b/amqp/src/main/java/io/cloudevents/amqp/ProtonAmqpMessageFactory.java index 835ab5cc1..8064529b8 100644 --- a/amqp/src/main/java/io/cloudevents/amqp/ProtonAmqpMessageFactory.java +++ b/amqp/src/main/java/io/cloudevents/amqp/ProtonAmqpMessageFactory.java @@ -60,6 +60,7 @@ public static MessageReader createReader(final Message message) throws CloudEven /** * Creates a MessageReader to read using the {@code content-type} property, {@code application-properties} and data payload * of a proton-based {@link Message}. This reader is able to read both binary and structured encoded {@link io.cloudevents.CloudEvent}. + * Attributes and extensions are read from {@code application-properties} with the {@code cloudEvents:} or the {@code cloudEvents_} prefix. * * @param contentType The {@code content-type} of the message payload. * @param props The {@code application-properties} section of the proton-message containing cloud event metadata (attributes and/or extensions). @@ -73,11 +74,19 @@ public static MessageReader createReader(final String contentType, final Applica return MessageUtils.parseStructuredOrBinaryMessage( () -> contentType, format -> new GenericStructuredMessageReader(format, payload), - () -> AmqpConstants.getApplicationProperty(props, AmqpConstants.APP_PROPERTY_SPEC_VERSION, String.class), + () -> specVersion(props), sv -> new ProtonAmqpBinaryMessageReader(sv, props, contentType, payload) ); } + private static String specVersion(final ApplicationProperties props) { + final String specVersion = AmqpConstants.getApplicationProperty(props, AmqpConstants.APP_PROPERTY_SPEC_VERSION, String.class); + if (specVersion != null) { + return specVersion; + } + return AmqpConstants.getApplicationProperty(props, AmqpConstants.APP_PROPERTY_SPEC_VERSION_UNDERSCORE, String.class); + } + /** * Creates a {@link MessageWriter} capable of translating both a structured and binary CloudEvent * to a proton-based AMQP 1.0 {@link Message}. diff --git a/amqp/src/main/java/io/cloudevents/amqp/impl/AmqpConstants.java b/amqp/src/main/java/io/cloudevents/amqp/impl/AmqpConstants.java index c865ba08f..f0ccd9c66 100644 --- a/amqp/src/main/java/io/cloudevents/amqp/impl/AmqpConstants.java +++ b/amqp/src/main/java/io/cloudevents/amqp/impl/AmqpConstants.java @@ -43,6 +43,12 @@ private AmqpConstants() { */ public static final String CE_PREFIX = "cloudEvents:"; + /** + * The alternative prefix name for CloudEvent attributes in the application-properties section, + * which JMS 2.0 clients can read and use in message selectors. Readers accept both prefixes. + */ + public static final String CE_UNDERSCORE_PREFIX = "cloudEvents_"; + /** * The AMQP 1.0 content-type message property */ @@ -62,6 +68,8 @@ private AmqpConstants() { public static final String APP_PROPERTY_SPEC_VERSION = ATTRIBUTES_TO_PROPERTYNAMES.get("specversion"); + public static final String APP_PROPERTY_SPEC_VERSION_UNDERSCORE = CE_UNDERSCORE_PREFIX + "specversion"; + /** * Gets the value of a specific application property. * diff --git a/amqp/src/main/java/io/cloudevents/amqp/impl/ProtonAmqpBinaryMessageReader.java b/amqp/src/main/java/io/cloudevents/amqp/impl/ProtonAmqpBinaryMessageReader.java index 3ac6f36e1..667574783 100644 --- a/amqp/src/main/java/io/cloudevents/amqp/impl/ProtonAmqpBinaryMessageReader.java +++ b/amqp/src/main/java/io/cloudevents/amqp/impl/ProtonAmqpBinaryMessageReader.java @@ -68,16 +68,17 @@ protected boolean isContentTypeHeader(final String key) { } /** - * Tests whether the given attribute key is prefixed with cloudEvents: + * Tests whether the given attribute key is prefixed with cloudEvents: or cloudEvents_ * * @param key The key to test for the presence of the prefix. - * @return True if the specified key starts with the prefix or + * @return True if the specified key starts with either prefix or * false otherwise. */ @Override protected boolean isCloudEventsHeader(final String key) { final int prefixLength = AmqpConstants.CE_PREFIX.length(); - return key.length() > prefixLength && key.startsWith(AmqpConstants.CE_PREFIX); + return key.length() > prefixLength + && (key.startsWith(AmqpConstants.CE_PREFIX) || key.startsWith(AmqpConstants.CE_UNDERSCORE_PREFIX)); } /** @@ -89,6 +90,7 @@ protected boolean isCloudEventsHeader(final String key) { */ @Override protected String toCloudEventsKey(final String key) { + // both prefixes have the same length return key.substring(AmqpConstants.CE_PREFIX.length()); } diff --git a/amqp/src/test/java/io/cloudevents/amqp/ProtonAmqpMessageFactoryTest.java b/amqp/src/test/java/io/cloudevents/amqp/ProtonAmqpMessageFactoryTest.java index 390b0f854..2544eb200 100644 --- a/amqp/src/test/java/io/cloudevents/amqp/ProtonAmqpMessageFactoryTest.java +++ b/amqp/src/test/java/io/cloudevents/amqp/ProtonAmqpMessageFactoryTest.java @@ -60,6 +60,18 @@ public void readBinary(final Map props, final String contentType assertThat(amqpReader.toEvent()).isEqualTo(event); } + @ParameterizedTest() + @MethodSource("binaryTestArguments") + public void readBinaryWithUnderscorePrefix(final Map props, final String contentType, final byte[] body, + final CloudEvent event) { + final Map underscoreProps = props.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey().replace(AmqpConstants.CE_PREFIX, AmqpConstants.CE_UNDERSCORE_PREFIX), Map.Entry::getValue)); + final Section bodySection = body != null ? new org.apache.qpid.proton.amqp.messaging.Data(new Binary(body)) : null; + final MessageReader amqpReader = ProtonAmqpMessageFactory.createReader(contentType, new ApplicationProperties(underscoreProps), bodySection); + assertThat(amqpReader.getEncoding()).isEqualTo(Encoding.BINARY); + assertThat(amqpReader.toEvent()).isEqualTo(event); + } + @ParameterizedTest() @MethodSource("io.cloudevents.core.test.Data#allEventsWithoutExtensions") public void readStructured(final CloudEvent event) {