Author: ningjiang
Date: Mon Jan 31 08:10:55 2011
New Revision: 1065505

URL: http://svn.apache.org/viewvc?rev=1065505&view=rev
Log:
CAMEL-3580 Type converter for CXFPayload data type

Added:
    
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java
   (with props)
    
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java
   (with props)
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java
   (with props)
    
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/
    
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
   (with props)
Modified:
    
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml

Added: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java?rev=1065505&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java
 (added)
+++ 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java
 Mon Jan 31 08:10:55 2011
@@ -0,0 +1,130 @@
+/**
+ * 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.converter;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.component.cxf.CxfPayload;
+import org.apache.camel.spi.TypeConverterRegistry;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+@Converter
+public final class CxfPayloadConverter {
+    private CxfPayloadConverter() {
+        // Helper class
+    }
+    @Converter
+    public static <T> CxfPayload<T> documentToCxfPayload(Document doc, 
Exchange exchange) {
+        return elementToCxfPayload(doc.getDocumentElement(), exchange);
+    }
+
+    @Converter
+    public static <T> CxfPayload<T> elementToCxfPayload(Element element, 
Exchange exchange) {
+        List<T> headers = new ArrayList<T>();
+        List<Element> body = new ArrayList<Element>();
+        body.add(element);
+        return new CxfPayload<T>(headers, body);
+    }
+
+    @Converter
+    public static <T> CxfPayload<T> nodeListToCxfPayload(NodeList nodeList, 
Exchange exchange) {
+        List<T> headers = new ArrayList<T>();
+        List<Element> body = new ArrayList<Element>();
+        for (int i = 0; i < nodeList.getLength(); i++) {
+            Node node = nodeList.item(i);
+            // add all nodes to the body that are elements
+            if (Element.class.isAssignableFrom(node.getClass())) {
+                body.add((Element) node);
+            }
+        }
+        return new CxfPayload<T>(headers, body);
+    }
+
+    @Converter
+    public static <T> NodeList cxfPayloadToNodeList(CxfPayload<T> payload, 
Exchange exchange) {
+        return new NodeListWrapper(payload.getBody());
+    }
+
+    @SuppressWarnings("unchecked")
+    @FallbackConverter
+    public static <T> T convertTo(Class<T> type, Exchange exchange, Object 
value, TypeConverterRegistry registry) {
+        // use fallback type converter, so we can probably convert into
+        // CxfPayloads from other types
+        if (type.isAssignableFrom(CxfPayload.class)) {
+            TypeConverter tc = registry.lookup(NodeList.class, 
value.getClass());
+            if (tc != null) {
+                NodeList nodeList = tc.convertTo(NodeList.class, exchange, 
value);
+                return (T) nodeListToCxfPayload(nodeList, exchange);
+            }
+            tc = registry.lookup(Document.class, value.getClass());
+            if (tc != null) {
+                Document document = tc.convertTo(Document.class, exchange, 
value);
+                return (T) documentToCxfPayload(document, exchange);
+            }
+            // maybe we can convert via an InputStream
+            CxfPayload<?> p = null;
+            p = convertVia(InputStream.class, exchange, value, registry);
+            if (p != null) {
+                return (T) p;
+            }
+            // String is the converter of last resort
+            p = convertVia(String.class, exchange, value, registry);
+            if (p != null) {
+                return (T) p;
+            }
+        }
+        // Convert a CxfPayload into something else
+        if (CxfPayload.class.isAssignableFrom(value.getClass())) {
+            TypeConverter tc = registry.lookup(type, NodeList.class);
+            if (tc != null) {
+                return tc.convertTo(type, cxfPayloadToNodeList((CxfPayload<?>) 
value, exchange));
+            }
+            // we cannot convert a node list, so we try the first item from the
+            // node list
+            tc = registry.lookup(type, Node.class);
+            if (tc != null) {
+                NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) 
value, exchange);
+                if (nodeList.getLength() > 0) {
+                    return tc.convertTo(type, nodeList.item(0));
+                }
+            }
+        }
+        return null;
+    }
+
+    private static <T, V> CxfPayload<T> convertVia(Class<V> via, Exchange 
exchange, Object value, TypeConverterRegistry registry) {
+        TypeConverter tc = registry.lookup(via, value.getClass());
+        if (tc != null) {
+            TypeConverter tc1 = registry.lookup(Document.class, via);
+            if (tc1 != null) {
+                V is = tc.convertTo(via, exchange, value);
+                Document document = tc1.convertTo(Document.class, exchange, 
is);
+                return documentToCxfPayload(document, exchange);
+            }
+        }
+        return null;
+    }
+}

Propchange: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java?rev=1065505&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java
 (added)
+++ 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java
 Mon Jan 31 08:10:55 2011
@@ -0,0 +1,39 @@
+/**
+ * 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.converter;
+
+import java.util.List;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class NodeListWrapper implements NodeList {
+    List<Element> elementList;
+
+    public NodeListWrapper(List<Element> elementList) {
+        this.elementList = elementList;
+    }
+
+    public int getLength() {
+        return elementList.size();
+    }
+
+    public Node item(int index) {
+        return elementList.get(index);
+    }
+}

Propchange: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/NodeListWrapper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java?rev=1065505&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java
 (added)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java
 Mon Jan 31 08:10:55 2011
@@ -0,0 +1,96 @@
+/**
+ * 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.converter;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.component.cxf.CxfPayload;
+import org.apache.camel.test.junit4.ExchangeTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CxfPayloadConverterTest extends ExchangeTestSupport {
+    private Document document;
+    private CxfPayload<String[]> payload;
+    private FileInputStream inputStream;
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        File file = new 
File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
+        DocumentBuilderFactory documentBuilderFactory = 
DocumentBuilderFactory.newInstance();
+        DocumentBuilder documentBuilder = 
documentBuilderFactory.newDocumentBuilder();
+        document = documentBuilder.parse(file);
+        document.getDocumentElement().normalize();
+        List<Element> body = new ArrayList<Element>();
+        body.add(document.getDocumentElement());
+        payload = new CxfPayload<String[]>(new ArrayList<String[]>(), body);
+        inputStream = new FileInputStream(file);
+    }
+
+    @Test
+    public void testDocumentToCxfPayload() {
+        CxfPayload<String[]> payload = 
CxfPayloadConverter.documentToCxfPayload(document, exchange);
+        assertNotNull(payload);
+        assertEquals("Get a wrong size of body", 1, payload.getBody().size());
+    }
+
+    @Test
+    public void testNodeListToCxfPayload() {
+        NodeList nodeList = document.getChildNodes();
+        CxfPayload<String[]> payload = 
CxfPayloadConverter.nodeListToCxfPayload(nodeList, exchange);
+        assertNotNull(payload);
+        assertEquals("Get a wrong size of body", 1, payload.getBody().size());
+    }
+    
+    @Test
+    public void testCxfPayloadToNodeList() {
+        NodeList nodeList = CxfPayloadConverter.cxfPayloadToNodeList(payload, 
exchange);
+        assertNotNull(nodeList);
+        System.out.println("The node list is " + nodeList.getLength());
+        assertEquals("Get a worng size of nodeList", 1,  nodeList.getLength());
+    }
+   
+    @Test
+    public void testToCxfPayload() {
+        // use default type converter
+        exchange.getIn().setBody(inputStream);
+        CxfPayload payload = exchange.getIn().getBody(CxfPayload.class);
+        assertTrue(payload instanceof CxfPayload);
+        assertEquals("Get a wrong size of body", 1, payload.getBody().size());
+    }
+    
+    @Test
+    public void testFromCxfPayload() {
+        exchange.getIn().setBody(payload);
+        InputStream inputStream = exchange.getIn().getBody(InputStream.class);
+        assertTrue(inputStream instanceof InputStream);       
+    }
+
+}

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

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

Added: 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml?rev=1065505&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
 (added)
+++ 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
 Mon Jan 31 08:10:55 2011
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root xmlns="http://www.test.org/foo";>
+       <bar/>
+</root>
\ No newline at end of file

Propchange: 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/converter/test.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml?rev=1065505&r1=1065504&r2=1065505&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml
 (original)
+++ 
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml
 Mon Jan 31 08:10:55 2011
@@ -35,7 +35,7 @@
     </jaxrs:serviceBeans>       
   </jaxrs:server>
   
-  <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"/>
+  <!--  bean id="jsonProvider" 
class="org.apache.cxf.jaxrs.provider.JSONProvider"/-->
 
   <bean id="customerService" 
class="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" />
    


Reply via email to