Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UTF-8 when using getBytes #491

Merged
merged 1 commit into from
Jan 5, 2023
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
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion kafka/src/test/java/io/cloudevents/kafka/KafkaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}

}
Loading