Renamed methods of writes to coerce with the builder apis (#237)
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
1f58088f3c
commit
2982f07f55
|
@ -17,6 +17,7 @@
|
|||
|
||||
package io.cloudevents.rw;
|
||||
|
||||
import io.cloudevents.lang.Nullable;
|
||||
import io.cloudevents.types.Time;
|
||||
|
||||
import java.net.URI;
|
||||
|
@ -35,7 +36,7 @@ public interface CloudEventAttributesWriter {
|
|||
* @param value
|
||||
* @throws CloudEventRWException
|
||||
*/
|
||||
void setAttribute(String name, String value) throws CloudEventRWException;
|
||||
CloudEventAttributesWriter withAttribute(String name, @Nullable String value) throws CloudEventRWException;
|
||||
|
||||
/**
|
||||
* Set attribute with type {@link URI}.
|
||||
|
@ -44,8 +45,8 @@ public interface CloudEventAttributesWriter {
|
|||
* @param value
|
||||
* @throws CloudEventRWException
|
||||
*/
|
||||
default void setAttribute(String name, URI value) throws CloudEventRWException {
|
||||
setAttribute(name, value.toString());
|
||||
default CloudEventAttributesWriter withAttribute(String name, @Nullable URI value) throws CloudEventRWException {
|
||||
return withAttribute(name, value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,8 +56,8 @@ public interface CloudEventAttributesWriter {
|
|||
* @param value
|
||||
* @throws CloudEventRWException
|
||||
*/
|
||||
default void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
setAttribute(name, Time.writeTime(value));
|
||||
default CloudEventAttributesWriter withAttribute(String name, @Nullable OffsetDateTime value) throws CloudEventRWException {
|
||||
return withAttribute(name, value == null ? null : Time.writeTime(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package io.cloudevents.rw;
|
||||
|
||||
import io.cloudevents.lang.Nullable;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
|
@ -31,7 +33,7 @@ public interface CloudEventExtensionsWriter {
|
|||
* @param value
|
||||
* @throws CloudEventRWException
|
||||
*/
|
||||
void setExtension(String name, String value) throws CloudEventRWException;
|
||||
CloudEventExtensionsWriter withExtension(String name, @Nullable String value) throws CloudEventRWException;
|
||||
|
||||
/**
|
||||
* Set attribute with type {@link URI}.
|
||||
|
@ -40,8 +42,8 @@ public interface CloudEventExtensionsWriter {
|
|||
* @param value
|
||||
* @throws CloudEventRWException
|
||||
*/
|
||||
default void setExtension(String name, Number value) throws CloudEventRWException {
|
||||
setExtension(name, value.toString());
|
||||
default CloudEventExtensionsWriter withExtension(String name, @Nullable Number value) throws CloudEventRWException {
|
||||
return withExtension(name, value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,8 +53,8 @@ public interface CloudEventExtensionsWriter {
|
|||
* @param value
|
||||
* @throws CloudEventRWException
|
||||
*/
|
||||
default void setExtension(String name, Boolean value) throws CloudEventRWException {
|
||||
setExtension(name, value.toString());
|
||||
default CloudEventExtensionsWriter withExtension(String name, @Nullable Boolean value) throws CloudEventRWException {
|
||||
return withExtension(name, value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -123,6 +123,7 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
|
|||
* @param value value of the extension attribute
|
||||
* @return self
|
||||
*/
|
||||
@Override
|
||||
CloudEventBuilder withExtension(@Nonnull String key, String value);
|
||||
|
||||
/**
|
||||
|
@ -132,6 +133,7 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
|
|||
* @param value value of the extension attribute
|
||||
* @return self
|
||||
*/
|
||||
@Override
|
||||
CloudEventBuilder withExtension(@Nonnull String key, Number value);
|
||||
|
||||
/**
|
||||
|
@ -141,7 +143,8 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
|
|||
* @param value value of the extension attribute
|
||||
* @return self
|
||||
*/
|
||||
CloudEventBuilder withExtension(@Nonnull String key, boolean value);
|
||||
@Override
|
||||
CloudEventBuilder withExtension(@Nonnull String key, Boolean value);
|
||||
|
||||
/**
|
||||
* Add to the builder all the extension key/values of the provided extension
|
||||
|
|
|
@ -65,11 +65,11 @@ public abstract class BaseCloudEvent implements CloudEvent, CloudEventReader {
|
|||
// TODO to be improved
|
||||
for (Map.Entry<String, Object> entry : this.extensions.entrySet()) {
|
||||
if (entry.getValue() instanceof String) {
|
||||
visitor.setExtension(entry.getKey(), (String) entry.getValue());
|
||||
visitor.withExtension(entry.getKey(), (String) entry.getValue());
|
||||
} else if (entry.getValue() instanceof Number) {
|
||||
visitor.setExtension(entry.getKey(), (Number) entry.getValue());
|
||||
visitor.withExtension(entry.getKey(), (Number) entry.getValue());
|
||||
} else if (entry.getValue() instanceof Boolean) {
|
||||
visitor.setExtension(entry.getKey(), (Boolean) entry.getValue());
|
||||
visitor.withExtension(entry.getKey(), (Boolean) entry.getValue());
|
||||
} else {
|
||||
// This should never happen because we build that map only through our builders
|
||||
throw new IllegalStateException("Illegal value inside extensions map: " + entry);
|
||||
|
|
|
@ -86,7 +86,7 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
|
|||
return self;
|
||||
}
|
||||
|
||||
public SELF withExtension(@Nonnull String key, boolean value) {
|
||||
public SELF withExtension(@Nonnull String key, Boolean value) {
|
||||
this.extensions.put(key, value);
|
||||
return self;
|
||||
}
|
||||
|
@ -113,21 +113,6 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
|
|||
return self;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
this.withExtension(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, Number value) throws CloudEventRWException {
|
||||
this.withExtension(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, Boolean value) throws CloudEventRWException {
|
||||
this.withExtension(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloudEvent end(byte[] value) throws CloudEventRWException {
|
||||
this.data = value;
|
||||
|
|
|
@ -43,20 +43,20 @@ public class CloudEventReaderAdapter implements CloudEventReader {
|
|||
|
||||
@Override
|
||||
public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException {
|
||||
writer.setAttribute("id", event.getId());
|
||||
writer.setAttribute("source", event.getSource());
|
||||
writer.setAttribute("type", event.getType());
|
||||
writer.withAttribute("id", event.getId());
|
||||
writer.withAttribute("source", event.getSource());
|
||||
writer.withAttribute("type", event.getType());
|
||||
if (event.getDataContentType() != null) {
|
||||
writer.setAttribute("datacontenttype", event.getDataContentType());
|
||||
writer.withAttribute("datacontenttype", event.getDataContentType());
|
||||
}
|
||||
if (event.getDataSchema() != null) {
|
||||
writer.setAttribute("dataschema", event.getDataSchema());
|
||||
writer.withAttribute("dataschema", event.getDataSchema());
|
||||
}
|
||||
if (event.getSubject() != null) {
|
||||
writer.setAttribute("subject", event.getSubject());
|
||||
writer.withAttribute("subject", event.getSubject());
|
||||
}
|
||||
if (event.getTime() != null) {
|
||||
writer.setAttribute("time", event.getTime());
|
||||
writer.withAttribute("time", event.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,11 +65,11 @@ public class CloudEventReaderAdapter implements CloudEventReader {
|
|||
for (String key : event.getExtensionNames()) {
|
||||
Object value = event.getExtension(key);
|
||||
if (value instanceof String) {
|
||||
visitor.setExtension(key, (String) value);
|
||||
visitor.withExtension(key, (String) value);
|
||||
} else if (value instanceof Number) {
|
||||
visitor.setExtension(key, (Number) value);
|
||||
visitor.withExtension(key, (Number) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
visitor.setExtension(key, (Boolean) value);
|
||||
visitor.withExtension(key, (Boolean) value);
|
||||
} else {
|
||||
// This should never happen because we build that map only through our builders
|
||||
throw new IllegalStateException("Illegal value inside extensions map: " + key + " " + value);
|
||||
|
|
|
@ -51,16 +51,16 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
|
|||
// in order to complete the visit in one loop
|
||||
this.forEachHeader((key, value) -> {
|
||||
if (isContentTypeHeader(key)) {
|
||||
visitor.setAttribute("datacontenttype", toCloudEventsValue(value));
|
||||
visitor.withAttribute("datacontenttype", toCloudEventsValue(value));
|
||||
} else if (isCloudEventsHeader(key)) {
|
||||
String name = toCloudEventsKey(key);
|
||||
if (name.equals("specversion")) {
|
||||
return;
|
||||
}
|
||||
if (this.version.getAllAttributes().contains(name)) {
|
||||
visitor.setAttribute(name, toCloudEventsValue(value));
|
||||
visitor.withAttribute(name, toCloudEventsValue(value));
|
||||
} else {
|
||||
visitor.setExtension(name, toCloudEventsValue(value));
|
||||
visitor.withExtension(name, toCloudEventsValue(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -77,14 +77,14 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
|
|||
public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException {
|
||||
this.forEachHeader((key, value) -> {
|
||||
if (isContentTypeHeader(key)) {
|
||||
writer.setAttribute("datacontenttype", toCloudEventsValue(value));
|
||||
writer.withAttribute("datacontenttype", toCloudEventsValue(value));
|
||||
} else if (isCloudEventsHeader(key)) {
|
||||
String name = toCloudEventsKey(key);
|
||||
if (name.equals("specversion")) {
|
||||
return;
|
||||
}
|
||||
if (this.version.getAllAttributes().contains(name)) {
|
||||
writer.setAttribute(name, toCloudEventsValue(value));
|
||||
writer.withAttribute(name, toCloudEventsValue(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -97,7 +97,7 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
|
|||
if (isCloudEventsHeader(key)) {
|
||||
String name = toCloudEventsKey(key);
|
||||
if (!this.version.getAllAttributes().contains(name)) {
|
||||
visitor.setExtension(name, toCloudEventsValue(value));
|
||||
visitor.withExtension(name, toCloudEventsValue(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -135,66 +135,66 @@ public final class CloudEventBuilder extends BaseCloudEventBuilder<CloudEventBui
|
|||
// Message impl
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public CloudEventBuilder withAttribute(String name, String value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "id":
|
||||
withId(value);
|
||||
return;
|
||||
return this;
|
||||
case "source":
|
||||
try {
|
||||
withSource(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "type":
|
||||
withType(value);
|
||||
return;
|
||||
return this;
|
||||
case "datacontenttype":
|
||||
withDataContentType(value);
|
||||
return;
|
||||
return this;
|
||||
case "datacontentencoding":
|
||||
// No-op, this information is not saved in the event because it's useful only for parsing
|
||||
return;
|
||||
return this;
|
||||
case "schemaurl":
|
||||
try {
|
||||
withSchemaUrl(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("schemaurl", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "subject":
|
||||
withSubject(value);
|
||||
return;
|
||||
return this;
|
||||
case "time":
|
||||
try {
|
||||
withTime(Time.parseTime(value));
|
||||
} catch (DateTimeParseException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, URI value) throws CloudEventRWException {
|
||||
public CloudEventBuilder withAttribute(String name, URI value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "source":
|
||||
withSource(value);
|
||||
return;
|
||||
return this;
|
||||
case "schemaurl":
|
||||
withDataSchema(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
public CloudEventBuilder withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
if ("time".equals(name)) {
|
||||
withTime(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
|
||||
}
|
||||
|
|
|
@ -122,38 +122,38 @@ public final class CloudEventV03 extends BaseCloudEvent {
|
|||
|
||||
@Override
|
||||
public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.ID.name().toLowerCase(),
|
||||
this.id
|
||||
);
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.SOURCE.name().toLowerCase(),
|
||||
this.source
|
||||
);
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.TYPE.name().toLowerCase(),
|
||||
this.type
|
||||
);
|
||||
if (this.datacontenttype != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.DATACONTENTTYPE.name().toLowerCase(),
|
||||
this.datacontenttype
|
||||
);
|
||||
}
|
||||
if (this.schemaurl != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.SCHEMAURL.name().toLowerCase(),
|
||||
this.schemaurl
|
||||
);
|
||||
}
|
||||
if (this.subject != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.SUBJECT.name().toLowerCase(),
|
||||
this.subject
|
||||
);
|
||||
}
|
||||
if (this.time != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.TIME.name().toLowerCase(),
|
||||
this.time
|
||||
);
|
||||
|
|
|
@ -35,63 +35,63 @@ class V1ToV03AttributesConverter implements CloudEventAttributesWriter {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public V1ToV03AttributesConverter withAttribute(String name, String value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "id":
|
||||
builder.withId(value);
|
||||
return;
|
||||
return this;
|
||||
case "source":
|
||||
try {
|
||||
builder.withSource(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "type":
|
||||
builder.withType(value);
|
||||
return;
|
||||
return this;
|
||||
case "datacontenttype":
|
||||
builder.withDataContentType(value);
|
||||
return;
|
||||
return this;
|
||||
case "dataschema":
|
||||
try {
|
||||
builder.withSchemaUrl(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "subject":
|
||||
builder.withSubject(value);
|
||||
return;
|
||||
return this;
|
||||
case "time":
|
||||
try {
|
||||
builder.withTime(Time.parseTime(value));
|
||||
} catch (DateTimeParseException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, URI value) throws CloudEventRWException {
|
||||
public V1ToV03AttributesConverter withAttribute(String name, URI value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "source":
|
||||
builder.withSource(value);
|
||||
return;
|
||||
return this;
|
||||
case "dataschema":
|
||||
builder.withSchemaUrl(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
public V1ToV03AttributesConverter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
if ("time".equals(name)) {
|
||||
builder.withTime(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
|
||||
}
|
||||
|
|
|
@ -132,63 +132,63 @@ public final class CloudEventBuilder extends BaseCloudEventBuilder<CloudEventBui
|
|||
// Message impl
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public CloudEventBuilder withAttribute(String name, String value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "id":
|
||||
withId(value);
|
||||
return;
|
||||
return this;
|
||||
case "source":
|
||||
try {
|
||||
withSource(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "type":
|
||||
withType(value);
|
||||
return;
|
||||
return this;
|
||||
case "datacontenttype":
|
||||
withDataContentType(value);
|
||||
return;
|
||||
return this;
|
||||
case "dataschema":
|
||||
try {
|
||||
withDataSchema(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "subject":
|
||||
withSubject(value);
|
||||
return;
|
||||
return this;
|
||||
case "time":
|
||||
try {
|
||||
withTime(Time.parseTime(value));
|
||||
} catch (DateTimeParseException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, URI value) throws CloudEventRWException {
|
||||
public CloudEventBuilder withAttribute(String name, URI value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "source":
|
||||
withSource(value);
|
||||
return;
|
||||
return this;
|
||||
case "dataschema":
|
||||
withDataSchema(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
public CloudEventBuilder withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
if ("time".equals(name)) {
|
||||
withTime(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
|
||||
}
|
||||
|
|
|
@ -117,38 +117,38 @@ public final class CloudEventV1 extends BaseCloudEvent {
|
|||
|
||||
@Override
|
||||
public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.ID.name().toLowerCase(),
|
||||
this.id
|
||||
);
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.SOURCE.name().toLowerCase(),
|
||||
this.source
|
||||
);
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.TYPE.name().toLowerCase(),
|
||||
this.type
|
||||
);
|
||||
if (this.datacontenttype != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.DATACONTENTTYPE.name().toLowerCase(),
|
||||
this.datacontenttype
|
||||
);
|
||||
}
|
||||
if (this.dataschema != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.DATASCHEMA.name().toLowerCase(),
|
||||
this.dataschema
|
||||
);
|
||||
}
|
||||
if (this.subject != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.SUBJECT.name().toLowerCase(),
|
||||
this.subject
|
||||
);
|
||||
}
|
||||
if (this.time != null) {
|
||||
writer.setAttribute(
|
||||
writer.withAttribute(
|
||||
ContextAttributes.TIME.name().toLowerCase(),
|
||||
this.time
|
||||
);
|
||||
|
|
|
@ -35,63 +35,63 @@ class V03ToV1AttributesConverter implements CloudEventAttributesWriter {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public V03ToV1AttributesConverter withAttribute(String name, String value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "id":
|
||||
builder.withId(value);
|
||||
return;
|
||||
return this;
|
||||
case "source":
|
||||
try {
|
||||
builder.withSource(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "type":
|
||||
builder.withType(value);
|
||||
return;
|
||||
return this;
|
||||
case "datacontenttype":
|
||||
builder.withDataContentType(value);
|
||||
return;
|
||||
return this;
|
||||
case "schemaurl":
|
||||
try {
|
||||
builder.withDataSchema(new URI(value));
|
||||
} catch (URISyntaxException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
case "subject":
|
||||
builder.withSubject(value);
|
||||
return;
|
||||
return this;
|
||||
case "time":
|
||||
try {
|
||||
builder.withTime(Time.parseTime(value));
|
||||
} catch (DateTimeParseException e) {
|
||||
throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
|
||||
}
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, URI value) throws CloudEventRWException {
|
||||
public V03ToV1AttributesConverter withAttribute(String name, URI value) throws CloudEventRWException {
|
||||
switch (name) {
|
||||
case "source":
|
||||
builder.withSource(value);
|
||||
return;
|
||||
return this;
|
||||
case "schemaurl":
|
||||
builder.withDataSchema(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
public V03ToV1AttributesConverter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
if ("time".equals(name)) {
|
||||
builder.withTime(value);
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
|
||||
}
|
||||
|
|
|
@ -76,11 +76,11 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
|
|||
public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException, IllegalStateException {
|
||||
for (Map.Entry<String, Object> e : this.attributes.entrySet()) {
|
||||
if (e.getValue() instanceof String) {
|
||||
writer.setAttribute(e.getKey(), (String) e.getValue());
|
||||
writer.withAttribute(e.getKey(), (String) e.getValue());
|
||||
} else if (e.getValue() instanceof OffsetDateTime) {
|
||||
writer.setAttribute(e.getKey(), (OffsetDateTime) e.getValue());
|
||||
writer.withAttribute(e.getKey(), (OffsetDateTime) e.getValue());
|
||||
} else if (e.getValue() instanceof URI) {
|
||||
writer.setAttribute(e.getKey(), (URI) e.getValue());
|
||||
writer.withAttribute(e.getKey(), (URI) e.getValue());
|
||||
} else {
|
||||
// This should never happen because we build that map only through our builders
|
||||
throw new IllegalStateException("Illegal value inside attributes map: " + e);
|
||||
|
@ -92,11 +92,11 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
|
|||
public void readExtensions(CloudEventExtensionsWriter visitor) throws CloudEventRWException, IllegalStateException {
|
||||
for (Map.Entry<String, Object> entry : this.extensions.entrySet()) {
|
||||
if (entry.getValue() instanceof String) {
|
||||
visitor.setExtension(entry.getKey(), (String) entry.getValue());
|
||||
visitor.withExtension(entry.getKey(), (String) entry.getValue());
|
||||
} else if (entry.getValue() instanceof Number) {
|
||||
visitor.setExtension(entry.getKey(), (Number) entry.getValue());
|
||||
visitor.withExtension(entry.getKey(), (Number) entry.getValue());
|
||||
} else if (entry.getValue() instanceof Boolean) {
|
||||
visitor.setExtension(entry.getKey(), (Boolean) entry.getValue());
|
||||
visitor.withExtension(entry.getKey(), (Boolean) entry.getValue());
|
||||
} else {
|
||||
// This should never happen because we build that map only through our builders
|
||||
throw new IllegalStateException("Illegal value inside extensions map: " + entry);
|
||||
|
@ -116,33 +116,39 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public MockBinaryMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
|
||||
this.attributes.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, URI value) throws CloudEventRWException {
|
||||
public MockBinaryMessageWriter withAttribute(String name, URI value) throws CloudEventRWException {
|
||||
this.attributes.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
public MockBinaryMessageWriter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
|
||||
this.attributes.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public MockBinaryMessageWriter withExtension(String name, String value) throws CloudEventRWException {
|
||||
this.extensions.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, Number value) throws CloudEventRWException {
|
||||
public MockBinaryMessageWriter withExtension(String name, Number value) throws CloudEventRWException {
|
||||
this.extensions.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, Boolean value) throws CloudEventRWException {
|
||||
public MockBinaryMessageWriter withExtension(String name, Boolean value) throws CloudEventRWException {
|
||||
this.extensions.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -60,14 +60,14 @@ public class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
|
|||
// Read mandatory attributes
|
||||
for (String attr : specVersion.getMandatoryAttributes()) {
|
||||
if (!"specversion".equals(attr)) {
|
||||
visitor.setAttribute(attr, getStringNode(this.node, this.p, attr));
|
||||
visitor.withAttribute(attr, getStringNode(this.node, this.p, attr));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse datacontenttype if any
|
||||
String contentType = getOptionalStringNode(this.node, this.p, "datacontenttype");
|
||||
if (contentType != null) {
|
||||
visitor.setAttribute("datacontenttype", contentType);
|
||||
visitor.withAttribute("datacontenttype", contentType);
|
||||
}
|
||||
|
||||
// Read optional attributes
|
||||
|
@ -75,7 +75,7 @@ public class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
|
|||
if (!"datacontentencoding".equals(attr)) { // Skip datacontentencoding, we need it later
|
||||
String val = getOptionalStringNode(this.node, this.p, attr);
|
||||
if (val != null) {
|
||||
visitor.setAttribute(attr, val);
|
||||
visitor.withAttribute(attr, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -127,16 +127,16 @@ public class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
|
|||
|
||||
switch (extensionValue.getNodeType()) {
|
||||
case BOOLEAN:
|
||||
visitor.setExtension(extensionName, extensionValue.booleanValue());
|
||||
visitor.withExtension(extensionName, extensionValue.booleanValue());
|
||||
break;
|
||||
case NUMBER:
|
||||
visitor.setExtension(extensionName, extensionValue.numberValue());
|
||||
visitor.withExtension(extensionName, extensionValue.numberValue());
|
||||
break;
|
||||
case STRING:
|
||||
visitor.setExtension(extensionName, extensionValue.textValue());
|
||||
visitor.withExtension(extensionName, extensionValue.textValue());
|
||||
break;
|
||||
default:
|
||||
visitor.setExtension(extensionName, extensionValue.toString());
|
||||
visitor.withExtension(extensionName, extensionValue.toString());
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -55,37 +55,41 @@ public class CloudEventSerializer extends StdSerializer<CloudEvent> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public FieldsSerializer withAttribute(String name, String value) throws CloudEventRWException {
|
||||
try {
|
||||
gen.writeStringField(name, value);
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public FieldsSerializer withExtension(String name, String value) throws CloudEventRWException {
|
||||
try {
|
||||
gen.writeStringField(name, value);
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, Number value) throws CloudEventRWException {
|
||||
public FieldsSerializer withExtension(String name, Number value) throws CloudEventRWException {
|
||||
try {
|
||||
gen.writeFieldName(name);
|
||||
provider.findValueSerializer(value.getClass()).serialize(value, gen, provider);
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, Boolean value) throws CloudEventRWException {
|
||||
public FieldsSerializer withExtension(String name, Boolean value) throws CloudEventRWException {
|
||||
try {
|
||||
gen.writeBooleanField(name, value);
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -57,13 +57,15 @@ public class HttpMessageWriter implements CloudEventWriter<Void>, MessageWriter<
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public HttpMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
|
||||
putHeader.accept(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public HttpMessageWriter withExtension(String name, String value) throws CloudEventRWException {
|
||||
putHeader.accept("ce-" + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -46,13 +46,15 @@ public final class RestfulWSClientMessageWriter implements CloudEventWriter<Void
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public RestfulWSClientMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
|
||||
this.context.getHeaders().add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public RestfulWSClientMessageWriter withExtension(String name, String value) throws CloudEventRWException {
|
||||
this.context.getHeaders().add(CloudEventsHeaders.CE_PREFIX + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -48,13 +48,15 @@ public final class RestfulWSMessageWriter implements CloudEventWriter<Void>, Mes
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public RestfulWSMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
|
||||
this.httpHeaders.add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public RestfulWSMessageWriter withExtension(String name, String value) throws CloudEventRWException {
|
||||
this.httpHeaders.add(CloudEventsHeaders.CE_PREFIX + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -45,13 +45,15 @@ public class VertxHttpServerResponseMessageWriterImpl implements MessageWriter<C
|
|||
// Binary visitor
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public VertxHttpServerResponseMessageWriterImpl withAttribute(String name, String value) throws CloudEventRWException {
|
||||
this.response.putHeader(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public VertxHttpServerResponseMessageWriterImpl withExtension(String name, String value) throws CloudEventRWException {
|
||||
this.response.putHeader("ce-" + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -24,7 +24,6 @@ import io.cloudevents.rw.CloudEventRWException;
|
|||
import io.cloudevents.rw.CloudEventWriter;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.HttpClientRequest;
|
||||
import io.vertx.core.http.HttpHeaders;
|
||||
import io.vertx.ext.web.client.HttpRequest;
|
||||
import io.vertx.ext.web.client.HttpResponse;
|
||||
|
@ -48,13 +47,15 @@ public class VertxWebClientRequestMessageWriterImpl implements MessageWriter<Clo
|
|||
// Binary visitor
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public VertxWebClientRequestMessageWriterImpl withAttribute(String name, String value) throws CloudEventRWException {
|
||||
this.request.headers().add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public VertxWebClientRequestMessageWriterImpl withExtension(String name, String value) throws CloudEventRWException {
|
||||
this.request.headers().add("ce-" + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -34,13 +34,15 @@ abstract class BaseKafkaMessageWriterImpl<R> implements MessageWriter<CloudEvent
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) throws CloudEventRWException {
|
||||
public BaseKafkaMessageWriterImpl<R> withAttribute(String name, String value) throws CloudEventRWException {
|
||||
headers.add(new RecordHeader(KafkaHeaders.ATTRIBUTES_TO_HEADERS.get(name), value.getBytes()));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExtension(String name, String value) throws CloudEventRWException {
|
||||
public BaseKafkaMessageWriterImpl<R> withExtension(String name, String value) throws CloudEventRWException {
|
||||
headers.add(new RecordHeader(KafkaHeaders.CE_PREFIX + name, value.getBytes()));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -44,7 +44,7 @@ public final class KafkaProducerMessageWriterImpl<K>
|
|||
|
||||
@Override
|
||||
public KafkaProducerMessageWriterImpl<K> create(SpecVersion version) {
|
||||
this.setAttribute("specversion", version.toString());
|
||||
this.withAttribute("specversion", version.toString());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public final class KafkaSerializerMessageWriterImpl extends BaseKafkaMessageWrit
|
|||
|
||||
@Override
|
||||
public KafkaSerializerMessageWriterImpl create(SpecVersion version) {
|
||||
this.setAttribute("specversion", version.toString());
|
||||
this.withAttribute("specversion", version.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue