camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] camel git commit: CAMEL-8774 Filter out the protocol message from the request message header
Date Thu, 14 May 2015 08:16:24 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.14.x 916720832 -> 1426de82c
  refs/heads/camel-2.15.x a9311042c -> 1d2166e95


CAMEL-8774 Filter out the protocol message from the request message header


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1d2166e9
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1d2166e9
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1d2166e9

Branch: refs/heads/camel-2.15.x
Commit: 1d2166e95599ee14e1c97d7f3df679682c178a56
Parents: a931104
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Thu May 14 16:01:54 2015 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Thu May 14 16:04:17 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/util/MessageHelper.java    | 21 ++++++++++++++-
 .../apache/camel/util/MessageHelperTest.java    | 28 ++++++++++++++++++++
 .../jetty/DefaultJettyHttpBinding.java          |  5 ++--
 3 files changed, 51 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1d2166e9/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java b/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
index f0cae78..49d2170 100644
--- a/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
@@ -26,6 +26,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+
 import javax.xml.transform.Source;
 
 import org.apache.camel.BytesSource;
@@ -36,6 +37,7 @@ import org.apache.camel.StreamCache;
 import org.apache.camel.StringSource;
 import org.apache.camel.WrappedFile;
 import org.apache.camel.spi.ExchangeFormatter;
+import org.apache.camel.spi.HeaderFilterStrategy;
 
 /**
  * Some helper methods when working with {@link org.apache.camel.Message}.
@@ -468,6 +470,18 @@ public final class MessageHelper {
      * @param override whether to override existing headers
      */
     public static void copyHeaders(Message source, Message target, boolean override) {
+        copyHeaders(source, target, null, override);
+    }
+    
+    /**
+     * Copies the headers from the source to the target message.
+     * 
+     * @param source the source message
+     * @param target the target message
+     * @param strategy the header filter strategy which could help us to filter the protocol
message headers
+     * @param override whether to override existing headers
+     */
+    public static void copyHeaders(Message source, Message target, HeaderFilterStrategy strategy,
boolean override) {
         if (!source.hasHeaders()) {
             return;
         }
@@ -477,7 +491,12 @@ public final class MessageHelper {
             Object value = entry.getValue();
 
             if (target.getHeader(key) == null || override) {
-                target.setHeader(key, value);
+                if (strategy == null) {
+                    target.setHeader(key, value);
+                } else if (!strategy.applyFilterToCamelHeaders(key, value, target.getExchange()))
{
+                    // Just make sure we don't copy the protocol headers to target
+                    target.setHeader(key, value);
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/1d2166e9/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java
index 7448989..fb08ab6 100644
--- a/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/MessageHelperTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.util;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.StringReader;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
 
@@ -29,7 +30,9 @@ import org.apache.camel.Message;
 import org.apache.camel.StreamCache;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultHeaderFilterStrategy;
 import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.spi.HeaderFilterStrategy;
 
 /**
  * Test cases for {@link MessageHelper}
@@ -117,6 +120,29 @@ public class MessageHelperTest extends TestCase {
         assertEquals(123, target.getHeader("foo"));
         assertEquals(456, target.getHeader("bar"));
     }
+    
+    public void testCopyHeadersWithHeaderFilterStrategy() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+        context.start();
+
+        message = new DefaultExchange(context).getIn();
+        
+        Message source = message;
+        Message target = message.getExchange().getOut();
+        
+        DefaultHeaderFilterStrategy headerFilterStrategy = new DefaultHeaderFilterStrategy();
+        headerFilterStrategy.setOutFilterPattern("foo");
+        
+        source.setHeader("foo", 123);
+        source.setHeader("bar", 456);
+        target.setHeader("bar", "yes");
+        
+        MessageHelper.copyHeaders(source, target, headerFilterStrategy, true);
+        
+        assertEquals(null, target.getHeader("foo"));
+        assertEquals(456, target.getHeader("bar"));
+        context.stop();
+    }
 
     public void testDumpAsXmlPlainBody() throws Exception {
         CamelContext context = new DefaultCamelContext();
@@ -213,5 +239,7 @@ public class MessageHelperTest extends TestCase {
         assertEquals("java.lang.Integer", dump.getHeaders().get(0).getType());
         assertEquals("123", dump.getHeaders().get(0).getValue());
     }
+    
+   
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1d2166e9/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
index 7b73a8a..2ee5a64 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
@@ -119,10 +119,11 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
                 }
             }
         }
-
+        
         // preserve headers from in by copying any non existing headers
         // to avoid overriding existing headers with old values
-        MessageHelper.copyHeaders(exchange.getIn(), answer, false);
+        // We also need to apply the HeaderFilterStrategy here
+        MessageHelper.copyHeaders(exchange.getIn(), answer, strategy, false);
 
         // extract body after headers has been set as we want to ensure content-type from
Jetty HttpExchange
         // has been populated first


Mime
View raw message