Package com.atlassian.labs.speakeasy.manager.convention

Source Code of com.atlassian.labs.speakeasy.manager.convention.JsonManifestHandler

package com.atlassian.labs.speakeasy.manager.convention;

import com.atlassian.labs.speakeasy.manager.PluginOperationFailedException;
import com.atlassian.labs.speakeasy.model.JsonManifest;
import com.atlassian.labs.speakeasy.util.JsonObjectMapper;
import com.atlassian.plugin.Plugin;
import com.atlassian.plugin.PluginArtifact;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;

import static com.atlassian.labs.speakeasy.util.KeyExtractor.extractFromFilename;


/**
*
*/
@Component
public class JsonManifestHandler
{

    public JsonManifest read(PluginArtifact artifact)
    {
        try
        {
            JsonManifest mf = JsonObjectMapper.read(JsonManifest.class,
                    new NamedBufferedInputStream(artifact.getResourceAsStream(JsonManifest.ATLASSIAN_EXTENSION_PATH)));
            if (mf.getKey() == null)
            {
                mf.setKey(extractFromFilename(artifact.getName()));
            }
            return mf;
        }
        catch (IOException e)
        {
            throw new PluginOperationFailedException("Unable to parse " + JsonManifest.ATLASSIAN_EXTENSION_PATH, e, null);
        }
    }

    public JsonManifest read(String key, InputStream in)
    {
        try
        {
            JsonManifest mf = JsonObjectMapper.read(JsonManifest.class,
                    new NamedBufferedInputStream(in));
            mf.setKey(key);
            return mf;
        }
        catch (IOException e)
        {
            throw new PluginOperationFailedException("Unable to parse " + JsonManifest.ATLASSIAN_EXTENSION_PATH, e, null);
        }
    }

    public JsonManifest read(Plugin plugin)
    {
        try
        {
            JsonManifest mf = JsonObjectMapper.read(JsonManifest.class,
                    new NamedBufferedInputStream(plugin.getResourceAsStream(JsonManifest.ATLASSIAN_EXTENSION_PATH)));
            mf.setKey(plugin.getKey());
            return mf;
        }
        catch (IOException e)
        {
            throw new PluginOperationFailedException("Unable to parse " + JsonManifest.ATLASSIAN_EXTENSION_PATH, e, null);
        }
    }

    public void write(JsonManifest manifest, OutputStream out) throws IOException
    {
        manifest.setKey(null);
        String serialized = JsonObjectMapper.write(manifest);
        IOUtils.copy(new StringReader(serialized), out);
    }

    private static class NamedBufferedInputStream extends BufferedInputStream
    {

        public NamedBufferedInputStream(InputStream in)
        {
            super(in);
        }

        @Override
        public String toString()
        {
            return JsonManifest.ATLASSIAN_EXTENSION_PATH;
        }
    }
}
TOP

Related Classes of com.atlassian.labs.speakeasy.manager.convention.JsonManifestHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.