You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/10/27 10:59:23 UTC

svn commit: r708121 - in /servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src: main/java/org/apache/servicemix/camel/ test/java/org/apache/servicemix/camel/

Author: gertv
Date: Mon Oct 27 02:59:23 2008
New Revision: 708121

URL: http://svn.apache.org/viewvc?rev=708121&view=rev
Log:
SM-1654: Allow for non-xml message body inside Camel route

Added:
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java
Modified:
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java

Modified: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java?rev=708121&r1=708120&r2=708121&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java Mon Oct 27 02:59:23 2008
@@ -38,6 +38,8 @@
 import org.apache.camel.Message;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * The binding of how Camel messages get mapped to JBI and back again
@@ -45,6 +47,9 @@
  * @version $Revision: 563665 $
  */
 public class JbiBinding {
+
+    private static final Log LOG = LogFactory.getLog(JbiBinding.class);
+
     private String messageExchangePattern;
 
     /**
@@ -56,10 +61,15 @@
     }
 
     public Source convertBodyToJbi(Exchange exchange, Object body) {
-        try {
-            return ExchangeHelper.convertToType(exchange, Source.class, body);    
-        } catch (NoTypeConversionAvailableException e) {
-            return null;
+        if (body instanceof Source) {
+            return (Source) body;
+        } else {
+            try {
+                return ExchangeHelper.convertToType(exchange, Source.class, body);
+            } catch (NoTypeConversionAvailableException e) {
+                LOG.warn("Unable to convert message body of type " + body.getClass() + " into an XML Source");
+                return null;
+            }
         }
     }
 

Modified: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java?rev=708121&r1=708120&r2=708121&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java Mon Oct 27 02:59:23 2008
@@ -51,7 +51,7 @@
         }
     }
 
-    public JbiExchange getJbiExchange() {
+    public JbiExchange getExchange() {
         return (JbiExchange) super.getExchange();
     }
 
@@ -122,7 +122,7 @@
     @Override
     protected Object createBody() {
         if (normalizedMessage != null) {
-            JbiExchange jbiExchange = getJbiExchange();
+            JbiExchange jbiExchange = getExchange();
             if (jbiExchange != null) {
                 return jbiExchange.getBinding().extractBodyFromJbi(jbiExchange, normalizedMessage);
             }
@@ -157,17 +157,14 @@
 //    @Override
     public void setBody(Object body) {
         if (normalizedMessage != null) {
-            if (!(body instanceof Source)) {
-                JbiExchange jbiExchange = getJbiExchange();
-                if (jbiExchange != null) {
-                    body = jbiExchange.getBinding().convertBodyToJbi(jbiExchange, body);
+            Source source = getExchange().getBinding().convertBodyToJbi(getExchange(), body);
+            if (source != null) {
+                try {
+                    normalizedMessage.setContent(source);
+                } catch (MessagingException e) {
+                    throw new JbiException(e);
                 }
-            }
-            try {
-                normalizedMessage.setContent((Source) body);
-            } catch (MessagingException e) {
-                throw new JbiException(e);
-            }
+            }        
         }
         super.setBody(body);
     }

Modified: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java?rev=708121&r1=708120&r2=708121&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java Mon Oct 27 02:59:23 2008
@@ -64,6 +64,4 @@
             
         };
     }
-    
-
 }

Added: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java?rev=708121&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java Mon Oct 27 02:59:23 2008
@@ -0,0 +1,98 @@
+/*
+ * 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.servicemix.camel;
+
+import java.util.List;
+
+import javax.jbi.messaging.InOnly;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.jbi.container.ActivationSpec;
+
+/**
+ * Tests on handling JBI InOnly exchanges by Camel
+ * The tests here try to convert to a non-xml body 
+ */
+public class JbiInOnlyToCamelObjectTest extends JbiTestSupport {
+    
+    private static final String MESSAGE = "<just><a>test</a></just>";
+
+    public void testInOnlyExchangeConvertBody() throws Exception {
+        MockEndpoint done = getMockEndpoint("mock:done");
+        done.expectedBodiesReceived(new MessageContainer(MESSAGE));
+        
+        ServiceMixClient client = new DefaultServiceMixClient(jbiContainer);
+        InOnly exchange = client.createInOnlyExchange();
+        exchange.setService(new QName("urn:test", "in-only"));
+        exchange.getInMessage().setContent(new StringSource(MESSAGE));
+        client.send(exchange);
+        
+        done.assertIsSatisfied();
+    }
+
+    @Override
+    protected void appendJbiActivationSpecs(List<ActivationSpec> activationSpecList) {
+        // no additional activation specs required
+    }
+
+    @Override
+    protected RouteBuilder createRoutes() {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                from("jbi:service:urn:test:in-only").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        String message = exchange.getIn().getBody(String.class);
+                        exchange.getOut().setBody(new MessageContainer(message));
+                    }
+                }).to("mock:done");
+            }
+            
+        };
+    }
+        
+    public static final class MessageContainer {
+        
+        private String message;
+
+        private MessageContainer(String message) {
+            this.message = message;
+        }
+        
+        @Override
+        public int hashCode() {
+            return message.hashCode();
+        }
+        
+        @Override
+        public boolean equals(Object arg0) {
+            if (arg0 instanceof MessageContainer) {
+                return ((MessageContainer) arg0).message.equals(message);
+            }
+            return super.equals(arg0);
+        }
+        
+    }
+}

Added: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java?rev=708121&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java Mon Oct 27 02:59:23 2008
@@ -0,0 +1,121 @@
+/*
+ * 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.servicemix.camel;
+
+import java.io.StringReader;
+import java.util.List;
+
+import javax.jbi.messaging.InOnly;
+import javax.xml.namespace.QName;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.jbi.container.ActivationSpec;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+
+/**
+ * Tests on correct handling of several XML Source implementations being sent by ServiceMix to Camel  
+ */
+public class JbiToCamelCbrTest extends JbiTestSupport {
+    
+    private static final String MESSAGE_IN_FRENCH = "<message>bonjour</message>";
+    private static final String MESSAGE_IN_ENGLISH = "<message>hello</message>";
+    private final SourceTransformer transformer = new SourceTransformer();
+    private Level level;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        // let's disable DEBUG logging for ServiceMix or all message content will be DOMSource anyway
+        if (level == null) {
+            level = Logger.getLogger("org.apache.servicemix").getLevel();
+            Logger.getLogger("org.apache.servicemix").setLevel(Level.INFO);
+        }
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        // restore the original log level
+        if (level != null) {
+            Logger.getLogger("org.apache.servicemix").setLevel(level);
+            level = null;
+        }
+    }
+
+    public void testCbrWithStringSource() throws Exception {
+        doTestCbr(new StringSource(MESSAGE_IN_FRENCH),
+                  new StringSource(MESSAGE_IN_ENGLISH));
+    }
+    
+    public void testCbrWithStreamSource() throws Exception {
+        doTestCbr(new StreamSource(new StringReader(MESSAGE_IN_FRENCH)),
+                  new StreamSource(new StringReader(MESSAGE_IN_ENGLISH)));
+    }
+    
+    public void testCbrWithDomSource() throws Exception {
+        doTestCbr(transformer.toDOMSource(new StringSource(MESSAGE_IN_FRENCH)),
+                  transformer.toDOMSource(new StringSource(MESSAGE_IN_ENGLISH)));
+    }
+    
+    public void doTestCbr(Source... bodies) throws Exception {
+        MockEndpoint french = getMockEndpoint("mock:french");
+        french.expectedMessageCount(1);
+        MockEndpoint english = getMockEndpoint("mock:english");
+        english.expectedMessageCount(1);
+        
+        ServiceMixClient client = new DefaultServiceMixClient(jbiContainer);
+        for (Source body : bodies) {
+            InOnly exchange = client.createInOnlyExchange();
+            exchange.setService(new QName("urn:test", "polyglot"));
+            
+            exchange.getInMessage().setContent(body);
+            client.sendSync(exchange);
+        }
+        
+        french.assertIsSatisfied();
+        english.assertIsSatisfied();
+    }
+    
+    @Override
+    protected void appendJbiActivationSpecs(List<ActivationSpec> activationSpecList) {
+        // no additional activation specs required
+    }
+
+    @Override
+    protected RouteBuilder createRoutes() {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                streamCaching(); // remove streamCaching and the conversion to String once we use Camel 1.5
+                from("jbi:service:urn:test:polyglot").streamCaching().convertBodyTo(String.class)
+                    .choice()
+                        .when().xpath("/message/text() = 'bonjour'").to("mock:french")
+                        .when().xpath("/message/text() = 'hello'").to("mock:english");
+            }
+        };
+    }    
+
+}