Skip to content

Commit

Permalink
Use UTF-8 when using getBytes
Browse files Browse the repository at this point in the history
Closes #488

Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
Doug Davis committed Dec 8, 2022
1 parent 9132a13 commit 1700171
Show file tree
Hide file tree
Showing 27 changed files with 69 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.cloudevents.core.data;

import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -8,23 +10,23 @@ class PojoCloudEventDataTest {

@Test
void testWrapAndMemoization() {
PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes());
PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes(StandardCharsets.UTF_8));

assertThat(data.getValue())
.isEqualTo(10);

byte[] firstConversion = data.toBytes();

assertThat(firstConversion)
.isEqualTo("10".getBytes());
.isEqualTo("10".getBytes(StandardCharsets.UTF_8));

assertThat(data.toBytes())
.isSameAs(firstConversion);
}

@Test
void testAlreadySerializedValue() {
byte[] serialized = "10".getBytes();
byte[] serialized = "10".getBytes(StandardCharsets.UTF_8);
PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, v -> serialized);

assertThat(data.getValue())
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/io/cloudevents/core/mock/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public byte[] serialize(CloudEvent event) {
event.getData() != null
? new String(Base64.getEncoder().encode(event.getData().toBytes()), StandardCharsets.UTF_8)
: "null"
).getBytes();
).getBytes(StandardCharsets.UTF_8);
}

@Override
Expand All @@ -70,7 +70,7 @@ public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper mapper) {
URI dataschema = splitted[5].equals("null") ? null : URI.create(splitted[5]);
String subject = splitted[6].equals("null") ? null : splitted[6];
OffsetDateTime time = splitted[7].equals("null") ? null : Time.parseTime(splitted[7]);
byte[] data = splitted[8].equals("null") ? null : Base64.getDecoder().decode(splitted[8].getBytes());
byte[] data = splitted[8].equals("null") ? null : Base64.getDecoder().decode(splitted[8].getBytes(StandardCharsets.UTF_8));

CloudEventBuilder builder = CloudEventBuilder.fromSpecVersion(sv)
.withId(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.cloudevents.core.mock;

import io.cloudevents.CloudEventData;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class MyCloudEventData implements CloudEventData {
Expand All @@ -14,7 +14,7 @@ public MyCloudEventData(int value) {

@Override
public byte[] toBytes() {
return Integer.toString(value).getBytes();
return Integer.toString(value).getBytes(StandardCharsets.UTF_8);
}

public int getValue() {
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/io/cloudevents/core/test/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.math.BigDecimal;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Objects;
import java.util.stream.Stream;
Expand All @@ -39,9 +40,9 @@ public class Data {
public static final String SUBJECT = "sub";
public static final OffsetDateTime TIME = Time.parseTime("2018-04-26T14:48:09+02:00");

public static byte[] DATA_JSON_SERIALIZED = "{}".getBytes();
public static byte[] DATA_XML_SERIALIZED = "<stuff></stuff>".getBytes();
public static byte[] DATA_TEXT_SERIALIZED = "Hello World Lorena!".getBytes();
public static byte[] DATA_JSON_SERIALIZED = "{}".getBytes(StandardCharsets.UTF_8);
public static byte[] DATA_XML_SERIALIZED = "<stuff></stuff>".getBytes(StandardCharsets.UTF_8);
public static byte[] DATA_TEXT_SERIALIZED = "Hello World Lorena!".getBytes(StandardCharsets.UTF_8);
public static byte[] BINARY_VALUE = { (byte) 0xE0, (byte) 0xFF, (byte) 0x00, (byte) 0x44, (byte) 0xAA }; // Base64: 4P8ARKo=

public static final CloudEvent V1_MIN = CloudEventBuilder.v1()
Expand Down
2 changes: 1 addition & 1 deletion docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final CloudEvent event = CloudEventBuilder.v1()
.withId("000")
.withType("example.demo")
.withSource(URI.create("http://example.com"))
.withData("text/plain","Hello world!".getBytes())
.withData("text/plain","Hello world!".getBytes("UTF-8"))
.build();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.PrintWriter;
import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
* A example vertx-based AMQP client that interacts with a remote AMQP server to send and receive CloudEvent messages.
Expand Down Expand Up @@ -71,7 +72,7 @@ private static void sendMessage() {
.withSource(URI.create("http://127.0.0.1/amqp-client"))
.withType("com.example.sampletype1")
.withTime(Time.parseTime("2020-11-06T21:47:12.037467+00:00"))
.withData(payload.toString().getBytes())
.withData(payload.toString().getBytes(StandardCharsets.UTF_8))
.build();

final Message message = ProtonAmqpMessageFactory.createWriter().writeBinary(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.PrintWriter;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -81,7 +82,7 @@ private static void onConnectRequest(final ProtonConnection con) {
.withType("com.example.sampletype1")
.withSource(URI.create("http://127.0.0.1/amqp-server"))
.withTime(OffsetDateTime.now())
.withData("{\"temp\": 5}".getBytes())
.withData("{\"temp\": 5}".getBytes(StandardCharsets.UTF_8))
.build();

final Message message = writer.writeBinary(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.kafka.common.serialization.StringSerializer;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.UUID;

Expand Down Expand Up @@ -53,7 +54,7 @@ public static void main(String[] args) {
// Create the event starting from the template
CloudEvent event = eventTemplate.newBuilder()
.withId(id)
.withData("text/plain", data.getBytes())
.withData("text/plain", data.getBytes(StandardCharsets.UTF_8))
.build();

// Send the record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CloudEventsJaxrsService {
public CloudEvent retrieveEvent(){
System.out.println("Received request for an event");
return CloudEventBuilder.v1()
.withData("{\"message\":\"Welcome to this Cloudevents + Microprofile example\"}".getBytes())
.withData("{\"message\":\"Welcome to this Cloudevents + Microprofile example\"}".getBytes(StandardCharsets.UTF_8))
.withDataContentType("application/json")
.withId("hello")
.withType("example.http")
Expand All @@ -45,7 +45,7 @@ public Response postEvent(CloudEvent event){
@Consumes(MediaType.APPLICATION_JSON)
public CloudEvent echo(CloudEvent event){
return CloudEventBuilder.v1()
.withData("application/json", ("{\"echo\": \"" + new String(event.getData().toBytes(),StandardCharsets.UTF_8) + "\"}").getBytes())
.withData("application/json", ("{\"echo\": \"" + new String(event.getData().toBytes(),StandardCharsets.UTF_8) + "\"}").getBytes(StandardCharsets.UTF_8))
.withId("echo")
.withType("echo.http")
.withSource(URI.create("http://localhost"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.cloudevents.examples.spring;

import java.net.URI;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -76,7 +77,7 @@ void structuredRequestResponseCloudEventToString() {
.bodyValue(CloudEventBuilder.v1() //
.withId("12345") //
.withType("io.spring.event") //
.withSource(URI.create("https://spring.io/events")).withData("{\"value\":\"Dave\"}".getBytes()) //
.withSource(URI.create("https://spring.io/events")).withData("{\"value\":\"Dave\"}".getBytes(StandardCharsets.UTF_8)) //
.build()) //
.exchange() //
.expectStatus().isOk() //
Expand All @@ -102,7 +103,7 @@ void structuredRequestResponseCloudEventToCloudEvent() {
.withId("12345") //
.withType("io.spring.event") //
.withSource(URI.create("https://spring.io/events")) //
.withData("{\"value\":\"Dave\"}".getBytes()) //
.withData("{\"value\":\"Dave\"}".getBytes(StandardCharsets.UTF_8)) //
.build()) //
.exchange() //
.expectStatus().isOk() //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -38,7 +39,7 @@ void setUp() {
.withId("12345") //
.withSource(URI.create("https://spring.io/events")) //
.withType("io.spring.event") //
.withData("{\"value\":\"Dave\"}".getBytes()) //
.withData("{\"value\":\"Dave\"}".getBytes(StandardCharsets.UTF_8)) //
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.vertx.ext.web.client.WebClient;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SampleHTTPClient {
Expand Down Expand Up @@ -41,7 +42,7 @@ public static void main(String[] args) {
// Create the event starting from the template
final CloudEvent event = eventTemplate.newBuilder()
.withId(UUID.randomUUID().toString())
.withData("text/plain", data.getBytes())
.withData("text/plain", data.getBytes(StandardCharsets.UTF_8))
.build();

Future<HttpResponse<Buffer>> responseFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.cloudevents.core.data.BytesCloudEventData;
import io.cloudevents.rw.*;

import java.nio.charset.StandardCharsets;
import java.io.IOException;

/**
Expand Down Expand Up @@ -122,7 +123,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
} else {
JsonNode dataNode = node.remove("data");
assertNodeType(dataNode, JsonNodeType.STRING, "data", "Because content type is not a json, only a string is accepted as data");
data = BytesCloudEventData.wrap(dataNode.asText().getBytes());
data = BytesCloudEventData.wrap(dataNode.asText().getBytes(StandardCharsets.UTF_8));
}
}
}
Expand All @@ -140,7 +141,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
} else {
JsonNode dataNode = node.remove("data");
assertNodeType(dataNode, JsonNodeType.STRING, "data", "Because content type is not a json, only a string is accepted as data");
data = BytesCloudEventData.wrap(dataNode.asText().getBytes());
data = BytesCloudEventData.wrap(dataNode.asText().getBytes(StandardCharsets.UTF_8));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.cloudevents.CloudEventData;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
Expand All @@ -40,7 +41,7 @@ public JsonCloudEventData(JsonNode node) {

@Override
public byte[] toBytes() {
return node.toString().getBytes();
return node.toString().getBytes(StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private static byte[] loadFile(String input) {
return String.join(
"",
Files.readAllLines(Paths.get(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(input)).toURI()), StandardCharsets.UTF_8)
).getBytes();
).getBytes(StandardCharsets.UTF_8);
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -28,7 +29,7 @@ public class PojoCloudEventDataMapperTest {
public void testWithBytes(PojoCloudEventDataMapper<MyPojo> mapper) {

CloudEvent event = CloudEventBuilder.v1(Data.V1_MIN)
.withData("application/json", myPojoSerialized.getBytes())
.withData("application/json", myPojoSerialized.getBytes(StandardCharsets.UTF_8))
.build();

PojoCloudEventData<MyPojo> mappedData = CloudEventUtils.mapData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import static io.cloudevents.core.test.Data.*;
Expand All @@ -59,7 +60,7 @@ void testRequestWithStructured(CloudEvent event, Vertx vertx, VertxTestContext t
testContext.verify(() -> {
assertThat(httpServerRequest.getHeader("content-type"))
.isEqualTo(expectedContentType);
assertThat(buf.getBytes())
assertThat(buf.getBytes(StandardCharsets.UTF_8))
.isEqualTo(expectedBuffer);
});
checkpoint.flag();
Expand Down Expand Up @@ -100,8 +101,8 @@ void testRequestWithBinary(CloudEvent event, MultiMap headers, Buffer body, Vert
.isEqualTo(e.getValue());
});
if (body != null) {
assertThat(buf.getBytes())
.isEqualTo(body.getBytes());
assertThat(buf.getBytes(StandardCharsets.UTF_8))
.isEqualTo(body.getBytes(StandardCharsets.UTF_8));
}
});
checkpoint.flag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import static io.cloudevents.core.test.Data.*;
Expand Down Expand Up @@ -70,7 +71,7 @@ void testReplyWithStructured(CloudEvent event, Vertx vertx, VertxTestContext tes
.isEqualTo(200);
assertThat(res.getHeader("content-type"))
.isEqualTo(CSVFormat.INSTANCE.serializedContentType());
assertThat(res.body().getBytes())
assertThat(res.body().getBytes(StandardCharsets.UTF_8))
.isEqualTo(CSVFormat.INSTANCE.serialize(event));

checkpoint.flag();
Expand Down Expand Up @@ -108,8 +109,8 @@ void testReplyWithBinary(CloudEvent event, MultiMap headers, Buffer body, Vertx
assertThat(res.getHeader(e.getKey())).isEqualTo(e.getValue());
});
if (body != null) {
assertThat(res.body().getBytes())
.isEqualTo(body.getBytes());
assertThat(res.body().getBytes(StandardCharsets.UTF_8))
.isEqualTo(body.getBytes(StandardCharsets.UTF_8));
}

checkpoint.flag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import io.cloudevents.core.message.MessageWriter;
import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.rw.CloudEventWriter;

import java.nio.charset.StandardCharsets;

import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.header.internals.RecordHeader;

Expand All @@ -40,7 +43,7 @@ public BaseKafkaMessageWriterImpl<R> withContextAttribute(String name, String va
if (headerName == null) {
headerName = KafkaHeaders.CE_PREFIX + name;
}
headers.add(new RecordHeader(headerName, value.getBytes()));
headers.add(new RecordHeader(headerName, value.getBytes(StandardCharsets.UTF_8)));
return this;
}

Expand All @@ -52,7 +55,7 @@ public R end(CloudEventData value) throws CloudEventRWException {

@Override
public R setEvent(EventFormat format, byte[] value) throws CloudEventRWException {
this.headers.add(new RecordHeader(KafkaHeaders.CONTENT_TYPE, format.serializedContentType().getBytes()));
this.headers.add(new RecordHeader(KafkaHeaders.CONTENT_TYPE, format.serializedContentType().getBytes(StandardCharsets.UTF_8)));
this.value = value;
return this.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.kafka.common.header.Headers;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void deserializerWithMapper() {
testDeserialize(
deserializer,
CloudEventBuilder.v1(Data.V1_MIN)
.withData("application/json", "10".getBytes())
.withData("application/json", "10".getBytes(StandardCharsets.UTF_8))
.build(),
CloudEventBuilder.v1(Data.V1_MIN)
.withData("application/json", new MyCloudEventData(10))
Expand Down
Loading

0 comments on commit 1700171

Please sign in to comment.