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 * Jackson XML data format 034 * 035 * @version 036 */ 037@Metadata(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 072 public JacksonXMLDataFormat() { 073 super("jacksonxml"); 074 } 075 076 public String getXmlMapper() { 077 return xmlMapper; 078 } 079 080 /** 081 * Lookup and use the existing XmlMapper with the given id. 082 */ 083 public void setXmlMapper(String xmlMapper) { 084 this.xmlMapper = xmlMapper; 085 } 086 087 public Boolean getPrettyPrint() { 088 return prettyPrint; 089 } 090 091 /** 092 * To enable pretty printing output nicely formatted. 093 * <p/> 094 * Is by default false. 095 */ 096 public void setPrettyPrint(Boolean prettyPrint) { 097 this.prettyPrint = prettyPrint; 098 } 099 100 public String getUnmarshalTypeName() { 101 return unmarshalTypeName; 102 } 103 104 /** 105 * Class name of the java type to use when unarmshalling 106 */ 107 public void setUnmarshalTypeName(String unmarshalTypeName) { 108 this.unmarshalTypeName = unmarshalTypeName; 109 } 110 111 public Class<?> getUnmarshalType() { 112 return unmarshalType; 113 } 114 115 /** 116 * Class of the java type to use when unarmshalling 117 */ 118 public void setUnmarshalType(Class<?> unmarshalType) { 119 this.unmarshalType = unmarshalType; 120 } 121 122 public Class<?> getJsonView() { 123 return jsonView; 124 } 125 126 /** 127 * When marshalling a POJO to JSON you might want to exclude certain fields 128 * from the JSON output. With Jackson you can use JSON views to accomplish 129 * this. This option is to refer to the class which has @JsonView 130 * annotations 131 */ 132 public void setJsonView(Class<?> jsonView) { 133 this.jsonView = jsonView; 134 } 135 136 public String getInclude() { 137 return include; 138 } 139 140 /** 141 * If you want to marshal a pojo to JSON, and the pojo has some fields with 142 * null values. And you want to skip these null values, you can set this 143 * option to <tt>NOT_NULL</tt> 144 */ 145 public void setInclude(String include) { 146 this.include = include; 147 } 148 149 public Boolean getAllowJmsType() { 150 return allowJmsType; 151 } 152 153 /** 154 * Used for JMS users to allow the JMSType header from the JMS spec to 155 * specify a FQN classname to use to unmarshal to. 156 */ 157 public void setAllowJmsType(Boolean allowJmsType) { 158 this.allowJmsType = allowJmsType; 159 } 160 161 public String getCollectionTypeName() { 162 return collectionTypeName; 163 } 164 165 /** 166 * Refers to a custom collection type to lookup in the registry to use. This 167 * option should rarely be used, but allows to use different collection 168 * types than java.util.Collection based as default. 169 */ 170 public void setCollectionTypeName(String collectionTypeName) { 171 this.collectionTypeName = collectionTypeName; 172 } 173 174 public Boolean getUseList() { 175 return useList; 176 } 177 178 /** 179 * To unarmshal to a List of Map or a List of Pojo. 180 */ 181 public void setUseList(Boolean useList) { 182 this.useList = useList; 183 } 184 185 public Boolean getEnableJaxbAnnotationModule() { 186 return enableJaxbAnnotationModule; 187 } 188 189 /** 190 * Whether to enable the JAXB annotations module when using jackson. When 191 * enabled then JAXB annotations can be used by Jackson. 192 */ 193 public void setEnableJaxbAnnotationModule(Boolean enableJaxbAnnotationModule) { 194 this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; 195 } 196 197 public String getModuleClassNames() { 198 return moduleClassNames; 199 } 200 201 /** 202 * To use custom Jackson modules com.fasterxml.jackson.databind.Module 203 * specified as a String with FQN class names. Multiple classes can be 204 * separated by comma. 205 */ 206 public void setModuleClassNames(String moduleClassNames) { 207 this.moduleClassNames = moduleClassNames; 208 } 209 210 public String getModuleRefs() { 211 return moduleRefs; 212 } 213 214 /** 215 * To use custom Jackson modules referred from the Camel registry. Multiple 216 * modules can be separated by comma. 217 */ 218 public void setModuleRefs(String moduleRefs) { 219 this.moduleRefs = moduleRefs; 220 } 221 222 public String getEnableFeatures() { 223 return enableFeatures; 224 } 225 226 /** 227 * Set of features to enable on the Jackson 228 * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. 229 * <p/> 230 * The features should be a name that matches a enum from 231 * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, 232 * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or 233 * <tt>com.fasterxml.jackson.databind.MapperFeature</tt> 234 * <p/> 235 * Multiple features can be separated by comma 236 */ 237 public void setEnableFeatures(String enableFeatures) { 238 this.enableFeatures = enableFeatures; 239 } 240 241 public String getDisableFeatures() { 242 return disableFeatures; 243 } 244 245 /** 246 * Set of features to disable on the Jackson 247 * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. 248 * <p/> 249 * The features should be a name that matches a enum from 250 * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, 251 * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or 252 * <tt>com.fasterxml.jackson.databind.MapperFeature</tt> 253 * <p/> 254 * Multiple features can be separated by comma 255 */ 256 public void setDisableFeatures(String disableFeatures) { 257 this.disableFeatures = disableFeatures; 258 } 259 260 @Override 261 public String getDataFormatName() { 262 return "jacksonxml"; 263 } 264 265 @Override 266 protected DataFormat createDataFormat(RouteContext routeContext) { 267 268 if (unmarshalType == null && unmarshalTypeName != null) { 269 try { 270 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 271 } catch (ClassNotFoundException e) { 272 throw ObjectHelper.wrapRuntimeCamelException(e); 273 } 274 } 275 if (collectionType == null && collectionTypeName != null) { 276 try { 277 collectionType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(collectionTypeName); 278 } catch (ClassNotFoundException e) { 279 throw ObjectHelper.wrapRuntimeCamelException(e); 280 } 281 } 282 283 return super.createDataFormat(routeContext); 284 } 285 286 @Override 287 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 288 if (xmlMapper != null) { 289 // must be a reference value 290 String ref = xmlMapper.startsWith("#") ? xmlMapper : "#" + xmlMapper; 291 setProperty(camelContext, dataFormat, "xmlMapper", ref); 292 } 293 if (unmarshalType != null) { 294 setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); 295 } 296 if (prettyPrint != null) { 297 setProperty(camelContext, dataFormat, "prettyPrint", prettyPrint); 298 } 299 if (jsonView != null) { 300 setProperty(camelContext, dataFormat, "jsonView", jsonView); 301 } 302 if (include != null) { 303 setProperty(camelContext, dataFormat, "include", include); 304 } 305 if (allowJmsType != null) { 306 setProperty(camelContext, dataFormat, "allowJmsType", allowJmsType); 307 } 308 if (collectionType != null) { 309 setProperty(camelContext, dataFormat, "collectionType", collectionType); 310 } 311 if (useList != null) { 312 setProperty(camelContext, dataFormat, "useList", useList); 313 } 314 if (enableJaxbAnnotationModule != null) { 315 setProperty(camelContext, dataFormat, "enableJaxbAnnotationModule", enableJaxbAnnotationModule); 316 } 317 if (moduleClassNames != null) { 318 setProperty(camelContext, dataFormat, "modulesClassNames", moduleClassNames); 319 } 320 if (moduleRefs != null) { 321 setProperty(camelContext, dataFormat, "moduleRefs", moduleRefs); 322 } 323 if (enableFeatures != null) { 324 setProperty(camelContext, dataFormat, "enableFeatures", enableFeatures); 325 } 326 if (disableFeatures != null) { 327 setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures); 328 } 329 } 330 331}