You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/09/11 10:30:42 UTC

svn commit: r813717 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/util/ test/java/org/apache/camel/impl/ test/java/org/apache/camel/management/

Author: davsclaus
Date: Fri Sep 11 08:30:41 2009
New Revision: 813717

URL: http://svn.apache.org/viewvc?rev=813717&view=rev
Log:
CAMEL-1995: Streams are by default not logged to avoid causing the stream to be read and then later not possible to read it again. There is a flag to control this behavior on camel context properties. Fixed CS.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyStreamsTest.java
      - copied, changed from r813675, camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextStopFailureTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/EventNotifierServiceStoppingFailedEventTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=813717&r1=813716&r2=813717&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Fri Sep 11 08:30:41 2009
@@ -81,6 +81,7 @@
     String INTERCEPTED_ENDPOINT = "CamelInterceptedEndpoint";
 
     String LOG_DEBUG_BODY_MAX_CHARS = "CamelLogDebugBodyMaxChars";
+    String LOG_DEBUG_BODY_STREAMS   = "CamelLogDebugStreams";
     String LOOP_INDEX               = "CamelLoopIndex";
     String LOOP_SIZE                = "CamelLoopSize";
 

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=813717&r1=813716&r2=813717&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 Fri Sep 11 08:30:41 2009
@@ -16,9 +16,17 @@
  */
 package org.apache.camel.util;
 
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import javax.xml.transform.stream.StreamSource;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.StreamCache;
+import org.apache.camel.converter.jaxp.BytesSource;
+import org.apache.camel.converter.jaxp.StringSource;
 
 /**
  * Some helper methods when working with {@link org.apache.camel.Message}.
@@ -123,6 +131,34 @@
      * @return the logging message
      */
     public static String extractBodyForLogging(Message message) {
+        Object obj = message.getBody();
+        if (obj == null) {
+            return "Message: [Body is null]";
+        }
+
+        // do not log streams by default
+        boolean streams = false;
+        if (message.getExchange() != null) {
+            String property = message.getExchange().getContext().getProperties().get(Exchange.LOG_DEBUG_BODY_STREAMS);
+            if (property != null) {
+                streams = message.getExchange().getContext().getTypeConverter().convertTo(Boolean.class, property);
+            }
+        }
+
+        if (obj instanceof StringSource || obj instanceof BytesSource) {
+            // these two are okay
+        } else if (!streams && obj instanceof StreamSource) {
+            return "Message: [Body is instance of java.xml.transform.StreamSource]";
+        } else if (!streams && obj instanceof InputStream) {
+            return "Message: [Body is instance of java.io.InputStream]";
+        } else if (!streams && obj instanceof OutputStream) {
+            return "Message: [Body is instance of java.io.OutputStream]";
+        } else if (!streams && obj instanceof Reader) {
+            return "Message: [Body is instance of java.io.Reader]";
+        } else if (!streams && obj instanceof Writer) {
+            return "Message: [Body is instance of java.io.Writer]";
+        }
+
         // default to 1000 chars
         int length = 1000;
 
@@ -133,11 +169,6 @@
             }
         }
 
-        Object obj = message.getBody();
-        if (obj == null) {
-            return "Message: [Body is null]";
-        }
-
         String body = obj.toString();
         if (body == null) {
             return "Message: [Body is null]";
@@ -150,4 +181,5 @@
 
         return "Message: " + body;
     }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextStopFailureTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextStopFailureTest.java?rev=813717&r1=813716&r2=813717&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextStopFailureTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextStopFailureTest.java Fri Sep 11 08:30:41 2009
@@ -44,7 +44,7 @@
         assertEquals("CBA", stopOrder);
     }
 
-    private class MyService implements Service {
+    private final class MyService implements Service {
 
         private String name;
         private boolean fail;

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyStreamsTest.java (from r813675, camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyStreamsTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyStreamsTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java&r1=813675&r2=813717&rev=813717&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyStreamsTest.java Fri Sep 11 08:30:41 2009
@@ -16,30 +16,41 @@
  */
 package org.apache.camel.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.jaxp.StringSource;
 
 /**
  * @version $Revision$
  */
-public class LogDebugBodyMaxCharsTest extends ContextTestSupport {
+public class LogDebugBodyStreamsTest extends ContextTestSupport {
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        context.getProperties().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "20");
+    public void testLogBodyStreamStringSourceDisabled() throws Exception {
+        context.getProperties().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
+        String msg = mock.getReceivedExchanges().get(0).getIn().toString();
+        assertEquals("Message: StringSource[<?xml version=\"1.0\"?><person><name>Claus</name></person>]", msg);
     }
 
-    public void testLogBodyMaxLengthTest() throws Exception {
-        // create a big body
-        StringBuffer sb = new StringBuffer();
-        for (int i = 0; i < 1000; i++) {
-            int value = i % 10;
-            sb.append(value);
-        }
-        String body = sb.toString();
+    public void testLogBodyStreamStringSourceDisabledByDefault() throws Exception {
+        context.getProperties().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);
@@ -48,28 +59,78 @@
 
         assertMockEndpointsSatisfied();
 
-        // should be clipped after 20 chars
+        // should be logged anyway
         String msg = mock.getReceivedExchanges().get(0).getIn().toString();
-        assertEquals("Message: 01234567890123456789... [Body clipped after 20 chars, total length is 1000]", msg);
+        assertEquals("Message: StringSource[<?xml version=\"1.0\"?><person><name>Claus</name></person>]", msg);
+    }
+
+    public void testLogBodyStreamStringSourceEnabled() throws Exception {
+        context.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");
+
+        StringSource body = new StringSource("<?xml version=\"1.0\"?><person><name>Claus</name></person>");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
 
-        // but body and clipped should not be the same
-        assertNotSame("clipped log and real body should not be the same", msg, mock.getReceivedExchanges().get(0).getIn().getBody(String.class));
+        template.sendBody("direct:start", body);
+
+        assertMockEndpointsSatisfied();
+
+        // should be logged anyway
+        String msg = mock.getReceivedExchanges().get(0).getIn().toString();
+        assertEquals("Message: StringSource[<?xml version=\"1.0\"?><person><name>Claus</name></person>]", msg);
     }
 
-    public void tesNotClipped() throws Exception {
+    public void testLogBodyStreamDisabled() throws Exception {
+        context.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "false");
+        
+        InputStream body = new ByteArrayInputStream("Hello World".getBytes());
+
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
 
-        template.sendBody("direct:start", "1234567890");
+        template.sendBody("direct:start", body);
+
+        assertMockEndpointsSatisfied();
+
+        // should NOT be logged
+        String msg = mock.getReceivedExchanges().get(0).getIn().toString();
+        assertEquals("Message: [Body is instance of java.io.InputStream]", msg);
+    }
+
+    public void testLogBodyStreamDisabledByDefault() throws Exception {
+        context.getProperties().remove(Exchange.LOG_DEBUG_BODY_STREAMS);
+
+        InputStream body = new ByteArrayInputStream("Hello World".getBytes());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", body);
 
         assertMockEndpointsSatisfied();
 
-        // should be clipped after 20 chars
+        // should NOT be logged
         String msg = mock.getReceivedExchanges().get(0).getIn().toString();
-        assertEquals("Message: 01234567890", msg);
+        assertEquals("Message: [Body is instance of java.io.InputStream]", msg);
+    }
+
+    public void testLogBodyStreamEnabled() throws Exception {
+        context.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");
+
+        InputStream body = new ByteArrayInputStream("Hello World".getBytes());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", body);
 
-        // but body and clipped should not be the same
-        assertNotSame("clipped log and real body should not be the same", msg, mock.getReceivedExchanges().get(0).getIn().getBody(String.class));
+        assertMockEndpointsSatisfied();
+
+        // should be logged
+        String msg = mock.getReceivedExchanges().get(0).getIn().toString();
+        assertNotSame("Message: [Body is instance of java.io.InputStream]", msg);
+        assertIsInstanceOf(InputStream.class, mock.getReceivedExchanges().get(0).getIn().getBody());
     }
 
     @Override
@@ -81,4 +142,4 @@
             }
         };
     }
-}
+}
\ No newline at end of file

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/management/EventNotifierServiceStoppingFailedEventTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/EventNotifierServiceStoppingFailedEventTest.java?rev=813717&r1=813716&r2=813717&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/EventNotifierServiceStoppingFailedEventTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/EventNotifierServiceStoppingFailedEventTest.java Fri Sep 11 08:30:41 2009
@@ -82,7 +82,7 @@
         assertEquals("Failure to stop service: B due to Fail B", event.toString());
     }
 
-    private class MyService implements Service {
+    private final class MyService implements Service {
 
         private String name;
         private boolean fail;