Allow providing an external validator

Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
This commit is contained in:
Ruben Romero Montes 2019-10-24 16:40:34 +02:00
parent b0a6101b23
commit 1cff239ab4
1 changed files with 35 additions and 19 deletions

View File

@ -42,8 +42,15 @@ import io.cloudevents.fun.EventBuilder;
*/ */
public final class CloudEventBuilder<T> implements public final class CloudEventBuilder<T> implements
EventBuilder<T, AttributesImpl> { EventBuilder<T, AttributesImpl> {
private CloudEventBuilder() {}
private CloudEventBuilder(Validator validator) {
if(validator == null) {
this.validator = getValidator();
} else {
this.validator = validator;
}
}
private static Validator VALIDATOR; private static Validator VALIDATOR;
public static final String SPEC_VERSION = "0.3"; public static final String SPEC_VERSION = "0.3";
@ -65,6 +72,7 @@ public final class CloudEventBuilder<T> implements
private T data; private T data;
private final Set<ExtensionFormat> extensions = new HashSet<>(); private final Set<ExtensionFormat> extensions = new HashSet<>();
private final Validator validator;
private static Validator getValidator() { private static Validator getValidator() {
if(null== VALIDATOR) { if(null== VALIDATOR) {
@ -78,54 +86,62 @@ public final class CloudEventBuilder<T> implements
* @param <T> The 'data' type * @param <T> The 'data' type
*/ */
public static <T> CloudEventBuilder<T> builder() { public static <T> CloudEventBuilder<T> builder() {
return new CloudEventBuilder<T>(); return new CloudEventBuilder<T>(null);
}
public static <T> CloudEventBuilder<T> builder(Validator validator) {
return new CloudEventBuilder<T>(validator);
} }
public static <T> CloudEventBuilder<T> builder( public static <T> CloudEventBuilder<T> builder(
CloudEvent<AttributesImpl, T> base) { CloudEvent<AttributesImpl, T> base) {
return builder(base, null);
}
public static <T> CloudEventBuilder<T> builder(
CloudEvent<AttributesImpl, T> base, Validator validator) {
Objects.requireNonNull(base); Objects.requireNonNull(base);
CloudEventBuilder<T> result = new CloudEventBuilder<>(); CloudEventBuilder<T> result = new CloudEventBuilder<>(validator);
AttributesImpl attributes = base.getAttributes(); AttributesImpl attributes = base.getAttributes();
result result
.withId(attributes.getId()) .withId(attributes.getId())
.withSource(attributes.getSource()) .withSource(attributes.getSource())
.withType(attributes.getType()); .withType(attributes.getType());
attributes.getTime().ifPresent(time -> { attributes.getTime().ifPresent(time -> {
result.withTime(time); result.withTime(time);
}); });
attributes.getSchemaurl().ifPresent((schema) -> { attributes.getSchemaurl().ifPresent((schema) -> {
result.withSchemaurl(schema); result.withSchemaurl(schema);
}); });
attributes.getDatacontenttype().ifPresent(dc -> { attributes.getDatacontenttype().ifPresent(dc -> {
result.withDatacontenttype(dc); result.withDatacontenttype(dc);
}); });
attributes.getDatacontentencoding().ifPresent(dce -> { attributes.getDatacontentencoding().ifPresent(dce -> {
result.withDatacontentencoding(dce); result.withDatacontentencoding(dce);
}); });
attributes.getSubject().ifPresent(subject -> { attributes.getSubject().ifPresent(subject -> {
result.withSubject(subject); result.withSubject(subject);
}); });
Accessor.extensionsOf(base) Accessor.extensionsOf(base)
.forEach(extension -> { .forEach(extension -> {
result.withExtension(extension); result.withExtension(extension);
}); });
base.getData().ifPresent(data -> { base.getData().ifPresent(data -> {
result.withData(data); result.withData(data);
}); });
return result; return result;
} }
/** /**
* Build an event from data and attributes * Build an event from data and attributes
* @param <T> the type of 'data' * @param <T> the type of 'data'
@ -194,9 +210,9 @@ public final class CloudEventBuilder<T> implements
new CloudEventImpl<T>(attributes, data, extensions); new CloudEventImpl<T>(attributes, data, extensions);
Set<ConstraintViolation<Object>> violations = Set<ConstraintViolation<Object>> violations =
getValidator().validate(cloudEvent); validator.validate(cloudEvent);
violations.addAll(getValidator().validate(cloudEvent.getAttributes())); violations.addAll(validator.validate(cloudEvent.getAttributes()));
final String errs = final String errs =
violations.stream() violations.stream()