Search in sources :

Example 31 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class NoStreamCachingTest method testMixed.

public void testMixed() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:a").to("mock:a");
            from("direct:b").streamCaching().to("mock:b");
        }
    });
    context.start();
    a.expectedMessageCount(1);
    b.expectedMessageCount(1);
    InputStream message = new ByteArrayInputStream(MESSAGE.getBytes());
    template.sendBody("direct:a", message);
    InputStream message2 = new ByteArrayInputStream(MESSAGE.getBytes());
    template.sendBody("direct:b", message2);
    assertMockEndpointsSatisfied();
    assertTrue(a.assertExchangeReceived(0).getIn().getBody() instanceof ByteArrayInputStream);
    assertEquals(a.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
    assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
    assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCache(org.apache.camel.StreamCache) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 32 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class MessageHelper method extractValueForLogging.

/**
     * Extracts the value for logging purpose.
     * <p/>
     * Will clip the value if its too big for logging.
     *
     * @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
     * @param obj     the value
     * @param message the message
     * @param prepend a message to prepend
     * @param allowStreams whether or not streams is allowed
     * @param allowFiles whether or not files is allowed (currently not in use)
     * @param maxChars limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
     * @return the logging message
     */
public static String extractValueForLogging(Object obj, Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars) {
    if (maxChars < 0) {
        return prepend + "[Body is not logged]";
    }
    if (obj == null) {
        return prepend + "[Body is null]";
    }
    if (!allowStreams) {
        if (obj instanceof Source && !(obj instanceof StringSource || obj instanceof BytesSource)) {
            // all other kinds we should not touch the body
            return prepend + "[Body is instance of java.xml.transform.Source]";
        } else if (obj instanceof StreamCache) {
            return prepend + "[Body is instance of org.apache.camel.StreamCache]";
        } else if (obj instanceof InputStream) {
            return prepend + "[Body is instance of java.io.InputStream]";
        } else if (obj instanceof OutputStream) {
            return prepend + "[Body is instance of java.io.OutputStream]";
        } else if (obj instanceof Reader) {
            return prepend + "[Body is instance of java.io.Reader]";
        } else if (obj instanceof Writer) {
            return prepend + "[Body is instance of java.io.Writer]";
        } else if (obj instanceof WrappedFile || obj instanceof File) {
            if (!allowFiles) {
                return prepend + "[Body is file based: " + obj + "]";
            }
        }
    }
    if (!allowFiles) {
        if (obj instanceof WrappedFile || obj instanceof File) {
            return prepend + "[Body is file based: " + obj + "]";
        }
    }
    // is the body a stream cache or input stream
    StreamCache cache = null;
    InputStream is = null;
    if (obj instanceof StreamCache) {
        cache = (StreamCache) obj;
        is = null;
    } else if (obj instanceof InputStream) {
        cache = null;
        is = (InputStream) obj;
    }
    // grab the message body as a string
    String body = null;
    if (message.getExchange() != null) {
        try {
            body = message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class, message.getExchange(), obj);
        } catch (Throwable e) {
        // ignore as the body is for logging purpose
        }
    }
    if (body == null) {
        try {
            body = obj.toString();
        } catch (Throwable e) {
        // ignore as the body is for logging purpose
        }
    }
    // reset stream cache after use
    if (cache != null) {
        cache.reset();
    } else if (is != null && is.markSupported()) {
        try {
            is.reset();
        } catch (IOException e) {
        // ignore
        }
    }
    if (body == null) {
        return prepend + "[Body is null]";
    }
    // clip body if length enabled and the body is too big
    if (maxChars > 0 && body.length() > maxChars) {
        body = body.substring(0, maxChars) + "... [Body clipped after " + maxChars + " chars, total length is " + body.length() + "]";
    }
    return prepend + body;
}
Also used : BytesSource(org.apache.camel.BytesSource) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) IOException(java.io.IOException) Source(javax.xml.transform.Source) BytesSource(org.apache.camel.BytesSource) StringSource(org.apache.camel.StringSource) StreamCache(org.apache.camel.StreamCache) WrappedFile(org.apache.camel.WrappedFile) StringSource(org.apache.camel.StringSource) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) Writer(java.io.Writer)

Example 33 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class CachedOutputStreamTest method testCacheStreamToMemory.

public void testCacheStreamToMemory() throws Exception {
    context.getStreamCachingStrategy().setSpoolThreshold(1024);
    context.start();
    CachedOutputStream cos = new CachedOutputStream(exchange);
    cos.write(TEST_STRING.getBytes("UTF-8"));
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("we should have no temp file", 0, files.length);
    StreamCache cache = cos.newStreamCache();
    assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache);
    String temp = IOConverter.toString((InputStream) cache, null);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    IOHelper.close(cos);
}
Also used : StreamCache(org.apache.camel.StreamCache) File(java.io.File)

Example 34 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class CachedOutputStreamTest method testCacheStreamToFileCloseStreamBeforeDone.

public void testCacheStreamToFileCloseStreamBeforeDone() throws Exception {
    context.start();
    CachedOutputStream cos = new CachedOutputStream(exchange);
    cos.write(TEST_STRING.getBytes("UTF-8"));
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    assertTrue("The file name should start with cos", files[0].startsWith("cos"));
    StreamCache cache = cos.newStreamCache();
    assertTrue("Should get the FileInputStreamCache", cache instanceof FileInputStreamCache);
    String temp = toString((InputStream) cache);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    cache.reset();
    temp = toString((InputStream) cache);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    ((InputStream) cache).close();
    files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    exchange.getUnitOfWork().done(exchange);
    files = file.list();
    assertEquals("we should have no temp file", 0, files.length);
    IOHelper.close(cos);
}
Also used : StreamCache(org.apache.camel.StreamCache) InputStream(java.io.InputStream) File(java.io.File)

Example 35 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class CachedOutputStreamTest method testCacheStreamToMemoryAsDiskIsDisabled.

public void testCacheStreamToMemoryAsDiskIsDisabled() throws Exception {
    // -1 disables disk based cache
    context.getStreamCachingStrategy().setSpoolThreshold(-1);
    context.start();
    CachedOutputStream cos = new CachedOutputStream(exchange);
    cos.write(TEST_STRING.getBytes("UTF-8"));
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("we should have no temp file", 0, files.length);
    StreamCache cache = cos.newStreamCache();
    assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache);
    String temp = IOConverter.toString((InputStream) cache, null);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    exchange.getUnitOfWork().done(exchange);
    IOHelper.close(cos);
}
Also used : StreamCache(org.apache.camel.StreamCache) File(java.io.File)

Aggregations

StreamCache (org.apache.camel.StreamCache)38 InputStream (java.io.InputStream)14 IOException (java.io.IOException)10 File (java.io.File)7 Exchange (org.apache.camel.Exchange)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ArrayList (java.util.ArrayList)5 StreamSource (javax.xml.transform.stream.StreamSource)5 KeyValueAnnotation (com.github.kristofa.brave.KeyValueAnnotation)4 StringReader (java.io.StringReader)4 OutputStream (java.io.OutputStream)3 SAXSource (javax.xml.transform.sax.SAXSource)3 Message (org.apache.camel.Message)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)3 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)3 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 Writer (java.io.Writer)2 Source (javax.xml.transform.Source)2