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.List;
021
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.XmlElementRef;
027import javax.xml.bind.annotation.XmlElements;
028import javax.xml.bind.annotation.XmlRootElement;
029import javax.xml.bind.annotation.XmlTransient;
030
031
032import org.apache.camel.model.OptionalIdentifiedDefinition;
033import org.apache.camel.model.RouteDefinition;
034import org.apache.camel.model.ToDefinition;
035import org.apache.camel.model.ToDynamicDefinition;
036import org.apache.camel.spi.Metadata;
037
038/**
039 * Rest command
040 */
041@Metadata(label = "rest")
042@XmlRootElement(name = "verb")
043@XmlAccessorType(XmlAccessType.FIELD)
044public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition> {
045
046    @XmlAttribute
047    private String method;
048
049    @XmlElementRef
050    private List<RestOperationParamDefinition> params = new ArrayList<RestOperationParamDefinition>();
051
052    @XmlElementRef
053    private List<RestOperationResponseMsgDefinition> responseMsgs = new ArrayList<RestOperationResponseMsgDefinition>();
054
055    @XmlAttribute
056    private String uri;
057
058    @XmlAttribute
059    private String consumes;
060
061    @XmlAttribute
062    private String produces;
063
064    @XmlAttribute
065    @Metadata(defaultValue = "auto")
066    private RestBindingMode bindingMode;
067
068    @XmlAttribute
069    private Boolean skipBindingOnErrorCode;
070
071    @XmlAttribute
072    private Boolean enableCORS;
073
074    @XmlAttribute
075    private String type;
076
077    @XmlAttribute
078    private String outType;
079
080    // used by XML DSL to either select a <to>, <toD>, or <route>
081    // so we need to use the common type OptionalIdentifiedDefinition
082    // must select one of them, and hence why they are all set to required = true, but the XSD is set to only allow one of the element
083    @XmlElements({
084            @XmlElement(required = true, name = "to", type = ToDefinition.class),
085            @XmlElement(required = true, name = "toD", type = ToDynamicDefinition.class),
086            @XmlElement(required = true, name = "route", type = RouteDefinition.class)}
087        )
088    private OptionalIdentifiedDefinition<?> toOrRoute;
089
090    // the Java DSL uses the to or route definition directory
091    @XmlTransient
092    private ToDefinition to;
093    @XmlTransient
094    private ToDynamicDefinition toD;
095    @XmlTransient
096    private RouteDefinition route;
097    @XmlTransient
098    private RestDefinition rest;
099    @XmlAttribute
100    private String routeId;
101    @XmlAttribute
102    private Boolean apiDocs;
103
104    @Override
105    public String getLabel() {
106        if (method != null) {
107            return method;
108        } else {
109            return "verb";
110        }
111    }
112
113    public List<RestOperationParamDefinition> getParams() {
114        return params;
115    }
116
117    /**
118     * To specify the REST operation parameters using Swagger.
119     */
120    public void setParams(List<RestOperationParamDefinition> params) {
121        this.params = params;
122    }
123
124    public List<RestOperationResponseMsgDefinition> getResponseMsgs() {
125        return responseMsgs;
126    }
127
128    /**
129     * Sets swagger operation response messages
130     */
131    public void setResponseMsgs(List<RestOperationResponseMsgDefinition> params) {
132        this.responseMsgs = responseMsgs;
133    }
134
135    public String getMethod() {
136        return method;
137    }
138
139    /**
140     * The HTTP verb such as GET or POST
141     */
142    public void setMethod(String method) {
143        this.method = method;
144    }
145
146    public String getUri() {
147        return uri;
148    }
149
150    /**
151     * Uri template of this REST service such as /{id}.
152     */
153    public void setUri(String uri) {
154        this.uri = uri;
155    }
156
157    public String getConsumes() {
158        return consumes;
159    }
160
161    /**
162     * To define the content type what the REST service consumes (accept as input), such as application/xml or application/json.
163     * This option will override what may be configured on a parent level
164     */
165    public void setConsumes(String consumes) {
166        this.consumes = consumes;
167    }
168
169    public String getProduces() {
170        return produces;
171    }
172
173    /**
174     * To define the content type what the REST service produces (uses for output), such as application/xml or application/json
175     * This option will override what may be configured on a parent level
176     */
177    public void setProduces(String produces) {
178        this.produces = produces;
179    }
180
181    public RestBindingMode getBindingMode() {
182        return bindingMode;
183    }
184
185    /**
186     * Sets the binding mode to use.
187     * This option will override what may be configured on a parent level
188     * <p/>
189     * The default value is auto
190     */
191    public void setBindingMode(RestBindingMode bindingMode) {
192        this.bindingMode = bindingMode;
193    }
194
195    public Boolean getSkipBindingOnErrorCode() {
196        return skipBindingOnErrorCode;
197    }
198
199    /**
200     * Whether to skip binding on output if there is a custom HTTP error code header.
201     * This allows to build custom error messages that do not bind to json / xml etc, as success messages otherwise will do.
202     * This option will override what may be configured on a parent level
203     */
204    public void setSkipBindingOnErrorCode(Boolean skipBindingOnErrorCode) {
205        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
206    }
207
208    public Boolean getEnableCORS() {
209        return enableCORS;
210    }
211
212    /**
213     * Whether to enable CORS headers in the HTTP response.
214     * This option will override what may be configured on a parent level
215     * <p/>
216     * The default value is false.
217     */
218    public void setEnableCORS(Boolean enableCORS) {
219        this.enableCORS = enableCORS;
220    }
221
222    public String getType() {
223        return type;
224    }
225
226    /**
227     * Sets the class name to use for binding from input to POJO for the incoming data
228     * This option will override what may be configured on a parent level
229     */
230    public void setType(String type) {
231        this.type = type;
232    }
233
234    public String getOutType() {
235        return outType;
236    }
237
238    /**
239     * Sets the class name to use for binding from POJO to output for the outgoing data
240     * This option will override what may be configured on a parent level
241     */
242    public void setOutType(String outType) {
243        this.outType = outType;
244    }
245
246    public String getRouteId() {
247        return routeId;
248    }
249
250    /**
251     * The route id this rest-dsl is using (read-only)
252     */
253    public void setRouteId(String routeId) {
254        this.routeId = routeId;
255    }
256
257    public Boolean getApiDocs() {
258        return apiDocs;
259    }
260
261    /**
262     * Whether to include or exclude the VerbDefinition in API documentation.
263     * <p/>
264     * The default value is true.
265     */
266    public void setApiDocs(Boolean apiDocs) {
267        this.apiDocs = apiDocs;
268    }
269
270    public RestDefinition getRest() {
271        return rest;
272    }
273
274    public void setRest(RestDefinition rest) {
275        this.rest = rest;
276    }
277
278    public RouteDefinition getRoute() {
279        if (route != null) {
280            return route;
281        } else if (toOrRoute instanceof RouteDefinition) {
282            return (RouteDefinition) toOrRoute;
283        } else {
284            return null;
285        }
286    }
287
288    public void setRoute(RouteDefinition route) {
289        this.route = route;
290        this.toOrRoute = route;
291    }
292
293    public ToDefinition getTo() {
294        if (to != null) {
295            return to;
296        } else if (toOrRoute instanceof ToDefinition) {
297            return (ToDefinition) toOrRoute;
298        } else {
299            return null;
300        }
301    }
302
303    public ToDynamicDefinition getToD() {
304        if (toD != null) {
305            return toD;
306        } else if (toOrRoute instanceof ToDynamicDefinition) {
307            return (ToDynamicDefinition) toOrRoute;
308        } else {
309            return null;
310        }
311    }
312
313    public void setTo(ToDefinition to) {
314        this.to = to;
315        this.toD = null;
316        this.toOrRoute = to;
317    }
318
319    public void setToD(ToDynamicDefinition to) {
320        this.to = null;
321        this.toD = to;
322        this.toOrRoute = to;
323    }
324
325    public OptionalIdentifiedDefinition<?> getToOrRoute() {
326        return toOrRoute;
327    }
328
329    /**
330     * To route from this REST service to a Camel endpoint, or an inlined route
331     */
332    public void setToOrRoute(OptionalIdentifiedDefinition<?> toOrRoute) {
333        this.toOrRoute = toOrRoute;
334    }
335
336    // Fluent API
337    // -------------------------------------------------------------------------
338
339    public RestDefinition get() {
340        return rest.get();
341    }
342
343    public RestDefinition get(String uri) {
344        return rest.get(uri);
345    }
346
347    public RestDefinition post() {
348        return rest.post();
349    }
350
351    public RestDefinition post(String uri) {
352        return rest.post(uri);
353    }
354
355    public RestDefinition put() {
356        return rest.put();
357    }
358
359    public RestDefinition put(String uri) {
360        return rest.put(uri);
361    }
362
363    public RestDefinition delete() {
364        return rest.delete();
365    }
366
367    public RestDefinition delete(String uri) {
368        return rest.delete(uri);
369    }
370
371    public RestDefinition head() {
372        return rest.head();
373    }
374
375    public RestDefinition head(String uri) {
376        return rest.head(uri);
377    }
378
379    public RestDefinition verb(String verb) {
380        return rest.verb(verb);
381    }
382
383    public RestDefinition verb(String verb, String uri) {
384        return rest.verb(verb, uri);
385    }
386
387    public String asVerb() {
388        // we do not want the jaxb model to repeat itself, by outputting <get method="get">
389        // so we defer the verb from the instance type
390        if (this instanceof GetVerbDefinition) {
391            return "get";
392        } else if (this instanceof PostVerbDefinition) {
393            return "post";
394        } else if (this instanceof PutVerbDefinition) {
395            return "put";
396        } else if (this instanceof PatchVerbDefinition) {
397            return "patch";
398        } else if (this instanceof DeleteVerbDefinition) {
399            return "delete";
400        } else if (this instanceof HeadVerbDefinition) {
401            return "head";
402        } else if (this instanceof OptionsVerbDefinition) {
403            return "options";
404        } else {
405            return method;
406        }
407    }
408
409
410
411}