allthingssecurity commented on code in PR #27587:
URL: https://github.com/apache/camel/pull/27587#discussion_r4228539186
##########
components/camel-cloudevents/src/main/java/org/apache/camel/component/cloudevents/transformer/CloudEventJsonDataTypeTransformer.java:
##########
@@ -140,6 +186,66 @@ private static void appendJsonString(StringBuilder
builder, String value) {
builder.append('"');
}
+ /**
+ * Whether the content type declares Json data: the subtype of its media
type, without parameters, is {@code json}
+ * or ends with {@code +json} (CloudEvents Json format, section 3.1). An
absent content type is Json, as the
+ * transformer declares {@code application/json} then.
+ */
+ static boolean isJsonContentType(Object contentType) {
Review Comment:
Done in a24341c1028b: `isJsonContentType` now takes a `String`, and the
caller converts the attribute value, which can be any object in the attribute
map. 27 tests, 0 failures.
_Claude Code on behalf of allthingssecurity_
##########
components/camel-cloudevents/src/main/java/org/apache/camel/component/cloudevents/transformer/CloudEventJsonDataTypeTransformer.java:
##########
@@ -76,28 +98,52 @@ public void transform(Message message, DataType fromType,
DataType toType) {
cloudEventAttributes.putIfAbsent(cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).json(),
cloudEvent.getEventTime(message.getExchange()));
- String body = MessageHelper.extractBodyAsString(message);
- cloudEventAttributes.putIfAbsent("data", body);
- cloudEventAttributes.putIfAbsent(
-
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json(),
dataContentType);
+ String dataContentTypeKey =
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json();
+ cloudEventAttributes.putIfAbsent(dataContentTypeKey,
dataContentType);
+ // the data is written as the datacontenttype of the event says
+ Object eventDataContentType =
cloudEventAttributes.get(dataContentTypeKey);
+ boolean jsonData = isJsonContentType(eventDataContentType);
+ if (message.getBody() instanceof byte[] bytes) {
+ String text = decodeText(bytes,
ExchangeHelper.getCharset(message.getExchange()));
+ if (text != null) {
+ cloudEventAttributes.putIfAbsent("data", text);
+ } else {
+ // decoding these bytes as text would replace the invalid
ones, so the data would be lost
+ cloudEventAttributes.putIfAbsent("data_base64",
Base64.getEncoder().encodeToString(bytes));
+ // a declared datacontenttype is kept: the route states
the format of these bytes, which may be valid
+ // in a charset other than the one of the exchange, so
only an assumed application/json is replaced
+ if (!contentTypeDeclared) {
+ // these bytes are not Json, and the datacontenttype
must reflect the format of the data
+ cloudEventAttributes.put(dataContentTypeKey,
CloudEvent.APPLICATION_OCTET_STREAM_MIME_TYPE);
+ }
+ }
+ } else {
+ String data = MessageHelper.extractBodyAsString(message);
+ // the data is optional, so a null body has none rather than
the text "null"
+ if (data != null) {
+ cloudEventAttributes.putIfAbsent("data", data);
+ }
+ }
headers.put(Exchange.CONTENT_TYPE, APPLICATION_CLOUDEVENTS_JSON);
- message.setBody(createCouldEventJsonObject(cloudEventAttributes));
+ message.setBody(createCouldEventJsonObject(cloudEventAttributes,
jsonData, contentTypeDeclared));
cloudEvent.attributes().stream().map(CloudEvent.Attribute::id).forEach(headers::remove);
}
}
- private String createCouldEventJsonObject(Map<String, Object>
cloudEventAttributes) {
+ private String createCouldEventJsonObject(
Review Comment:
Renamed to `createCloudEventJsonObject` in a24341c1028b (private method, two
references).
_Claude Code on behalf of allthingssecurity_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]