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.StringHelper; 032 033/** 034 * To specify the rest operation response headers using Swagger. 035 * <p> 036 * This maps to the Swagger Response Header Object. 037 */ 038@Metadata(label = "rest") 039@XmlRootElement(name = "responseHeader") 040@XmlAccessorType(XmlAccessType.FIELD) 041public class RestOperationResponseHeaderDefinition { 042 043 @XmlTransient 044 private RestOperationResponseMsgDefinition response; 045 046 @XmlAttribute(required = true) 047 private String name; 048 049 @XmlAttribute 050 @Metadata(defaultValue = "") 051 private String description; 052 053 @XmlAttribute 054 @Metadata(defaultValue = "csv") 055 private CollectionFormat collectionFormat; 056 057 @XmlAttribute 058 @Metadata(defaultValue = "string") 059 private String arrayType; 060 061 @XmlAttribute 062 @Metadata(defaultValue = "string") 063 private String dataType; 064 065 @XmlAttribute 066 private String dataFormat; 067 068 @XmlElementWrapper(name = "allowableValues") 069 @XmlElement(name = "value") 070 private List<String> allowableValues; 071 072 @XmlAttribute 073 private String example; 074 075 public RestOperationResponseHeaderDefinition(RestOperationResponseMsgDefinition response) { 076 this(); 077 this.response = response; 078 } 079 080 public RestOperationResponseHeaderDefinition() { 081 this.collectionFormat = CollectionFormat.csv; 082 this.arrayType = "string"; 083 this.dataType = "string"; 084 } 085 086 /** 087 * Ends the configuration of this response message 088 */ 089 public RestOperationResponseMsgDefinition endResponseHeader() { 090 return response; 091 } 092 093 public String getName() { 094 return name; 095 } 096 097 public void setName(String name) { 098 this.name = name; 099 } 100 101 public String getDescription() { 102 return description; 103 } 104 105 public void setDescription(String description) { 106 this.description = description; 107 } 108 109 public CollectionFormat getCollectionFormat() { 110 return collectionFormat; 111 } 112 113 /** 114 * Sets the Swagger Parameter collection format. 115 */ 116 public void setCollectionFormat(CollectionFormat collectionFormat) { 117 this.collectionFormat = collectionFormat; 118 } 119 120 121 public String getArrayType() { 122 return arrayType; 123 } 124 125 /** 126 * Sets the Swagger Parameter array type. 127 * Required if data type is "array". 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<String>(); 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>, <tt>boolean</tt> 217 */ 218 public RestOperationResponseHeaderDefinition dataType(String type) { 219 setDataType(type); 220 return this; 221 } 222 223 /** 224 * The data format of the parameter such as <tt>binary</tt>, <tt>date</tt>, <tt>date-time</tt>, <tt>password</tt>. 225 * The format is usually derived from the dataType alone. However you can set this option for more fine grained control 226 * of the format in use. 227 */ 228 public RestOperationResponseHeaderDefinition dataFormat(String type) { 229 setDataFormat(type); 230 return this; 231 } 232 233 /** 234 * Allowed values of the header when its an enum type 235 */ 236 public RestOperationResponseHeaderDefinition allowableValues(List<String> allowableValues) { 237 setAllowableValues(allowableValues); 238 return this; 239 } 240 241 /** 242 * Allowed values of the parameter when its an enum type 243 */ 244 public RestOperationResponseHeaderDefinition allowableValues(String... allowableValues) { 245 setAllowableValues(Arrays.asList(allowableValues)); 246 return this; 247 } 248 249 /** 250 * Sets an example of this header. 251 */ 252 public RestOperationResponseHeaderDefinition example(String example) { 253 setExample(example); 254 return this; 255 } 256 257 /** 258 * Ends the configuration of this header 259 */ 260 public RestOperationResponseMsgDefinition endHeader() { 261 // name and type is mandatory 262 StringHelper.notEmpty(name, "name"); 263 StringHelper.notEmpty(dataType, "dataType"); 264 return response; 265 } 266 267}