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;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlAttribute;
026import javax.xml.bind.annotation.XmlElement;
027import javax.xml.bind.annotation.XmlElementWrapper;
028import javax.xml.bind.annotation.XmlRootElement;
029import javax.xml.bind.annotation.XmlTransient;
030
031import org.apache.camel.spi.Metadata;
032import org.apache.camel.util.StringHelper;
033
034/**
035 * To specify the rest operation response headers using Swagger.
036 * <p>
037 * This maps to the Swagger Response Header Object.
038 */
039@Metadata(label = "rest")
040@XmlRootElement(name = "responseHeader")
041@XmlAccessorType(XmlAccessType.FIELD)
042public class RestOperationResponseHeaderDefinition {
043
044    @XmlTransient
045    private RestOperationResponseMsgDefinition response;
046
047    @XmlAttribute(required = true)
048    private String name;
049
050    @XmlAttribute
051    @Metadata(defaultValue = "")
052    private String description;
053
054    @XmlAttribute
055    @Metadata(defaultValue = "csv")
056    private CollectionFormat collectionFormat;
057
058    @XmlAttribute
059    @Metadata(defaultValue = "string")
060    private String arrayType;
061
062    @XmlAttribute
063    @Metadata(defaultValue = "string")
064    private String dataType;
065
066    @XmlAttribute
067    private String dataFormat;
068
069    @XmlElementWrapper(name = "allowableValues")
070    @XmlElement(name = "value")
071    private List<String> allowableValues;
072
073    @XmlAttribute
074    private String example;
075
076    public RestOperationResponseHeaderDefinition(RestOperationResponseMsgDefinition response) {
077        this();
078        this.response = response;
079    }
080
081    public RestOperationResponseHeaderDefinition() {
082        this.collectionFormat = CollectionFormat.csv;
083        this.arrayType = "string";
084        this.dataType = "string";
085    }
086
087    /**
088     * Ends the configuration of this response message
089     */
090    public RestOperationResponseMsgDefinition endResponseHeader() {
091        return response;
092    }
093
094    public String getName() {
095        return name;
096    }
097
098    public void setName(String name) {
099        this.name = name;
100    }
101
102    public String getDescription() {
103        return description;
104    }
105
106    public void setDescription(String description) {
107        this.description = description;
108    }
109
110    public CollectionFormat getCollectionFormat() {
111        return collectionFormat;
112    }
113
114    /**
115     * Sets the Swagger Parameter collection format.
116     */
117    public void setCollectionFormat(CollectionFormat collectionFormat) {
118        this.collectionFormat = collectionFormat;
119    }
120
121    public String getArrayType() {
122        return arrayType;
123    }
124
125    /**
126     * Sets the Swagger Parameter array type. Required if data type is "array".
127     * Describes the type of items in the array.
128     */
129    public void setArrayType(String arrayType) {
130        this.arrayType = arrayType;
131    }
132
133    public String getDataType() {
134        return dataType;
135    }
136
137    /**
138     * Sets the Swagger header data type.
139     */
140    public void setDataType(String dataType) {
141        this.dataType = dataType;
142    }
143
144    public String getDataFormat() {
145        return dataFormat;
146    }
147
148    /**
149     * Sets the Swagger Parameter data format.
150     */
151    public void setDataFormat(String dataFormat) {
152        this.dataFormat = dataFormat;
153    }
154
155    public List<String> getAllowableValues() {
156        if (allowableValues != null) {
157            return allowableValues;
158        }
159
160        return new ArrayList<>();
161    }
162
163    public String getExample() {
164        return example;
165    }
166
167    /**
168     * Sets the Swagger example
169     */
170    public void setExample(String example) {
171        this.example = example;
172    }
173
174    /**
175     * Sets the Swagger Parameter list of allowable values.
176     */
177    public void setAllowableValues(List<String> allowableValues) {
178        this.allowableValues = allowableValues;
179    }
180
181    /**
182     * Name of the parameter.
183     * <p>
184     * This option is mandatory.
185     */
186    public RestOperationResponseHeaderDefinition name(String name) {
187        setName(name);
188        return this;
189    }
190
191    /**
192     * Description of the parameter.
193     */
194    public RestOperationResponseHeaderDefinition description(String name) {
195        setDescription(name);
196        return this;
197    }
198
199    /**
200     * Sets the collection format.
201     */
202    public RestOperationResponseHeaderDefinition collectionFormat(CollectionFormat collectionFormat) {
203        setCollectionFormat(collectionFormat);
204        return this;
205    }
206
207    /**
208     * The data type of the array data type
209     */
210    public RestOperationResponseHeaderDefinition arrayType(String arrayType) {
211        setArrayType(arrayType);
212        return this;
213    }
214
215    /**
216     * The data type of the header such as <tt>string</tt>, <tt>integer</tt>,
217     * <tt>boolean</tt>
218     */
219    public RestOperationResponseHeaderDefinition dataType(String type) {
220        setDataType(type);
221        return this;
222    }
223
224    /**
225     * The data format of the parameter such as <tt>binary</tt>, <tt>date</tt>,
226     * <tt>date-time</tt>, <tt>password</tt>. The format is usually derived from
227     * the dataType alone. However you can set this option for more fine grained
228     * control of the format in use.
229     */
230    public RestOperationResponseHeaderDefinition dataFormat(String type) {
231        setDataFormat(type);
232        return this;
233    }
234
235    /**
236     * Allowed values of the header when its an enum type
237     */
238    public RestOperationResponseHeaderDefinition allowableValues(List<String> allowableValues) {
239        setAllowableValues(allowableValues);
240        return this;
241    }
242
243    /**
244     * Allowed values of the parameter when its an enum type
245     */
246    public RestOperationResponseHeaderDefinition allowableValues(String... allowableValues) {
247        setAllowableValues(Arrays.asList(allowableValues));
248        return this;
249    }
250
251    /**
252     * Sets an example of this header.
253     */
254    public RestOperationResponseHeaderDefinition example(String example) {
255        setExample(example);
256        return this;
257    }
258
259    /**
260     * Ends the configuration of this header
261     */
262    public RestOperationResponseMsgDefinition endHeader() {
263        // name and type is mandatory
264        StringHelper.notEmpty(name, "name");
265        StringHelper.notEmpty(dataType, "dataType");
266        return response;
267    }
268
269}