You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/05/11 03:48:18 UTC

svn commit: r773447 - in /camel/branches/camel-1.x: ./ camel-core/src/main/java/org/apache/camel/processor/Splitter.java camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java

Author: ningjiang
Date: Mon May 11 01:48:18 2009
New Revision: 773447

URL: http://svn.apache.org/viewvc?rev=773447&view=rev
Log:
Merged revisions 773446 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r773446 | ningjiang | 2009-05-11 09:29:15 +0800 (Mon, 11 May 2009) | 1 line
  
  CAMEL-1597 Added support of splitting the Message with header and body
........

Added:
    camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java   (with props)
Modified:
    camel/branches/camel-1.x/   (props changed)
    camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/processor/Splitter.java

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon May 11 01:48:18 2009
@@ -1 +1 @@
-/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551,765154,765686,765729,765743,765824,766016,766289,766584,766588,766590,766602,766673,767403,767824,768342,769239,769346,769368,769434,770172,770906,771303,773193
+/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551,765154,765686,765729,765743,765824,766016,766289,766584,766588,766590,766602,766673,767403,767824,768342,769239,769346,769368,769434,770172,770906,771303,773193,773446

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/processor/Splitter.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/processor/Splitter.java?rev=773447&r1=773446&r2=773447&view=diff
==============================================================================
--- camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/processor/Splitter.java (original)
+++ camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/processor/Splitter.java Mon May 11 01:48:18 2009
@@ -92,8 +92,12 @@
                     public Object next() {
                         Object part = iterator.next();
                         Exchange newExchange = exchange.copy();
-                        Message in = newExchange.getIn();
-                        in.setBody(part);
+                        if (part instanceof Message) {
+                            newExchange.setIn((Message)part);
+                        } else {
+                            Message in = newExchange.getIn();
+                            in.setBody(part);
+                        }
                         return new ProcessorExchangePair(getProcessors().iterator().next(), newExchange);
                     }
 
@@ -118,8 +122,12 @@
         while (iter.hasNext()) {
             Object part = iter.next();
             Exchange newExchange = exchange.copy();
-            Message in = newExchange.getIn();
-            in.setBody(part);
+            if (part instanceof Message) {
+                newExchange.setIn((Message)part);
+            } else {
+                Message in = newExchange.getIn();
+                in.setBody(part);
+            }
             result.add(new ProcessorExchangePair(getProcessors().iterator().next(), newExchange));
         }
         return result;

Added: camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java?rev=773447&view=auto
==============================================================================
--- camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java (added)
+++ camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java Mon May 11 01:48:18 2009
@@ -0,0 +1,137 @@
+/**
+ * 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.processor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Body;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Header;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class SplitterPojoTest extends ContextTestSupport {
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("mySplitterBean", new MySplitterBean());       
+        return jndi;
+    }
+
+    public void testSplitBodyWithPojoBean() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.reset();
+        mock.expectedBodiesReceived("James", "Jonathan", "Hadrian", "Claus", "Willem");
+
+        template.sendBody("direct:body", "James,Jonathan,Hadrian,Claus,Willem");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    public void testSplitMessageWithPojoBean() throws Exception {
+        String users[] = {"James", "Jonathan", "Hadrian", "Claus", "Willem"};
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.reset();
+        mock.expectedMessageCount(5);
+        template.sendBodyAndHeader("direct:message", "Test Body Message", "user", "James,Jonathan,Hadrian,Claus,Willem");
+        int i = 0;
+        for (Exchange exchange : mock.getExchanges()) {
+            assertEquals("We got a wrong body ", "Test Body Message", exchange.getIn().getBody());
+            assertEquals("We got a wrong header ", users[i], exchange.getIn().getHeader("user"));
+            i++;
+        }
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: e1
+                from("direct:body")
+                        // here we use a POJO bean mySplitterBean to do the split of the payload
+                        .splitter().method("mySplitterBean", "splitBody")
+                        .to("mock:result");
+                from("direct:message")
+                        // here we use a POJO bean mySplitterBean to do the split of the message 
+                        // with a certain header value
+                        .splitter().method("mySplitterBean", "splitMessage")
+                        .to("mock:result");
+                // END SNIPPET: e1
+                
+                
+            }
+        };
+    }
+
+    // START SNIPPET: e2
+    public class MySplitterBean {
+
+        /**
+         * The split body method returns something that is iteratable such as a java.util.List.
+         *
+         * @param body the payload of the incoming message
+         * @return a list containing each part splitted
+         */
+        public List splitBody(String body) {
+            // since this is based on an unit test you can of couse
+            // use different logic for splitting as Camel have out
+            // of the box support for splitting a String based on comma
+            // but this is for show and tell, since this is java code
+            // you have the full power how you like to split your messages
+            List answer = new ArrayList();
+            String[] parts = body.split(",");
+            for (String part : parts) {
+                answer.add(part);
+            }
+            return answer;
+        }
+        
+        /**
+         * The split message method returns something that is iteratable such as a java.util.List.
+         *
+         * @param header the header of the incoming message with the name user
+         * @param body the payload of the incoming message
+         * @return a list containing each part splitted
+         */
+        public List<Message> splitMessage(@Header(name = "user") String header, @Body String body) {
+            // we can leverage the Parameter Binding Annotations  
+            // http://camel.apache.org/parameter-binding-annotations.html
+            // to access the message header and body at same time, 
+            // then create the message that we want, splitter will
+            // take care rest of them.
+            List<Message> answer = new ArrayList<Message>();
+            String[] parts = header.split(",");
+            for (String part : parts) {
+                DefaultMessage message = new DefaultMessage();
+                message.setHeader("user", part);
+                message.setBody(body);
+                answer.add(message);
+            }
+            return answer;
+        }
+    }
+    // END SNIPPET: e2
+
+}
\ No newline at end of file

Propchange: camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date