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    @XmlAttribute
078    private String dataFormat;
079
080    @XmlElementWrapper(name = "allowableValues")
081    @XmlElement(name = "value")
082    private List<String> allowableValues;
083
084    @XmlAttribute
085    @Metadata(defaultValue = "")
086    private String access;
087
088    public RestOperationParamDefinition() {
089    }
090
091    public RestOperationParamDefinition(VerbDefinition verb) {
092        this.verb = verb;
093    }
094
095    public RestParamType getType() {
096        return type != null ? type : RestParamType.path;
097    }
098
099    /**
100     * Sets the Swagger Parameter type.
101     */
102    public void setType(RestParamType type) {
103        this.type = type;
104    }
105
106    public String getName() {
107        return name;
108    }
109
110    /**
111     * Sets the Swagger Parameter name.
112     */
113    public void setName(String name) {
114        this.name = name;
115    }
116
117    public String getDescription() {
118        return description != null ? description : "";
119    }
120
121    /**
122     * Sets the Swagger Parameter description.
123     */
124    public void setDescription(String description) {
125        this.description = description;
126    }
127
128    /**
129     * Sets the Swagger Parameter default value.
130     */
131    public String getDefaultValue() {
132        return defaultValue != null ? defaultValue : "";
133    }
134
135    public void setDefaultValue(String defaultValue) {
136        this.defaultValue = defaultValue;
137    }
138
139    public Boolean getRequired() {
140        return required != null ? required : true;
141    }
142
143    /**
144     * Sets the Swagger Parameter required flag.
145     */
146    public void setRequired(Boolean required) {
147        this.required = required;
148    }
149
150    public CollectionFormat getCollectionFormat() {
151        return collectionFormat;
152    }
153
154    /**
155     * Sets the Swagger Parameter collection format.
156     */
157    public void setCollectionFormat(CollectionFormat collectionFormat) {
158        this.collectionFormat = collectionFormat;
159    }
160
161    public String getArrayType() {
162        return arrayType;
163    }
164
165    /**
166     * Sets the Swagger Parameter array type.
167     * Required if data type is "array". Describes the type of items in the array.
168     */
169    public void setArrayType(String arrayType) {
170        this.arrayType = arrayType;
171    }
172
173    public String getDataType() {
174        return dataType != null ? dataType : "string";
175    }
176
177    /**
178     * Sets the Swagger Parameter data type.
179     */
180    public void setDataType(String dataType) {
181        this.dataType = dataType;
182    }
183
184    public String getDataFormat() {
185        return dataFormat;
186    }
187
188    /**
189     * Sets the Swagger Parameter data format.
190     */
191    public void setDataFormat(String dataFormat) {
192        this.dataFormat = dataFormat;
193    }
194
195    public List<String> getAllowableValues() {
196        if (allowableValues != null) {
197            return allowableValues;
198        }
199
200        return new ArrayList<String>();
201    }
202
203    /**
204     * Sets the Swagger Parameter list of allowable values (enum).
205     */
206    public void setAllowableValues(List<String> allowableValues) {
207        this.allowableValues = allowableValues;
208    }
209
210    /**
211     * Gets the Swagger Parameter paramAccess flag.
212     *
213     * @deprecated is not in use in swagger specification 2.0
214     */
215    @Deprecated
216    public String getAccess() {
217        return access != null ? access : "";
218    }
219
220    /**
221     * Sets the Swagger Parameter paramAccess flag.
222     *
223     * @deprecated is not in use in swagger specification 2.0
224     */
225    @Deprecated
226    public void setAccess(String access) {
227        this.access = access;
228    }
229
230    /**
231     * Name of the parameter.
232     * <p/>
233     * This option is mandatory.
234     */
235    public RestOperationParamDefinition name(String name) {
236        setName(name);
237        return this;
238    }
239
240    /**
241     * Description of the parameter.
242     */
243    public RestOperationParamDefinition description(String name) {
244        setDescription(name);
245        return this;
246    }
247
248    /**
249     * The default value of the parameter.
250     */
251    public RestOperationParamDefinition defaultValue(String name) {
252        setDefaultValue(name);
253        return this;
254    }
255
256    /**
257     * Whether the parameter is required
258     */
259    public RestOperationParamDefinition required(Boolean required) {
260        setRequired(required);
261        return this;
262    }
263
264    /**
265     * Sets the collection format.
266     */
267    public RestOperationParamDefinition collectionFormat(CollectionFormat collectionFormat) {
268        setCollectionFormat(collectionFormat);
269        return this;
270    }
271
272    /**
273     * The data type of the array data type
274     */
275    public RestOperationParamDefinition arrayType(String arrayType) {
276        setArrayType(arrayType);
277        return this;
278    }
279
280    /**
281     * The data type of the parameter such as <tt>string</tt>, <tt>integer</tt>, <tt>boolean</tt>
282     */
283    public RestOperationParamDefinition dataType(String type) {
284        setDataType(type);
285        return this;
286    }
287
288    /**
289     * The data format of the parameter such as <tt>binary</tt>, <tt>date</tt>, <tt>date-time</tt>, <tt>password</tt>.
290     * The format is usually derived from the dataType alone. However you can set this option for more fine grained control
291     * of the format in use.
292     */
293    public RestOperationParamDefinition dataFormat(String type) {
294        setDataFormat(type);
295        return this;
296    }
297
298    /**
299     * Allowed values of the parameter when its an enum type
300     */
301    public RestOperationParamDefinition allowableValues(List<String> allowableValues) {
302        setAllowableValues(allowableValues);
303        return this;
304    }
305
306    /**
307     * Allowed values of the parameter when its an enum type
308     */
309    public RestOperationParamDefinition allowableValues(String... allowableValues) {
310        setAllowableValues(Arrays.asList(allowableValues));
311        return this;
312    }
313
314    /**
315     * The parameter type such as body, form, header, path, query
316     */
317    public RestOperationParamDefinition type(RestParamType type) {
318        setType(type);
319        return this;
320    }
321
322    /**
323     * Parameter access. Use <tt>false</tt> or <tt>internal</tt> to indicate the parameter
324     * should be hidden for the public.
325     *
326     * @deprecated is not in use in swagger specification 2.0
327     */
328    @Deprecated
329    public RestOperationParamDefinition access(String paramAccess) {
330        setAccess(paramAccess);
331        return this;
332    }
333
334    /**
335     * Ends the configuration of this parameter
336     */
337    public RestDefinition endParam() {
338        // name is mandatory
339        ObjectHelper.notEmpty(name, "name");
340        verb.getParams().add(this);
341        return verb.getRest();
342    }
343
344}