All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.atleon.protobuf.ProtobufKafkaDeserializer Maven / Gradle / Ivy

package io.atleon.protobuf;

import com.google.protobuf.Message;
import org.apache.kafka.common.serialization.Deserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

public final class ProtobufKafkaDeserializer implements Deserializer {

    /**
     * Qualified class name of the type of {@link Message} to deserialize into
     */
    public static final String KEY_MESSAGE_TYPE_CONFIG = "protobuf.key.message.type";

    /**
     * Qualified class name of the type of {@link Message} to deserialize into
     */
    public static final String VALUE_MESSAGE_TYPE_CONFIG = "protobuf.value.message.type";

    /**
     * Qualified class name of the type of {@link Message} to deserialize into
     *
     * @deprecated Use {@value #KEY_MESSAGE_TYPE_CONFIG} or {@value #VALUE_MESSAGE_TYPE_CONFIG}
     */
    @Deprecated
    public static final String MESSAGE_TYPE_CONFIG = "protobuf.message.type";

    private static final Logger LOGGER = LoggerFactory.getLogger(ProtobufKafkaDeserializer.class);

    private Function parser;

    @Override
    public void configure(Map configs, boolean isKey) {
        this.parser = loadParser(configs, isKey ? KEY_MESSAGE_TYPE_CONFIG : VALUE_MESSAGE_TYPE_CONFIG);
    }

    @Override
    public T deserialize(String topic, byte[] data) {
        return data == null ? null : parser.apply(data);
    }

    private static  Function loadParser(Map configs, String specificKey) {
        Optional> parser;
        if ((parser = ProtobufMessages.loadParser(configs, specificKey, byte[].class)).isPresent()) {
            return parser.get();
        } else if ((parser = ProtobufMessages.loadParser(configs, MESSAGE_TYPE_CONFIG, byte[].class)).isPresent()) {
            LOGGER.warn("Deprecated config '{}'. Please configure '{}'.", MESSAGE_TYPE_CONFIG, specificKey);
            return parser.get();
        } else {
            throw new IllegalArgumentException("Missing config: " + specificKey);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy