From 1beaf00865a3e8a23dddd77f793155805b1eda01 Mon Sep 17 00:00:00 2001 From: vbhat6 Date: Wed, 7 Feb 2024 10:12:33 +0530 Subject: [PATCH] feat: Customizing Cloudevents validation Signed-off-by: vbhat6 --- .../provider/CloudEventValidatorProvider.java | 28 ++++--------------- .../core/v03/CloudEventBuilder.java | 9 +++++- .../core/v1/CloudEventBuilder.java | 2 +- .../core/v1/CloudEventBuilderTest.java | 12 ++++++++ 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java b/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java index c96245256..2f6afd616 100644 --- a/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java +++ b/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java @@ -28,18 +28,15 @@ */ public class CloudEventValidatorProvider { - private static CloudEventValidatorProvider cloudEventValidatorProvider; + private static final CloudEventValidatorProvider cloudEventValidatorProvider = new CloudEventValidatorProvider(); - private ServiceLoader loader; + private final ServiceLoader loader; private CloudEventValidatorProvider(){ loader = ServiceLoader.load(CloudEventValidator.class); } - public static synchronized CloudEventValidatorProvider getInstance(){ - if(cloudEventValidatorProvider == null){ - cloudEventValidatorProvider = new CloudEventValidatorProvider(); - } + public static CloudEventValidatorProvider getInstance() { return cloudEventValidatorProvider; } @@ -48,23 +45,8 @@ public static synchronized CloudEventValidatorProvider getInstance(){ * @param cloudEvent */ public void validate(CloudEvent cloudEvent){ - try{ - // - Iterator validatorIterator = loader.iterator(); - while (validatorIterator.hasNext()){ - CloudEventValidator validator = validatorIterator.next(); - validator.validate(cloudEvent); - - } - } catch (ServiceConfigurationError serviceError) { - - serviceError.printStackTrace(); + for (final CloudEventValidator validator : loader) { + validator.validate(cloudEvent); } - } - - - - - } diff --git a/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java index 7f043ce7e..152498cc3 100644 --- a/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java @@ -16,9 +16,12 @@ */ package io.cloudevents.core.v03; +import io.cloudevents.CloudEvent; import io.cloudevents.SpecVersion; import io.cloudevents.core.CloudEventUtils; import io.cloudevents.core.impl.BaseCloudEventBuilder; +import io.cloudevents.core.provider.CloudEventValidatorProvider; +import io.cloudevents.core.v1.CloudEventV1; import io.cloudevents.rw.CloudEventContextReader; import io.cloudevents.rw.CloudEventContextWriter; import io.cloudevents.rw.CloudEventRWException; @@ -122,7 +125,11 @@ public CloudEventV03 build() { throw createMissingAttributeException("type"); } - return new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions); + CloudEventV03 cloudEvent = new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions); + final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance(); + validator.validate(cloudEvent); + + return cloudEvent; } @Override diff --git a/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java index bf2ac7a85..060fa65df 100644 --- a/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java @@ -123,7 +123,7 @@ public CloudEvent build() { CloudEvent cloudEvent = new CloudEventV1(id, source, type, datacontenttype, dataschema, subject, time, this.data, this.extensions); - CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance(); + final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance(); validator.validate(cloudEvent); return cloudEvent; 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"); + } + }