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.rest;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlElementWrapper;
027import javax.xml.bind.annotation.XmlRootElement;
028import javax.xml.bind.annotation.XmlTransient;
029
030import org.apache.camel.spi.Metadata;
031import org.apache.camel.util.ObjectHelper;
032
033/**
034 * To specify the rest operation parameters using Swagger.
035 * <p/>
036 * This maps to the Swagger Parameter Message Object.
037 */
038@Metadata(label = "rest")
039@XmlRootElement(name = "param")
040@XmlAccessorType(XmlAccessType.FIELD)
041public class RestOperationParamDefinition {
042
043    @XmlTransient
044    private VerbDefinition verb;
045
046    @XmlAttribute(required = true)
047    private String name;
048
049    @XmlAttribute(required = true)
050    @Metadata(defaultValue = "path")
051    private RestParamType type;
052
053    @XmlAttribute
054    @Metadata(defaultValue = "")
055    private String description;
056
057    @XmlAttribute
058    @Metadata(defaultValue = "")
059    private String defaultValue;
060
061    @XmlAttribute
062    @Metadata(defaultValue = "true")
063    private Boolean required;
064
065    @XmlAttribute
066    @Metadata(defaultValue = "csv")
067    private CollectionFormat collectionFormat;
068
069    @XmlAttribute
070    @Metadata(defaultValue = "string")
071    private String arrayType;
072
073    @XmlAttribute
074    @Metadata(defaultValue = "string")
075    private String dataType;
076
077    @XmlElementWrapper(name = "allowableValues")
078    @XmlElement(name = "value")
079    private List<String> allowableValues;
080
081    @XmlAttribute
082    @Metadata(defaultValue = "")
083    private String access;
084
085    public RestOperationParamDefinition() {
086    }
087
088    public RestOperationParamDefinition(VerbDefinition verb) {
089        this.verb = verb;
090    }
091
092    public RestParamType getType() {
093        return type != null ? type : RestParamType.path;
094    }
095
096    /**
097     * Sets the Swagger Parameter type.
098     */
099    public void setType(RestParamType type) {
100        this.type = type;
101    }
102
103    public String getName() {
104        return name;
105    }
106
107    /**
108     * Sets the Swagger Parameter name.
109     */
110    public void setName(String name) {
111        this.name = name;
112    }
113
114    public String getDescription() {
115        return description != null ? description : "";
116    }
117
118    /**
119     * Sets the Swagger Parameter description.
120     */
121    public void setDescription(String description) {
122        this.description = description;
123    }
124
125    /**
126     * Sets the Swagger Parameter default value.
127     */
128    public String getDefaultValue() {
129        return defaultValue != null ? defaultValue : "";
130    }
131
132    public void setDefaultValue(String defaultValue) {
133        this.defaultValue = defaultValue;
134    }
135
136    public Boolean getRequired() {
137        return required != null ? required : true;
138    }
139
140    /**
141     * Sets the Swagger Parameter required flag.
142     */
143    public void setRequired(Boolean required) {
144        this.required = required;
145    }
146
147    public CollectionFormat getCollectionFormat() {
148        return collectionFormat;
149    }
150
151    /**
152     * Sets the Swagger Parameter collection format.
153     */
154    public void setCollectionFormat(CollectionFormat collectionFormat) {
155        this.collectionFormat = collectionFormat;
156    }
157
158    public String getArrayType() {
159        return arrayType;
160    }
161
162    /**
163     * Sets the Swagger Parameter array type.
164     * Required if data type is "array". Describes the type of items in the array.
165     */
166    public void setArrayType(String arrayType) {
167        this.arrayType = arrayType;
168    }
169
170    public String getDataType() {
171        return dataType != null ? dataType : "string";
172    }
173
174    /**
175     * Sets the Swagger Parameter data type.
176     */
177    public void setDataType(String dataType) {
178        this.dataType = dataType;
179    }
180
181    public List<String> getAllowableValues() {
182        if (allowableValues != null) {
183            return allowableValues;
184        }
185
186        return new ArrayList<String>();
187    }
188
189    /**
190     * Sets the Swagger Parameter list of allowable values (enum).
191     */
192    public void setAllowableValues(List<String> allowableValues) {
193        this.allowableValues = allowableValues;
194    }
195
196    /**
197     * Gets the Swagger Parameter paramAccess flag.
198     *
199     * @deprecated is not in use in swagger specification 2.0
200     */
201    @Deprecated
202    public String getAccess() {
203        return access != null ? access : "";
204    }
205
206    /**
207     * Sets the Swagger Parameter paramAccess flag.
208     *
209     * @deprecated is not in use in swagger specification 2.0
210     */
211    @Deprecated
212    public void setAccess(String access) {
213        this.access = access;
214    }
215
216    /**
217     * Name of the parameter.
218     * <p/>
219     * This option is mandatory.
220     */
221    public RestOperationParamDefinition name(String name) {
222        setName(name);
223        return this;
224    }
225
226    /**
227     * Description of the parameter.
228     */
229    public RestOperationParamDefinition description(String name) {
230        setDescription(name);
231        return this;
232    }
233
234    /**
235     * The default value of the parameter.
236     */
237    public RestOperationParamDefinition defaultValue(String name) {
238        setDefaultValue(name);
239        return this;
240    }
241
242    /**
243     * Whether the parameter is required
244     */
245    public RestOperationParamDefinition required(Boolean required) {
246        setRequired(required);
247        return this;
248    }
249
250    /**
251     * Sets the collection format.
252     */
253    public RestOperationParamDefinition collectionFormat(CollectionFormat collectionFormat) {
254        setCollectionFormat(collectionFormat);
255        return this;
256    }
257
258    /**
259     * The data type of the array data type
260     */
261    public RestOperationParamDefinition arrayType(String arrayType) {
262        setArrayType(arrayType);
263        return this;
264    }
265
266    /**
267     * The data type of the parameter such as <tt>string</tt>, <tt>integer</tt>, <tt>boolean</tt>
268     */
269    public RestOperationParamDefinition dataType(String type) {
270        setDataType(type);
271        return this;
272    }
273
274    /**
275     * Allowed values of the parameter when its an enum type
276     */
277    public RestOperationParamDefinition allowableValues(List<String> allowableValues) {
278        setAllowableValues(allowableValues);
279        return this;
280    }
281
282    /**
283     * Allowed values of the parameter when its an enum type
284     */
285    public RestOperationParamDefinition allowableValues(String... allowableValues) {
286        setAllowableValues(Arrays.asList(allowableValues));
287        return this;
288    }
289
290    /**
291     * The parameter type such as body, form, header, path, query
292     */
293    public RestOperationParamDefinition type(RestParamType type) {
294        setType(type);
295        return this;
296    }
297
298    /**
299     * Parameter access. Use <tt>false</tt> or <tt>internal</tt> to indicate the parameter
300     * should be hidden for the public.
301     *
302     * @deprecated is not in use in swagger specification 2.0
303     */
304    @Deprecated
305    public RestOperationParamDefinition access(String paramAccess) {
306        setAccess(paramAccess);
307        return this;
308    }
309
310    /**
311     * Ends the configuration of this parameter
312     */
313    public RestDefinition endParam() {
314        // name is mandatory
315        ObjectHelper.notEmpty(name, "name");
316        verb.getParams().add(this);
317        return verb.getRest();
318    }
319
320}