camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1094156 - in /camel/trunk/components/camel-cxf/src: main/java/org/apache/camel/component/cxf/ main/java/org/apache/camel/component/cxf/util/ test/java/org/apache/camel/component/cxf/ test/java/org/apache/camel/component/cxf/util/
Date Sun, 17 Apr 2011 15:18:07 GMT
Author: davsclaus
Date: Sun Apr 17 15:18:07 2011
New Revision: 1094156

URL: http://svn.apache.org/viewvc?rev=1094156&view=rev
Log:
CAMEL-3842: Uses insensitive keys for CXF protocol headers. Thanks to Aki for the patch.

Added:
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
  (with props)
Modified:
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfHeaderHelper.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfMessageHelperTest.java

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java?rev=1094156&r1=1094155&r2=1094156&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
Sun Apr 17 15:18:07 2011
@@ -24,6 +24,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.TreeMap;
 
 import javax.activation.DataHandler;
 import javax.security.auth.Subject;
@@ -519,7 +520,8 @@ public class DefaultCxfBinding implement
             Map<String, Object> cxfContext) {
         
         // get cxf transport headers (if any) from camel exchange
-        Map<String, List<String>> transportHeaders = new HashMap<String, List<String>>();
+        // use a treemap to keep ordering and ignore key case
+        Map<String, List<String>> transportHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
         if (camelExchange != null) {
             Map<String, List<String>> h = (Map)camelExchange.getProperty(Message.PROTOCOL_HEADERS);
             if (h != null) {

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfHeaderHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfHeaderHelper.java?rev=1094156&r1=1094155&r2=1094156&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfHeaderHelper.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfHeaderHelper.java
Sun Apr 17 15:18:07 2011
@@ -17,9 +17,9 @@
 package org.apache.camel.component.cxf.util;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.HeaderFilterStrategy;
@@ -55,7 +55,8 @@ public final class CxfHeaderHelper {
             CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
 
         if (cxfHeaders == null) {
-            cxfHeaders = new HashMap<String, List<String>>();
+            // use a treemap to keep ordering and ignore key case
+            cxfHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
             message.put(Message.PROTOCOL_HEADERS, cxfHeaders);
         }
 

Added: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java?rev=1094156&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
(added)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
Sun Apr 17 15:18:07 2011
@@ -0,0 +1,218 @@
+/**
+ * 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.component.cxf;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultHeaderFilterStrategy;
+import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.cxf.attachment.AttachmentImpl;
+import org.apache.cxf.binding.Binding;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.message.Attachment;
+import org.apache.cxf.message.Message;
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * 
+ */
+public class DefaultCxfBindingTest extends Assert {
+    private DefaultCamelContext context = new DefaultCamelContext();
+
+    @Test
+    public void testSetGetHeaderFilterStrategy() {
+        DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
+        HeaderFilterStrategy hfs = new DefaultHeaderFilterStrategy();
+        
+        cxfBinding.setHeaderFilterStrategy(hfs);        
+        assertSame("The header filter strategy is set", hfs, cxfBinding.getHeaderFilterStrategy());
+    }
+    
+    @Test
+    public void testPopulateCxfRequestFromExchange() {
+        DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
+        cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
+        Exchange exchange = new DefaultExchange(context);
+        org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
+        exchange.setProperty(CxfConstants.CXF_EXCHANGE, cxfExchange);
+        exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
+        Map<String, Object> requestContext = new HashMap<String, Object>();
+        
+        exchange.getIn().setHeader("soapAction", "urn:hello:world");
+        exchange.getIn().setHeader("MyFruitHeader", "peach");
+        exchange.getIn().addAttachment("att-1", new DataHandler(new FileDataSource("pom.xml")));
+
+        cxfBinding.populateCxfRequestFromExchange(cxfExchange, exchange, requestContext);
+        
+        // check the protocol headers
+        Map<String, List<String>> headers = CastUtils.cast((Map)requestContext.get(Message.PROTOCOL_HEADERS));
+        assertNotNull(headers);
+        assertTrue(headers.size() == 2);
+        
+        verifyHeader(headers, "soapaction", "urn:hello:world");
+        verifyHeader(headers, "SoapAction", "urn:hello:world");
+        verifyHeader(headers, "SOAPAction", "urn:hello:world");
+        verifyHeader(headers, "myfruitheader", "peach");
+        verifyHeader(headers, "myFruitHeader", "peach");
+        verifyHeader(headers, "MYFRUITHEADER", "peach");
+        
+        Set<Attachment> attachments = CastUtils.cast((Set)requestContext.get(CxfConstants.CAMEL_CXF_ATTACHMENTS));
+        assertNotNull(attachments);
+        assertNotNull(attachments.size() == 1);
+        Attachment att = attachments.iterator().next();
+        assertEquals("att-1", att.getId());
+    }
+    
+    @Test
+    public void testPopupalteExchangeFromCxfResponse() {
+        DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
+        cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
+        Exchange exchange = new DefaultExchange(context);
+        org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
+        exchange.setProperty(CxfConstants.CXF_EXCHANGE, cxfExchange);
+        exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
+        Map<String, Object> responseContext = new HashMap<String, Object>();
+        responseContext.put(org.apache.cxf.message.Message.RESPONSE_CODE, Integer.valueOf(200));
+        Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
+        headers.put("content-type", Arrays.asList("text/xml;charset=UTF-8"));
+        headers.put("Content-Length", Arrays.asList("241"));
+        responseContext.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
+        org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
+        cxfExchange.setInMessage(cxfMessage);
+        
+        Set<Attachment> attachments = new HashSet<Attachment>();
+        attachments.add(new AttachmentImpl("att-1", new DataHandler(new FileDataSource("pom.xml"))));
+        cxfMessage.setAttachments(attachments);
+        
+        cxfBinding.populateExchangeFromCxfResponse(exchange, cxfExchange, responseContext);
+        
+        Map<String, Object> camelHeaders = exchange.getOut().getHeaders();
+        assertNotNull(camelHeaders);
+        assertEquals(responseContext, camelHeaders.get(Client.RESPONSE_CONTEXT));
+        
+        Map<String, DataHandler> camelAttachments = exchange.getOut().getAttachments();
+        assertNotNull(camelAttachments);
+        assertNotNull(camelAttachments.get("att-1"));
+    }
+
+    @Test
+    public void testPopupalteCxfResponseFromExchange() {
+        DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
+        cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
+        Exchange exchange = new DefaultExchange(context);
+        org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
+        exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
+        
+        exchange.getOut().setHeader("soapAction", "urn:hello:world");
+        exchange.getOut().setHeader("MyFruitHeader", "peach");
+        exchange.getOut().addAttachment("att-1", new DataHandler(new FileDataSource("pom.xml")));
+        
+        IMocksControl control = EasyMock.createNiceControl();
+        
+        Endpoint endpoint = control.createMock(Endpoint.class);
+        Binding binding = control.createMock(Binding.class);
+        EasyMock.expect(endpoint.getBinding()).andReturn(binding);
+        org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
+        EasyMock.expect(binding.createMessage()).andReturn(cxfMessage);
+        cxfExchange.put(Endpoint.class, endpoint);
+        control.replay();
+        
+        cxfBinding.populateCxfResponseFromExchange(exchange, cxfExchange);
+        
+        cxfMessage = cxfExchange.getOutMessage();
+        assertNotNull(cxfMessage);
+        
+        Map<String, List<String>> headers = CastUtils.cast((Map)cxfMessage.get(Message.PROTOCOL_HEADERS));
+        assertNotNull(headers);
+        assertTrue(headers.size() == 2);
+        
+        verifyHeader(headers, "soapaction", "urn:hello:world");
+        verifyHeader(headers, "SoapAction", "urn:hello:world");
+        verifyHeader(headers, "SOAPAction", "urn:hello:world");
+        verifyHeader(headers, "myfruitheader", "peach");
+        verifyHeader(headers, "myFruitHeader", "peach");
+        verifyHeader(headers, "MYFRUITHEADER", "peach");
+        
+        Collection<Attachment> attachments = cxfMessage.getAttachments();
+        assertNotNull(attachments);
+        assertNotNull(attachments.size() == 1);
+        Attachment att = attachments.iterator().next();
+        assertEquals("att-1", att.getId());
+    }
+
+    @Test
+    public void testPopupalteExchangeFromCxfRequest() {
+        DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
+        cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
+        Exchange exchange = new DefaultExchange(context);
+        org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
+        exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
+        org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
+        Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
+        headers.put("content-type", Arrays.asList("text/xml;charset=UTF-8"));
+        headers.put("Content-Length", Arrays.asList("241"));
+        headers.put("soapAction", Arrays.asList("urn:hello:world"));
+        headers.put("myfruitheader", Arrays.asList("peach"));
+        cxfMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
+
+        Set<Attachment> attachments = new HashSet<Attachment>();
+        attachments.add(new AttachmentImpl("att-1", new DataHandler(new FileDataSource("pom.xml"))));
+        cxfMessage.setAttachments(attachments);
+        
+        cxfExchange.setInMessage(cxfMessage);
+
+        cxfBinding.populateExchangeFromCxfRequest(cxfExchange, exchange);
+        
+        Map<String, Object> camelHeaders = exchange.getIn().getHeaders();
+        assertNotNull(camelHeaders);
+        assertEquals(camelHeaders.get("soapaction"), "urn:hello:world");
+        assertEquals(camelHeaders.get("SoapAction"), "urn:hello:world");
+        assertEquals(camelHeaders.get("content-type"), "text/xml;charset=UTF-8");
+        assertEquals(camelHeaders.get("content-length"), "241");
+        assertEquals(camelHeaders.get("MyFruitHeader"), "peach");
+        
+        Map<String, DataHandler> camelAttachments = exchange.getIn().getAttachments();
+        assertNotNull(camelAttachments);
+        assertNotNull(camelAttachments.get("att-1"));
+        
+    }
+
+    private void verifyHeader(Map<String, List<String>> headers, String name,
String value) {
+        List<String> values = headers.get(name);
+        assertTrue("The entry must be available", values != null && values.size()
== 1);
+        assertEquals("The value must match", values.get(0), value);
+    }
+
+}

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfMessageHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfMessageHelperTest.java?rev=1094156&r1=1094155&r2=1094156&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfMessageHelperTest.java
(original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfMessageHelperTest.java
Sun Apr 17 15:18:07 2011
@@ -21,6 +21,8 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.List;
+import java.util.Map;
 
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.dom.DOMSource;
@@ -30,7 +32,9 @@ import org.apache.camel.impl.DefaultCame
 import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.util.IOHelper;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.message.Message;
 import org.apache.cxf.staxutils.StaxUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -74,6 +78,28 @@ public class CxfMessageHelperTest extend
         assertNotNull("The input stream should not be null", is);
         assertEquals("Don't get the right message", toString(is), REQUEST_STRING);
 
+        // transport header's case insensitiveness
+        // String
+        exchange.getIn().setBody("hello world");
+        exchange.getIn().setHeader("soapAction", "urn:hello:world");
+        message = CxfMessageHelper.getCxfInMessage(headerFilterStrategy, exchange, false);
+        // test message
+        Map<String, List<String>> headers = CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
+        
+        // verify there is no duplicate
+        assertNotNull("The headers must be present", headers);
+        assertTrue("There must be one header entry", headers.size() == 1);
+        
+        // verify the soapaction can be retrieved in case-insensitive ways
+        verifyHeader(headers, "soapaction", "urn:hello:world");
+        verifyHeader(headers, "SoapAction", "urn:hello:world");
+        verifyHeader(headers, "SOAPAction", "urn:hello:world");
+    }
+
+    private void verifyHeader(Map<String, List<String>> headers, String name,
String value) {
+        List<String> values = headers.get(name);
+        assertTrue("The entry must be available", values != null && values.size()
== 1);
+        assertEquals("The value must match", values.get(0), value);
     }
 
     private String toString(InputStream is) throws IOException {



Mime
View raw message