Search in sources :

Example 11 with StringWriter

use of java.io.StringWriter in project camel by apache.

the class XmlConverter method toString.

/**
     * Converts the given input Source into text
     */
@Converter
public String toString(Source source, Exchange exchange) throws TransformerException {
    if (source == null) {
        return null;
    } else if (source instanceof StringSource) {
        return ((StringSource) source).getText();
    } else if (source instanceof BytesSource) {
        return new String(((BytesSource) source).getData());
    } else {
        StringWriter buffer = new StringWriter();
        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.toString();
            }
        }
        // using the old way to deal with it
        toResult(source, new StreamResult(buffer));
        return buffer.toString();
    }
}
Also used : BytesSource(org.apache.camel.BytesSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StringSource(org.apache.camel.StringSource) Properties(java.util.Properties) Converter(org.apache.camel.Converter)

Example 12 with StringWriter

use of java.io.StringWriter in project camel by apache.

the class LogCustomLoggerTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    sw1 = new StringWriter();
    sw2 = new StringWriter();
    ConsumingAppender.newAppender(LogCustomLoggerTest.class.getCanonicalName(), "LogCustomLoggerTest", Level.TRACE, event -> sw1.append(event.getLoggerName()));
    ConsumingAppender.newAppender("provided.logger1.name", "logger1", Level.TRACE, event -> sw1.append(event.getLoggerName()));
    ConsumingAppender.newAppender("provided.logger2.name", "logger2", Level.TRACE, event -> sw1.append(event.getLoggerName()));
    ConsumingAppender.newAppender("irrelevant.logger.name", "irrelevant", Level.TRACE, event -> sw1.append(event.getLoggerName()));
    ConsumingAppender.newAppender(LogComponent.class.getCanonicalName(), "LogComponent", Level.TRACE, event -> sw2.append(event.getLoggerName()));
}
Also used : StringWriter(java.io.StringWriter) Before(org.junit.Before)

Example 13 with StringWriter

use of java.io.StringWriter in project camel by apache.

the class LogProcessorWithProvidedLoggerTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    sw = new StringWriter();
    ConsumingAppender.newAppender("org.apache.camel.customlogger", "customlogger", Level.TRACE, event -> sw.append(event.getLoggerName() + " " + event.getLevel().toString() + " " + event.getMessage().getFormattedMessage()));
}
Also used : StringWriter(java.io.StringWriter) Before(org.junit.Before)

Example 14 with StringWriter

use of java.io.StringWriter in project camel by apache.

the class DataFormatConcurrentTest method createPayloads.

/**
     * the individual size of one record is:
     * fooBarSize = 1  -> 104 bytes
     * fooBarSize = 50 -> 2046 bytes
     * @return the payloads used for this stress test
     * @throws Exception
     */
public ByteArrayInputStream[] createPayloads(int testCount) throws Exception {
    Foo foo = new Foo();
    for (int x = 0; x < fooBarSize; x++) {
        Bar bar = new Bar();
        bar.setName("Name: " + x);
        bar.setValue("value: " + x);
        foo.getBarRefs().add(bar);
    }
    Marshaller m = JAXBContext.newInstance(Foo.class, Bar.class).createMarshaller();
    StringWriter writer = new StringWriter();
    m.marshal(foo, writer);
    byte[] payload = writer.toString().getBytes();
    ByteArrayInputStream[] streams = new ByteArrayInputStream[testCount];
    for (int i = 0; i < testCount; i++) {
        streams[i] = new ByteArrayInputStream(payload);
    }
    return streams;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 15 with StringWriter

use of java.io.StringWriter in project camel by apache.

the class NoClassDefFoundErrorWrapExceptionTest method testNoClassDef.

@Test
public void testNoClassDef() throws Exception {
    try {
        template.requestBody("activemq:start?transferException=true", "Hello World");
        fail("Should throw exception");
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String s = sw.toString();
        assertTrue(s.contains("java.lang.LinkageError"));
        assertTrue(s.contains("Cannot do this"));
        assertTrue(s.contains("org.apache.camel.component.jms.issues.ProcessorFail.process"));
    }
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)3175 PrintWriter (java.io.PrintWriter)1057 Test (org.junit.Test)612 IOException (java.io.IOException)516 StringReader (java.io.StringReader)232 Writer (java.io.Writer)211 StreamResult (javax.xml.transform.stream.StreamResult)207 File (java.io.File)194 InputStreamReader (java.io.InputStreamReader)140 HashMap (java.util.HashMap)136 Transformer (javax.xml.transform.Transformer)125 InputStream (java.io.InputStream)119 Map (java.util.Map)116 ArrayList (java.util.ArrayList)106 DOMSource (javax.xml.transform.dom.DOMSource)99 BufferedReader (java.io.BufferedReader)96 ByteArrayInputStream (java.io.ByteArrayInputStream)84 Reader (java.io.Reader)77 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)75 HttpServletResponse (javax.servlet.http.HttpServletResponse)73