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 javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlElement; 023import javax.xml.bind.annotation.XmlElements; 024import javax.xml.bind.annotation.XmlRootElement; 025import javax.xml.bind.annotation.XmlTransient; 026 027import org.apache.camel.model.OptionalIdentifiedDefinition; 028import org.apache.camel.model.RouteDefinition; 029import org.apache.camel.model.ToDefinition; 030import org.apache.camel.spi.Metadata; 031 032/** 033 * Rest command 034 */ 035@Metadata(label = "rest") 036@XmlRootElement(name = "verb") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition> { 039 040 @XmlAttribute 041 private String method; 042 043 @XmlAttribute 044 private String uri; 045 046 @XmlAttribute 047 private String consumes; 048 049 @XmlAttribute 050 private String produces; 051 052 @XmlAttribute @Metadata(defaultValue = "auto") 053 private RestBindingMode bindingMode; 054 055 @XmlAttribute 056 private Boolean skipBindingOnErrorCode; 057 058 @XmlAttribute 059 private Boolean enableCORS; 060 061 @XmlAttribute 062 private String type; 063 064 @XmlAttribute 065 private String outType; 066 067 // used by XML DSL to either select a <to> or <route> 068 // so we need to use the common type OptionalIdentifiedDefinition 069 @XmlElements({ 070 @XmlElement(required = false, name = "to", type = ToDefinition.class), 071 @XmlElement(required = false, name = "route", type = RouteDefinition.class)} 072 ) 073 private OptionalIdentifiedDefinition<?> toOrRoute; 074 075 // the Java DSL uses the to or route definition directory 076 @XmlTransient 077 private ToDefinition to; 078 @XmlTransient 079 private RouteDefinition route; 080 @XmlTransient 081 private RestDefinition rest; 082 083 @Override 084 public String getLabel() { 085 if (method != null) { 086 return method; 087 } else { 088 return "verb"; 089 } 090 } 091 092 public String getMethod() { 093 return method; 094 } 095 096 /** 097 * The HTTP verb such as GET or POST 098 */ 099 public void setMethod(String method) { 100 this.method = method; 101 } 102 103 public String getUri() { 104 return uri; 105 } 106 107 /** 108 * Uri template of this REST service such as /{id}. 109 */ 110 public void setUri(String uri) { 111 this.uri = uri; 112 } 113 114 public String getConsumes() { 115 return consumes; 116 } 117 118 /** 119 * To define the content type what the REST service consumes (accept as input), such as application/xml or application/json. 120 * This option will override what may be configured on a parent level 121 */ 122 public void setConsumes(String consumes) { 123 this.consumes = consumes; 124 } 125 126 public String getProduces() { 127 return produces; 128 } 129 130 /** 131 * To define the content type what the REST service produces (uses for output), such as application/xml or application/json 132 * This option will override what may be configured on a parent level 133 */ 134 public void setProduces(String produces) { 135 this.produces = produces; 136 } 137 138 public RestBindingMode getBindingMode() { 139 return bindingMode; 140 } 141 142 /** 143 * Sets the binding mode to use. 144 * This option will override what may be configured on a parent level 145 * <p/> 146 * The default value is auto 147 */ 148 public void setBindingMode(RestBindingMode bindingMode) { 149 this.bindingMode = bindingMode; 150 } 151 152 public Boolean getSkipBindingOnErrorCode() { 153 return skipBindingOnErrorCode; 154 } 155 156 /** 157 * Whether to skip binding on output if there is a custom HTTP error code header. 158 * This allows to build custom error messages that do not bind to json / xml etc, as success messages otherwise will do. 159 * This option will override what may be configured on a parent level 160 */ 161 public void setSkipBindingOnErrorCode(Boolean skipBindingOnErrorCode) { 162 this.skipBindingOnErrorCode = skipBindingOnErrorCode; 163 } 164 165 public Boolean getEnableCORS() { 166 return enableCORS; 167 } 168 169 /** 170 * Whether to enable CORS headers in the HTTP response. 171 * This option will override what may be configured on a parent level 172 * <p/> 173 * The default value is false. 174 */ 175 public void setEnableCORS(Boolean enableCORS) { 176 this.enableCORS = enableCORS; 177 } 178 179 public String getType() { 180 return type; 181 } 182 183 /** 184 * Sets the class name to use for binding from input to POJO for the incoming data 185 * This option will override what may be configured on a parent level 186 */ 187 public void setType(String type) { 188 this.type = type; 189 } 190 191 public String getOutType() { 192 return outType; 193 } 194 195 /** 196 * Sets the class name to use for binding from POJO to output for the outgoing data 197 * This option will override what may be configured on a parent level 198 */ 199 public void setOutType(String outType) { 200 this.outType = outType; 201 } 202 203 public RestDefinition getRest() { 204 return rest; 205 } 206 207 public void setRest(RestDefinition rest) { 208 this.rest = rest; 209 } 210 211 public RouteDefinition getRoute() { 212 if (route != null) { 213 return route; 214 } else if (toOrRoute instanceof RouteDefinition) { 215 return (RouteDefinition) toOrRoute; 216 } else { 217 return null; 218 } 219 } 220 221 public void setRoute(RouteDefinition route) { 222 this.route = route; 223 this.toOrRoute = route; 224 } 225 226 public ToDefinition getTo() { 227 if (to != null) { 228 return to; 229 } else if (toOrRoute instanceof ToDefinition) { 230 return (ToDefinition) toOrRoute; 231 } else { 232 return null; 233 } 234 } 235 236 public void setTo(ToDefinition to) { 237 this.to = to; 238 this.toOrRoute = to; 239 } 240 241 public OptionalIdentifiedDefinition<?> getToOrRoute() { 242 return toOrRoute; 243 } 244 245 /** 246 * To route from this REST service to a Camel endpoint, or an inlined route 247 */ 248 public void setToOrRoute(OptionalIdentifiedDefinition<?> toOrRoute) { 249 this.toOrRoute = toOrRoute; 250 } 251 252 // Fluent API 253 // ------------------------------------------------------------------------- 254 255 public RestDefinition get() { 256 return rest.get(); 257 } 258 259 public RestDefinition get(String uri) { 260 return rest.get(uri); 261 } 262 263 public RestDefinition post() { 264 return rest.post(); 265 } 266 267 public RestDefinition post(String uri) { 268 return rest.post(uri); 269 } 270 271 public RestDefinition put() { 272 return rest.put(); 273 } 274 275 public RestDefinition put(String uri) { 276 return rest.put(uri); 277 } 278 279 public RestDefinition delete() { 280 return rest.delete(); 281 } 282 283 public RestDefinition delete(String uri) { 284 return rest.delete(uri); 285 } 286 287 public RestDefinition head() { 288 return rest.head(); 289 } 290 291 public RestDefinition head(String uri) { 292 return rest.head(uri); 293 } 294 295 public RestDefinition verb(String verb) { 296 return rest.verb(verb); 297 } 298 299 public RestDefinition verb(String verb, String uri) { 300 return rest.verb(verb, uri); 301 } 302 303 public String asVerb() { 304 // we do not want the jaxb model to repeat itself, by outputting <get method="get"> 305 // so we defer the verb from the instance type 306 if (this instanceof GetVerbDefinition) { 307 return "get"; 308 } else if (this instanceof PostVerbDefinition) { 309 return "post"; 310 } else if (this instanceof PutVerbDefinition) { 311 return "put"; 312 } else if (this instanceof DeleteVerbDefinition) { 313 return "delete"; 314 } else if (this instanceof HeadVerbDefinition) { 315 return "head"; 316 } else { 317 return method; 318 } 319 } 320 321}