diff --git a/api/src/main/java/io/cloudevents/CloudEvent.java b/api/src/main/java/io/cloudevents/CloudEvent.java
index 815ab999..f1a3b4b3 100644
--- a/api/src/main/java/io/cloudevents/CloudEvent.java
+++ b/api/src/main/java/io/cloudevents/CloudEvent.java
@@ -21,24 +21,11 @@ import java.util.Map;
import java.util.Optional;
/**
- * An abstract event envelope, representing the 0.1 version of the CNCF CloudEvent spec.
+ * An abstract event envelope, representing the 0.2 version of the CNCF CloudEvent spec.
*
*/
public interface CloudEvent {
- // required
- String EVENT_TYPE_KEY = "ce-type";
- String SPECVERSION_KEY = "ce-specversion";
- String SOURCE_KEY = "ce-source";
- String EVENT_ID_KEY = "ce-id";
-
- // none-required
- String CONTENT_TYPE_KEY = "contenttype";
- String DATA__KEY = "data";
- String EVENT_TIME_KEY = "ce-time";
- String SCHEMA_URL_KEY = "ce-schemaurl";
- String HEADER_PREFIX = "ce-x-";
-
/**
* Type of occurrence which has happened. Often this property is used for routing, observability, policy enforcement, etc.
*/
diff --git a/api/src/main/java/io/cloudevents/CloudEventBuilder.java b/api/src/main/java/io/cloudevents/CloudEventBuilder.java
index 5b78422e..cd736f6f 100644
--- a/api/src/main/java/io/cloudevents/CloudEventBuilder.java
+++ b/api/src/main/java/io/cloudevents/CloudEventBuilder.java
@@ -28,8 +28,7 @@ import java.util.Map;
*/
public class CloudEventBuilder {
- public static String SPEC_VERSION = "0.2";
-
+ private String specversion;
private String contentType;
private String type;
private URI source;
@@ -38,6 +37,14 @@ public class CloudEventBuilder {
private URI schemaURL;
private T data;
+ /**
+ * The version of the CloudEvents specification which the event uses.
+ */
+ public CloudEventBuilder specVersion(final String specVersion) {
+ this.specversion = specVersion;
+ return this;
+ }
+
/**
* Type of occurrence which has happened. Often this property is used for routing, observability, policy enforcement, etc.
*/
@@ -101,10 +108,15 @@ public class CloudEventBuilder {
*/
public CloudEvent build() {
+ // forcing latest (default) version
+ if (specversion == null) {
+ specversion = SpecVersion.DEFAULT.toString();
+ }
+
if (type == null || source == null || id == null) {
throw new IllegalArgumentException("please provide all required fields");
}
- return new DefaultCloudEventImpl(type, SPEC_VERSION, source, id, time, schemaURL, contentType, data);
+ return new DefaultCloudEventImpl(type, specversion, source, id, time, schemaURL, contentType, data);
}
}
\ No newline at end of file
diff --git a/api/src/main/java/io/cloudevents/SpecVersion.java b/api/src/main/java/io/cloudevents/SpecVersion.java
new file mode 100644
index 00000000..1b2236ae
--- /dev/null
+++ b/api/src/main/java/io/cloudevents/SpecVersion.java
@@ -0,0 +1,68 @@
+/**
+ * Copyright 2018 The CloudEvents Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.cloudevents;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public enum SpecVersion {
+
+ V_01("0.1"),
+ V_02("0.2"),
+ DEFAULT(V_02.toString());
+
+ private final String version;
+
+ SpecVersion(final String version) {
+ this.version = version;
+ }
+
+ @Override
+ public String toString() {
+ return version;
+ }
+
+ public String version() {
+ return version;
+ }
+
+ public static SpecVersion fromVersion(final String version) {
+ if (version == null)
+ return null;
+
+ final SpecVersion specVersion= VERSION_TO_SPEC.get(version);
+
+ if (specVersion == null)
+ throw new IllegalArgumentException();
+
+ return specVersion;
+ }
+
+ private static final Map VERSION_TO_SPEC =
+ new HashMap<>();
+
+ static
+ {
+ SpecVersion[] instances = SpecVersion.class.getEnumConstants();
+
+ for (int i = 0; i < instances.length; i++)
+ {
+ VERSION_TO_SPEC.put(instances[i].toString(), instances[i]);
+ }
+ }
+
+
+}
diff --git a/api/src/main/java/io/cloudevents/http/HttpTransportAttributes.java b/api/src/main/java/io/cloudevents/http/HttpTransportAttributes.java
new file mode 100644
index 00000000..b2cded89
--- /dev/null
+++ b/api/src/main/java/io/cloudevents/http/HttpTransportAttributes.java
@@ -0,0 +1,46 @@
+/**
+ * Copyright 2018 The CloudEvents Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.cloudevents.http;
+
+import io.cloudevents.SpecVersion;
+
+import static io.cloudevents.SpecVersion.V_01;
+
+public interface HttpTransportAttributes {
+
+ // required attrs
+ String typeKey();
+ String specVersionKey();
+ String sourceKey();
+ String idKey();
+
+ // none-required attrs
+ String timeKey();
+ String schemaUrlKey();
+
+ static HttpTransportAttributes getHttpAttributesForSpec(final SpecVersion specVersion) {
+
+ switch (specVersion) {
+
+ case V_01: return new V01HttpTransportMappers();
+ case V_02:
+ case DEFAULT: return new V02HttpTransportMappers();
+ }
+
+ // you should not be here!
+ throw new IllegalArgumentException("Could not find proper version");
+ }
+}
diff --git a/api/src/main/java/io/cloudevents/http/V01HttpTransportMappers.java b/api/src/main/java/io/cloudevents/http/V01HttpTransportMappers.java
new file mode 100644
index 00000000..d32b3a73
--- /dev/null
+++ b/api/src/main/java/io/cloudevents/http/V01HttpTransportMappers.java
@@ -0,0 +1,51 @@
+/**
+ * Copyright 2018 The CloudEvents Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.cloudevents.http;
+
+public class V01HttpTransportMappers implements HttpTransportAttributes {
+
+ public static final String SPEC_VERSION_KEY = "ce-cloudEventsVersion";
+
+ @Override
+ public String typeKey() {
+ return "ce-eventType";
+ }
+
+ @Override
+ public String specVersionKey() {
+ return SPEC_VERSION_KEY;
+ }
+
+ @Override
+ public String sourceKey() {
+ return "ce-source";
+ }
+
+ @Override
+ public String idKey() {
+ return "ce-eventID";
+ }
+
+ @Override
+ public String timeKey() {
+ return "ce-eventTime";
+ }
+
+ @Override
+ public String schemaUrlKey() {
+ return "ce-schemaURL";
+ }
+}
diff --git a/api/src/main/java/io/cloudevents/http/V02HttpTransportMappers.java b/api/src/main/java/io/cloudevents/http/V02HttpTransportMappers.java
new file mode 100644
index 00000000..ae97c20e
--- /dev/null
+++ b/api/src/main/java/io/cloudevents/http/V02HttpTransportMappers.java
@@ -0,0 +1,47 @@
+/**
+ * Copyright 2018 The CloudEvents Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.cloudevents.http;
+
+public class V02HttpTransportMappers extends V01HttpTransportMappers {
+
+ public static final String SPEC_VERSION_KEY = "ce-specversion";
+
+ @Override
+ public String typeKey() {
+ return "ce-type";
+ }
+
+ @Override
+ public String specVersionKey() {
+ return SPEC_VERSION_KEY;
+ }
+
+ @Override
+ public String idKey() {
+ return "ce-id";
+ }
+
+ @Override
+ public String timeKey() {
+ return "ce-time";
+ }
+
+ @Override
+ public String schemaUrlKey() {
+ return "ce-schemaurl";
+ }
+
+}
diff --git a/api/src/main/java/io/cloudevents/impl/DefaultCloudEventImpl.java b/api/src/main/java/io/cloudevents/impl/DefaultCloudEventImpl.java
index 298aaf81..dc7369ba 100644
--- a/api/src/main/java/io/cloudevents/impl/DefaultCloudEventImpl.java
+++ b/api/src/main/java/io/cloudevents/impl/DefaultCloudEventImpl.java
@@ -15,8 +15,11 @@
*/
package io.cloudevents.impl;
+import com.fasterxml.jackson.annotation.JsonAlias;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.cloudevents.CloudEvent;
+import io.cloudevents.SpecVersion;
import java.io.Serializable;
import java.net.URI;
@@ -29,11 +32,12 @@ import java.util.Optional;
*
* @param generic type of the underlying data field.
*/
+@JsonIgnoreProperties(value = { "eventTypeVersion", "extensions" }) // was removed from 0.1
public class DefaultCloudEventImpl implements CloudEvent, Serializable {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 2L;
- private String specversion = "0.2";
+ private String specversion;
private String type = null;
private URI source = null;
private String id = null;
@@ -99,10 +103,12 @@ public class DefaultCloudEventImpl implements CloudEvent, Serializable {
// protected setters, used for (JSON) deserialization
+ @JsonAlias({"specversion", "cloudEventsVersion"})
void setSpecversion(String specversion) {
this.specversion = specversion;
}
+ @JsonAlias({"type", "eventType"})
void setType(String type) {
this.type = type;
}
@@ -111,11 +117,13 @@ public class DefaultCloudEventImpl implements CloudEvent, Serializable {
this.source = source;
}
+ @JsonAlias({"id", "eventID"})
void setId(String id) {
this.id = id;
}
@JsonDeserialize(using = ZonedDateTimeDeserializer.class)
+ @JsonAlias({"time", "eventTime"})
void setTime(ZonedDateTime time) {
this.time = time;
}
diff --git a/api/src/test/java/io/cloudevents/CloudEventBuilderTest.java b/api/src/test/java/io/cloudevents/CloudEventBuilderTest.java
index 2d8ac89b..ab6dbde8 100644
--- a/api/src/test/java/io/cloudevents/CloudEventBuilderTest.java
+++ b/api/src/test/java/io/cloudevents/CloudEventBuilderTest.java
@@ -65,7 +65,7 @@ public class CloudEventBuilderTest {
assertThat(simpleKeyValueEvent.getSchemaURL().get()).isEqualTo(schemaUri);
assertThat(simpleKeyValueEvent.getType()).isEqualTo(type);
assertThat(simpleKeyValueEvent.getSource()).isEqualTo(src);
- assertThat(simpleKeyValueEvent.getSepcVersion()).isEqualTo("0.2");
+ assertThat(simpleKeyValueEvent.getSepcVersion()).isEqualTo(SpecVersion.DEFAULT.toString());
}
@Test
@@ -88,7 +88,7 @@ public class CloudEventBuilderTest {
assertThat(simpleKeyValueEvent.getId()).isEqualTo(id);
assertThat(simpleKeyValueEvent.getType()).isEqualTo(type);
assertThat(simpleKeyValueEvent.getSource()).isEqualTo(src);
- assertThat(simpleKeyValueEvent.getSepcVersion()).isEqualTo("0.2");
+ assertThat(simpleKeyValueEvent.getSepcVersion()).isEqualTo(SpecVersion.DEFAULT.toString());
}
@Test
@@ -109,6 +109,27 @@ public class CloudEventBuilderTest {
assertThat(simpleKeyValueEvent.getSource()).isEqualTo(src);
}
+ @Test
+ public void test01BuilderWithoutDataAndUrn() {
+
+ // given
+ final String id = UUID.randomUUID().toString();
+ final URI src = URI.create("urn:event:from:myapi/resourse/123");
+ final String type = "some.Cloud.Event.Type";
+
+ // when
+ final CloudEvent