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

com.github.nagyesta.lowkeyvault.model.json.util.AbstractBase64ZipDeserializer Maven / Gradle / Ivy

There is a newer version: 2.5.81
Show newest version
package com.github.nagyesta.lowkeyvault.model.json.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Optional;
import java.util.zip.GZIPInputStream;

/**
 * Deserializer Base64 decoding and unzipping json snippets.
 *
 * @param  The type of the entity.
 */
@Slf4j
public abstract class AbstractBase64ZipDeserializer extends JsonDeserializer {

    private final Base64Deserializer base64Deserializer;
    private final ObjectMapper objectMapper;

    protected AbstractBase64ZipDeserializer(final Base64Deserializer base64Deserializer, final ObjectMapper objectMapper) {
        this.base64Deserializer = base64Deserializer;
        this.objectMapper = objectMapper;
    }

    @Override
    public E deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
        final Optional bytes = Optional.ofNullable(base64Deserializer.deserializeBase64(jsonParser));
        return bytes.filter(v -> v.length > 0)
                .map(this::decompressWrappedObject)
                .orElse(null);
    }

    private E decompressWrappedObject(final byte[] bytes) {
        //noinspection LocalCanBeFinal
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
             GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream)) {
            final String json = new String(gzipInputStream.readAllBytes());
            return objectMapper.reader().readValue(json, getType());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalArgumentException("Unable to decompress input.");
        }
    }

    protected abstract Class getType();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy