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:09 UTC

svn commit: r708120 - in /servicemix/components/engines/servicemix-camel/trunk/src: main/java/org/apache/servicemix/camel/ test/java/org/apache/servicemix/camel/

Author: gertv
Date: Mon Oct 27 02:59:08 2008
New Revision: 708120

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

Added:
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java
Modified:
    servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java
    servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java

Modified: servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java?rev=708120&r1=708119&r2=708120&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java (original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java Mon Oct 27 02:59:08 2008
@@ -59,11 +59,15 @@
     }
 
     public Source convertBodyToJbi(Exchange exchange, Object body) {
-        try {
-           return ExchangeHelper.convertToType(exchange, Source.class, body);
-        } catch (NoTypeConversionAvailableException e) {
-           LOG.warn("Unable to convert " + body.getClass() + " to an XML Source, value will be null");
-           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/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java?rev=708120&r1=708119&r2=708120&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java (original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiMessage.java Mon Oct 27 02:59:08 2008
@@ -155,13 +155,13 @@
 //    @Override
     public void setBody(Object body) {
         if (normalizedMessage != null) {
-            if (!(body instanceof Source)) {
-                body = getExchange().getBinding().convertBodyToJbi(getExchange(), body);
-            }
-            try {
-                normalizedMessage.setContent((Source) body);
-            } catch (MessagingException e) {
-                throw new JbiException(e);
+            Source source = getExchange().getBinding().convertBodyToJbi(getExchange(), body);
+            if (source != null) {
+                try {
+                    normalizedMessage.setContent(source);
+                } catch (MessagingException e) {
+                    throw new JbiException(e);
+                }
             }
         }
         super.setBody(body);

Modified: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java?rev=708120&r1=708119&r2=708120&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java (original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyTest.java Mon Oct 27 02:59:08 2008
@@ -91,6 +91,4 @@
             
         };
     }
-    
-
 }

Added: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java?rev=708120&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java (added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyToCamelObjectTest.java Mon Oct 27 02:59:08 2008
@@ -0,0 +1,101 @@
+/*
+ * 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.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOut;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.camel.Converter;
+import org.apache.camel.Processor;
+import org.apache.camel.Exchange;
+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 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/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java?rev=708120&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java (added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiToCamelCbrTest.java Mon Oct 27 02:59:08 2008
@@ -0,0 +1,127 @@
+/*
+ * 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.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.List;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessagingException;
+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;
+
+import sun.security.action.GetLongAction;
+
+/**
+ * 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 = null;
+    
+    @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");
+            }
+        };
+    }    
+
+}