001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.dataformat;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.model.DataFormatDefinition;
027import org.apache.camel.spi.DataFormat;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.RouteContext;
030import org.apache.camel.util.ObjectHelper;
031
032/**
033 * JacksonXML data format is used for unmarshal a XML payload to POJO or to marshal POJO back to XML payload.
034 *
035 * @version
036 */
037@Metadata(firstVersion = "2.16.0", label = "dataformat,transformation,xml", title = "JacksonXML")
038@XmlRootElement(name = "jacksonxml")
039@XmlAccessorType(XmlAccessType.FIELD)
040public class JacksonXMLDataFormat extends DataFormatDefinition {
041    @XmlAttribute
042    private String xmlMapper;
043    @XmlAttribute
044    private Boolean prettyPrint;
045    @XmlAttribute
046    private String unmarshalTypeName;
047    @XmlTransient
048    private Class<?> unmarshalType;
049    @XmlAttribute
050    private Class<?> jsonView;
051    @XmlAttribute
052    private String include;
053    @XmlAttribute
054    private Boolean allowJmsType;
055    @XmlAttribute
056    private String collectionTypeName;
057    @XmlTransient
058    private Class<?> collectionType;
059    @XmlAttribute
060    private Boolean useList;
061    @XmlAttribute
062    private Boolean enableJaxbAnnotationModule;
063    @XmlAttribute
064    private String moduleClassNames;
065    @XmlAttribute
066    private String moduleRefs;
067    @XmlAttribute
068    private String enableFeatures;
069    @XmlAttribute
070    private String disableFeatures;
071    @XmlAttribute
072    private Boolean allowUnmarshallType;
073
074    public JacksonXMLDataFormat() {
075        super("jacksonxml");
076    }
077
078    public String getXmlMapper() {
079        return xmlMapper;
080    }
081
082    /**
083     * Lookup and use the existing XmlMapper with the given id.
084     */
085    public void setXmlMapper(String xmlMapper) {
086        this.xmlMapper = xmlMapper;
087    }
088
089    public Boolean getPrettyPrint() {
090        return prettyPrint;
091    }
092
093    /**
094     * To enable pretty printing output nicely formatted.
095     * <p/>
096     * Is by default false.
097     */
098    public void setPrettyPrint(Boolean prettyPrint) {
099        this.prettyPrint = prettyPrint;
100    }
101
102    public String getUnmarshalTypeName() {
103        return unmarshalTypeName;
104    }
105
106    /**
107     * Class name of the java type to use when unarmshalling
108     */
109    public void setUnmarshalTypeName(String unmarshalTypeName) {
110        this.unmarshalTypeName = unmarshalTypeName;
111    }
112
113    public Class<?> getUnmarshalType() {
114        return unmarshalType;
115    }
116
117    /**
118     * Class of the java type to use when unarmshalling
119     */
120    public void setUnmarshalType(Class<?> unmarshalType) {
121        this.unmarshalType = unmarshalType;
122    }
123
124    public Class<?> getJsonView() {
125        return jsonView;
126    }
127
128    /**
129     * When marshalling a POJO to JSON you might want to exclude certain fields
130     * from the JSON output. With Jackson you can use JSON views to accomplish
131     * this. This option is to refer to the class which has @JsonView
132     * annotations
133     */
134    public void setJsonView(Class<?> jsonView) {
135        this.jsonView = jsonView;
136    }
137
138    public String getInclude() {
139        return include;
140    }
141
142    /**
143     * If you want to marshal a pojo to JSON, and the pojo has some fields with
144     * null values. And you want to skip these null values, you can set this
145     * option to <tt>NOT_NULL</tt>
146     */
147    public void setInclude(String include) {
148        this.include = include;
149    }
150
151    public Boolean getAllowJmsType() {
152        return allowJmsType;
153    }
154
155    /**
156     * Used for JMS users to allow the JMSType header from the JMS spec to
157     * specify a FQN classname to use to unmarshal to.
158     */
159    public void setAllowJmsType(Boolean allowJmsType) {
160        this.allowJmsType = allowJmsType;
161    }
162
163    public String getCollectionTypeName() {
164        return collectionTypeName;
165    }
166
167    /**
168     * Refers to a custom collection type to lookup in the registry to use. This
169     * option should rarely be used, but allows to use different collection
170     * types than java.util.Collection based as default.
171     */
172    public void setCollectionTypeName(String collectionTypeName) {
173        this.collectionTypeName = collectionTypeName;
174    }
175
176    public Boolean getUseList() {
177        return useList;
178    }
179
180    /**
181     * To unarmshal to a List of Map or a List of Pojo.
182     */
183    public void setUseList(Boolean useList) {
184        this.useList = useList;
185    }
186
187    public Boolean getEnableJaxbAnnotationModule() {
188        return enableJaxbAnnotationModule;
189    }
190
191    /**
192     * Whether to enable the JAXB annotations module when using jackson. When
193     * enabled then JAXB annotations can be used by Jackson.
194     */
195    public void setEnableJaxbAnnotationModule(Boolean enableJaxbAnnotationModule) {
196        this.enableJaxbAnnotationModule = enableJaxbAnnotationModule;
197    }
198
199    public String getModuleClassNames() {
200        return moduleClassNames;
201    }
202
203    /**
204     * To use custom Jackson modules com.fasterxml.jackson.databind.Module
205     * specified as a String with FQN class names. Multiple classes can be
206     * separated by comma.
207     */
208    public void setModuleClassNames(String moduleClassNames) {
209        this.moduleClassNames = moduleClassNames;
210    }
211
212    public String getModuleRefs() {
213        return moduleRefs;
214    }
215
216    /**
217     * To use custom Jackson modules referred from the Camel registry. Multiple
218     * modules can be separated by comma.
219     */
220    public void setModuleRefs(String moduleRefs) {
221        this.moduleRefs = moduleRefs;
222    }
223
224    public String getEnableFeatures() {
225        return enableFeatures;
226    }
227
228    /**
229     * Set of features to enable on the Jackson
230     * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>.
231     * <p/>
232     * The features should be a name that matches a enum from
233     * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
234     * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or
235     * <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
236     * <p/>
237     * Multiple features can be separated by comma
238     */
239    public void setEnableFeatures(String enableFeatures) {
240        this.enableFeatures = enableFeatures;
241    }
242
243    public String getDisableFeatures() {
244        return disableFeatures;
245    }
246
247    /**
248     * Set of features to disable on the Jackson
249     * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>.
250     * <p/>
251     * The features should be a name that matches a enum from
252     * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
253     * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or
254     * <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
255     * <p/>
256     * Multiple features can be separated by comma
257     */
258    public void setDisableFeatures(String disableFeatures) {
259        this.disableFeatures = disableFeatures;
260    }
261    
262    public Boolean getAllowUnmarshallType() {
263        return allowUnmarshallType;
264    }
265
266    /**
267     * If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling.
268     * <p/>
269     * This should only be enabled when desired to be used.
270     */
271    public void setAllowUnmarshallType(Boolean allowUnmarshallType) {
272        this.allowUnmarshallType = allowUnmarshallType;
273    }
274
275    @Override
276    public String getDataFormatName() {
277        return "jacksonxml";
278    }
279
280    @Override
281    protected DataFormat createDataFormat(RouteContext routeContext) {
282
283        if (unmarshalType == null && unmarshalTypeName != null) {
284            try {
285                unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName);
286            } catch (ClassNotFoundException e) {
287                throw ObjectHelper.wrapRuntimeCamelException(e);
288            }
289        }
290        if (collectionType == null && collectionTypeName != null) {
291            try {
292                collectionType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(collectionTypeName);
293            } catch (ClassNotFoundException e) {
294                throw ObjectHelper.wrapRuntimeCamelException(e);
295            }
296        }
297
298        return super.createDataFormat(routeContext);
299    }
300
301    @Override
302    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
303        if (xmlMapper != null) {
304            // must be a reference value
305            String ref = xmlMapper.startsWith("#") ? xmlMapper : "#" + xmlMapper;
306            setProperty(camelContext, dataFormat, "xmlMapper", ref);
307        }
308        if (unmarshalType != null) {
309            setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType);
310        }
311        if (prettyPrint != null) {
312            setProperty(camelContext, dataFormat, "prettyPrint", prettyPrint);
313        }
314        if (jsonView != null) {
315            setProperty(camelContext, dataFormat, "jsonView", jsonView);
316        }
317        if (include != null) {
318            setProperty(camelContext, dataFormat, "include", include);
319        }
320        if (allowJmsType != null) {
321            setProperty(camelContext, dataFormat, "allowJmsType", allowJmsType);
322        }
323        if (collectionType != null) {
324            setProperty(camelContext, dataFormat, "collectionType", collectionType);
325        }
326        if (useList != null) {
327            setProperty(camelContext, dataFormat, "useList", useList);
328        }
329        if (enableJaxbAnnotationModule != null) {
330            setProperty(camelContext, dataFormat, "enableJaxbAnnotationModule", enableJaxbAnnotationModule);
331        }
332        if (moduleClassNames != null) {
333            setProperty(camelContext, dataFormat, "modulesClassNames", moduleClassNames);
334        }
335        if (moduleRefs != null) {
336            setProperty(camelContext, dataFormat, "moduleRefs", moduleRefs);
337        }
338        if (enableFeatures != null) {
339            setProperty(camelContext, dataFormat, "enableFeatures", enableFeatures);
340        }
341        if (disableFeatures != null) {
342            setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures);
343        }
344        if (allowUnmarshallType != null) {
345            setProperty(camelContext, dataFormat, "allowUnmarshallType", allowUnmarshallType);
346        }
347    }
348
349}