Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/main/java/io/cloudevents/CloudEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ public interface CloudEvent extends CloudEventAttributes, CloudEventExtensions {
* The event data
*/
@Nullable
byte[] getData();
CloudEventData getData();
}
13 changes: 13 additions & 0 deletions api/src/main/java/io/cloudevents/CloudEventData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.cloudevents;

/**
* Interface that defines a wrapper for CloudEvent data.
*/
public interface CloudEventData {

/**
* @return this CloudEventData, represented as bytes. Note: this operation may be expensive, depending on the internal representation of data
*/
byte[] toBytes();

}
4 changes: 3 additions & 1 deletion api/src/main/java/io/cloudevents/rw/CloudEventWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package io.cloudevents.rw;

import io.cloudevents.CloudEventData;

/**
* Interface to write the content (CloudEvents attributes, extensions and payload) from a
* {@link io.cloudevents.rw.CloudEventReader} to a new representation.
Expand All @@ -30,7 +32,7 @@ public interface CloudEventWriter<R> extends CloudEventAttributesWriter, CloudEv
*
* @return an eventual return value
*/
R end(byte[] value) throws CloudEventRWException;
R end(CloudEventData data) throws CloudEventRWException;

/**
* End the visit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.cloudevents.core.builder;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.Extension;
import io.cloudevents.SpecVersion;
import io.cloudevents.rw.CloudEventWriter;
Expand Down Expand Up @@ -116,6 +117,33 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
*/
CloudEventBuilder withData(String dataContentType, URI dataSchema, byte[] data);

/**
* Set the {@code data} of the event
*
* @param data data of the event
* @return self
*/
CloudEventBuilder withData(CloudEventData data);

/**
* Set the {@code datacontenttype} and {@code data} of the event
*
* @param dataContentType datacontenttype of the event
* @param data data of the event
* @return self
*/
CloudEventBuilder withData(String dataContentType, CloudEventData data);

/**
* Set the {@code datacontenttype}, {@code dataschema} and {@code data} of the event
*
* @param dataContentType datacontenttype of the event
* @param dataSchema dataschema of the event
* @param data data of the event
* @return self
*/
CloudEventBuilder withData(String dataContentType, URI dataSchema, CloudEventData data);

/**
* Set an extension with provided key and string value
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.cloudevents.core.data;

import io.cloudevents.CloudEventData;

import java.util.Arrays;
import java.util.Objects;

public class BytesCloudEventData implements CloudEventData {

private final byte[] value;

public BytesCloudEventData(byte[] value) {
Objects.requireNonNull(value);
this.value = value;
}

@Override
public byte[] toBytes() {
return this.value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BytesCloudEventData that = (BytesCloudEventData) o;
return Arrays.equals(value, that.value);
}

@Override
public int hashCode() {
return Arrays.hashCode(value);
}

@Override
public String toString() {
return "BytesCloudEventData{" +
"value=" + Arrays.toString(value) +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.cloudevents.core.impl;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.rw.*;

import java.util.HashMap;
Expand All @@ -26,16 +27,16 @@

public abstract class BaseCloudEvent implements CloudEvent, CloudEventReader {

private final byte[] data;
private final CloudEventData data;
protected final Map<String, Object> extensions;

protected BaseCloudEvent(byte[] data, Map<String, Object> extensions) {
protected BaseCloudEvent(CloudEventData data, Map<String, Object> extensions) {
this.data = data;
this.extensions = extensions != null ? extensions : new HashMap<>();
}

@Override
public byte[] getData() {
public CloudEventData getData() {
return this.data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package io.cloudevents.core.impl;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.Extension;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.data.BytesCloudEventData;
import io.cloudevents.rw.CloudEventRWException;

import javax.annotation.Nonnull;
Expand All @@ -32,7 +34,7 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
// This is a little trick for enabling fluency
private final SELF self;

protected byte[] data;
protected CloudEventData data;
protected Map<String, Object> extensions;

@SuppressWarnings("unchecked")
Expand All @@ -59,7 +61,7 @@ public BaseCloudEventBuilder(CloudEvent event) {
// to encode data

public SELF withData(byte[] data) {
this.data = data;
this.data = new BytesCloudEventData(data);
return this.self;
}

Expand All @@ -76,6 +78,24 @@ public SELF withData(String dataContentType, URI dataSchema, byte[] data) {
return this.self;
}

public SELF withData(CloudEventData data) {
this.data = data;
return this.self;
}

public SELF withData(String dataContentType, CloudEventData data) {
withDataContentType(dataContentType);
withData(data);
return this.self;
}

public SELF withData(String dataContentType, URI dataSchema, CloudEventData data) {
withDataContentType(dataContentType);
withDataSchema(dataSchema);
withData(data);
return this.self;
}

public SELF withExtension(@Nonnull String key, String value) {
this.extensions.put(key, value);
return self;
Expand Down Expand Up @@ -114,7 +134,7 @@ public SELF withExtension(@Nonnull Extension extension) {
}

@Override
public CloudEvent end(byte[] value) throws CloudEventRWException {
public CloudEvent end(CloudEventData value) throws CloudEventRWException {
this.data = value;
return build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.cloudevents.core.message.impl;

import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion;
import io.cloudevents.rw.*;

Expand All @@ -34,9 +35,9 @@
public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBinaryMessageReader {

private final SpecVersion version;
private final byte[] body;
private final CloudEventData body;

protected BaseGenericBinaryMessageReaderImpl(SpecVersion version, byte[] body) {
protected BaseGenericBinaryMessageReaderImpl(SpecVersion version, CloudEventData body) {
Objects.requireNonNull(version);
this.version = version;
this.body = body;
Expand Down Expand Up @@ -66,7 +67,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
});

// Set the payload
if (this.body != null && this.body.length != 0) {
if (this.body != null) {
return visitor.end(this.body);
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/io/cloudevents/core/v03/CloudEventV03.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.cloudevents.core.v03;

import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.impl.BaseCloudEvent;
import io.cloudevents.lang.Nullable;
Expand All @@ -24,7 +25,6 @@

import java.net.URI;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

Expand All @@ -47,7 +47,7 @@ public final class CloudEventV03 extends BaseCloudEvent {
public CloudEventV03(String id, URI source, String type,
OffsetDateTime time, URI schemaurl,
String datacontenttype, String subject,
byte[] data, Map<String, Object> extensions) {
CloudEventData data, Map<String, Object> extensions) {
super(data, extensions);

this.id = id;
Expand Down Expand Up @@ -172,13 +172,13 @@ public boolean equals(Object o) {
Objects.equals(schemaurl, that.schemaurl) &&
Objects.equals(getSubject(), that.getSubject()) &&
Objects.equals(getTime(), that.getTime()) &&
Arrays.equals(getData(), that.getData()) &&
Objects.equals(getData(), that.getData()) &&
Objects.equals(this.extensions, that.extensions);
}

@Override
public int hashCode() {
return Objects.hash(getId(), getSource(), getType(), datacontenttype, schemaurl, getSubject(), getTime(), Arrays.hashCode(getData()), this.extensions);
return Objects.hash(getId(), getSource(), getType(), datacontenttype, schemaurl, getSubject(), getTime(), getData(), this.extensions);
}

@Override
Expand All @@ -191,7 +191,7 @@ public String toString() {
", schemaurl=" + schemaurl +
", subject='" + subject + '\'' +
", time=" + time +
", data=" + Arrays.toString(getData()) +
", data=" + getData() +
", extensions" + this.extensions +
'}';
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/io/cloudevents/core/v1/CloudEventV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
package io.cloudevents.core.v1;

import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.impl.BaseCloudEvent;
import io.cloudevents.rw.CloudEventAttributesWriter;
import io.cloudevents.rw.CloudEventRWException;

import java.net.URI;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

Expand All @@ -46,7 +46,7 @@ public final class CloudEventV1 extends BaseCloudEvent {
public CloudEventV1(String id, URI source,
String type, String datacontenttype,
URI dataschema, String subject, OffsetDateTime time,
byte[] data, Map<String, Object> extensions) {
CloudEventData data, Map<String, Object> extensions) {
super(data, extensions);

this.id = id;
Expand Down Expand Up @@ -167,13 +167,13 @@ public boolean equals(Object o) {
Objects.equals(dataschema, that.getDataSchema()) &&
Objects.equals(getSubject(), that.getSubject()) &&
Objects.equals(getTime(), that.getTime()) &&
Arrays.equals(getData(), that.getData()) &&
Objects.equals(getData(), that.getData()) &&
Objects.equals(this.extensions, that.extensions);
}

@Override
public int hashCode() {
return Objects.hash(getId(), getSource(), getType(), datacontenttype, dataschema, getSubject(), getTime(), Arrays.hashCode(getData()), this.extensions);
return Objects.hash(getId(), getSource(), getType(), datacontenttype, dataschema, getSubject(), getTime(), getData(), this.extensions);
}

@Override
Expand All @@ -186,7 +186,7 @@ public String toString() {
", dataschema=" + dataschema +
", subject='" + subject + '\'' +
", time=" + time +
", data=" + Arrays.toString(getData()) +
", data=" + getData() +
", extensions=" + this.extensions +
'}';
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/io/cloudevents/core/mock/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public byte[] serialize(CloudEvent event) {
? Time.writeTime(event.getTime())
: "null",
event.getData() != null
? new String(Base64.getEncoder().encode(event.getData()), StandardCharsets.UTF_8)
? new String(Base64.getEncoder().encode(event.getData().toBytes()), StandardCharsets.UTF_8)
: "null"
).getBytes();
}
Expand Down
Loading