Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ private AmqpConstants() {
*/
public static final String CE_PREFIX = "cloudEvents:";

/**
* The alternative prefix name for CloudEvent attributes in the <em>application-properties</em> 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 <em>content-type</em> message property
*/
Expand All @@ -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 <em>application property</em>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ protected boolean isContentTypeHeader(final String key) {
}

/**
* Tests whether the given attribute key is prefixed with <em>cloudEvents:</em>
* Tests whether the given attribute key is prefixed with <em>cloudEvents:</em> or <em>cloudEvents_</em>
*
* @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));
}

/**
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public void readBinary(final Map<String, Object> props, final String contentType
assertThat(amqpReader.toEvent()).isEqualTo(event);
}

@ParameterizedTest()
@MethodSource("binaryTestArguments")
public void readBinaryWithUnderscorePrefix(final Map<String, Object> props, final String contentType, final byte[] body,
final CloudEvent event) {
final Map<String, Object> 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) {
Expand Down
Loading