diff --git a/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java b/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java new file mode 100644 index 000000000..2f6afd616 --- /dev/null +++ b/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java @@ -0,0 +1,52 @@ +/* + * Copyright 2018-Present 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.core.provider;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.core.validator.CloudEventValidator;
+
+import java.util.Iterator;
+import java.util.ServiceConfigurationError;
+import java.util.ServiceLoader;
+
+/**
+ * CloudEventValidatorProvider is a singleton class which loads and access CE Validator service providers on behalf of service clients.
+ */
+public class CloudEventValidatorProvider {
+
+ private static final CloudEventValidatorProvider cloudEventValidatorProvider = new CloudEventValidatorProvider();
+
+ private final ServiceLoader
+ * 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.core.validator;
+
+import io.cloudevents.CloudEvent;
+
+/**
+ * @author Vinay Bhat
+ * Interface which defines validation for CloudEvents attributes and extensions.
+ */
+public interface CloudEventValidator {
+
+ /**
+ * Validate the attributes of a CloudEvent.
+ *
+ * @param cloudEvent the CloudEvent to validate
+ */
+ void validate(CloudEvent cloudEvent);
+}
diff --git a/core/src/test/java/io/cloudevents/core/test/CloudEventCustomValidator.java b/core/src/test/java/io/cloudevents/core/test/CloudEventCustomValidator.java
new file mode 100644
index 000000000..55fe0a024
--- /dev/null
+++ b/core/src/test/java/io/cloudevents/core/test/CloudEventCustomValidator.java
@@ -0,0 +1,17 @@
+package io.cloudevents.core.test;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.core.validator.CloudEventValidator;
+
+public class CloudEventCustomValidator implements CloudEventValidator {
+
+ @Override
+ public void validate(CloudEvent cloudEvent) {
+ String namespace = null;
+ if ((namespace = (String) cloudEvent.getExtension("namespace")) != null &&
+ !namespace.equals("sales")){
+ throw new IllegalStateException("Expecting sales in namespace extension");
+ }
+ }
+
+}
diff --git a/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java b/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java
index a0c6cc6ef..1f7c9fcc9 100644
--- a/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java
+++ b/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java
@@ -142,4 +142,16 @@ void testMissingType() {
).hasMessageContaining("Attribute 'type' cannot be null");
}
+ @Test
+ void testValidatorProvider(){
+ assertThatCode(() -> CloudEventBuilder
+ .v1()
+ .withId("000")
+ .withSource(URI.create("http://localhost"))
+ .withType(TYPE)
+ .withExtension("namespace", "order")
+ .build()
+ ).hasMessageContaining("Expecting sales in namespace extension");
+ }
+
}
diff --git a/core/src/test/resources/META-INF/services/io.cloudevents.core.validator.CloudEventValidator b/core/src/test/resources/META-INF/services/io.cloudevents.core.validator.CloudEventValidator
new file mode 100644
index 000000000..c265df28c
--- /dev/null
+++ b/core/src/test/resources/META-INF/services/io.cloudevents.core.validator.CloudEventValidator
@@ -0,0 +1 @@
+io.cloudevents.core.test.CloudEventCustomValidator