Search in sources :

Example 1 with DefaultCloudEventMetadataBuilder

use of io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder in project smallrye-reactive-messaging by smallrye.

the class KafkaCloudEventHelper method createFromStructuredCloudEvent.

public static <T, K> IncomingKafkaCloudEventMetadata<K, T> createFromStructuredCloudEvent(ConsumerRecord<K, T> record) {
    DefaultCloudEventMetadataBuilder<T> builder = new DefaultCloudEventMetadataBuilder<>();
    JsonObject content;
    if (record.value() instanceof JsonObject) {
        content = (JsonObject) record.value();
    } else if (record.value() instanceof String) {
        content = new JsonObject((String) record.value());
    } else if (record.value() instanceof byte[]) {
        byte[] bytes = (byte[]) record.value();
        Buffer buffer = Buffer.buffer(bytes);
        content = buffer.toJsonObject();
    } else {
        throw new IllegalArgumentException("Invalid value type. Structured Cloud Event can only be created from String, JsonObject and byte[], found: " + record.value().getClass());
    }
    // 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));
    }
    // Extensions
    if (record.key() != null) {
        builder.withExtension(CE_KAFKA_KEY, record.key());
    }
    builder.withExtension(CE_KAFKA_TOPIC, record.topic());
    // Data
    Object data = content.getValue("data");
    // noinspection unchecked
    builder.withData((T) data);
    BaseCloudEventMetadata<T> cloudEventMetadata = builder.build();
    cloudEventMetadata.validate();
    return new DefaultIncomingKafkaCloudEventMetadata<>(new DefaultIncomingCloudEventMetadata<>(cloudEventMetadata));
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with DefaultCloudEventMetadataBuilder

use of io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder 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));
}
Also used : DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) java.util(java.util) Record(io.smallrye.reactive.messaging.kafka.Record) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ZonedDateTime(java.time.ZonedDateTime) Headers(org.apache.kafka.common.header.Headers) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) CE_KAFKA_TOPIC(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_TOPIC) IncomingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata) RuntimeKafkaSinkConfiguration(io.smallrye.reactive.messaging.kafka.impl.RuntimeKafkaSinkConfiguration) CloudEventMetadata(io.smallrye.reactive.messaging.ce.CloudEventMetadata) DefaultIncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata) BaseCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.BaseCloudEventMetadata) CE_KAFKA_KEY(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_KEY) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) Instant(java.time.Instant) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Message(org.eclipse.microprofile.reactive.messaging.Message) OutgoingCloudEventMetadata(io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata) OutgoingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata) Buffer(io.vertx.mutiny.core.buffer.Buffer) Header(org.apache.kafka.common.header.Header) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) DateTimeFormatter(java.time.format.DateTimeFormatter) ZonedDateTime(java.time.ZonedDateTime) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder)

Example 3 with DefaultCloudEventMetadataBuilder

use of io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder 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);
}
Also used : DefaultIncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Section(org.apache.qpid.proton.amqp.messaging.Section)

Example 4 with DefaultCloudEventMetadataBuilder

use of io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder 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);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) DefaultIncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

DefaultCloudEventMetadataBuilder (io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder)4 JsonObject (io.vertx.core.json.JsonObject)4 DefaultIncomingCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata)3 Buffer (io.vertx.mutiny.core.buffer.Buffer)2 ZonedDateTime (java.time.ZonedDateTime)2 CloudEventMetadata (io.smallrye.reactive.messaging.ce.CloudEventMetadata)1 OutgoingCloudEventMetadata (io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata)1 BaseCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.BaseCloudEventMetadata)1 IncomingKafkaCloudEventMetadata (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata)1 CE_KAFKA_KEY (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_KEY)1 CE_KAFKA_TOPIC (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_TOPIC)1 Record (io.smallrye.reactive.messaging.kafka.Record)1 IncomingKafkaRecordMetadata (io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata)1 OutgoingKafkaRecordMetadata (io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata)1 RuntimeKafkaSinkConfiguration (io.smallrye.reactive.messaging.kafka.impl.RuntimeKafkaSinkConfiguration)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Instant (java.time.Instant)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)1