You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/06/06 12:21:35 UTC

[camel] 01/03: CAMEL-13619 - Camel-CBOR: Align options of the dataformat as the other Jackson dataformat, since CBORMapper extends ObjectMapper

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 523017a92475551951a08aaf2777401624f3333f
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Jun 6 13:27:16 2019 +0200

    CAMEL-13619 - Camel-CBOR: Align options of the dataformat as the other Jackson dataformat, since CBORMapper extends ObjectMapper
---
 .../camel-cbor/src/main/docs/cbor-dataformat.adoc  |   6 +-
 .../camel/component/cbor/CBORDataFormat.java       | 190 +++++++++++++++++++++
 .../camel/model/dataformat/CBORDataFormat.java     |  71 ++++++++
 .../reifier/dataformat/CBORDataFormatReifier.java  |  12 ++
 .../springboot/CBORDataFormatConfiguration.java    |  61 +++++++
 5 files changed, 339 insertions(+), 1 deletion(-)

diff --git a/components/camel-cbor/src/main/docs/cbor-dataformat.adoc b/components/camel-cbor/src/main/docs/cbor-dataformat.adoc
index bf7e263..950dfa4 100644
--- a/components/camel-cbor/src/main/docs/cbor-dataformat.adoc
+++ b/components/camel-cbor/src/main/docs/cbor-dataformat.adoc
@@ -19,7 +19,7 @@ from("activemq:My.Queue").
 ### CBOR Options
 
 // dataformat options: START
-The CBOR dataformat supports 7 options, which are listed below.
+The CBOR dataformat supports 11 options, which are listed below.
 
 
 
@@ -32,6 +32,10 @@ The CBOR dataformat supports 7 options, which are listed below.
 | collectionTypeName |  | String | Refers to a custom collection type to lookup in the registry to use. This option should rarely be used, but allows to use different collection types than java.util.Collection based as default.
 | useList | false | Boolean | To unarmshal to a List of Map or a List of Pojo.
 | allowUnmarshallType | false | Boolean | If enabled then Jackson CBOR is allowed to attempt to use the CamelCBORUnmarshalType header during the unmarshalling. This should only be enabled when desired to be used.
+| prettyPrint | false | Boolean | To enable pretty printing output nicely formatted. Is by default false.
+| allowJmsType | false | Boolean | Used for JMS users to allow the JMSType header from the JMS spec to specify a FQN classname to use to unmarshal to.
+| enableFeatures |  | String | Set of features to enable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma
+| disableFeatures |  | String | Set of features to disable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma
 | contentTypeHeader | false | Boolean | Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc.
 |===
 // dataformat options: END
diff --git a/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java b/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
index b91ae11..fb1e715 100644
--- a/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
+++ b/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
@@ -21,9 +21,15 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.MapperFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.databind.type.CollectionType;
 import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
 
@@ -32,6 +38,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.DataFormatName;
 import org.apache.camel.spi.annotations.Dataformat;
+import org.apache.camel.support.ObjectHelper;
 import org.apache.camel.support.service.ServiceSupport;
 
 @Dataformat("cbor")
@@ -44,6 +51,11 @@ public class CBORDataFormat extends ServiceSupport implements DataFormat, DataFo
     private boolean allowUnmarshallType;
     private Class<? extends Collection> collectionType;
     private boolean useList;
+    private boolean prettyPrint;
+    private boolean allowJmsType;
+    private String enableFeatures;
+    private String disableFeatures;
+    private boolean enableJacksonTypeConverter;
 
     /**
      * Use the default CBOR Jackson {@link ObjectMapper} and {@link Object}
@@ -74,6 +86,9 @@ public class CBORDataFormat extends ServiceSupport implements DataFormat, DataFo
         if (allowUnmarshallType) {
             type = exchange.getIn().getHeader(CBORConstants.UNMARSHAL_TYPE, String.class);
         }
+        if (type == null && isAllowJmsType()) {
+            type = exchange.getIn().getHeader("JMSType", String.class);
+        }
         if (type != null) {
             clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type);
         }
@@ -89,6 +104,14 @@ public class CBORDataFormat extends ServiceSupport implements DataFormat, DataFo
     public String getDataFormatName() {
         return "cbor";
     }
+    
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
 
     public ObjectMapper getObjectMapper() {
         return objectMapper;
@@ -152,6 +175,120 @@ public class CBORDataFormat extends ServiceSupport implements DataFormat, DataFo
         setCollectionType(null);
         setUnmarshalType(HashMap.class);
     }
+    
+    public boolean isPrettyPrint() {
+        return prettyPrint;
+    }
+
+    public void setPrettyPrint(boolean prettyPrint) {
+        this.prettyPrint = prettyPrint;
+    }
+    
+    /**
+     * Allows jackson to use the <tt>JMSType</tt> header as an indicator what
+     * the classname is for unmarshaling json content to POJO
+     * <p/>
+     * By default this option is <tt>false</tt>.
+     */
+    public void setAllowJmsType(boolean allowJmsType) {
+        this.allowJmsType = allowJmsType;
+    }
+    
+    public boolean isAllowJmsType() {
+        return allowJmsType;
+    }
+    
+    public String getEnableFeatures() {
+        return enableFeatures;
+    }
+    
+    public boolean isEnableJacksonTypeConverter() {
+        return enableJacksonTypeConverter;
+    }
+
+    /**
+     * If enabled then Jackson is allowed to attempt to be used during Camels
+     * <a href="https://camel.apache.org/type-converter.html">type converter</a>
+     * as a {@link org.apache.camel.FallbackConverter} that attempts to convert
+     * POJOs to/from {@link Map}/{@link List} types.
+     * <p/>
+     * This should only be enabled when desired to be used.
+     */
+    public void setEnableJacksonTypeConverter(boolean enableJacksonTypeConverter) {
+        this.enableJacksonTypeConverter = enableJacksonTypeConverter;
+    }
+
+    /**
+     * Set of features to enable on the Jackson {@link ObjectMapper}. The
+     * features should be a name that matches a enum from
+     * {@link SerializationFeature}, {@link DeserializationFeature}, or
+     * {@link MapperFeature}.
+     */
+    public void setEnableFeatures(String enableFeatures) {
+        this.enableFeatures = enableFeatures;
+    }
+
+    public String getDisableFeatures() {
+        return disableFeatures;
+    }
+
+    /**
+     * Set of features to disable on the Jackson {@link ObjectMapper}. The
+     * features should be a name that matches a enum from
+     * {@link SerializationFeature}, {@link DeserializationFeature}, or
+     * {@link MapperFeature}.
+     */
+    public void setDisableFeatures(String disableFeatures) {
+        this.disableFeatures = disableFeatures;
+    }
+
+    public void enableFeature(SerializationFeature feature) {
+        if (enableFeatures == null) {
+            enableFeatures = feature.name();
+        } else {
+            enableFeatures += "," + feature.name();
+        }
+    }
+
+    public void enableFeature(DeserializationFeature feature) {
+        if (enableFeatures == null) {
+            enableFeatures = feature.name();
+        } else {
+            enableFeatures += "," + feature.name();
+        }
+    }
+
+    public void enableFeature(MapperFeature feature) {
+        if (enableFeatures == null) {
+            enableFeatures = feature.name();
+        } else {
+            enableFeatures += "," + feature.name();
+        }
+    }
+
+    public void disableFeature(SerializationFeature feature) {
+        if (disableFeatures == null) {
+            disableFeatures = feature.name();
+        } else {
+            disableFeatures += "," + feature.name();
+        }
+    }
+
+    public void disableFeature(DeserializationFeature feature) {
+        if (disableFeatures == null) {
+            disableFeatures = feature.name();
+        } else {
+            disableFeatures += "," + feature.name();
+        }
+    }
+
+    public void disableFeature(MapperFeature feature) {
+        if (disableFeatures == null) {
+            disableFeatures = feature.name();
+        } else {
+            disableFeatures += "," + feature.name();
+        }
+    }
 
     @Override
     protected void doStart() throws Exception {
@@ -176,6 +313,59 @@ public class CBORDataFormat extends ServiceSupport implements DataFormat, DataFo
         if (useList) {
             setCollectionType(ArrayList.class);
         }
+        
+        if (prettyPrint) {
+        	objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
+        }
+        
+        if (enableFeatures != null) {
+            Iterator<?> it = ObjectHelper.createIterator(enableFeatures);
+            while (it.hasNext()) {
+                String enable = it.next().toString();
+                // it can be different kind
+                SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, enable);
+                if (sf != null) {
+                    objectMapper.enable(sf);
+                    continue;
+                }
+                DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, enable);
+                if (df != null) {
+                    objectMapper.enable(df);
+                    continue;
+                }
+                MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, enable);
+                if (mf != null) {
+                    objectMapper.enable(mf);
+                    continue;
+                }
+                throw new IllegalArgumentException("Enable feature: " + enable
+                                                   + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
+            }
+        }
+        if (disableFeatures != null) {
+            Iterator<?> it = ObjectHelper.createIterator(disableFeatures);
+            while (it.hasNext()) {
+                String disable = it.next().toString();
+                // it can be different kind
+                SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, disable);
+                if (sf != null) {
+                    objectMapper.disable(sf);
+                    continue;
+                }
+                DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, disable);
+                if (df != null) {
+                    objectMapper.disable(df);
+                    continue;
+                }
+                MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, disable);
+                if (mf != null) {
+                    objectMapper.disable(mf);
+                    continue;
+                }
+                throw new IllegalArgumentException("Disable feature: " + disable
+                                                   + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
+            }
+        }
     }
 
     @Override
diff --git a/core/camel-core/src/main/java/org/apache/camel/model/dataformat/CBORDataFormat.java b/core/camel-core/src/main/java/org/apache/camel/model/dataformat/CBORDataFormat.java
index 8a5a82e..3d91d35 100644
--- a/core/camel-core/src/main/java/org/apache/camel/model/dataformat/CBORDataFormat.java
+++ b/core/camel-core/src/main/java/org/apache/camel/model/dataformat/CBORDataFormat.java
@@ -50,6 +50,14 @@ public class CBORDataFormat extends DataFormatDefinition {
     private Boolean useList;
     @XmlAttribute
     private Boolean allowUnmarshallType;
+    @XmlAttribute
+    private Boolean prettyPrint;
+    @XmlAttribute
+    private Boolean allowJmsType;
+    @XmlAttribute
+    private String enableFeatures;
+    @XmlAttribute
+    private String disableFeatures;
 
     public CBORDataFormat() {
         super("cbor");
@@ -92,6 +100,31 @@ public class CBORDataFormat extends DataFormatDefinition {
     public Class<?> getUnmarshalType() {
         return unmarshalType;
     }
+    
+    public Boolean getPrettyPrint() {
+        return prettyPrint;
+    }
+
+    /**
+     * To enable pretty printing output nicely formatted.
+     * <p/>
+     * Is by default false.
+     */
+    public void setPrettyPrint(Boolean prettyPrint) {
+        this.prettyPrint = prettyPrint;
+    }
+    
+    public Boolean getAllowJmsType() {
+        return allowJmsType;
+    }
+
+    /**
+     * Used for JMS users to allow the JMSType header from the JMS spec to
+     * specify a FQN classname to use to unmarshal to.
+     */
+    public void setAllowJmsType(Boolean allowJmsType) {
+        this.allowJmsType = allowJmsType;
+    }
 
     /**
      * Class of the java type to use when unarmshalling
@@ -145,6 +178,44 @@ public class CBORDataFormat extends DataFormatDefinition {
     public void setAllowUnmarshallType(Boolean allowUnmarshallType) {
         this.allowUnmarshallType = allowUnmarshallType;
     }
+    
+    public String getEnableFeatures() {
+        return enableFeatures;
+    }
+
+    /**
+     * Set of features to enable on the Jackson
+     * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>.
+     * <p/>
+     * The features should be a name that matches a enum from
+     * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
+     * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or
+     * <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
+     * <p/>
+     * Multiple features can be separated by comma
+     */
+    public void setEnableFeatures(String enableFeatures) {
+        this.enableFeatures = enableFeatures;
+    }
+
+    public String getDisableFeatures() {
+        return disableFeatures;
+    }
+
+    /**
+     * Set of features to disable on the Jackson
+     * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>.
+     * <p/>
+     * The features should be a name that matches a enum from
+     * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
+     * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or
+     * <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
+     * <p/>
+     * Multiple features can be separated by comma
+     */
+    public void setDisableFeatures(String disableFeatures) {
+        this.disableFeatures = disableFeatures;
+    }
 
     @Override
     public String getDataFormatName() {
diff --git a/core/camel-core/src/main/java/org/apache/camel/reifier/dataformat/CBORDataFormatReifier.java b/core/camel-core/src/main/java/org/apache/camel/reifier/dataformat/CBORDataFormatReifier.java
index 5dfcb98..b0978fb 100644
--- a/core/camel-core/src/main/java/org/apache/camel/reifier/dataformat/CBORDataFormatReifier.java
+++ b/core/camel-core/src/main/java/org/apache/camel/reifier/dataformat/CBORDataFormatReifier.java
@@ -67,6 +67,18 @@ public class CBORDataFormatReifier extends DataFormatReifier<CBORDataFormat> {
         if (definition.getAllowUnmarshallType() != null) {
             setProperty(camelContext, dataFormat, "allowUnmarshallType", definition.getAllowUnmarshallType());
         }
+        if (definition.getPrettyPrint() != null) {
+            setProperty(camelContext, dataFormat, "prettyPrint", definition.getPrettyPrint());
+        }
+        if (definition.getAllowJmsType() != null) {
+            setProperty(camelContext, dataFormat, "allowJmsType", definition.getAllowJmsType());
+        }
+        if (definition.getEnableFeatures() != null) {
+            setProperty(camelContext, dataFormat, "enableFeatures", definition.getEnableFeatures());
+        }
+        if (definition.getDisableFeatures() != null) {
+            setProperty(camelContext, dataFormat, "disableFeatures", definition.getDisableFeatures());
+        }
     }
 
 }
diff --git a/platforms/spring-boot/components-starter/camel-cbor-starter/src/main/java/org/apache/camel/component/cbor/springboot/CBORDataFormatConfiguration.java b/platforms/spring-boot/components-starter/camel-cbor-starter/src/main/java/org/apache/camel/component/cbor/springboot/CBORDataFormatConfiguration.java
index 851bf7e..c7414cf 100644
--- a/platforms/spring-boot/components-starter/camel-cbor-starter/src/main/java/org/apache/camel/component/cbor/springboot/CBORDataFormatConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-cbor-starter/src/main/java/org/apache/camel/component/cbor/springboot/CBORDataFormatConfiguration.java
@@ -68,6 +68,35 @@ public class CBORDataFormatConfiguration
      */
     private Boolean allowUnmarshallType = false;
     /**
+     * To enable pretty printing output nicely formatted. Is by default false.
+     */
+    private Boolean prettyPrint = false;
+    /**
+     * Used for JMS users to allow the JMSType header from the JMS spec to
+     * specify a FQN classname to use to unmarshal to.
+     */
+    private Boolean allowJmsType = false;
+    /**
+     * Set of features to enable on the Jackson
+     * com.fasterxml.jackson.databind.ObjectMapper. The features should be a
+     * name that matches a enum from
+     * com.fasterxml.jackson.databind.SerializationFeature,
+     * com.fasterxml.jackson.databind.DeserializationFeature, or
+     * com.fasterxml.jackson.databind.MapperFeature Multiple features can be
+     * separated by comma
+     */
+    private String enableFeatures;
+    /**
+     * Set of features to disable on the Jackson
+     * com.fasterxml.jackson.databind.ObjectMapper. The features should be a
+     * name that matches a enum from
+     * com.fasterxml.jackson.databind.SerializationFeature,
+     * com.fasterxml.jackson.databind.DeserializationFeature, or
+     * com.fasterxml.jackson.databind.MapperFeature Multiple features can be
+     * separated by comma
+     */
+    private String disableFeatures;
+    /**
      * Whether the data format should set the Content-Type header with the type
      * from the data format if the data format is capable of doing so. For
      * example application/xml for data formats marshalling to XML, or
@@ -123,6 +152,38 @@ public class CBORDataFormatConfiguration
         this.allowUnmarshallType = allowUnmarshallType;
     }
 
+    public Boolean getPrettyPrint() {
+        return prettyPrint;
+    }
+
+    public void setPrettyPrint(Boolean prettyPrint) {
+        this.prettyPrint = prettyPrint;
+    }
+
+    public Boolean getAllowJmsType() {
+        return allowJmsType;
+    }
+
+    public void setAllowJmsType(Boolean allowJmsType) {
+        this.allowJmsType = allowJmsType;
+    }
+
+    public String getEnableFeatures() {
+        return enableFeatures;
+    }
+
+    public void setEnableFeatures(String enableFeatures) {
+        this.enableFeatures = enableFeatures;
+    }
+
+    public String getDisableFeatures() {
+        return disableFeatures;
+    }
+
+    public void setDisableFeatures(String disableFeatures) {
+        this.disableFeatures = disableFeatures;
+    }
+
     public Boolean getContentTypeHeader() {
         return contentTypeHeader;
     }