-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a new text embedded format (#858)
* feat: added a new text embedded format Signed-off-by: Antonio Pedro <[email protected]> * test: added tests Signed-off-by: Antonio Pedro <[email protected]> * doc: updated docs for the new format Signed-off-by: Antonio Pedro <[email protected]> * Update generated documentation Signed-off-by: Paolo Patierno <[email protected]> Signed-off-by: Antonio Pedro <[email protected]> * add get content type to match on Poll request Signed-off-by: Antonio Pedro <[email protected]> * handling incorrect value Signed-off-by: Antonio Pedro <[email protected]> * added test Signed-off-by: Antonio Pedro <[email protected]> * remove unused method Signed-off-by: Antonio Pedro <[email protected]> * replace depracted put in ObjectNode object Signed-off-by: Antonio Pedro <[email protected]> * replace depracted put in ObjectNode object Signed-off-by: Antonio Pedro <[email protected]> * added test Signed-off-by: Antonio Pedro <[email protected]> * remove unused method Signed-off-by: Antonio Pedro <[email protected]> * rebase with upstream/main Signed-off-by: Antonio Pedro <[email protected]> * Revert "rebase with upstream/main" This reverts commit b009700. Signed-off-by: Antonio Pedro <[email protected]> * change serializer and content type Signed-off-by: Antonio Pedro <[email protected]> * added missing docs and CHANGELOG Signed-off-by: Antonio Pedro <[email protected]> * added missing word xD Signed-off-by: António Pedro <[email protected]> --------- Signed-off-by: Antonio Pedro <[email protected]> Signed-off-by: Paolo Patierno <[email protected]> Signed-off-by: António Pedro <[email protected]> Co-authored-by: Paolo Patierno <[email protected]>
- Loading branch information
1 parent
e1180e4
commit c3e96c7
Showing
13 changed files
with
331 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/io/strimzi/kafka/bridge/http/converter/HttpTextMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright Strimzi authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.strimzi.kafka.bridge.http.converter; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import io.strimzi.kafka.bridge.converter.MessageConverter; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.header.Header; | ||
import org.apache.kafka.common.header.Headers; | ||
import org.apache.kafka.common.header.internals.RecordHeader; | ||
import org.apache.kafka.common.header.internals.RecordHeaders; | ||
|
||
import javax.xml.bind.DatatypeConverter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Implementation of a message converter to deal with the "text" embedded data format | ||
*/ | ||
@SuppressWarnings("checkstyle:NPathComplexity") | ||
public class HttpTextMessageConverter implements MessageConverter<byte[], byte[], byte[], byte[]> { | ||
@Override | ||
public ProducerRecord<byte[], byte[]> toKafkaRecord(String kafkaTopic, Integer partition, byte[] message) { | ||
|
||
Integer partitionFromBody = null; | ||
byte[] key = null; | ||
byte[] value = null; | ||
Headers headers = new RecordHeaders(); | ||
|
||
JsonNode json = JsonUtils.bytesToJson(message); | ||
|
||
if (!json.isEmpty()) { | ||
if (json.has("key")) { | ||
key = json.get("key").asText().getBytes(); | ||
} | ||
if (json.has("value")) { | ||
JsonNode valueNode = json.get("value"); | ||
if (!valueNode.isTextual()) { | ||
throw new IllegalStateException("Because the embedded format is 'text', the value must be a string"); | ||
} | ||
value = valueNode.asText().getBytes(); | ||
} | ||
if (json.has("headers")) { | ||
ArrayNode jsonArray = (ArrayNode) json.get("headers"); | ||
for (JsonNode jsonObject : jsonArray) { | ||
headers.add(new RecordHeader(jsonObject.get("key").asText(), DatatypeConverter.parseBase64Binary(jsonObject.get("value").asText()))); | ||
} | ||
} | ||
if (json.has("partition")) { | ||
partitionFromBody = json.get("partition").asInt(); | ||
} | ||
if (partition != null && partitionFromBody != null) { | ||
throw new IllegalStateException("Partition specified in body and in request path"); | ||
} | ||
if (partition != null) { | ||
partitionFromBody = partition; | ||
} | ||
} | ||
return new ProducerRecord<>(kafkaTopic, partitionFromBody, key, value, headers); | ||
} | ||
|
||
@Override | ||
public List<ProducerRecord<byte[], byte[]>> toKafkaRecords(String kafkaTopic, Integer partition, byte[] messages) { | ||
|
||
List<ProducerRecord<byte[], byte[]>> records = new ArrayList<>(); | ||
|
||
JsonNode json = JsonUtils.bytesToJson(messages); | ||
ArrayNode jsonArray = (ArrayNode) json.get("records"); | ||
|
||
for (JsonNode jsonObj : jsonArray) { | ||
records.add(toKafkaRecord(kafkaTopic, partition, JsonUtils.jsonToBytes(jsonObj))); | ||
} | ||
return records; | ||
} | ||
|
||
@Override | ||
public byte[] toMessage(String address, ConsumerRecord<byte[], byte[]> record) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public byte[] toMessages(ConsumerRecords<byte[], byte[]> records) { | ||
ArrayNode jsonArray = JsonUtils.createArrayNode(); | ||
|
||
for (ConsumerRecord<byte[], byte[]> record : records) { | ||
ObjectNode jsonObject = JsonUtils.createObjectNode(); | ||
|
||
jsonObject.set("topic", new TextNode(record.topic())); | ||
jsonObject.set("key", record.key() != null ? new TextNode(new String(record.key())) : null); | ||
jsonObject.set("value", record.value() != null ? new TextNode(new String(record.value())) : null); | ||
jsonObject.put("partition", record.partition()); | ||
jsonObject.put("offset", record.offset()); | ||
|
||
ArrayNode headers = JsonUtils.createArrayNode(); | ||
|
||
for (Header kafkaHeader : record.headers()) { | ||
ObjectNode header = JsonUtils.createObjectNode(); | ||
|
||
header.set("key", new TextNode(kafkaHeader.key())); | ||
header.put("value", DatatypeConverter.printBase64Binary(kafkaHeader.value())); | ||
headers.add(header); | ||
} | ||
if (!headers.isEmpty()) { | ||
jsonObject.set("headers", headers); | ||
} | ||
jsonArray.add(jsonObject); | ||
} | ||
return JsonUtils.jsonToBytes(jsonArray); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.