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.CollectionStringBuffer; 031import org.apache.camel.util.ObjectHelper; 032 033/** 034 * Json data format 035 * 036 * @version 037 */ 038@Metadata(label = "dataformat,transformation,json", title = "JSon") 039@XmlRootElement(name = "json") 040@XmlAccessorType(XmlAccessType.FIELD) 041public class JsonDataFormat extends DataFormatDefinition { 042 @XmlAttribute 043 private String objectMapper; 044 @XmlAttribute 045 private Boolean prettyPrint; 046 @XmlAttribute @Metadata(defaultValue = "XStream") 047 private JsonLibrary library = JsonLibrary.XStream; 048 @XmlAttribute 049 private String unmarshalTypeName; 050 @XmlTransient 051 private Class<?> unmarshalType; 052 @XmlAttribute 053 private Class<?> jsonView; 054 @XmlAttribute 055 private String include; 056 @XmlAttribute 057 private Boolean allowJmsType; 058 @XmlAttribute 059 private String collectionTypeName; 060 @XmlTransient 061 private Class<?> collectionType; 062 @XmlAttribute 063 private Boolean useList; 064 @XmlAttribute 065 private Boolean enableJaxbAnnotationModule; 066 @XmlAttribute 067 private String moduleClassNames; 068 @XmlAttribute 069 private String moduleRefs; 070 @XmlAttribute 071 private String enableFeatures; 072 @XmlAttribute 073 private String disableFeatures; 074 @XmlAttribute 075 private String permissions; 076 077 public JsonDataFormat() { 078 super("json"); 079 } 080 081 public JsonDataFormat(JsonLibrary library) { 082 this.library = library; 083 } 084 085 public String getObjectMapper() { 086 return objectMapper; 087 } 088 089 /** 090 * Lookup and use the existing ObjectMapper with the given id when using Jackson. 091 */ 092 public void setObjectMapper(String objectMapper) { 093 this.objectMapper = objectMapper; 094 } 095 096 public Boolean getPrettyPrint() { 097 return prettyPrint; 098 } 099 100 /** 101 * To enable pretty printing output nicely formatted. 102 * <p/> 103 * Is by default false. 104 */ 105 public void setPrettyPrint(Boolean prettyPrint) { 106 this.prettyPrint = prettyPrint; 107 } 108 109 public String getUnmarshalTypeName() { 110 return unmarshalTypeName; 111 } 112 113 /** 114 * Class name of the java type to use when unarmshalling 115 */ 116 public void setUnmarshalTypeName(String unmarshalTypeName) { 117 this.unmarshalTypeName = unmarshalTypeName; 118 } 119 120 public Class<?> getUnmarshalType() { 121 return unmarshalType; 122 } 123 124 /** 125 * Class of the java type to use when unarmshalling 126 */ 127 public void setUnmarshalType(Class<?> unmarshalType) { 128 this.unmarshalType = unmarshalType; 129 } 130 131 public JsonLibrary getLibrary() { 132 return library; 133 } 134 135 /** 136 * Which json library to use such. 137 * <p/> 138 * Is by default xstream 139 */ 140 public void setLibrary(JsonLibrary library) { 141 this.library = library; 142 } 143 144 public Class<?> getJsonView() { 145 return jsonView; 146 } 147 148 /** 149 * When marshalling a POJO to JSON you might want to exclude certain fields from the JSON output. 150 * With Jackson you can use JSON views to accomplish this. This option is to refer to the class 151 * which has @JsonView annotations 152 */ 153 public void setJsonView(Class<?> jsonView) { 154 this.jsonView = jsonView; 155 } 156 157 public String getInclude() { 158 return include; 159 } 160 161 /** 162 * If you want to marshal a pojo to JSON, and the pojo has some fields with null values. 163 * And you want to skip these null values, you can set this option to <tt>NOT_NULL</tt> 164 */ 165 public void setInclude(String include) { 166 this.include = include; 167 } 168 169 public Boolean getAllowJmsType() { 170 return allowJmsType; 171 } 172 173 /** 174 * Used for JMS users to allow the JMSType header from the JMS spec to specify a FQN classname 175 * to use to unmarshal to. 176 */ 177 public void setAllowJmsType(Boolean allowJmsType) { 178 this.allowJmsType = allowJmsType; 179 } 180 181 public String getCollectionTypeName() { 182 return collectionTypeName; 183 } 184 185 /** 186 * Refers to a custom collection type to lookup in the registry to use. This option should rarely be used, but allows 187 * to use different collection types than java.util.Collection based as default. 188 */ 189 public void setCollectionTypeName(String collectionTypeName) { 190 this.collectionTypeName = collectionTypeName; 191 } 192 193 public Boolean getUseList() { 194 return useList; 195 } 196 197 /** 198 * To unarmshal to a List of Map or a List of Pojo. 199 */ 200 public void setUseList(Boolean useList) { 201 this.useList = useList; 202 } 203 204 public Boolean getEnableJaxbAnnotationModule() { 205 return enableJaxbAnnotationModule; 206 } 207 208 /** 209 * Whether to enable the JAXB annotations module when using jackson. When enabled then JAXB annotations 210 * can be used by Jackson. 211 */ 212 public void setEnableJaxbAnnotationModule(Boolean enableJaxbAnnotationModule) { 213 this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; 214 } 215 216 public String getModuleClassNames() { 217 return moduleClassNames; 218 } 219 220 /** 221 * To use custom Jackson modules com.fasterxml.jackson.databind.Module specified as a String with FQN class names. 222 * Multiple classes can be separated by comma. 223 */ 224 public void setModuleClassNames(String moduleClassNames) { 225 this.moduleClassNames = moduleClassNames; 226 } 227 228 public String getModuleRefs() { 229 return moduleRefs; 230 } 231 232 /** 233 * To use custom Jackson modules referred from the Camel registry. 234 * Multiple modules can be separated by comma. 235 */ 236 public void setModuleRefs(String moduleRefs) { 237 this.moduleRefs = moduleRefs; 238 } 239 240 public String getEnableFeatures() { 241 return enableFeatures; 242 } 243 244 /** 245 * Set of features to enable on the Jackson <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. 246 * <p/> 247 * The features should be a name that matches a enum from <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, 248 * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or <tt>com.fasterxml.jackson.databind.MapperFeature</tt> 249 * <p/> 250 * Multiple features can be separated by comma 251 */ 252 public void setEnableFeatures(String enableFeatures) { 253 this.enableFeatures = enableFeatures; 254 } 255 256 public String getDisableFeatures() { 257 return disableFeatures; 258 } 259 260 /** 261 * Set of features to disable on the Jackson <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. 262 * <p/> 263 * The features should be a name that matches a enum from <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, 264 * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or <tt>com.fasterxml.jackson.databind.MapperFeature</tt> 265 * <p/> 266 * Multiple features can be separated by comma 267 */ 268 public void setDisableFeatures(String disableFeatures) { 269 this.disableFeatures = disableFeatures; 270 } 271 272 public String getPermissions() { 273 return permissions; 274 } 275 276 /** 277 * Adds permissions that controls which Java packages and classes XStream is allowed to use during 278 * unmarshal from xml/json to Java beans. 279 * <p/> 280 * A permission must be configured either here or globally using a JVM system property. The permission 281 * can be specified in a syntax where a plus sign is allow, and minus sign is deny. 282 * <br/> 283 * Wildcards is supported by using <tt>.*</tt> as prefix. For example to allow <tt>com.foo</tt> and all subpackages 284 * then specfy <tt>+com.foo.*</tt>. Multiple permissions can be configured separated by comma, such as 285 * <tt>+com.foo.*,-com.foo.bar.MySecretBean</tt>. 286 * <br/> 287 * The following default permission is always included: <tt>"-*,java.lang.*,java.util.*"</tt> unless 288 * its overridden by specifying a JVM system property with they key <tt>org.apache.camel.xstream.permissions</tt>. 289 */ 290 public void setPermissions(String permissions) { 291 this.permissions = permissions; 292 } 293 294 /** 295 * To add permission for the given pojo classes. 296 * @param type the pojo class(es) xstream should use as allowed permission 297 * @see #setPermissions(String) 298 */ 299 public void setPermissions(Class<?>... type) { 300 CollectionStringBuffer csb = new CollectionStringBuffer(","); 301 for (Class<?> clazz : type) { 302 csb.append("+"); 303 csb.append(clazz.getName()); 304 } 305 setPermissions(csb.toString()); 306 } 307 308 @Override 309 public String getDataFormatName() { 310 // json data format is special as the name can be from different bundles 311 return "json-" + library.name().toLowerCase(); 312 } 313 314 @Override 315 protected DataFormat createDataFormat(RouteContext routeContext) { 316 if (library == JsonLibrary.XStream) { 317 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-xstream"); 318 } else if (library == JsonLibrary.Jackson) { 319 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-jackson"); 320 } else { 321 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-gson"); 322 } 323 324 if (unmarshalType == null && unmarshalTypeName != null) { 325 try { 326 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 327 } catch (ClassNotFoundException e) { 328 throw ObjectHelper.wrapRuntimeCamelException(e); 329 } 330 } 331 if (collectionType == null && collectionTypeName != null) { 332 try { 333 collectionType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(collectionTypeName); 334 } catch (ClassNotFoundException e) { 335 throw ObjectHelper.wrapRuntimeCamelException(e); 336 } 337 } 338 339 return super.createDataFormat(routeContext); 340 } 341 342 @Override 343 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 344 if (objectMapper != null) { 345 // must be a reference value 346 String ref = objectMapper.startsWith("#") ? objectMapper : "#" + objectMapper; 347 setProperty(camelContext, dataFormat, "objectMapper", ref); 348 } 349 if (unmarshalType != null) { 350 setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); 351 } 352 if (prettyPrint != null) { 353 setProperty(camelContext, dataFormat, "prettyPrint", prettyPrint); 354 } 355 if (jsonView != null) { 356 setProperty(camelContext, dataFormat, "jsonView", jsonView); 357 } 358 if (include != null) { 359 setProperty(camelContext, dataFormat, "include", include); 360 } 361 if (allowJmsType != null) { 362 setProperty(camelContext, dataFormat, "allowJmsType", allowJmsType); 363 } 364 if (collectionType != null) { 365 setProperty(camelContext, dataFormat, "collectionType", collectionType); 366 } 367 if (useList != null) { 368 setProperty(camelContext, dataFormat, "useList", useList); 369 } 370 if (enableJaxbAnnotationModule != null) { 371 setProperty(camelContext, dataFormat, "enableJaxbAnnotationModule", enableJaxbAnnotationModule); 372 } 373 if (moduleClassNames != null) { 374 setProperty(camelContext, dataFormat, "modulesClassNames", moduleClassNames); 375 } 376 if (moduleRefs != null) { 377 setProperty(camelContext, dataFormat, "moduleRefs", moduleRefs); 378 } 379 if (enableFeatures != null) { 380 setProperty(camelContext, dataFormat, "enableFeatures", enableFeatures); 381 } 382 if (disableFeatures != null) { 383 setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures); 384 } 385 if (permissions != null) { 386 setProperty(camelContext, dataFormat, "permissions", permissions); 387 } 388 // if we have the unmarshal type, but no permission set, then use it to be allowed 389 if (permissions == null && unmarshalType != null) { 390 String allow = "+" + unmarshalType.getName(); 391 setProperty(camelContext, dataFormat, "permissions", allow); 392 } 393 } 394 395}