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

com.fasterxml.clustermate.client.util.GenericContentConverter Maven / Gradle / Ivy

There is a newer version: 0.10.5
Show newest version
package com.fasterxml.clustermate.client.util;

import java.io.*;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;

import com.fasterxml.clustermate.api.ContentType;

/**
 * {link ContentConverter} implementation that handles variations
 * in two dimensions: in target type (one converter instance created
 * per type), and in underlying data format (which is chosen dynamically)
 */
public class GenericContentConverter implements ContentConverter
{
    protected final static SmileFactory SMILE_FACTORY = new SmileFactory();

    protected final ObjectReader _jsonReader;

    public GenericContentConverter(ObjectMapper jsonMapper, Class targetType) {
        _jsonReader = jsonMapper.reader(targetType);
    }

    public GenericContentConverter(ObjectMapper jsonMapper, JavaType targetType) {
        _jsonReader = jsonMapper.reader(targetType);
    }
    
    @Override
    public T convert(ContentType contentType, InputStream in)
        throws IOException
    {
        return _chooseReader(contentType).readValue(in);
    }

    @Override
    public T convert(ContentType contentType, byte[] buffer, int offset,
            int length) throws IOException {
        return _chooseReader(contentType).readValue(buffer, offset, length);
    }

    protected ObjectReader _chooseReader(ContentType contentType) {
        switch (contentType) {
        case SMILE: // only special case is Smile
            return _jsonReader.with(SMILE_FACTORY);
        case JSON:
            return _jsonReader;
        default:
        }
        // TEXT not supported..
        throw new IllegalArgumentException("Can not handle content type '"+contentType+"'");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy