From baeabf6c40a6f50168c979f2417ddeed0b0f5a8a Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Thu, 8 Dec 2022 16:00:00 +0000 Subject: [PATCH] Use UTF-8 when using getBytes Closes #488 Signed-off-by: Doug Davis --- .../io/cloudevents/core/data/PojoCloudEventDataTest.java | 8 +++++--- .../src/test/java/io/cloudevents/core/mock/CSVFormat.java | 4 ++-- .../java/io/cloudevents/core/mock/MyCloudEventData.java | 4 ++-- core/src/test/java/io/cloudevents/core/test/Data.java | 7 ++++--- docs/core.md | 2 +- .../io/cloudevents/examples/amqp/vertx/AmqpClient.java | 3 ++- .../io/cloudevents/examples/amqp/vertx/AmqpServer.java | 3 ++- .../io/cloudevents/examples/kafka/SampleProducer.java | 3 ++- .../examples/microprofile/CloudEventsJaxrsService.java | 4 ++-- .../cloudevents/examples/spring/DemoApplicationTests.java | 5 +++-- .../io/cloudevents/examples/spring/WebClientTests.java | 3 ++- .../io/cloudevents/examples/vertx/SampleHTTPClient.java | 3 ++- .../io/cloudevents/jackson/CloudEventDeserializer.java | 5 +++-- .../java/io/cloudevents/jackson/JsonCloudEventData.java | 3 ++- .../test/java/io/cloudevents/jackson/JsonFormatTest.java | 2 +- .../cloudevents/jackson/PojoCloudEventDataMapperTest.java | 3 ++- .../kafka/impl/BaseKafkaMessageWriterImpl.java | 7 +++++-- .../io/cloudevents/kafka/CloudEventDeserializerTest.java | 3 ++- .../io/cloudevents/kafka/KafkaMessageFactoryTest.java | 3 ++- .../cloudevents/kafka/KafkaProducerMessageWriterTest.java | 3 ++- kafka/src/test/java/io/cloudevents/kafka/KafkaUtils.java | 4 +++- .../spring/messaging/CloudEventMessageConverter.java | 4 ++-- .../cloudevents/spring/codec/CloudEventDecoderTests.java | 3 ++- .../cloudevents/spring/codec/CloudEventEncoderTests.java | 3 ++- .../spring/messaging/CloudEventMessageConverterTests.java | 7 ++++--- 25 files changed, 61 insertions(+), 38 deletions(-) diff --git a/core/src/test/java/io/cloudevents/core/data/PojoCloudEventDataTest.java b/core/src/test/java/io/cloudevents/core/data/PojoCloudEventDataTest.java index 8270009b4..f59a4bf8a 100644 --- a/core/src/test/java/io/cloudevents/core/data/PojoCloudEventDataTest.java +++ b/core/src/test/java/io/cloudevents/core/data/PojoCloudEventDataTest.java @@ -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; @@ -8,7 +10,7 @@ class PojoCloudEventDataTest { @Test void testWrapAndMemoization() { - PojoCloudEventData data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes()); + PojoCloudEventData data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes(StandardCharsets.UTF_8)); assertThat(data.getValue()) .isEqualTo(10); @@ -16,7 +18,7 @@ void testWrapAndMemoization() { byte[] firstConversion = data.toBytes(); assertThat(firstConversion) - .isEqualTo("10".getBytes()); + .isEqualTo("10".getBytes(StandardCharsets.UTF_8)); assertThat(data.toBytes()) .isSameAs(firstConversion); @@ -24,7 +26,7 @@ void testWrapAndMemoization() { @Test void testAlreadySerializedValue() { - byte[] serialized = "10".getBytes(); + byte[] serialized = "10".getBytes(StandardCharsets.UTF_8); PojoCloudEventData data = PojoCloudEventData.wrap(10, v -> serialized); assertThat(data.getValue()) diff --git a/core/src/test/java/io/cloudevents/core/mock/CSVFormat.java b/core/src/test/java/io/cloudevents/core/mock/CSVFormat.java index adaba4724..4b63d944d 100644 --- a/core/src/test/java/io/cloudevents/core/mock/CSVFormat.java +++ b/core/src/test/java/io/cloudevents/core/mock/CSVFormat.java @@ -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 @@ -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) diff --git a/core/src/test/java/io/cloudevents/core/mock/MyCloudEventData.java b/core/src/test/java/io/cloudevents/core/mock/MyCloudEventData.java index f0520f9a8..5b04de188 100644 --- a/core/src/test/java/io/cloudevents/core/mock/MyCloudEventData.java +++ b/core/src/test/java/io/cloudevents/core/mock/MyCloudEventData.java @@ -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 { @@ -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() { diff --git a/core/src/test/java/io/cloudevents/core/test/Data.java b/core/src/test/java/io/cloudevents/core/test/Data.java index 557e05d12..268726151 100644 --- a/core/src/test/java/io/cloudevents/core/test/Data.java +++ b/core/src/test/java/io/cloudevents/core/test/Data.java @@ -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; @@ -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 = "".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 = "".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() diff --git a/docs/core.md b/docs/core.md index 992c3aca2..17c9c539a 100644 --- a/docs/core.md +++ b/docs/core.md @@ -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(); ``` diff --git a/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpClient.java b/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpClient.java index d35faa36d..5b35b3e60 100644 --- a/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpClient.java +++ b/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpClient.java @@ -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. @@ -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); diff --git a/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpServer.java b/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpServer.java index 0eb230706..eb6d73157 100644 --- a/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpServer.java +++ b/examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpServer.java @@ -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; @@ -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); diff --git a/examples/kafka/src/main/java/io/cloudevents/examples/kafka/SampleProducer.java b/examples/kafka/src/main/java/io/cloudevents/examples/kafka/SampleProducer.java index 86a2ddf9f..415f6ea3c 100644 --- a/examples/kafka/src/main/java/io/cloudevents/examples/kafka/SampleProducer.java +++ b/examples/kafka/src/main/java/io/cloudevents/examples/kafka/SampleProducer.java @@ -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; @@ -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 diff --git a/examples/restful-ws-microprofile-liberty/src/main/java/io/cloudevents/examples/microprofile/CloudEventsJaxrsService.java b/examples/restful-ws-microprofile-liberty/src/main/java/io/cloudevents/examples/microprofile/CloudEventsJaxrsService.java index eef7fe882..8aa9783e0 100644 --- a/examples/restful-ws-microprofile-liberty/src/main/java/io/cloudevents/examples/microprofile/CloudEventsJaxrsService.java +++ b/examples/restful-ws-microprofile-liberty/src/main/java/io/cloudevents/examples/microprofile/CloudEventsJaxrsService.java @@ -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") @@ -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")) diff --git a/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/DemoApplicationTests.java b/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/DemoApplicationTests.java index 2e691671c..b98c7c3c0 100644 --- a/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/DemoApplicationTests.java +++ b/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/DemoApplicationTests.java @@ -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; @@ -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() // @@ -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() // diff --git a/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/WebClientTests.java b/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/WebClientTests.java index 590a4f975..33d997ce1 100644 --- a/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/WebClientTests.java +++ b/examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/WebClientTests.java @@ -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; @@ -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(); } diff --git a/examples/vertx/src/main/java/io/cloudevents/examples/vertx/SampleHTTPClient.java b/examples/vertx/src/main/java/io/cloudevents/examples/vertx/SampleHTTPClient.java index a28718bca..9bba22291 100644 --- a/examples/vertx/src/main/java/io/cloudevents/examples/vertx/SampleHTTPClient.java +++ b/examples/vertx/src/main/java/io/cloudevents/examples/vertx/SampleHTTPClient.java @@ -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 { @@ -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> responseFuture; diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java index 0e848fff8..4d306964a 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java @@ -33,6 +33,7 @@ import io.cloudevents.core.data.BytesCloudEventData; import io.cloudevents.rw.*; +import java.nio.charset.StandardCharsets; import java.io.IOException; /** @@ -122,7 +123,7 @@ public , V> V read(CloudEventWriterFactory 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)); } } } @@ -140,7 +141,7 @@ public , V> V read(CloudEventWriterFactory 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)); } } } diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonCloudEventData.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonCloudEventData.java index 77a34cb7e..6e9c19b72 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonCloudEventData.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonCloudEventData.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.JsonNode; import io.cloudevents.CloudEventData; +import java.nio.charset.StandardCharsets; import java.util.Objects; /** @@ -40,7 +41,7 @@ public JsonCloudEventData(JsonNode node) { @Override public byte[] toBytes() { - return node.toString().getBytes(); + return node.toString().getBytes(StandardCharsets.UTF_8); } /** diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java index c5fbb9ed1..f82d25926 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java @@ -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); } diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/PojoCloudEventDataMapperTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/PojoCloudEventDataMapperTest.java index a7eccbaad..24e2c25f6 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/PojoCloudEventDataMapperTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/PojoCloudEventDataMapperTest.java @@ -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; @@ -28,7 +29,7 @@ public class PojoCloudEventDataMapperTest { public void testWithBytes(PojoCloudEventDataMapper mapper) { CloudEvent event = CloudEventBuilder.v1(Data.V1_MIN) - .withData("application/json", myPojoSerialized.getBytes()) + .withData("application/json", myPojoSerialized.getBytes(StandardCharsets.UTF_8)) .build(); PojoCloudEventData mappedData = CloudEventUtils.mapData( diff --git a/kafka/src/main/java/io/cloudevents/kafka/impl/BaseKafkaMessageWriterImpl.java b/kafka/src/main/java/io/cloudevents/kafka/impl/BaseKafkaMessageWriterImpl.java index 7ba4e192c..34ef64b5a 100644 --- a/kafka/src/main/java/io/cloudevents/kafka/impl/BaseKafkaMessageWriterImpl.java +++ b/kafka/src/main/java/io/cloudevents/kafka/impl/BaseKafkaMessageWriterImpl.java @@ -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; @@ -40,7 +43,7 @@ public BaseKafkaMessageWriterImpl 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; } @@ -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(); } diff --git a/kafka/src/test/java/io/cloudevents/kafka/CloudEventDeserializerTest.java b/kafka/src/test/java/io/cloudevents/kafka/CloudEventDeserializerTest.java index 6ab7606be..d7fc5e6cf 100644 --- a/kafka/src/test/java/io/cloudevents/kafka/CloudEventDeserializerTest.java +++ b/kafka/src/test/java/io/cloudevents/kafka/CloudEventDeserializerTest.java @@ -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; @@ -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)) diff --git a/kafka/src/test/java/io/cloudevents/kafka/KafkaMessageFactoryTest.java b/kafka/src/test/java/io/cloudevents/kafka/KafkaMessageFactoryTest.java index bdbaea1b5..c066f66c9 100644 --- a/kafka/src/test/java/io/cloudevents/kafka/KafkaMessageFactoryTest.java +++ b/kafka/src/test/java/io/cloudevents/kafka/KafkaMessageFactoryTest.java @@ -29,6 +29,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.*; @@ -55,7 +56,7 @@ public void readStructured(CloudEvent event) { byte[] serializedEvent = CSVFormat.INSTANCE.serialize(event); MessageReader message = KafkaMessageFactory.createReader( - new RecordHeaders().add("content-type", (CSVFormat.INSTANCE.serializedContentType() + "; charset=utf8").getBytes()), + new RecordHeaders().add("content-type", (CSVFormat.INSTANCE.serializedContentType() + "; charset=utf8").getBytes(StandardCharsets.UTF_8)), serializedEvent ); diff --git a/kafka/src/test/java/io/cloudevents/kafka/KafkaProducerMessageWriterTest.java b/kafka/src/test/java/io/cloudevents/kafka/KafkaProducerMessageWriterTest.java index 41c2b2fd7..684daf6e4 100644 --- a/kafka/src/test/java/io/cloudevents/kafka/KafkaProducerMessageWriterTest.java +++ b/kafka/src/test/java/io/cloudevents/kafka/KafkaProducerMessageWriterTest.java @@ -30,6 +30,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.*; @@ -63,7 +64,7 @@ void testRequestWithStructured(CloudEvent event) { assertThat(producerRecord.key()) .isEqualTo(key); assertThat(producerRecord.headers()) - .containsExactly(new RecordHeader(KafkaHeaders.CONTENT_TYPE, expectedContentType.getBytes())); + .containsExactly(new RecordHeader(KafkaHeaders.CONTENT_TYPE, expectedContentType.getBytes(StandardCharsets.UTF_8))); assertThat(producerRecord.value()) .isEqualTo(expectedBuffer); } diff --git a/kafka/src/test/java/io/cloudevents/kafka/KafkaUtils.java b/kafka/src/test/java/io/cloudevents/kafka/KafkaUtils.java index b571cd46e..ed775ea0d 100644 --- a/kafka/src/test/java/io/cloudevents/kafka/KafkaUtils.java +++ b/kafka/src/test/java/io/cloudevents/kafka/KafkaUtils.java @@ -17,6 +17,8 @@ package io.cloudevents.kafka; +import java.nio.charset.StandardCharsets; + import org.apache.kafka.common.header.internals.RecordHeader; import org.apache.kafka.common.header.internals.RecordHeaders; @@ -31,7 +33,7 @@ static RecordHeaders kafkaHeaders(RecordHeader... headers) { } static RecordHeader header(String key, String value) { - return new RecordHeader(key, value.getBytes()); + return new RecordHeader(key, value.getBytes(StandardCharsets.UTF_8)); } } diff --git a/spring/src/main/java/io/cloudevents/spring/messaging/CloudEventMessageConverter.java b/spring/src/main/java/io/cloudevents/spring/messaging/CloudEventMessageConverter.java index 68c41f572..2aabc6bb0 100644 --- a/spring/src/main/java/io/cloudevents/spring/messaging/CloudEventMessageConverter.java +++ b/spring/src/main/java/io/cloudevents/spring/messaging/CloudEventMessageConverter.java @@ -27,7 +27,7 @@ import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.converter.MessageConverter; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * A {@link MessageConverter} that can translate to and from a {@link Message @@ -96,7 +96,7 @@ private byte[] getBinaryData(Message message) { return (byte[]) payload; } else if (payload instanceof String) { - return ((String) payload).getBytes(Charset.defaultCharset()); + return ((String) payload).getBytes(StandardCharsets.UTF_8); } return null; } diff --git a/spring/src/test/java/io/cloudevents/spring/codec/CloudEventDecoderTests.java b/spring/src/test/java/io/cloudevents/spring/codec/CloudEventDecoderTests.java index 9b83f9eca..32b720ed1 100644 --- a/spring/src/test/java/io/cloudevents/spring/codec/CloudEventDecoderTests.java +++ b/spring/src/test/java/io/cloudevents/spring/codec/CloudEventDecoderTests.java @@ -16,6 +16,7 @@ package io.cloudevents.spring.codec; import java.net.URI; +import java.nio.charset.StandardCharsets; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; @@ -57,7 +58,7 @@ void doesDecodeJson() { CloudEvent attributes = CloudEventBuilder.v1().withId("A234-1234-1234") .withSource(URI.create("https://spring.io/")) .withType("org.springframework") - .withData("{\"name\":\"hello\"}".getBytes()).build(); + .withData("{\"name\":\"hello\"}".getBytes(StandardCharsets.UTF_8)).build(); Flux value = encoder.encode(attributes, DefaultDataBufferFactory.sharedInstance, ResolvableType.forClass(CloudEvent.class), diff --git a/spring/src/test/java/io/cloudevents/spring/codec/CloudEventEncoderTests.java b/spring/src/test/java/io/cloudevents/spring/codec/CloudEventEncoderTests.java index e43d4bd28..7cea976b1 100644 --- a/spring/src/test/java/io/cloudevents/spring/codec/CloudEventEncoderTests.java +++ b/spring/src/test/java/io/cloudevents/spring/codec/CloudEventEncoderTests.java @@ -16,6 +16,7 @@ package io.cloudevents.spring.codec; import java.net.URI; +import java.nio.charset.StandardCharsets; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; @@ -55,7 +56,7 @@ void doesEncodeJson() { CloudEventEncoder encoder = new CloudEventEncoder(); CloudEvent attributes = CloudEventBuilder.v1().withId("A234-1234-1234") .withSource(URI.create("https://spring.io/")) - .withType("org.springframework").withData("hello".getBytes()).build(); + .withType("org.springframework").withData("hello".getBytes(StandardCharsets.UTF_8)).build(); Flux value = encoder.encode(attributes, DefaultDataBufferFactory.sharedInstance, ResolvableType.forClass(CloudEvent.class), diff --git a/spring/src/test/java/io/cloudevents/spring/messaging/CloudEventMessageConverterTests.java b/spring/src/test/java/io/cloudevents/spring/messaging/CloudEventMessageConverterTests.java index eaa84e1ae..cc7d19d3f 100644 --- a/spring/src/test/java/io/cloudevents/spring/messaging/CloudEventMessageConverterTests.java +++ b/spring/src/test/java/io/cloudevents/spring/messaging/CloudEventMessageConverterTests.java @@ -16,6 +16,7 @@ package io.cloudevents.spring.messaging; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Map; @@ -85,7 +86,7 @@ void validCloudEvent() { @Test void structuredCloudEvent() { - byte[] payload = JSON.getBytes(); + byte[] payload = JSON.getBytes(StandardCharsets.UTF_8); Message message = MessageBuilder.withPayload(payload) .setHeader(MessageHeaders.CONTENT_TYPE, "application/cloudevents+json").build(); CloudEvent event = (CloudEvent) converter.fromMessage(message, CloudEvent.class); @@ -112,14 +113,14 @@ void structuredCloudEventStringPayload() { void fromCloudEvent() { CloudEvent attributes = CloudEventBuilder.v1().withId("A234-1234-1234") .withSource(URI.create("https://spring.io/")).withType("org.springframework") - .withData("hello".getBytes()).build(); + .withData("hello".getBytes(StandardCharsets.UTF_8)).build(); Message message = converter.toMessage(attributes, new MessageHeaders(Collections.emptyMap())); Map headers = message.getHeaders(); assertThat(headers.get("ce-id")).isEqualTo("A234-1234-1234"); assertThat(headers.get("ce-specversion")).isEqualTo("1.0"); assertThat(headers.get("ce-source")).isEqualTo("https://spring.io/"); assertThat(headers.get("ce-type")).isEqualTo("org.springframework"); - assertThat("hello".getBytes().equals(message.getPayload())); + assertThat("hello".getBytes(StandardCharsets.UTF_8).equals(message.getPayload())); } @Test