camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1146608 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/util/MessageHelper.java test/java/org/apache/camel/util/MessageHelperTest.java
Date Thu, 14 Jul 2011 09:09:19 GMT
Author: davsclaus
Date: Thu Jul 14 09:09:18 2011
New Revision: 1146608

URL: http://svn.apache.org/viewvc?rev=1146608&view=rev
Log:
Added copyHeaders method to MessageHelper.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java

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=1146608&r1=1146607&r2=1146608&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 Thu Jul
14 09:09:18 2011
@@ -309,4 +309,26 @@ public final class MessageHelper {
         return sb.toString();
     }
 
+    /**
+     * Copies the headers from the source to the target message.
+     *
+     * @param source the source message
+     * @param target the target message
+     * @param override whether to override existing headers
+     */
+    public static void copyHeaders(Message source, Message target, boolean override) {
+        if (!source.hasHeaders()) {
+            return;
+        }
+
+        for (Map.Entry<String, Object> entry : source.getHeaders().entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+
+            if (target.getHeader(key) == null || override) {
+                target.setHeader(key, value);
+            }
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java?rev=1146608&r1=1146607&r2=1146608&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java Thu
Jul 14 09:09:18 2011
@@ -71,4 +71,32 @@ public class MessageHelperTest extends T
         assertEquals("iso-8859-1", MessageHelper.getContentEncoding(message));
     }
 
+    public void testCopyHeaders() throws Exception {
+        Message source = message;
+        Message target = new DefaultMessage();
+
+        source.setHeader("foo", 123);
+        source.setHeader("bar", 456);
+        target.setHeader("bar", "yes");
+
+        MessageHelper.copyHeaders(source, target, false);
+
+        assertEquals(123, target.getHeader("foo"));
+        assertEquals("yes", target.getHeader("bar"));
+    }
+
+    public void testCopyHeadersOverride() throws Exception {
+        Message source = message;
+        Message target = new DefaultMessage();
+
+        source.setHeader("foo", 123);
+        source.setHeader("bar", 456);
+        target.setHeader("bar", "yes");
+
+        MessageHelper.copyHeaders(source, target, true);
+
+        assertEquals(123, target.getHeader("foo"));
+        assertEquals(456, target.getHeader("bar"));
+    }
+
 }



Mime
View raw message