camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r773446 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/processor/Splitter.java test/java/org/apache/camel/processor/SplitterPojoTest.java
Date Mon, 11 May 2009 01:29:15 GMT
Author: ningjiang
Date: Mon May 11 01:29:15 2009
New Revision: 773446

URL: http://svn.apache.org/viewvc?rev=773446&view=rev
Log:
CAMEL-1597 Added support of splitting the Message with header and body

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Splitter.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Splitter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Splitter.java?rev=773446&r1=773445&r2=773446&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Splitter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Splitter.java Mon May
11 01:29:15 2009
@@ -89,8 +89,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);
                     }
 
@@ -115,8 +119,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;

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java?rev=773446&r1=773445&r2=773446&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
Mon May 11 01:29:15 2009
@@ -19,9 +19,14 @@
 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;
 
 /**
@@ -32,28 +37,50 @@
     @Override
     protected JndiRegistry createRegistry() throws Exception {
         JndiRegistry jndi = super.createRegistry();
-        jndi.bind("mySplitterBean", new MySplitterBean());
+        jndi.bind("mySplitterBean", new MySplitterBean());       
         return jndi;
     }
 
-    public void testSplitWithPojoBean() throws Exception {
+    public void testSplitBodyWithPojoBean() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.reset();
         mock.expectedBodiesReceived("James", "Jonathan", "Hadrian", "Claus", "Willem");
 
-        template.sendBody("direct:start", "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:start")
+                from("direct:body")
                         // here we use a POJO bean mySplitterBean to do the split of the
payload
-                        .split().method("mySplitterBean")
+                        .split().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
+                        .split().method("mySplitterBean", "splitMessage")
                         .to("mock:result");
                 // END SNIPPET: e1
+                
+                
             }
         };
     }
@@ -62,12 +89,12 @@
     public class MySplitterBean {
 
         /**
-         * The split method returns something that is iteratable such as a java.util.List.
+         * 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 split(String body) {
+        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
@@ -80,6 +107,30 @@
             }
             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(value = "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
 



Mime
View raw message