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 * Boon data format is used for unmarshal a JSon payload to POJO or to marshal POJO back to JSon payload. 034 */ 035@Metadata(firstVersion = "2.16.0", label = "dataformat,transformation,json", title = "Boon") 036@XmlRootElement(name = "boon") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class BoonDataFormat extends DataFormatDefinition { 039 040 @XmlAttribute(required = true) 041 private String unmarshalTypeName; 042 @XmlTransient 043 private Class<?> unmarshalType; 044 @XmlAttribute 045 private Boolean useList; 046 047 public BoonDataFormat() { 048 super("boon"); 049 } 050 051 public BoonDataFormat(Class<?> unmarshalType) { 052 this(); 053 setUnmarshalType(unmarshalType); 054 } 055 056 public Class<?> getUnmarshalType() { 057 return unmarshalType; 058 } 059 060 /** 061 * Class name of the java type to use when unarmshalling 062 */ 063 public void setUnmarshalType(Class<?> unmarshalType) { 064 this.unmarshalType = unmarshalType; 065 } 066 067 public String getUnmarshalTypeName() { 068 return unmarshalTypeName; 069 } 070 071 /** 072 * Class name of the java type to use when unarmshalling 073 */ 074 public void setUnmarshalTypeName(String unmarshalTypeName) { 075 this.unmarshalTypeName = unmarshalTypeName; 076 } 077 078 public boolean isUseList() { 079 return useList; 080 } 081 082 /** 083 * To unarmshal to a List of Map or a List of Pojo. 084 */ 085 public void setUseList(boolean useList) { 086 this.useList = useList; 087 } 088 089 @Override 090 protected DataFormat createDataFormat(RouteContext routeContext) { 091 if (unmarshalType == null && unmarshalTypeName != null) { 092 try { 093 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 094 } catch (ClassNotFoundException e) { 095 throw ObjectHelper.wrapRuntimeCamelException(e); 096 } 097 } 098 return super.createDataFormat(routeContext); 099 } 100 101 @Override 102 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 103 if (unmarshalType != null) { 104 setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); 105 } 106 if (useList != null) { 107 setProperty(camelContext, dataFormat, "useList", useList); 108 } 109 } 110}