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