-
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #395 from Cyril-Edme/ISS-330-send-headers-while-pr…
…oducing-to-kafka Iss 330 send headers while producing to kafka
- Loading branch information
Showing
17 changed files
with
438 additions
and
22 deletions.
There are no files selected for viewing
65 changes: 64 additions & 1 deletion
65
core/src/main/java/org/jsmart/zerocode/core/di/provider/GsonSerDeProvider.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 |
---|---|---|
@@ -1,16 +1,79 @@ | ||
package org.jsmart.zerocode.core.di.provider; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import org.apache.kafka.common.header.Headers; | ||
import org.apache.kafka.common.header.internals.RecordHeaders; | ||
|
||
import javax.inject.Provider; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class GsonSerDeProvider implements Provider<Gson> { | ||
|
||
@Override | ||
public Gson get() { | ||
return new GsonBuilder() | ||
.registerTypeAdapterFactory(KafkaHeadersAdapter.FACTORY) | ||
.create(); | ||
} | ||
|
||
static class KafkaHeadersAdapter extends TypeAdapter<Headers> { | ||
|
||
static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
if (type.getRawType() == Headers.class) { | ||
return (TypeAdapter<T>) new KafkaHeadersAdapter(gson); | ||
} | ||
return null; | ||
} | ||
}; | ||
|
||
private final Gson gson; | ||
|
||
public KafkaHeadersAdapter(Gson gson) { | ||
this.gson = gson; | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter writer, Headers value) throws IOException { | ||
if (value == null || !value.iterator().hasNext()) { | ||
writer.nullValue(); | ||
} else { | ||
Map<String, String> headers = new HashMap<>(); | ||
value.forEach(header -> headers.put(header.key(), new String(header.value()))); | ||
gson.getAdapter(Map.class).write(writer, headers); | ||
} | ||
} | ||
|
||
@Override | ||
public Headers read(JsonReader reader) throws IOException { | ||
Headers headers = null; | ||
JsonToken peek = reader.peek(); | ||
if (JsonToken.NULL.equals(peek)) { | ||
reader.nextNull(); | ||
} else { | ||
Map<String, String> map = gson.getAdapter(Map.class).read(reader); | ||
|
||
return (new Gson()); | ||
headers = new RecordHeaders(); | ||
for (Map.Entry<String, String> entry : map.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
headers.add(key, value == null ? null : value.getBytes()); | ||
} | ||
} | ||
|
||
return headers; | ||
} | ||
} | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/jsmart/zerocode/core/kafka/helper/ProducerRecordBuilder.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,39 @@ | ||
package org.jsmart.zerocode.core.kafka.helper; | ||
|
||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.header.Headers; | ||
import org.apache.kafka.common.header.internals.RecordHeaders; | ||
|
||
import java.util.Map; | ||
|
||
public class ProducerRecordBuilder { | ||
private String topic; | ||
private Object key; | ||
private Object value; | ||
private Headers headers; | ||
|
||
private ProducerRecordBuilder() {} | ||
|
||
public static ProducerRecordBuilder from(String topic, Object key, Object value) { | ||
ProducerRecordBuilder producerRecordBuilder = new ProducerRecordBuilder(); | ||
|
||
producerRecordBuilder.topic = topic; | ||
producerRecordBuilder.key = key; | ||
producerRecordBuilder.value = value; | ||
|
||
return producerRecordBuilder; | ||
} | ||
|
||
public ProducerRecordBuilder withHeaders(Map<String, String> headers) { | ||
if (headers != null) { | ||
this.headers = new RecordHeaders(); | ||
headers.forEach((hKey, hValue) -> this.headers.add(hKey, hValue.getBytes())); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public ProducerRecord<Object, Object> build() { | ||
return new ProducerRecord<>(topic, null, null, key, value, headers); | ||
} | ||
} |
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
Oops, something went wrong.