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.HashMap; 020import java.util.HashSet; 021import java.util.Map; 022import java.util.Set; 023 024import javax.xml.bind.annotation.XmlAccessType; 025import javax.xml.bind.annotation.XmlAccessorType; 026import javax.xml.bind.annotation.XmlAttribute; 027import javax.xml.bind.annotation.XmlRootElement; 028import javax.xml.bind.annotation.XmlTransient; 029 030import org.apache.camel.model.OptionalIdentifiedDefinition; 031import org.apache.camel.spi.Metadata; 032 033/** 034 * To configure rest binding 035 */ 036@Metadata(label = "rest") 037@XmlRootElement(name = "restBinding") 038@XmlAccessorType(XmlAccessType.FIELD) 039public class RestBindingDefinition extends OptionalIdentifiedDefinition<RestBindingDefinition> { 040 041 @XmlTransient 042 private Map<String, String> defaultValues; 043 044 @XmlTransient 045 private Boolean requiredBody; 046 047 @XmlTransient 048 private Set<String> requiredHeaders; 049 050 @XmlTransient 051 private Set<String> requiredQueryParameters; 052 053 @XmlAttribute 054 private String consumes; 055 056 @XmlAttribute 057 private String produces; 058 059 @XmlAttribute 060 @Metadata(defaultValue = "off") 061 private RestBindingMode bindingMode; 062 063 @XmlAttribute 064 private String type; 065 066 @XmlAttribute 067 private String outType; 068 069 @XmlAttribute 070 private Boolean skipBindingOnErrorCode; 071 072 @XmlAttribute 073 private Boolean clientRequestValidation; 074 075 @XmlAttribute 076 private Boolean enableCORS; 077 078 @XmlAttribute 079 private String component; 080 081 public RestBindingDefinition() { 082 } 083 084 @Override 085 public String toString() { 086 return "RestBinding"; 087 } 088 089 public String getConsumes() { 090 return consumes; 091 } 092 093 /** 094 * Adds a default value for the query parameter 095 * 096 * @param paramName query parameter name 097 * @param defaultValue the default value 098 */ 099 public void addDefaultValue(String paramName, String defaultValue) { 100 if (defaultValues == null) { 101 defaultValues = new HashMap<>(); 102 } 103 defaultValues.put(paramName, defaultValue); 104 } 105 106 /** 107 * Adds a required query parameter 108 * 109 * @param paramName query parameter name 110 */ 111 public void addRequiredQueryParameter(String paramName) { 112 if (requiredQueryParameters == null) { 113 requiredQueryParameters = new HashSet<>(); 114 } 115 requiredQueryParameters.add(paramName); 116 } 117 118 public Set<String> getRequiredQueryParameters() { 119 return requiredQueryParameters; 120 } 121 122 /** 123 * Adds a required HTTP header 124 * 125 * @param headerName HTTP header name 126 */ 127 public void addRequiredHeader(String headerName) { 128 if (requiredHeaders == null) { 129 requiredHeaders = new HashSet<>(); 130 } 131 requiredHeaders.add(headerName); 132 } 133 134 public Set<String> getRequiredHeaders() { 135 return requiredHeaders; 136 } 137 138 public Boolean getRequiredBody() { 139 return requiredBody; 140 } 141 142 public void setRequiredBody(Boolean requiredBody) { 143 this.requiredBody = requiredBody; 144 } 145 146 /** 147 * Gets the registered default values for query parameters 148 */ 149 public Map<String, String> getDefaultValues() { 150 return defaultValues; 151 } 152 153 /** 154 * Sets the component name that this definition will apply to 155 */ 156 public void setComponent(String component) { 157 this.component = component; 158 } 159 160 public String getComponent() { 161 return component; 162 } 163 164 /** 165 * To define the content type what the REST service consumes (accept as 166 * input), such as application/xml or application/json 167 */ 168 public void setConsumes(String consumes) { 169 this.consumes = consumes; 170 } 171 172 public String getProduces() { 173 return produces; 174 } 175 176 /** 177 * To define the content type what the REST service produces (uses for 178 * output), such as application/xml or application/json 179 */ 180 public void setProduces(String produces) { 181 this.produces = produces; 182 } 183 184 public RestBindingMode getBindingMode() { 185 return bindingMode; 186 } 187 188 /** 189 * Sets the binding mode to use. 190 * <p/> 191 * The default value is off 192 */ 193 public void setBindingMode(RestBindingMode bindingMode) { 194 this.bindingMode = bindingMode; 195 } 196 197 public String getType() { 198 return type; 199 } 200 201 /** 202 * Sets the class name to use for binding from input to POJO for the 203 * incoming data 204 * <p/> 205 * The canonical name of the class of the input data. Append a [] to the end 206 * of the canonical name if you want the input to be an array type. 207 */ 208 public void setType(String type) { 209 this.type = type; 210 } 211 212 public String getOutType() { 213 return outType; 214 } 215 216 /** 217 * Sets the class name to use for binding from POJO to output for the 218 * outgoing data 219 * <p/> 220 * The canonical name of the class of the input data. Append a [] to the end 221 * of the canonical name if you want the input to be an array type. 222 */ 223 public void setOutType(String outType) { 224 this.outType = outType; 225 } 226 227 public Boolean getSkipBindingOnErrorCode() { 228 return skipBindingOnErrorCode; 229 } 230 231 /** 232 * Whether to skip binding on output if there is a custom HTTP error code 233 * header. This allows to build custom error messages that do not bind to 234 * json / xml etc, as success messages otherwise will do. 235 */ 236 public void setSkipBindingOnErrorCode(Boolean skipBindingOnErrorCode) { 237 this.skipBindingOnErrorCode = skipBindingOnErrorCode; 238 } 239 240 public Boolean getClientRequestValidation() { 241 return clientRequestValidation; 242 } 243 244 /** 245 * Whether to enable validation of the client request to check whether the 246 * Content-Type and Accept headers from the client is supported by the 247 * Rest-DSL configuration of its consumes/produces settings. 248 * <p/> 249 * This can be turned on, to enable this check. In case of validation error, 250 * then HTTP Status codes 415 or 406 is returned. 251 * <p/> 252 * The default value is false. 253 */ 254 public void setClientRequestValidation(Boolean clientRequestValidation) { 255 this.clientRequestValidation = clientRequestValidation; 256 } 257 258 public Boolean getEnableCORS() { 259 return enableCORS; 260 } 261 262 /** 263 * Whether to enable CORS headers in the HTTP response. 264 * <p/> 265 * The default value is false. 266 */ 267 public void setEnableCORS(Boolean enableCORS) { 268 this.enableCORS = enableCORS; 269 } 270 271 @Override 272 public String getShortName() { 273 return "restBinding"; 274 } 275 276 @Override 277 public String getLabel() { 278 return ""; 279 } 280}