Search in sources :

Example 6 with BytesSource

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

the class XmlBeansConverterTest method toXmlObjectFromSource.

@Test
public void toXmlObjectFromSource() throws Exception {
    XmlObject result = XmlBeansConverter.toXmlObject(new BytesSource(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext()));
    assertBuyStocks(result);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) BytesSource(org.apache.camel.BytesSource) XmlObject(org.apache.xmlbeans.XmlObject) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 7 with BytesSource

use of org.apache.camel.BytesSource 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 8 with BytesSource

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

the class BytesSourceTest method testBytesSourceCtrSystemId.

public void testBytesSourceCtrSystemId() {
    BytesSource bs = new BytesSource("foo".getBytes(), "Camel");
    assertNotNull(bs.getData());
    assertEquals("BytesSource[foo]", bs.toString());
    assertEquals("Camel", bs.getSystemId());
    assertNotNull(bs.getInputStream());
    assertNotNull(bs.getReader());
}
Also used : BytesSource(org.apache.camel.BytesSource)

Example 9 with BytesSource

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

the class XmlConverter method toByteArray.

/**
     * Converts the given input Source into bytes
     */
@Converter
public byte[] toByteArray(Source source, Exchange exchange) throws TransformerException {
    if (source instanceof BytesSource) {
        return ((BytesSource) source).getData();
    } else {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        if (exchange != null) {
            // check the camelContext properties first
            Properties properties = ObjectHelper.getCamelPropertiesWithPrefix(OUTPUT_PROPERTIES_PREFIX, exchange.getContext());
            if (properties.size() > 0) {
                toResult(source, new StreamResult(buffer), properties);
                return buffer.toByteArray();
            }
        }
        // using the old way to deal with it
        toResult(source, new StreamResult(buffer));
        return buffer.toByteArray();
    }
}
Also used : BytesSource(org.apache.camel.BytesSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) Converter(org.apache.camel.Converter)

Aggregations

BytesSource (org.apache.camel.BytesSource)9 StringSource (org.apache.camel.StringSource)3 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 StreamResult (javax.xml.transform.stream.StreamResult)2 Converter (org.apache.camel.Converter)2 StreamCache (org.apache.camel.StreamCache)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Source (javax.xml.transform.Source)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 Exchange (org.apache.camel.Exchange)1 WrappedFile (org.apache.camel.WrappedFile)1 DefaultLSResourceResolver (org.apache.camel.component.validator.DefaultLSResourceResolver)1 XmlSignatureException (org.apache.camel.component.xmlsecurity.api.XmlSignatureException)1