Search in sources :

Example 1 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 2 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 3 with OutputStreamBuilder

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

the class JettyContentExchange9 method send.

public void send(HttpClient client) throws IOException {
    org.eclipse.jetty.client.api.Request.Listener listener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            onRequestComplete();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            onConnectionFailed(failure);
        }
    };
    InputStreamResponseListener responseListener = new InputStreamResponseListener() {

        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            byte[] buffer = new byte[content.limit()];
            content.get(buffer);
            try {
                osb.write(buffer);
                callback.succeeded();
            } catch (IOException e) {
                callback.failed(e);
            }
        }

        @Override
        public void onComplete(Result result) {
            if (result.isFailed()) {
                doTaskCompleted(result.getFailure());
            } else {
                try {
                    Object content = osb.build();
                    if (content instanceof byte[]) {
                        onResponseComplete(result, (byte[]) content);
                    } else {
                        StreamCache cos = (StreamCache) content;
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        cos.writeTo(baos);
                        onResponseComplete(result, baos.toByteArray());
                    }
                } catch (IOException e) {
                    doTaskCompleted(e);
                }
            }
        }
    };
    request.followRedirects(supportRedirect).listener(listener).send(responseListener);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StreamCache(org.apache.camel.StreamCache) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 4 with OutputStreamBuilder

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

the class DataFormatTransformer method transform.

/**
     * Perform data transformation with specified from/to type using DataFormat.
     * @param message message to apply transformation
     * @param from 'from' data type
     * @param to 'to' data type
     */
@Override
public void transform(Message message, DataType from, DataType to) throws Exception {
    Exchange exchange = message.getExchange();
    CamelContext context = exchange.getContext();
    // Unmarshaling into Java Object
    if ((to == null || to.isJavaType()) && (from.equals(getFrom()) || from.getModel().equals(getModel()))) {
        DataFormat dataFormat = getDataFormat(exchange);
        LOG.debug("Unmarshaling with '{}'", dataFormat);
        Object answer = dataFormat.unmarshal(exchange, message.getBody(InputStream.class));
        if (to != null && to.getName() != null) {
            Class<?> toClass = context.getClassResolver().resolveClass(to.getName());
            if (!toClass.isAssignableFrom(answer.getClass())) {
                LOG.debug("Converting to '{}'", toClass.getName());
                answer = context.getTypeConverter().mandatoryConvertTo(toClass, answer);
            }
        }
        message.setBody(answer);
    // Marshaling from Java Object
    } else if ((from == null || from.isJavaType()) && (to.equals(getTo()) || to.getModel().equals(getModel()))) {
        Object input = message.getBody();
        if (from != null && from.getName() != null) {
            Class<?> fromClass = context.getClassResolver().resolveClass(from.getName());
            if (!fromClass.isAssignableFrom(input.getClass())) {
                LOG.debug("Converting to '{}'", fromClass.getName());
                input = context.getTypeConverter().mandatoryConvertTo(fromClass, input);
            }
        }
        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
        DataFormat dataFormat = getDataFormat(exchange);
        LOG.debug("Marshaling with '{}'", dataFormat);
        dataFormat.marshal(exchange, message.getBody(), osb);
        message.setBody(osb.build());
    } else {
        throw new IllegalArgumentException("Unsupported transformation: from='" + from + ", to='" + to + "'");
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelContext(org.apache.camel.CamelContext) InputStream(java.io.InputStream) DataFormat(org.apache.camel.spi.DataFormat) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 5 with OutputStreamBuilder

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

the class GzipDataFormat method unmarshal.

public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
    GZIPInputStream unzipInput = null;
    OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
    try {
        unzipInput = new GZIPInputStream(inputStream);
        IOHelper.copy(unzipInput, osb);
        return osb.build();
    } finally {
        // must close all input streams
        IOHelper.close(osb, unzipInput, inputStream);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) 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