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;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlElements;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.camel.Processor;
027import org.apache.camel.model.dataformat.AvroDataFormat;
028import org.apache.camel.model.dataformat.Base64DataFormat;
029import org.apache.camel.model.dataformat.BeanioDataFormat;
030import org.apache.camel.model.dataformat.BindyDataFormat;
031import org.apache.camel.model.dataformat.BoonDataFormat;
032import org.apache.camel.model.dataformat.CastorDataFormat;
033import org.apache.camel.model.dataformat.CryptoDataFormat;
034import org.apache.camel.model.dataformat.CsvDataFormat;
035import org.apache.camel.model.dataformat.CustomDataFormat;
036import org.apache.camel.model.dataformat.FlatpackDataFormat;
037import org.apache.camel.model.dataformat.GzipDataFormat;
038import org.apache.camel.model.dataformat.HL7DataFormat;
039import org.apache.camel.model.dataformat.IcalDataFormat;
040import org.apache.camel.model.dataformat.JacksonXMLDataFormat;
041import org.apache.camel.model.dataformat.JaxbDataFormat;
042import org.apache.camel.model.dataformat.JibxDataFormat;
043import org.apache.camel.model.dataformat.JsonDataFormat;
044import org.apache.camel.model.dataformat.PGPDataFormat;
045import org.apache.camel.model.dataformat.ProtobufDataFormat;
046import org.apache.camel.model.dataformat.RssDataFormat;
047import org.apache.camel.model.dataformat.SerializationDataFormat;
048import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
049import org.apache.camel.model.dataformat.StringDataFormat;
050import org.apache.camel.model.dataformat.SyslogDataFormat;
051import org.apache.camel.model.dataformat.TarFileDataFormat;
052import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
053import org.apache.camel.model.dataformat.UniVocityCsvDataFormat;
054import org.apache.camel.model.dataformat.UniVocityFixedWidthDataFormat;
055import org.apache.camel.model.dataformat.UniVocityTsvDataFormat;
056import org.apache.camel.model.dataformat.XMLBeansDataFormat;
057import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
058import org.apache.camel.model.dataformat.XStreamDataFormat;
059import org.apache.camel.model.dataformat.XmlJsonDataFormat;
060import org.apache.camel.model.dataformat.XmlRpcDataFormat;
061import org.apache.camel.model.dataformat.YAMLDataFormat;
062import org.apache.camel.model.dataformat.ZipDataFormat;
063import org.apache.camel.model.dataformat.ZipFileDataFormat;
064import org.apache.camel.processor.MarshalProcessor;
065import org.apache.camel.spi.DataFormat;
066import org.apache.camel.spi.Metadata;
067import org.apache.camel.spi.RouteContext;
068
069/**
070 * Marshals data into a specified format for transmission over a transport or component
071 *
072 * @version
073 */
074@Metadata(label = "eip,transformation")
075@XmlRootElement(name = "marshal")
076@XmlAccessorType(XmlAccessType.FIELD)
077public class MarshalDefinition extends NoOutputDefinition<MarshalDefinition> {
078
079    // TODO: Camel 3.0, ref attribute should be removed as CustomDataFormat is to be used instead
080
081    // cannot use @XmlElementRef as it doesn't allow optional properties
082    @XmlElements({
083        @XmlElement(required = false, name = "avro", type = AvroDataFormat.class),
084        @XmlElement(required = false, name = "base64", type = Base64DataFormat.class),
085        @XmlElement(required = false, name = "beanio", type = BeanioDataFormat.class),
086        @XmlElement(required = false, name = "bindy", type = BindyDataFormat.class),
087        @XmlElement(required = false, name = "boon", type = BoonDataFormat.class),
088        @XmlElement(required = false, name = "castor", type = CastorDataFormat.class),
089        @XmlElement(required = false, name = "crypto", type = CryptoDataFormat.class),
090        @XmlElement(required = false, name = "csv", type = CsvDataFormat.class),
091        @XmlElement(required = false, name = "custom", type = CustomDataFormat.class),
092        @XmlElement(required = false, name = "flatpack", type = FlatpackDataFormat.class),
093        @XmlElement(required = false, name = "gzip", type = GzipDataFormat.class),
094        @XmlElement(required = false, name = "hl7", type = HL7DataFormat.class),
095        @XmlElement(required = false, name = "ical", type = IcalDataFormat.class),
096        @XmlElement(required = false, name = "jacksonxml", type = JacksonXMLDataFormat.class),
097        @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class),
098        @XmlElement(required = false, name = "jibx", type = JibxDataFormat.class),
099        @XmlElement(required = false, name = "json", type = JsonDataFormat.class),
100        @XmlElement(required = false, name = "protobuf", type = ProtobufDataFormat.class),
101        @XmlElement(required = false, name = "rss", type = RssDataFormat.class),
102        @XmlElement(required = false, name = "secureXML", type = XMLSecurityDataFormat.class),
103        @XmlElement(required = false, name = "serialization", type = SerializationDataFormat.class),
104        @XmlElement(required = false, name = "soapjaxb", type = SoapJaxbDataFormat.class),
105        @XmlElement(required = false, name = "string", type = StringDataFormat.class),
106        @XmlElement(required = false, name = "syslog", type = SyslogDataFormat.class),
107        @XmlElement(required = false, name = "tarfile", type = TarFileDataFormat.class),
108        @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
109        @XmlElement(required = false, name = "univocity-csv", type = UniVocityCsvDataFormat.class),
110        @XmlElement(required = false, name = "univocity-fixed", type = UniVocityFixedWidthDataFormat.class),
111        @XmlElement(required = false, name = "univocity-tsv", type = UniVocityTsvDataFormat.class),
112        @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
113        @XmlElement(required = false, name = "xmljson", type = XmlJsonDataFormat.class),
114        @XmlElement(required = false, name = "xmlrpc", type = XmlRpcDataFormat.class),
115        @XmlElement(required = false, name = "xstream", type = XStreamDataFormat.class),
116        @XmlElement(required = false, name = "pgp", type = PGPDataFormat.class),
117        @XmlElement(required = false, name = "yaml", type = YAMLDataFormat.class),
118        @XmlElement(required = false, name = "zip", type = ZipDataFormat.class),
119        @XmlElement(required = false, name = "zipFile", type = ZipFileDataFormat.class)}
120        )
121    private DataFormatDefinition dataFormatType;
122
123    @XmlAttribute
124    @Deprecated
125    private String ref;
126
127    public MarshalDefinition() {
128    }
129
130    public MarshalDefinition(DataFormatDefinition dataFormatType) {
131        this.dataFormatType = dataFormatType;
132    }
133
134    public MarshalDefinition(String ref) {
135        this.ref = ref;
136    }
137
138    @Override
139    public String toString() {
140        return "Marshal[" + description() + "]";
141    }
142
143    protected String description() {
144        return dataFormatType != null ? dataFormatType.toString() : "ref:" + ref;
145    }
146
147    @Override
148    public String getLabel() {
149        return "marshal[" + description() + "]";
150    }
151
152    public String getRef() {
153        return ref;
154    }
155
156    /**
157     * To refer to a custom data format to use as marshaller
158     *
159     * @deprecated use uri with ref:uri instead
160     */
161    @Deprecated
162    public void setRef(String ref) {
163        this.ref = ref;
164    }
165
166    public DataFormatDefinition getDataFormatType() {
167        return dataFormatType;
168    }
169
170    /**
171     * The data format to be used
172     */
173    public void setDataFormatType(DataFormatDefinition dataFormatType) {
174        this.dataFormatType = dataFormatType;
175    }
176
177    @Override
178    public Processor createProcessor(RouteContext routeContext) {
179        DataFormat dataFormat = DataFormatDefinition.getDataFormat(routeContext, getDataFormatType(), ref);
180        return new MarshalProcessor(dataFormat);
181    }
182}