Search in sources :

Example 6 with OutputStreamBuilder

use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.

the class LZFDataFormat method unmarshal.

@Override
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
    InputStream compressedInput = null;
    OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
    try {
        compressedInput = new LZFInputStream(inputStream);
        IOHelper.copy(compressedInput, osb);
        return osb.build();
    } finally {
        // must close all input streams
        IOHelper.close(osb, compressedInput, inputStream);
    }
}
Also used : LZFInputStream(com.ning.compress.lzf.LZFInputStream) InputStream(java.io.InputStream) LZFInputStream(com.ning.compress.lzf.LZFInputStream) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 7 with OutputStreamBuilder

use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.

the class ZipFileDataFormat method unmarshal.

@Override
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
    if (usingIterator) {
        ZipIterator zipIterator = new ZipIterator(exchange.getIn());
        zipIterator.setAllowEmptyDirectory(allowEmptyDirectory);
        return zipIterator;
    } else {
        ZipInputStream zis = new ZipInputStream(inputStream);
        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
        try {
            ZipEntry entry = zis.getNextEntry();
            if (entry != null) {
                exchange.getOut().setHeader(FILE_NAME, entry.getName());
                IOHelper.copy(zis, osb);
            }
            entry = zis.getNextEntry();
            if (entry != null) {
                throw new IllegalStateException("Zip file has more than 1 entry.");
            }
            return osb.build();
        } finally {
            IOHelper.close(zis, osb);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 8 with OutputStreamBuilder

use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.

the class TarFileDataFormat method unmarshal.

@Override
public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
    if (usingIterator) {
        return new TarIterator(exchange.getIn(), stream);
    } else {
        BufferedInputStream bis = new BufferedInputStream(stream);
        TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis);
        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
        try {
            TarArchiveEntry entry = tis.getNextTarEntry();
            if (entry != null) {
                exchange.getOut().setHeader(FILE_NAME, entry.getName());
                IOHelper.copy(tis, osb);
            }
            entry = tis.getNextTarEntry();
            if (entry != null) {
                throw new IllegalStateException("Tar file has more than 1 entry.");
            }
            return osb.build();
        } finally {
            IOHelper.close(osb, tis, bis);
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedInputStream(java.io.BufferedInputStream) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 9 with OutputStreamBuilder

use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.

the class DropboxAPIFacade method downloadSingleFile.

private Map.Entry<String, Object> downloadSingleFile(String path) throws DropboxException {
    try {
        OutputStreamBuilder target = OutputStreamBuilder.withExchange(exchange);
        DbxEntry.File downloadedFile = client.getFile(path, null, target);
        if (downloadedFile != null) {
            LOG.debug("downloaded path={}", path);
            return new AbstractMap.SimpleEntry<>(path, target.build());
        } else {
            return null;
        }
    } catch (DbxException e) {
        throw new DropboxException(path + " does not exist or can't obtain metadata");
    } catch (IOException e) {
        throw new DropboxException(path + " can't obtain a stream");
    }
}
Also used : DbxEntry(com.dropbox.core.DbxEntry) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) IOException(java.io.IOException) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder) DbxException(com.dropbox.core.DbxException)

Example 10 with OutputStreamBuilder

use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.

the class MarshalProcessor method process.

public boolean process(Exchange exchange, AsyncCallback callback) {
    ObjectHelper.notNull(dataFormat, "dataFormat");
    // if stream caching is enabled then use that so we can stream accordingly
    // for example to overflow to disk for big streams
    OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
    Message in = exchange.getIn();
    Object body = in.getBody();
    // lets setup the out message before we invoke the dataFormat
    // so that it can mutate it if necessary
    Message out = exchange.getOut();
    out.copyFrom(in);
    try {
        dataFormat.marshal(exchange, body, osb);
        out.setBody(osb.build());
    } catch (Throwable e) {
        // remove OUT message, as an exception occurred
        exchange.setOut(null);
        exchange.setException(e);
    }
    callback.done(true);
    return true;
}
Also used : Message(org.apache.camel.Message) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Aggregations

OutputStreamBuilder (org.apache.camel.converter.stream.OutputStreamBuilder)11 InputStream (java.io.InputStream)3 IOException (java.io.IOException)2 DbxEntry (com.dropbox.core.DbxEntry)1 DbxException (com.dropbox.core.DbxException)1 LZFInputStream (com.ning.compress.lzf.LZFInputStream)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Key (java.security.Key)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 CipherInputStream (javax.crypto.CipherInputStream)1 AsyncCallback (org.apache.camel.AsyncCallback)1 CamelContext (org.apache.camel.CamelContext)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 StreamCache (org.apache.camel.StreamCache)1