use of io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class KafkaCloudEventHelper method createFromBinaryCloudEvent.
public static <T, K> IncomingKafkaCloudEventMetadata<K, T> createFromBinaryCloudEvent(ConsumerRecord<?, T> record) {
DefaultCloudEventMetadataBuilder<T> builder = new DefaultCloudEventMetadataBuilder<>();
// Build a map containing all the headers
// We remove the entry at each access to filter out extension attribute.
Map<String, String> headers = new HashMap<>();
record.headers().forEach(kh -> {
String key = kh.key();
// Rules 3.2.3 - Force UTF-8
String value = new String(kh.value(), StandardCharsets.UTF_8);
headers.put(key, value);
});
// Required
builder.withSpecVersion(headers.remove(KAFKA_HEADER_FOR_SPEC_VERSION));
builder.withId(headers.remove(KAFKA_HEADER_FOR_ID));
String source = headers.remove(KAFKA_HEADER_FOR_SOURCE);
if (source == null) {
throw new IllegalArgumentException("The Kafka record must contain the " + KAFKA_HEADER_FOR_SOURCE + " header");
}
builder.withSource(URI.create(source));
builder.withType(headers.remove(KAFKA_HEADER_FOR_TYPE));
// Optional
// Rules 3.2.1 - Set datacontenttype to the record's content type header
String ct = headers.remove(KAFKA_HEADER_CONTENT_TYPE);
if (ct != null) {
builder.withDataContentType(ct);
}
String schema = headers.remove(KAFKA_HEADER_FOR_SCHEMA);
if (schema != null) {
builder.withDataSchema(URI.create(schema));
}
String subject = headers.remove(KAFKA_HEADER_FOR_SUBJECT);
if (subject != null) {
builder.withSubject(subject);
}
String time = headers.remove(KAFKA_HEADER_FOR_TIME);
if (time != null) {
ZonedDateTime parse = ZonedDateTime.parse(time, RFC3339_DATE_FORMAT);
builder.withTimestamp(parse);
}
// Extensions
if (record.key() != null) {
builder.withExtension(CE_KAFKA_KEY, record.key());
}
builder.withExtension(CE_KAFKA_TOPIC, record.topic());
headers.entrySet().stream().filter(entry -> entry.getKey().startsWith(CE_HEADER_PREFIX)).forEach(entry -> {
String key = entry.getKey().substring(CE_HEADER_PREFIX.length());
// Implementation choice: Extension attributes are stored as String.
builder.withExtension(key, entry.getValue());
});
// Data
builder.withData(record.value());
BaseCloudEventMetadata<T> cloudEventMetadata = builder.build();
return new DefaultIncomingKafkaCloudEventMetadata<>(new DefaultIncomingCloudEventMetadata<>(cloudEventMetadata));
}
use of io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class AmqpCloudEventHelper method createFromStructuredCloudEvent.
public static <T> IncomingCloudEventMetadata<T> createFromStructuredCloudEvent(io.vertx.amqp.AmqpMessage message) {
DefaultCloudEventMetadataBuilder<T> builder = new DefaultCloudEventMetadataBuilder<>();
JsonObject content;
Section body = message.unwrap().getBody();
if (body.getType() == Section.SectionType.AmqpValue) {
// String value
content = new JsonObject(message.bodyAsString());
} else if (body.getType() == Section.SectionType.Data) {
// Byte[]
content = message.bodyAsBinary().toJsonObject();
} else {
throw new IllegalArgumentException("Invalid value type. Structured Cloud Event can only be created from String, JsonObject and byte[]");
}
// Required
builder.withSpecVersion(content.getString(CloudEventMetadata.CE_ATTRIBUTE_SPEC_VERSION));
builder.withId(content.getString(CloudEventMetadata.CE_ATTRIBUTE_ID));
String source = content.getString(CloudEventMetadata.CE_ATTRIBUTE_SOURCE);
if (source == null) {
throw new IllegalArgumentException("The JSON value must contain the " + CloudEventMetadata.CE_ATTRIBUTE_SOURCE + " attribute");
}
builder.withSource(URI.create(source));
builder.withType(content.getString(CloudEventMetadata.CE_ATTRIBUTE_TYPE));
// Optional
String ct = content.getString(CloudEventMetadata.CE_ATTRIBUTE_DATA_CONTENT_TYPE);
if (ct != null) {
builder.withDataContentType(ct);
}
String schema = content.getString(CloudEventMetadata.CE_ATTRIBUTE_DATA_SCHEMA);
if (schema != null) {
builder.withDataSchema(URI.create(schema));
}
String subject = content.getString(CloudEventMetadata.CE_ATTRIBUTE_SUBJECT);
if (subject != null) {
builder.withSubject(subject);
}
String time = content.getString(CloudEventMetadata.CE_ATTRIBUTE_TIME);
if (time != null) {
builder.withTimestamp(ZonedDateTime.parse(time, RFC3339_DATE_FORMAT));
}
// Data
Object data = content.getValue("data");
// noinspection unchecked
builder.withData((T) data);
BaseCloudEventMetadata<T> cloudEventMetadata = builder.build();
cloudEventMetadata.validate();
return new DefaultIncomingCloudEventMetadata<>(cloudEventMetadata);
}
use of io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class AmqpCloudEventHelper method createFromBinaryCloudEvent.
public static <T> IncomingCloudEventMetadata<T> createFromBinaryCloudEvent(io.vertx.amqp.AmqpMessage message, io.smallrye.reactive.messaging.amqp.AmqpMessage<T> parent) {
DefaultCloudEventMetadataBuilder<T> builder = new DefaultCloudEventMetadataBuilder<>();
// Create a copy as we are going to remove the entries.
JsonObject applicationProperties = message.applicationProperties().copy();
// Required
builder.withSpecVersion(applicationProperties.getString(AMQP_HEADER_FOR_SPEC_VERSION));
builder.withId(applicationProperties.getString(AMQP_HEADER_FOR_ID));
String source = applicationProperties.getString(AMQP_HEADER_FOR_SOURCE);
if (source == null) {
throw new IllegalArgumentException("The Kafka record must contain the " + AMQP_HEADER_FOR_SOURCE + " header");
}
builder.withSource(URI.create(source));
builder.withType(applicationProperties.getString(AMQP_HEADER_FOR_TYPE));
// Optional
// Rules 3.1.1 - Set datacontenttype to the record's content type header
String ct = message.contentType();
if (ct != null) {
builder.withDataContentType(ct);
}
String schema = applicationProperties.getString(AMQP_HEADER_FOR_SCHEMA);
if (schema != null) {
builder.withDataSchema(URI.create(schema));
}
String subject = applicationProperties.getString(AMQP_HEADER_FOR_SUBJECT);
if (subject != null) {
builder.withSubject(subject);
}
String time = applicationProperties.getString(AMQP_HEADER_FOR_TIME);
if (time != null) {
ZonedDateTime parse = ZonedDateTime.parse(time, RFC3339_DATE_FORMAT);
builder.withTimestamp(parse);
}
applicationProperties.remove(AMQP_HEADER_FOR_SPEC_VERSION);
applicationProperties.remove(AMQP_HEADER_FOR_ID);
applicationProperties.remove(AMQP_HEADER_FOR_SOURCE);
applicationProperties.remove(AMQP_HEADER_FOR_TYPE);
applicationProperties.remove(AMQP_HEADER_FOR_SCHEMA);
applicationProperties.remove(AMQP_HEADER_FOR_SUBJECT);
applicationProperties.remove(AMQP_HEADER_FOR_TIME);
applicationProperties.forEach(entry -> {
if (entry.getKey().startsWith(CE_HEADER_PREFIX)) {
String key = entry.getKey().substring(CE_HEADER_PREFIX.length());
builder.withExtension(key, entry.getValue());
}
});
// Data
builder.withData(parent.getPayload());
BaseCloudEventMetadata<T> cloudEventMetadata = builder.build();
return new DefaultIncomingCloudEventMetadata<>(cloudEventMetadata);
}
use of io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class IncomingCloudEventMetadataTest method testOptionalAttribute.
@Test
public void testOptionalAttribute() {
ZonedDateTime time = ZonedDateTime.now();
IncomingCloudEventMetadata<String> event = new DefaultIncomingCloudEventMetadata<>(builder.withId("id").withSource(URI.create("test://cloud.event")).withType("type").withDataSchema(URI.create("http://schema.org")).withSubject("subject").withTimestamp(time).withDataContentType("application/json").build());
assertThat(event.getDataSchema()).hasValue(URI.create("http://schema.org"));
assertThat(event.getSubject()).hasValue("subject");
assertThat(event.getTimeStamp()).hasValue(time);
assertThat(event.getDataContentType()).hasValue("application/json");
assertThat(event.getExtension(IncomingCloudEventMetadata.CE_ATTRIBUTE_DATA_SCHEMA)).hasValue(URI.create("http://schema.org"));
assertThat(event.getExtension(IncomingCloudEventMetadata.CE_ATTRIBUTE_SUBJECT)).hasValue("subject");
assertThat(event.getExtension(IncomingCloudEventMetadata.CE_ATTRIBUTE_TIME)).hasValue(time);
assertThat(event.getExtension(IncomingCloudEventMetadata.CE_ATTRIBUTE_DATA_CONTENT_TYPE)).hasValue("application/json");
event = new DefaultIncomingCloudEventMetadata<>(new DefaultCloudEventMetadataBuilder<String>().withId("id").withSource(URI.create("test://cloud.event")).withType("type").build());
assertThat(event.getDataSchema()).isEmpty();
assertThat(event.getSubject()).isEmpty();
assertThat(event.getTimeStamp()).isEmpty();
assertThat(event.getDataContentType()).isEmpty();
}
Aggregations