Skip to content

Commit

Permalink
Support cloud events spec v3 #105
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Oct 15, 2019
1 parent c7721e1 commit 7852a13
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,73 @@
*/
package org.apache.camel.component.knative.spi;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

public interface CloudEvent {
/**
* The CloudEvent spec version.
*/
String version();
Attributes attributes();

interface Attributes {
/**
* List of supported attributes.
*/
Collection<Attribute> attributes();

/**
* Find attribute by id.
*/
default Optional<Attribute> attribute(String id) {
return attributes().stream()
.filter(a -> Objects.equals(id, a.id()))
.findFirst();
}

/**
* Mandatory find attribute by id.
*/
default Attribute mandatoryAttribute(String id) {
return attributes().stream()
.filter(a -> Objects.equals(id, a.id()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unable to find attribute with id: " + id));
}

interface Attribute {
/**
* The ID of the attributes, can be used to look it up.
*/
String id();
String source();
String spec();
String type();
String time();

/**
* The name of the http header.
*/
String http();

/**
* The name of the json field.
*/
String json();

static Attribute simple(String id, String http, String json) {
return new Attribute() {
@Override
public String id() {
return id;
}

@Override
public String http() {
return http;
}

@Override
public String json() {
return json;
}
};
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,60 @@
*/
package org.apache.camel.component.knative.spi;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

public enum CloudEvents implements CloudEvent {
V01(new CloudEventV01()),
V02(new CloudEventV02());
//
// V0.1 - https://github.com/cloudevents/spec/blob/v0.1/spec.md
//
V01(new CloudEventImpl(
"0.1",
Arrays.asList(
Attribute.simple("type", "CE-EventType", "eventType"),
Attribute.simple("type.version", "CE-EventTypeVersion", "eventTypeVersion"),
Attribute.simple("version", "CE-CloudEventsVersion", "cloudEventsVersion"),
Attribute.simple("source", "CE-Source", "source"),
Attribute.simple("id", "CE-EventID", "eventID"),
Attribute.simple("time", "CE-EventTime", "eventTime"),
Attribute.simple("schema.url", "CE-SchemaURL", "schemaURL"),
Attribute.simple("content.type", "ContentType", "contentType"),
Attribute.simple("extensions", "CE-Extensions", "extensions")
)
)),
//
// V0.2 - https://github.com/cloudevents/spec/blob/v0.2/spec.md
//
V02(new CloudEventImpl(
"0.2",
Arrays.asList(
Attribute.simple("type", "ce-type", "type"),
Attribute.simple("version", "ce-specversion", "specversion"),
Attribute.simple("source", "ce-source", "source"),
Attribute.simple("id", "ce-id", "id"),
Attribute.simple("time", "ce-time", "time"),
Attribute.simple("schema.url", "ce-schemaurl", "schemaurl"),
Attribute.simple("content.type", "Content-Type", "contenttype")
)
)),
//
// V0.3 - https://github.com/cloudevents/spec/blob/v0.3/spec.md
//
V03(new CloudEventImpl(
"0.3",
Arrays.asList(
Attribute.simple("id", "ce-id", "id"),
Attribute.simple("source", "ce-source", "source"),
Attribute.simple("version", "ce-specversion", "specversion"),
Attribute.simple("type", "ce-type", "type"),
Attribute.simple("data.content.encoding", "ce-datacontentencoding", "datacontentencoding"),
Attribute.simple("data.content.type", "ce-datacontenttype", "datacontenttype"),
Attribute.simple("schema.url", "ce-schemaurl", "schemaurl"),
Attribute.simple("subject", "ce-subject", "subject"),
Attribute.simple("time", "ce-time", "time")
)
));

private final CloudEvent instance;

Expand All @@ -34,7 +83,7 @@ public String version() {
}

@Override
public Attributes attributes() {
public Collection<Attribute> attributes() {
return instance.attributes();
}

Expand All @@ -47,5 +96,25 @@ public static CloudEvent fromSpecVersion(String version) {

throw new IllegalArgumentException("Unable to find an implementation fo CloudEvents spec: " + version);
}

private static class CloudEventImpl implements CloudEvent {
private final String version;
private final Collection<Attribute> attributes;

public CloudEventImpl(String version, Collection<Attribute> attributes) {
this.version = version;
this.attributes = attributes;
}

@Override
public String version() {
return version;
}

@Override
public Collection<Attribute> attributes() {
return attributes;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class Knative {
public static final String KNATIVE_API_VERSION = "knative.apiVersion";
public static final String CONTENT_TYPE = "content.type";
public static final String MIME_STRUCTURED_CONTENT_MODE = "application/cloudevents+json";
public static final String MIME_BATCH_CONTENT_MODE = "application/cloudevents-batch+json";
public static final String CAMEL_ENDPOINT_KIND = "camel.endpoint.kind";

public static final String SERVICE_META_HOST = "service.host";
Expand Down
Loading

0 comments on commit 7852a13

Please sign in to comment.