Author: davsclaus Date: Tue Apr 16 13:12:01 2013 New Revision: 1468408 URL: http://svn.apache.org/r1468408 Log: CAMEL-6133: Avoid touch Source bodies in message helper logger.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/SAXSourceLogBodyTest.java camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xml Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java?rev=1468408&r1=1468407&r2=1468408&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java Tue Apr 16 13:12:01 2013 @@ -23,6 +23,7 @@ import java.io.Reader; import java.io.Writer; import java.util.Map; import java.util.TreeMap; +import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import org.apache.camel.BytesSource; @@ -185,9 +186,8 @@ public final class MessageHelper { * @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 - * @param maxChars limit to maximum number of chars. Use 0 or negative value - * to not limit at all. + * @param allowFiles whether or not files is allowed (currently not in use) + * @param maxChars limit to maximum number of chars. Use 0 or negative value to not limit at all. * @return the logging message */ public static String extractBodyForLogging(Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars) { @@ -197,12 +197,10 @@ public final class MessageHelper { } if (!allowStreams) { - if (obj instanceof StreamSource && !(obj instanceof StringSource || obj instanceof BytesSource)) { - /* - * Generally do not log StreamSources but as StringSource and - * ByteSource are memory based they are ok - */ - return prepend + "[Body is instance of java.xml.transform.StreamSource]"; + if (obj instanceof Source && !(obj instanceof StringSource || obj instanceof BytesSource)) { + // for Source its only StringSource or BytesSource that is okay as they are memory based + // 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) { Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/SAXSourceLogBodyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/SAXSourceLogBodyTest.java?rev=1468408&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/SAXSourceLogBodyTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/SAXSourceLogBodyTest.java Tue Apr 16 13:12:01 2013 @@ -0,0 +1,70 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.xslt; + +import java.io.File; +import java.io.InputStream; +import javax.xml.transform.sax.SAXSource; + +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.LoggingLevel; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class SAXSourceLogBodyTest extends ContextTestSupport { + + public void testSAXSource() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + + template.sendBody("direct:start", new File("src/test/resources/xslt/staff/staff.xml")); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").streamCaching() + // attach a SaxSource to body + .process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + byte[] data = exchange.getIn().getBody(byte[].class); + InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, data); + XMLReader xmlReader = XMLReaderFactory.createXMLReader(); + exchange.getIn().setBody(new SAXSource(xmlReader, new InputSource(is))); + } + }) + // The ${body} will toString the body and print it, so we need to enable stream caching + .log(LoggingLevel.WARN, "${body}") + .to("xslt:xslt/common/staff_template.xsl") + .to("log:result") + .to("mock:result"); + } + }; + } +} Added: camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xml URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xml?rev=1468408&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xml (added) +++ camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xml Tue Apr 16 13:12:01 2013 @@ -0,0 +1,60 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<staff> + + <programmer> + <name>Bugs Bunny</name> + <dob>03/21/1970</dob> + <age>31</age> + <address>4895 Wabbit Hole Road</address> + <phone>865-111-1111</phone> + </programmer> + + <programmer> + <name>Daisy Duck</name> + <dob>08/09/1949</dob> + <age>51</age> + <address>748 Golden Pond</address> + <phone>865-222-2222</phone> + </programmer> + + <programmer> + <name>Minnie Mouse</name> + <dob>04/13/1977</dob> + <age>24</age> + <address>4064 Cheese Factory Blvd</address> + <phone>865-333-3333</phone> + </programmer> + + <programmer> + <name>Pluto</name> + <dob>07/04/1979</dob> + <age>21</age> + <address>414 Dog Lane</address> + <phone>865-333-3333</phone> + </programmer> + + <programmer> + <name>Road Runner</name> + <dob>01/19/1953</dob> + <age>48</age> + <address>135 Desert View Street</address> + <phone>none</phone> + </programmer> + +</staff> \ No newline at end of file