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 122 public String getArrayType() { 123 return arrayType; 124 } 125 126 /** 127 * Sets the Swagger Parameter array type. 128 * Required if data type is "array". Describes the type of items in the array. 129 */ 130 public void setArrayType(String arrayType) { 131 this.arrayType = arrayType; 132 } 133 134 public String getDataType() { 135 return dataType; 136 } 137 138 /** 139 * Sets the Swagger header data type. 140 */ 141 public void setDataType(String dataType) { 142 this.dataType = dataType; 143 } 144 145 public String getDataFormat() { 146 return dataFormat; 147 } 148 149 /** 150 * Sets the Swagger Parameter data format. 151 */ 152 public void setDataFormat(String dataFormat) { 153 this.dataFormat = dataFormat; 154 } 155 156 public List<String> getAllowableValues() { 157 if (allowableValues != null) { 158 return allowableValues; 159 } 160 161 return new ArrayList<>(); 162 } 163 164 public String getExample() { 165 return example; 166 } 167 168 /** 169 * Sets the Swagger example 170 */ 171 public void setExample(String example) { 172 this.example = example; 173 } 174 175 /** 176 * Sets the Swagger Parameter list of allowable values. 177 */ 178 public void setAllowableValues(List<String> allowableValues) { 179 this.allowableValues = allowableValues; 180 } 181 182 /** 183 * Name of the parameter. 184 * <p> 185 * This option is mandatory. 186 */ 187 public RestOperationResponseHeaderDefinition name(String name) { 188 setName(name); 189 return this; 190 } 191 192 /** 193 * Description of the parameter. 194 */ 195 public RestOperationResponseHeaderDefinition description(String name) { 196 setDescription(name); 197 return this; 198 } 199 200 /** 201 * Sets the collection format. 202 */ 203 public RestOperationResponseHeaderDefinition collectionFormat(CollectionFormat collectionFormat) { 204 setCollectionFormat(collectionFormat); 205 return this; 206 } 207 208 /** 209 * The data type of the array data type 210 */ 211 public RestOperationResponseHeaderDefinition arrayType(String arrayType) { 212 setArrayType(arrayType); 213 return this; 214 } 215 216 /** 217 * The data type of the header such as <tt>string</tt>, <tt>integer</tt>, <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>, <tt>date-time</tt>, <tt>password</tt>. 226 * The format is usually derived from the dataType alone. However you can set this option for more fine grained control 227 * of the format in use. 228 */ 229 public RestOperationResponseHeaderDefinition dataFormat(String type) { 230 setDataFormat(type); 231 return this; 232 } 233 234 /** 235 * Allowed values of the header when its an enum type 236 */ 237 public RestOperationResponseHeaderDefinition allowableValues(List<String> allowableValues) { 238 setAllowableValues(allowableValues); 239 return this; 240 } 241 242 /** 243 * Allowed values of the parameter when its an enum type 244 */ 245 public RestOperationResponseHeaderDefinition allowableValues(String... allowableValues) { 246 setAllowableValues(Arrays.asList(allowableValues)); 247 return this; 248 } 249 250 /** 251 * Sets an example of this header. 252 */ 253 public RestOperationResponseHeaderDefinition example(String example) { 254 setExample(example); 255 return this; 256 } 257 258 /** 259 * Ends the configuration of this header 260 */ 261 public RestOperationResponseMsgDefinition endHeader() { 262 // name and type is mandatory 263 StringHelper.notEmpty(name, "name"); 264 StringHelper.notEmpty(dataType, "dataType"); 265 return response; 266 } 267 268}