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

net.n2oapp.framework.config.compile.pipeline.N2oDeserializePipeline Maven / Gradle / Ivy

There is a newer version: 7.28.2
Show newest version
package net.n2oapp.framework.config.compile.pipeline;

import net.n2oapp.framework.api.MetadataEnvironment;
import net.n2oapp.framework.api.metadata.SourceMetadata;
import net.n2oapp.framework.api.metadata.pipeline.DeserializePersistTerminalPipeline;
import net.n2oapp.framework.api.metadata.pipeline.DeserializePipeline;
import net.n2oapp.framework.api.metadata.pipeline.DeserializeTerminalPipeline;
import org.apache.commons.io.IOUtils;

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

import static net.n2oapp.framework.api.metadata.pipeline.PipelineOperationType.*;

public class N2oDeserializePipeline extends N2oPipeline implements DeserializePipeline {

    protected N2oDeserializePipeline(MetadataEnvironment env) {
        super(env);
    }

    @Override
    public DeserializeTerminalPipeline deserialize() {
        pullOp(DESERIALIZE);
        return new DeserializeTerminalPipeline<>() {
            @Override
            public  S get(InputStream json, Class sourceClass) {
                return execute(new DummyCompileContext<>("undefined", sourceClass), null, json);
            }

            @Override
            public DeserializePersistTerminalPipeline persist() {
                pullOp(PERSIST);
                return new DeserializePersistTerminalPipeline() {
                    @Override
                    public  InputStream get(InputStream json, Class sourceClass) {
                        return execute(new DummyCompileContext<>("undefined", sourceClass), null, json);
                    }

                    @Override
                    public  void set(InputStream json, Class sourceClass, OutputStream output) {
                        try (InputStream is = get(json, sourceClass)) {
                            IOUtils.copy(is, output);
                        } catch (IOException e) {
                            throw new IllegalStateException(e);
                        }
                    }
                };
            }

            @Override
            public DeserializeTerminalPipeline validate() {
                pullOp(VALIDATE);
                return this;
            }

            @Override
            public DeserializeTerminalPipeline merge() {
                pullOp(MERGE);
                return this;
            }

            @Override
            public DeserializeTerminalPipeline transform() {
                pullOp(SOURCE_TRANSFORM);
                return this;
            }

            @Override
            public DeserializeTerminalPipeline cache() {
                pullOp(SOURCE_CACHE);
                return this;
            }

            @Override
            public DeserializeTerminalPipeline copy() {
                pullOp(COPY);
                return this;
            }
        };
    }

}