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

io.apicurio.registry.serde.AbstractSerializer Maven / Gradle / Ivy

The newest version!
package io.apicurio.registry.serde;

import io.apicurio.registry.resolver.ParsedSchema;
import io.apicurio.registry.resolver.SchemaLookupResult;
import io.apicurio.registry.resolver.SchemaParser;
import io.apicurio.registry.resolver.SchemaResolver;
import io.apicurio.registry.resolver.strategy.ArtifactReferenceResolverStrategy;
import io.apicurio.registry.rest.client.RegistryClient;
import io.apicurio.registry.serde.config.SerdeConfig;
import io.apicurio.registry.serde.data.SerdeMetadata;
import io.apicurio.registry.serde.data.SerdeRecord;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;

import static io.apicurio.registry.serde.BaseSerde.MAGIC_BYTE;

public abstract class AbstractSerializer implements AutoCloseable {

    private final BaseSerde baseSerde;

    public AbstractSerializer() {
        this.baseSerde = new BaseSerde<>();
    }

    public AbstractSerializer(RegistryClient client) {
        this.baseSerde = new BaseSerde<>(client);
    }

    public AbstractSerializer(SchemaResolver schemaResolver) {
        this.baseSerde = new BaseSerde<>(schemaResolver);
    }

    public AbstractSerializer(RegistryClient client, SchemaResolver schemaResolver) {
        this.baseSerde = new BaseSerde<>(client, schemaResolver);
    }

    public AbstractSerializer(RegistryClient client, ArtifactReferenceResolverStrategy strategy,
            SchemaResolver schemaResolver) {
        this.baseSerde = new BaseSerde<>(client, strategy, schemaResolver);
    }

    public abstract SchemaParser schemaParser();

    public abstract void serializeData(ParsedSchema schema, U data, OutputStream out) throws IOException;

    public void configure(SerdeConfig config, boolean isKey) {
        baseSerde.configure(config, isKey, schemaParser());
    }

    public byte[] serializeData(String topic, U data) {
        // just return null
        if (data == null) {
            return null;
        }
        try {
            SerdeMetadata resolverMetadata = new SerdeMetadata(topic, baseSerde.isKey());

            SchemaLookupResult schema = baseSerde.getSchemaResolver()
                    .resolveSchema(new SerdeRecord<>(resolverMetadata, data));

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            out.write(MAGIC_BYTE);
            baseSerde.getIdHandler().writeId(schema.toArtifactReference(), out);
            this.serializeData(schema.getParsedSchema(), data, out);

            return out.toByteArray();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    public BaseSerde getSerdeConfigurer() {
        return baseSerde;
    }

    @Override
    public void close() {
        this.baseSerde.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy