|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package io.github.eocqrs.kafka.json; |
| 24 | + |
| 25 | +import com.jcabi.xml.XMLDocument; |
| 26 | +import io.github.eocqrs.eokson.JsonOf; |
| 27 | +import io.github.eocqrs.eokson.JsonXML; |
| 28 | +import io.github.eocqrs.kafka.ConsumerSettings; |
| 29 | +import io.github.eocqrs.kafka.ProducerSettings; |
| 30 | +import io.github.eocqrs.kafka.xml.ConsumerXmlMapParams; |
| 31 | +import io.github.eocqrs.kafka.xml.ProducerXmlMapParams; |
| 32 | +import lombok.SneakyThrows; |
| 33 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 34 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 35 | +import org.cactoos.io.ResourceOf; |
| 36 | + |
| 37 | +/** |
| 38 | + * Allow creating custom Consumer/Producer from JSON. |
| 39 | + * |
| 40 | + * @param <K> The key |
| 41 | + * @param <X> The value |
| 42 | + * @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) |
| 43 | + * @since 0.4.6 |
| 44 | + */ |
| 45 | +public final class KfJsonFlexible<K, X> |
| 46 | + implements ConsumerSettings<K, X>, ProducerSettings<K, X> { |
| 47 | + |
| 48 | + /** |
| 49 | + * JSON name. |
| 50 | + */ |
| 51 | + private final String name; |
| 52 | + |
| 53 | + /** |
| 54 | + * Ctor. |
| 55 | + * |
| 56 | + * @param nm JSON name |
| 57 | + */ |
| 58 | + public KfJsonFlexible(final String nm) { |
| 59 | + this.name = nm; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + @SneakyThrows |
| 64 | + public KafkaConsumer<K, X> consumer() { |
| 65 | + return new KafkaConsumer<>( |
| 66 | + new ConsumerXmlMapParams( |
| 67 | + new XMLDocument( |
| 68 | + new JsonXML( |
| 69 | + new JsonOf( |
| 70 | + new ResourceOf( |
| 71 | + this.name |
| 72 | + ).stream() |
| 73 | + ), |
| 74 | + "consumer" |
| 75 | + ).asString() |
| 76 | + ) |
| 77 | + ).value() |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + @SneakyThrows |
| 83 | + public KafkaProducer<K, X> producer() { |
| 84 | + return new KafkaProducer<>( |
| 85 | + new ProducerXmlMapParams( |
| 86 | + new XMLDocument( |
| 87 | + new JsonXML( |
| 88 | + new JsonOf( |
| 89 | + new ResourceOf( |
| 90 | + this.name |
| 91 | + ).stream() |
| 92 | + ), |
| 93 | + "producer" |
| 94 | + ).asString() |
| 95 | + ) |
| 96 | + ).value() |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments