You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/09/24 11:37:08 UTC

svn commit: r1000776 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/ components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/ ...

Author: davsclaus
Date: Fri Sep 24 09:37:08 2010
New Revision: 1000776

URL: http://svn.apache.org/viewvc?rev=1000776&view=rev
Log:
CAMEL-3153: Added omitFeilds to xstream data format. Thanks to Henryk for part of the work.

Added:
    camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.java
    camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java   (with props)
    camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.xml
      - copied, changed from r1000757, camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java
    camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java?rev=1000776&r1=1000775&r2=1000776&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java Fri Sep 24 09:37:08 2010
@@ -57,6 +57,10 @@ public class XStreamDataFormat extends D
     @XmlElement(name = "aliases")
     private Map<String, String> aliases;
 
+    @XmlJavaTypeAdapter(OmitFieldsAdapter.class)
+    @XmlElement(name = "omitFields")
+    private Map<String, String[]> omitFields;
+
     @XmlJavaTypeAdapter(ImplicitCollectionsAdapter.class)
     @XmlElement(name = "implicitCollections")
     private Map<String, String[]> implicitCollections;
@@ -103,6 +107,14 @@ public class XStreamDataFormat extends D
         this.aliases = aliases;
     }
 
+    public Map<String, String[]> getOmitFields() {
+        return omitFields;
+    }
+
+    public void setOmitFields(Map<String, String[]> omitFields) {
+        this.omitFields = omitFields;
+    }
+
     public Map<String, String[]> getImplicitCollections() {
         return implicitCollections;
     }
@@ -124,15 +136,15 @@ public class XStreamDataFormat extends D
         if (encoding != null) {
             setProperty(dataFormat, "encoding", encoding);
         }
-
         if (this.converters != null) {
             setProperty(dataFormat, "converters", this.converters);
         }
-
         if (this.aliases != null) {
             setProperty(dataFormat, "aliases", this.aliases);
         }
-
+        if (this.omitFields != null) {
+            setProperty(dataFormat, "omitFields", this.omitFields);
+        }
         if (this.implicitCollections != null) {
             setProperty(dataFormat, "implicitCollections", this.implicitCollections);
         }
@@ -202,8 +214,7 @@ public class XStreamDataFormat extends D
         public ImplicitCollectionList marshal(Map<String, String[]> v) throws Exception {
             List<ImplicitCollectionEntry> list = new ArrayList<ImplicitCollectionEntry>();
             for (String clsName : v.keySet()) {
-                ImplicitCollectionEntry entry = new ImplicitCollectionEntry(
-                        clsName, v.get(clsName));
+                ImplicitCollectionEntry entry = new ImplicitCollectionEntry(clsName, v.get(clsName));
                 list.add(entry);
             }
 
@@ -271,7 +282,7 @@ public class XStreamDataFormat extends D
 
         @Override
         public String toString() {
-            return "Alias [ImplicitCollection=" + clsName + ", fields=" + Arrays.asList(this.fields) + "]";
+            return "Alias[ImplicitCollection=" + clsName + ", fields=" + Arrays.asList(this.fields) + "]";
         }
     }
 
@@ -290,7 +301,7 @@ public class XStreamDataFormat extends D
             }
             AliasList jaxbMap = new AliasList();
             jaxbMap.setList(ret);
-            return jaxbMap; // ret.toArray( new JaxbMapEntry[ret.size()] );
+            return jaxbMap;
         }
 
         @Override
@@ -353,7 +364,88 @@ public class XStreamDataFormat extends D
 
         @Override
         public String toString() {
-            return "Alias [name=" + name + ", class=" + clsName + "]";
+            return "Alias[name=" + name + ", class=" + clsName + "]";
+        }
+    }
+
+    @XmlTransient
+    public static class OmitFieldsAdapter
+            extends XmlAdapter<OmitFieldList, Map<String, String[]>> {
+
+        @Override
+        public OmitFieldList marshal(Map<String, String[]> v) throws Exception {
+            List<OmitFieldEntry> list = new ArrayList<OmitFieldEntry>();
+            for (String clsName : v.keySet()) {
+                OmitFieldEntry entry = new OmitFieldEntry(clsName, v.get(clsName));
+                list.add(entry);
+            }
+
+            OmitFieldList collectionList = new OmitFieldList();
+            collectionList.setList(list);
+
+            return collectionList;
+        }
+
+        @Override
+        public Map<String, String[]> unmarshal(OmitFieldList v) throws Exception {
+            Map<String, String[]> map = new HashMap<String, String[]>();
+            for (OmitFieldEntry entry : v.getList()) {
+                map.put(entry.getClsName(), entry.getFields());
+            }
+            return map;
+        }
+    }
+
+    @XmlAccessorType(XmlAccessType.NONE)
+    public static class OmitFieldList {
+        @XmlElement(name = "omitField")
+        private List<OmitFieldEntry> list = new ArrayList<OmitFieldEntry>();
+
+        public List<OmitFieldEntry> getList() {
+            return list;
+        }
+
+        public void setList(List<OmitFieldEntry> list) {
+            this.list = list;
+        }
+    }
+
+    @XmlAccessorType(XmlAccessType.NONE)
+    public static class OmitFieldEntry {
+
+        @XmlAttribute(name = "class")
+        private String clsName;
+
+        @XmlElement(name = "field")
+        private String[] fields;
+
+        public OmitFieldEntry() {
+        }
+
+        public OmitFieldEntry(String clsName, String[] fields) {
+            this.clsName = clsName;
+            this.fields = fields;
+        }
+
+        public String getClsName() {
+            return clsName;
+        }
+
+        public void setClsName(String clsName) {
+            this.clsName = clsName;
+        }
+
+        public String[] getFields() {
+            return fields;
+        }
+
+        public void setFields(String[] fields) {
+            this.fields = fields;
+        }
+
+        @Override
+        public String toString() {
+            return "OmitField[" + clsName + ", fields=" + Arrays.asList(this.fields) + "]";
         }
     }
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java?rev=1000776&r1=1000775&r2=1000776&view=diff
==============================================================================
--- camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java (original)
+++ camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java Fri Sep 24 09:37:08 2010
@@ -49,6 +49,7 @@ public abstract class AbstractXStreamWra
     private StaxConverter staxConverter;
     private List<String> converters;
     private Map<String, String> aliases;
+    private Map<String, String[]> omitFields;
     private Map<String, String[]> implicitCollections;
 
     public AbstractXStreamWrapper() {
@@ -87,6 +88,14 @@ public abstract class AbstractXStreamWra
                 }
             }
 
+            if (this.omitFields != null) {
+                for (Entry<String, String[]> entry : this.omitFields.entrySet()) {
+                    for (String name : entry.getValue()) {
+                        xstream.omitField(resolver.resolveMandatoryClass(entry.getKey()), name);
+                    }
+                }
+            }
+
             if (this.converters != null) {
                 for (String name : this.converters) {
                     Class<Converter> converterClass = resolver.resolveMandatoryClass(name, Converter.class);
@@ -157,6 +166,14 @@ public abstract class AbstractXStreamWra
         this.implicitCollections = implicitCollections;
     }
 
+    public Map<String, String[]> getOmitFields() {
+        return omitFields;
+    }
+
+    public void setOmitFields(Map<String, String[]> omitFields) {
+        this.omitFields = omitFields;
+    }
+
     public XStream getXstream() {
         return xstream;
     }

Added: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.java?rev=1000776&view=auto
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.java (added)
+++ camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.java Fri Sep 24 09:37:08 2010
@@ -0,0 +1,55 @@
+/**
+ * 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.dataformat.xstream;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringMarshalOmitFieldsTest extends CamelSpringTestSupport {
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.xml");
+    }
+
+    @Test
+    public void testOmitPrice() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        PurchaseOrder purchaseOrder = new PurchaseOrder();
+        purchaseOrder.setName("foo");
+        purchaseOrder.setPrice(49);
+        purchaseOrder.setAmount(3);
+
+        template.sendBody("direct:start", purchaseOrder);
+
+        assertMockEndpointsSatisfied();
+
+        String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+        assertTrue("Should contain name field", body.contains("<name>"));
+        assertFalse("Should not contain price field", body.contains("price"));
+        assertTrue("Should contain amount field", body.contains("<amount>"));
+    }
+
+}

Added: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java?rev=1000776&view=auto
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java (added)
+++ camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java Fri Sep 24 09:37:08 2010
@@ -0,0 +1,49 @@
+/**
+ * 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.dataformat.xstream;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.thoughtworks.xstream.XStream;
+import org.apache.camel.impl.DefaultClassResolver;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class XStreamDataFormatOmitFieldsTest extends CamelTestSupport {
+
+    @Test
+    public void testOmitPrice() {
+        PurchaseOrder purchaseOrder = new PurchaseOrder();
+        purchaseOrder.setName("foo");
+        purchaseOrder.setPrice(1);
+
+        XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
+        Map<String, String[]> omitFields = new HashMap<String, String[]>();
+        omitFields.put(PurchaseOrder.class.getName(), new String[]{"price"});
+        xStreamDataFormat.setOmitFields(omitFields);
+
+        XStream xStream = xStreamDataFormat.createXStream(new DefaultClassResolver());
+        String marshalledOrder = xStream.toXML(purchaseOrder);
+
+        assertTrue(!marshalledOrder.contains("<price>"));
+    }
+
+}

Propchange: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatOmitFieldsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.xml (from r1000757, camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.xml?p2=camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.xml&p1=camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml&r1=1000757&r2=1000776&rev=1000776&view=diff
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml (original)
+++ camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalOmitFieldsTest.xml Fri Sep 24 09:37:08 2010
@@ -22,28 +22,28 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <!-- START SNIPPET: e1 -->
-    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
         
-        <!-- we define the json xstream data formats to be used (xstream is default) -->
+        <!-- START SNIPPET: e1 -->
         <dataFormats>
-            <xstream id="xstream-utf8" encoding="UTF-8"/>
-            <xstream id="xstream-default"/>
+            <xstream id="xstream">
+                <!-- omit the price file from the PurchaseOrder class -->
+                <omitFields>
+                    <omitField class="org.apache.camel.dataformat.xstream.PurchaseOrder">
+                        <field>price</field>
+                    </omitField>
+                </omitFields>
+            </xstream>
         </dataFormats>
+        <!-- END SNIPPET: e1 -->
 
         <route>
-            <from uri="direct:in"/>
-            <marshal ref="xstream-default"/>
-            <to uri="mock:result"/>
-        </route>
-
-        <route>
-            <from uri="direct:in-UTF-8"/>
-            <marshal ref="xstream-utf8"/>
+            <from uri="direct:start"/>
+            <marshal ref="xstream"/>
+            <convertBodyTo type="String"/>
             <to uri="mock:result"/>
         </route>
 
     </camelContext>
-    <!-- END SNIPPET: e1 -->
 
 </beans>