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.reifier.dataformat; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.RuntimeCamelException; 021import org.apache.camel.model.DataFormatDefinition; 022import org.apache.camel.model.dataformat.JsonDataFormat; 023import org.apache.camel.model.dataformat.JsonLibrary; 024import org.apache.camel.spi.DataFormat; 025 026public class JsonDataFormatReifier extends DataFormatReifier<JsonDataFormat> { 027 028 public JsonDataFormatReifier(DataFormatDefinition definition) { 029 super((JsonDataFormat)definition); 030 } 031 032 @Override 033 protected DataFormat doCreateDataFormat(CamelContext camelContext) { 034 if (definition.getLibrary() == JsonLibrary.XStream) { 035 setProperty(camelContext, this, "dataFormatName", "json-xstream"); 036 } else if (definition.getLibrary() == JsonLibrary.Jackson) { 037 setProperty(camelContext, this, "dataFormatName", "json-jackson"); 038 } else if (definition.getLibrary() == JsonLibrary.Gson) { 039 setProperty(camelContext, this, "dataFormatName", "json-gson"); 040 } else if (definition.getLibrary() == JsonLibrary.Fastjson) { 041 setProperty(camelContext, this, "dataFormatName", "json-fastjson"); 042 } else { 043 setProperty(camelContext, this, "dataFormatName", "json-johnzon"); 044 } 045 046 if (definition.getUnmarshalType() == null && definition.getUnmarshalTypeName() != null) { 047 try { 048 definition.setUnmarshalType(camelContext.getClassResolver().resolveMandatoryClass(definition.getUnmarshalTypeName())); 049 } catch (ClassNotFoundException e) { 050 throw RuntimeCamelException.wrapRuntimeCamelException(e); 051 } 052 } 053 if (definition.getCollectionType() == null && definition.getCollectionTypeName() != null) { 054 try { 055 definition.setCollectionType(camelContext.getClassResolver().resolveMandatoryClass(definition.getCollectionTypeName())); 056 } catch (ClassNotFoundException e) { 057 throw RuntimeCamelException.wrapRuntimeCamelException(e); 058 } 059 } 060 061 return super.doCreateDataFormat(camelContext); 062 } 063 064 @Override 065 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 066 if (definition.getObjectMapper() != null) { 067 // must be a reference value 068 String ref = definition.getObjectMapper().startsWith("#") ? definition.getObjectMapper() : "#" + definition.getObjectMapper(); 069 setProperty(camelContext, dataFormat, "objectMapper", ref); 070 } 071 if (definition.getUseDefaultObjectMapper() != null) { 072 setProperty(camelContext, dataFormat, "useDefaultObjectMapper", definition.getUseDefaultObjectMapper()); 073 } 074 if (definition.getUnmarshalType() != null) { 075 setProperty(camelContext, dataFormat, "unmarshalType", definition.getUnmarshalType()); 076 } 077 if (definition.getPrettyPrint() != null) { 078 setProperty(camelContext, dataFormat, "prettyPrint", definition.getPrettyPrint()); 079 } 080 if (definition.getJsonView() != null) { 081 setProperty(camelContext, dataFormat, "jsonView", definition.getJsonView()); 082 } 083 if (definition.getInclude() != null) { 084 setProperty(camelContext, dataFormat, "include", definition.getInclude()); 085 } 086 if (definition.getAllowJmsType() != null) { 087 setProperty(camelContext, dataFormat, "allowJmsType", definition.getAllowJmsType()); 088 } 089 if (definition.getCollectionTypeName() != null) { 090 setProperty(camelContext, dataFormat, "collectionType", definition.getCollectionTypeName()); 091 } 092 if (definition.getUseList() != null) { 093 setProperty(camelContext, dataFormat, "useList", definition.getUseList()); 094 } 095 if (definition.getEnableJaxbAnnotationModule() != null) { 096 setProperty(camelContext, dataFormat, "enableJaxbAnnotationModule", definition.getEnableJaxbAnnotationModule()); 097 } 098 if (definition.getModuleClassNames() != null) { 099 setProperty(camelContext, dataFormat, "moduleClassNames", definition.getModuleClassNames()); 100 } 101 if (definition.getModuleRefs() != null) { 102 setProperty(camelContext, dataFormat, "moduleRefs", definition.getModuleRefs()); 103 } 104 if (definition.getEnableFeatures() != null) { 105 setProperty(camelContext, dataFormat, "enableFeatures", definition.getEnableFeatures()); 106 } 107 if (definition.getDisableFeatures() != null) { 108 setProperty(camelContext, dataFormat, "disableFeatures", definition.getDisableFeatures()); 109 } 110 if (definition.getPermissions() != null) { 111 setProperty(camelContext, dataFormat, "permissions", definition.getPermissions()); 112 } 113 if (definition.getAllowUnmarshallType() != null) { 114 setProperty(camelContext, dataFormat, "allowUnmarshallType", definition.getAllowUnmarshallType()); 115 } 116 // if we have the unmarshal type, but no permission set, then use it to 117 // be allowed 118 if (definition.getPermissions() == null && definition.getUnmarshalType() != null) { 119 String allow = "+" + definition.getUnmarshalType().getName(); 120 setProperty(camelContext, dataFormat, "permissions", allow); 121 } 122 } 123 124}