Search in sources :

Example 11 with StringSource

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

the class JmsXMLRouteTest method testLondonWithStringSourceAsObject.

@Test
public void testLondonWithStringSourceAsObject() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:london");
    mock.expectedMessageCount(1);
    mock.message(0).body(String.class).contains("James");
    Source source = new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<person user=\"james\">\n" + "  <firstName>James</firstName>\n" + "  <lastName>Strachan</lastName>\n" + "  <city>London</city>\n" + "</person>");
    assertNotNull(source);
    template.sendBody("direct:object", source);
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StringSource(org.apache.camel.StringSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StringSource(org.apache.camel.StringSource) Test(org.junit.Test)

Example 12 with StringSource

use of org.apache.camel.StringSource 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 13 with StringSource

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

the class StringSourceTest method testSerialization.

public void testSerialization() throws Exception {
    StringSource expected = new StringSource(expectedBody, "mySystemID", "utf-8");
    expected.setPublicId("myPublicId");
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream output = new ObjectOutputStream(buffer);
    output.writeObject(expected);
    output.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Object object = in.readObject();
    assertTrue("is a StringSource", object instanceof StringSource);
    StringSource actual = (StringSource) object;
    assertEquals("source.text", expected.getPublicId(), actual.getPublicId());
    assertEquals("source.text", expected.getSystemId(), actual.getSystemId());
    assertEquals("source.text", expected.getEncoding(), actual.getEncoding());
    assertEquals("source.text", expected.getText(), actual.getText());
    String value = converter.convertTo(String.class, actual);
    assertEquals("text value of StringSource", expectedBody, value);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StringSource(org.apache.camel.StringSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 14 with StringSource

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

the class LogDebugBodyStreamsTest method testLogBodyStreamStringSourceDisabled.

public void testLogBodyStreamStringSourceDisabled() throws Exception {
    context.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_STREAMS, "false");
    StringSource body = new StringSource("<?xml version=\"1.0\"?><person><name>Claus</name></person>");
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBody("direct:start", body);
    assertMockEndpointsSatisfied();
    // should be logged anyway
    TraceExchangeFormatter myFormatter = context.getRegistry().lookupByNameAndType("logFormatter", TraceExchangeFormatter.class);
    String msg = myFormatter.getMessage();
    assertTrue(msg.endsWith("Body: <?xml version=\"1.0\"?><person><name>Claus</name></person>]"));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StringSource(org.apache.camel.StringSource)

Example 15 with StringSource

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

the class LogDebugBodyStreamsTest method testLogBodyStreamStringSourceDisabledByDefault.

public void testLogBodyStreamStringSourceDisabledByDefault() throws Exception {
    context.getGlobalOptions().remove(Exchange.LOG_DEBUG_BODY_STREAMS);
    StringSource body = new StringSource("<?xml version=\"1.0\"?><person><name>Claus</name></person>");
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBody("direct:start", body);
    assertMockEndpointsSatisfied();
    // should be logged anyway
    TraceExchangeFormatter myFormatter = context.getRegistry().lookupByNameAndType("logFormatter", TraceExchangeFormatter.class);
    String msg = myFormatter.getMessage();
    assertTrue(msg.endsWith("Body: <?xml version=\"1.0\"?><person><name>Claus</name></person>]"));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StringSource(org.apache.camel.StringSource)

Aggregations

StringSource (org.apache.camel.StringSource)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 Source (javax.xml.transform.Source)6 Test (org.junit.Test)5 StreamSource (javax.xml.transform.stream.StreamSource)4 File (java.io.File)3 BytesSource (org.apache.camel.BytesSource)3 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 Converter (org.apache.camel.Converter)2 StreamCache (org.apache.camel.StreamCache)2 WrappedFile (org.apache.camel.WrappedFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1