Skip to content

Commit 5037a69

Browse files
Add method BytesCloudEventData#wrap (#291)
* Add the method `BytesCloudEventData#wrap` to be consistent with #289 Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Removed all usages of constructor Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent ceb5a2e commit 5037a69

File tree

10 files changed

+37
-28
lines changed

10 files changed

+37
-28
lines changed

amqp/src/main/java/io/cloudevents/amqp/impl/ProtonAmqpBinaryMessageReader.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@
1717

1818
package io.cloudevents.amqp.impl;
1919

20-
import java.util.Objects;
21-
import java.util.function.BiConsumer;
22-
23-
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
24-
2520
import io.cloudevents.SpecVersion;
2621
import io.cloudevents.core.data.BytesCloudEventData;
2722
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl;
23+
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
24+
25+
import java.util.Objects;
26+
import java.util.function.BiConsumer;
2827

2928
/**
3029
* An AMQP 1.0 message reader that can be read as a <em>CloudEvent</em>.
3130
* <p>
32-
*
31+
*
3332
* This reader reads sections of an AMQP message to construct a CloudEvent representation by doing the following:
3433
* <ul>
3534
* <li> If the content-type property is set for an AMQP message, the value of the property
3635
* is represented as a cloud event datacontenttype attribute.
3736
* <li> If the (mandatory) application-properties of the AMQP message contains attributes and/or extentions,
3837
* this reader will represent each property/extension as a cloud event attribute.
3938
* </ul>
40-
*
39+
*
4140
*/
4241
public final class ProtonAmqpBinaryMessageReader extends BaseGenericBinaryMessageReaderImpl<String, Object> {
4342

@@ -53,12 +52,12 @@ public final class ProtonAmqpBinaryMessageReader extends BaseGenericBinaryMessag
5352
* The applicationProperties MUST not be {@code null}.
5453
* @param contentType The content-type property of the AMQP message or {@code null} if the message content type is unknown.
5554
* @param payload The message payload or {@code null} if the message does not contain any payload.
56-
*
55+
*
5756
* @throws NullPointerException if the applicationPropereties is {@code null}.
5857
*/
59-
public ProtonAmqpBinaryMessageReader(final SpecVersion version, final ApplicationProperties applicationProperties,
60-
final String contentType, final byte[] payload) {
61-
super(version, payload != null && payload.length > 0 ? new BytesCloudEventData(payload) : null);
58+
public ProtonAmqpBinaryMessageReader(final SpecVersion version, final ApplicationProperties applicationProperties,
59+
final String contentType, final byte[] payload) {
60+
super(version, payload != null && payload.length > 0 ? BytesCloudEventData.wrap(payload) : null);
6261
this.contentType = contentType;
6362
this.applicationProperties = Objects.requireNonNull(applicationProperties);
6463
}
@@ -70,7 +69,7 @@ protected boolean isContentTypeHeader(final String key) {
7069

7170
/**
7271
* Tests whether the given attribute key is prefixed with <em>cloudEvents:</em>
73-
*
72+
*
7473
* @param key The key to test for the presence of the prefix.
7574
* @return True if the specified key starts with the prefix or
7675
* false otherwise.
@@ -83,9 +82,9 @@ protected boolean isCloudEventsHeader(final String key) {
8382

8483
/**
8584
* Gets the cloud event attribute key without the preceding prefix.
86-
*
85+
*
8786
* @param key The key containing the AMQP specific prefix.
88-
*
87+
*
8988
* @return The key without the prefix.
9089
*/
9190
@Override
@@ -119,7 +118,7 @@ protected void forEachHeader(final BiConsumer<String, Object> fn) {
119118
* Gets the cloud event representation of the value.
120119
* <p>
121120
* This method simply returns the string representation of the type of value passed as argument.
122-
*
121+
*
123122
* @param value The value of a CloudEvent attribute or extension.
124123
*
125124
* @return The string representation of the specified value.

core/src/main/java/io/cloudevents/core/data/BytesCloudEventData.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class BytesCloudEventData implements CloudEventData {
99

1010
private final byte[] value;
1111

12+
/**
13+
* @deprecated use {@link BytesCloudEventData#wrap(byte[])}
14+
*/
1215
public BytesCloudEventData(byte[] value) {
1316
Objects.requireNonNull(value);
1417
this.value = value;
@@ -38,4 +41,12 @@ public String toString() {
3841
"value=" + Arrays.toString(value) +
3942
'}';
4043
}
44+
45+
/**
46+
* @param value byte array to wrap
47+
* @return byte array wrapped in a {@link BytesCloudEventData}, which implements {@link CloudEventData}.
48+
*/
49+
public static BytesCloudEventData wrap(byte[] value) {
50+
return new BytesCloudEventData(value);
51+
}
4152
}

core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public BaseCloudEventBuilder(CloudEvent event) {
6868
// to encode data
6969

7070
public SELF withData(byte[] data) {
71-
this.data = new BytesCloudEventData(data);
71+
this.data = BytesCloudEventData.wrap(data);
7272
return this.self;
7373
}
7474

core/src/test/java/io/cloudevents/core/mock/CSVFormat.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package io.cloudevents.core.mock;
1919

2020
import io.cloudevents.CloudEvent;
21-
import io.cloudevents.CloudEventData;
2221
import io.cloudevents.SpecVersion;
2322
import io.cloudevents.core.builder.CloudEventBuilder;
2423
import io.cloudevents.core.data.BytesCloudEventData;
@@ -60,7 +59,7 @@ public byte[] serialize(CloudEvent event) {
6059
}
6160

6261
@Override
63-
public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper<? extends CloudEventData> mapper) {
62+
public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper mapper) {
6463
String[] splitted = new String(bytes, StandardCharsets.UTF_8).split(Pattern.quote(","));
6564
SpecVersion sv = SpecVersion.parse(splitted[0]);
6665

@@ -91,7 +90,7 @@ public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper<? extends Cloud
9190
builder.withTime(time);
9291
}
9392
if (data != null) {
94-
builder.withData(mapper.map(new BytesCloudEventData(data)));
93+
builder.withData(mapper.map(BytesCloudEventData.wrap(data)));
9594
}
9695
return builder.build();
9796
}

core/src/test/java/io/cloudevents/core/mock/MockBinaryMessageWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public MockBinaryMessageWriter(SpecVersion version, Map<String, Object> attribut
4646
}
4747

4848
public MockBinaryMessageWriter(SpecVersion version, Map<String, Object> attributes, byte[] data, Map<String, Object> extensions) {
49-
this(version, attributes, new BytesCloudEventData(data), extensions);
49+
this(version, attributes, BytesCloudEventData.wrap(data), extensions);
5050
}
5151

5252
public MockBinaryMessageWriter() {

formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
9090
boolean isBase64 = "base64".equals(getOptionalStringNode(this.node, this.p, "datacontentencoding"));
9191
if (node.has("data")) {
9292
if (isBase64) {
93-
data = new BytesCloudEventData(node.remove("data").binaryValue());
93+
data = BytesCloudEventData.wrap(node.remove("data").binaryValue());
9494
} else {
9595
if (JsonFormat.dataIsJsonContentType(contentType)) {
9696
// This solution is quite bad, but i see no alternatives now.
@@ -99,7 +99,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
9999
} else {
100100
JsonNode dataNode = node.remove("data");
101101
assertNodeType(dataNode, JsonNodeType.STRING, "data", "Because content type is not a json, only a string is accepted as data");
102-
data = new BytesCloudEventData(dataNode.asText().getBytes());
102+
data = BytesCloudEventData.wrap(dataNode.asText().getBytes());
103103
}
104104
}
105105
}
@@ -108,7 +108,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
108108
throw MismatchedInputException.from(p, CloudEvent.class, "CloudEvent cannot have both 'data' and 'data_base64' fields");
109109
}
110110
if (node.has("data_base64")) {
111-
data = new BytesCloudEventData(node.remove("data_base64").binaryValue());
111+
data = BytesCloudEventData.wrap(node.remove("data_base64").binaryValue());
112112
} else if (node.has("data")) {
113113
if (JsonFormat.dataIsJsonContentType(contentType)) {
114114
// This solution is quite bad, but i see no alternatives now.
@@ -117,7 +117,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
117117
} else {
118118
JsonNode dataNode = node.remove("data");
119119
assertNodeType(dataNode, JsonNodeType.STRING, "data", "Because content type is not a json, only a string is accepted as data");
120-
data = new BytesCloudEventData(dataNode.asText().getBytes());
120+
data = BytesCloudEventData.wrap(dataNode.asText().getBytes());
121121
}
122122
}
123123
}

http/basic/src/main/java/io/cloudevents/http/impl/HttpMessageReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public HttpMessageReader(SpecVersion version, Consumer<BiConsumer<String, String
3737
}
3838

3939
public HttpMessageReader(SpecVersion version, Consumer<BiConsumer<String, String>> forEachHeader, byte[] body) {
40-
this(version, forEachHeader, body != null && body.length > 0 ? new BytesCloudEventData(body) : null);
40+
this(version, forEachHeader, body != null && body.length > 0 ? BytesCloudEventData.wrap(body) : null);
4141
}
4242

4343
@Override

http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/BinaryRestfulWSMessageReaderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class BinaryRestfulWSMessageReaderImpl extends BaseGenericBinaryMes
3333
private final MultivaluedMap<String, String> headers;
3434

3535
public BinaryRestfulWSMessageReaderImpl(SpecVersion version, MultivaluedMap<String, String> headers, byte[] body) {
36-
super(version, body != null && body.length > 0 ? new BytesCloudEventData(body) : null);
36+
super(version, body != null && body.length > 0 ? BytesCloudEventData.wrap(body) : null);
3737

3838
Objects.requireNonNull(headers);
3939
this.headers = headers;

http/vertx/src/main/java/io/cloudevents/http/vertx/impl/BinaryVertxMessageReaderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class BinaryVertxMessageReaderImpl extends BaseGenericBinaryMessageReader
3232
private final MultiMap headers;
3333

3434
public BinaryVertxMessageReaderImpl(SpecVersion version, MultiMap headers, Buffer body) {
35-
super(version, body != null && body.length() > 0 ? new BytesCloudEventData(body.getBytes()) : null);
35+
super(version, body != null && body.length() > 0 ? BytesCloudEventData.wrap(body.getBytes()) : null);
3636

3737
Objects.requireNonNull(headers);
3838
this.headers = headers;

kafka/src/main/java/io/cloudevents/kafka/impl/KafkaBinaryMessageReaderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class KafkaBinaryMessageReaderImpl extends BaseGenericBinaryMessageReader
3131
private final Headers headers;
3232

3333
public KafkaBinaryMessageReaderImpl(SpecVersion version, Headers headers, byte[] payload) {
34-
super(version, payload != null && payload.length > 0 ? new BytesCloudEventData(payload) : null);
34+
super(version, payload != null && payload.length > 0 ? BytesCloudEventData.wrap(payload) : null);
3535

3636
Objects.requireNonNull(headers);
3737
this.headers = headers;

0 commit comments

Comments
 (0)