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 * SOAP is a data format which uses JAXB2 and JAX-WS annotations to marshal and unmarshal SOAP payloads.
032 */
033@Metadata(firstVersion = "2.3.0", label = "dataformat,transformation,xml", title = "SOAP")
034@XmlRootElement(name = "soapjaxb")
035@XmlAccessorType(XmlAccessType.FIELD)
036public class SoapJaxbDataFormat extends DataFormatDefinition {
037    @XmlAttribute(required = true)
038    private String contextPath;
039    @XmlAttribute
040    private String encoding;
041    @XmlAttribute
042    private String elementNameStrategyRef;
043    @XmlTransient
044    private Object elementNameStrategy;
045    @XmlAttribute @Metadata(defaultValue = "1.1")
046    private String version;
047    @XmlAttribute
048    private String namespacePrefixRef;
049    @XmlAttribute
050    private String schema;
051
052    public SoapJaxbDataFormat() {
053        super("soapjaxb");
054    }
055    
056    public SoapJaxbDataFormat(String contextPath) {
057        this();
058        setContextPath(contextPath);
059    }
060    
061    public SoapJaxbDataFormat(String contextPath, String elementNameStrategyRef) {
062        this();
063        setContextPath(contextPath);
064        setElementNameStrategyRef(elementNameStrategyRef);
065    }
066    
067    public SoapJaxbDataFormat(String contextPath, Object elementNameStrategy) {
068        this();
069        setContextPath(contextPath);
070        setElementNameStrategy(elementNameStrategy);
071    }
072
073    /**
074     * Package name where your JAXB classes are located.
075     */
076    public void setContextPath(String contextPath) {
077        this.contextPath = contextPath;
078    }
079
080    public String getContextPath() {
081        return contextPath;
082    }
083
084    /**
085     * To overrule and use a specific encoding
086     */
087    public void setEncoding(String encoding) {
088        this.encoding = encoding;
089    }
090
091    public String getEncoding() {
092        return encoding;
093    }
094
095    /**
096     * Refers to an element strategy to lookup from the registry.
097     * <p/>
098     * An element name strategy is used for two purposes. The first is to find a xml element name for a given object
099     * and soap action when marshaling the object into a SOAP message. The second is to find an Exception class for a given soap fault name.
100     * <p/>
101     * The following three element strategy class name is provided out of the box.
102     * QNameStrategy - Uses a fixed qName that is configured on instantiation. Exception lookup is not supported
103     * TypeNameStrategy - Uses the name and namespace from the @XMLType annotation of the given type. If no namespace is set then package-info is used. Exception lookup is not supported
104     * ServiceInterfaceStrategy - Uses information from a webservice interface to determine the type name and to find the exception class for a SOAP fault
105     * <p/>
106     * All three classes is located in the package name org.apache.camel.dataformat.soap.name
107     * <p/>
108     * If you have generated the web service stub code with cxf-codegen or a similar tool then you probably
109     * will want to use the ServiceInterfaceStrategy. In the case you have no annotated service interface you should use QNameStrategy or TypeNameStrategy.
110     */
111    public void setElementNameStrategyRef(String elementNameStrategyRef) {
112        this.elementNameStrategyRef = elementNameStrategyRef;
113    }
114
115    public String getElementNameStrategyRef() {
116        return elementNameStrategyRef;
117    }
118
119    public String getVersion() {
120        return version;
121    }
122
123    /**
124     * SOAP version should either be 1.1 or 1.2.
125     * <p/>
126     * Is by default 1.1
127     */
128    public void setVersion(String version) {
129        this.version = version;
130    }
131
132    /**
133     * Sets an element strategy instance to use.
134     * <p/>
135     * An element name strategy is used for two purposes. The first is to find a xml element name for a given object
136     * and soap action when marshaling the object into a SOAP message. The second is to find an Exception class for a given soap fault name.
137     * <p/>
138     * The following three element strategy class name is provided out of the box.
139     * QNameStrategy - Uses a fixed qName that is configured on instantiation. Exception lookup is not supported
140     * TypeNameStrategy - Uses the name and namespace from the @XMLType annotation of the given type. If no namespace is set then package-info is used. Exception lookup is not supported
141     * ServiceInterfaceStrategy - Uses information from a webservice interface to determine the type name and to find the exception class for a SOAP fault
142     * <p/>
143     * All three classes is located in the package name org.apache.camel.dataformat.soap.name
144     * <p/>
145     * If you have generated the web service stub code with cxf-codegen or a similar tool then you probably
146     * will want to use the ServiceInterfaceStrategy. In the case you have no annotated service interface you should use QNameStrategy or TypeNameStrategy.
147     */
148    public void setElementNameStrategy(Object elementNameStrategy) {
149        this.elementNameStrategy = elementNameStrategy;
150    }
151
152    public Object getElementNameStrategy() {
153        return elementNameStrategy;
154    }
155
156    public String getNamespacePrefixRef() {
157        return namespacePrefixRef;
158    }
159
160    /**
161     * When marshalling using JAXB or SOAP then the JAXB implementation will automatic assign namespace prefixes,
162     * such as ns2, ns3, ns4 etc. To control this mapping, Camel allows you to refer to a map which contains the desired mapping.
163     */
164    public void setNamespacePrefixRef(String namespacePrefixRef) {
165        this.namespacePrefixRef = namespacePrefixRef;
166    }
167    
168    public String getSchema() {
169        return schema;
170    }
171
172    /**
173     * To validate against an existing schema.
174     * Your can use the prefix classpath:, file:* or *http: to specify how the resource should by resolved.
175     * You can separate multiple schema files by using the ',' character.
176     */
177    public void setSchema(String schema) {
178        this.schema = schema;
179    }
180
181    @Override
182    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
183        if (elementNameStrategy != null) {
184            setProperty(camelContext, dataFormat, "elementNameStrategy", elementNameStrategy);
185        }
186        if (elementNameStrategyRef != null) {
187            setProperty(camelContext, dataFormat, "elementNameStrategyRef", elementNameStrategyRef);
188        }
189        if (encoding != null) {
190            setProperty(camelContext, dataFormat, "encoding", encoding);
191        }
192        if (version != null) {
193            setProperty(camelContext, dataFormat, "version", version);
194        }
195        if (namespacePrefixRef != null) {
196            setProperty(camelContext, dataFormat, "namespacePrefixRef", namespacePrefixRef);
197        }
198        if (schema != null) {
199            setProperty(camelContext, dataFormat, "schema", schema);
200        }
201        setProperty(camelContext, dataFormat, "contextPath", contextPath);
202    }
203
204}