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; 029 030/** 031 * The Protobuf data format is used for serializing between Java objects and the Google Protobuf protocol. 032 * 033 * @version 034 */ 035@Metadata(firstVersion = "2.2.0", label = "dataformat,transformation", title = "Protobuf") 036@XmlRootElement(name = "protobuf") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class ProtobufDataFormat extends DataFormatDefinition { 039 @XmlAttribute 040 private String instanceClass; 041 @XmlAttribute @Metadata(enums = "native,json", defaultValue = "native") 042 private String contentTypeFormat; 043 @XmlTransient 044 private Object defaultInstance; 045 046 public ProtobufDataFormat() { 047 super("protobuf"); 048 } 049 050 public ProtobufDataFormat(String instanceClass) { 051 this(); 052 setInstanceClass(instanceClass); 053 } 054 055 public ProtobufDataFormat(String instanceClass, String contentTypeFormat) { 056 this(); 057 setInstanceClass(instanceClass); 058 setContentTypeFormat(contentTypeFormat); 059 } 060 061 public String getInstanceClass() { 062 return instanceClass; 063 } 064 065 /** 066 * Name of class to use when unarmshalling 067 */ 068 public void setInstanceClass(String instanceClass) { 069 this.instanceClass = instanceClass; 070 } 071 072 /** 073 * Defines a content type format in which protobuf message will be 074 * serialized/deserialized from(to) the Java been. 075 * The format can either be native or json for either native protobuf or json fields representation. 076 * The default value is native. 077 */ 078 public void setContentTypeFormat(String contentTypeFormat) { 079 this.contentTypeFormat = contentTypeFormat; 080 } 081 082 public String getContentTypeFormat() { 083 return contentTypeFormat; 084 } 085 086 public Object getDefaultInstance() { 087 return defaultInstance; 088 } 089 090 public void setDefaultInstance(Object defaultInstance) { 091 this.defaultInstance = defaultInstance; 092 } 093 094 @Override 095 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 096 if (this.instanceClass != null) { 097 setProperty(camelContext, dataFormat, "instanceClass", instanceClass); 098 } 099 if (this.contentTypeFormat != null) { 100 setProperty(camelContext, dataFormat, "contentTypeFormat", contentTypeFormat); 101 } 102 if (this.defaultInstance != null) { 103 setProperty(camelContext, dataFormat, "defaultInstance", defaultInstance); 104 } 105 } 106 107}