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;
030
031@XmlRootElement(name = "verb")
032@XmlAccessorType(XmlAccessType.FIELD)
033public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition> {
034
035    @XmlAttribute
036    private String method;
037
038    @XmlAttribute
039    private String uri;
040
041    @XmlAttribute
042    private String consumes;
043
044    @XmlAttribute
045    private String produces;
046
047    @XmlAttribute
048    private RestBindingMode bindingMode;
049
050    @XmlAttribute
051    private Boolean skipBindingOnErrorCode;
052
053    @XmlAttribute
054    private Boolean enableCORS;
055
056    @XmlAttribute
057    private String type;
058
059    @XmlAttribute
060    private String outType;
061
062    // used by XML DSL to either select a <to> or <route>
063    // so we need to use the common type OptionalIdentifiedDefinition
064    @XmlElements({
065            @XmlElement(required = false, name = "to", type = ToDefinition.class),
066            @XmlElement(required = false, name = "route", type = RouteDefinition.class)}
067    )
068    private OptionalIdentifiedDefinition<?> toOrRoute;
069
070    // the Java DSL uses the to or route definition directory
071    @XmlTransient
072    private ToDefinition to;
073    @XmlTransient
074    private RouteDefinition route;
075    @XmlTransient
076    private RestDefinition rest;
077
078    @Override
079    public String getLabel() {
080        if (method != null) {
081            return method;
082        } else {
083            return "verb";
084        }
085    }
086
087    public String getMethod() {
088        return method;
089    }
090
091    public void setMethod(String method) {
092        this.method = method;
093    }
094
095    public String getUri() {
096        return uri;
097    }
098
099    public void setUri(String uri) {
100        this.uri = uri;
101    }
102
103    public String getConsumes() {
104        return consumes;
105    }
106
107    public void setConsumes(String consumes) {
108        this.consumes = consumes;
109    }
110
111    public String getProduces() {
112        return produces;
113    }
114
115    public void setProduces(String produces) {
116        this.produces = produces;
117    }
118
119    public RestBindingMode getBindingMode() {
120        return bindingMode;
121    }
122
123    public void setBindingMode(RestBindingMode bindingMode) {
124        this.bindingMode = bindingMode;
125    }
126
127    public Boolean getSkipBindingOnErrorCode() {
128        return skipBindingOnErrorCode;
129    }
130
131    public void setSkipBindingOnErrorCode(Boolean skipBindingOnErrorCode) {
132        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
133    }
134
135    public Boolean getEnableCORS() {
136        return enableCORS;
137    }
138
139    public void setEnableCORS(Boolean enableCORS) {
140        this.enableCORS = enableCORS;
141    }
142
143    public String getType() {
144        return type;
145    }
146
147    public void setType(String type) {
148        this.type = type;
149    }
150
151    public String getOutType() {
152        return outType;
153    }
154
155    public void setOutType(String outType) {
156        this.outType = outType;
157    }
158
159    public RestDefinition getRest() {
160        return rest;
161    }
162
163    public void setRest(RestDefinition rest) {
164        this.rest = rest;
165    }
166
167    public RouteDefinition getRoute() {
168        if (route != null) {
169            return route;
170        } else if (toOrRoute instanceof RouteDefinition) {
171            return (RouteDefinition) toOrRoute;
172        } else {
173            return null;
174        }
175    }
176
177    public void setRoute(RouteDefinition route) {
178        this.route = route;
179        this.toOrRoute = route;
180    }
181
182    public ToDefinition getTo() {
183        if (to != null) {
184            return to;
185        } else if (toOrRoute instanceof ToDefinition) {
186            return (ToDefinition) toOrRoute;
187        } else {
188            return null;
189        }
190    }
191
192    public void setTo(ToDefinition to) {
193        this.to = to;
194        this.toOrRoute = to;
195    }
196
197    public OptionalIdentifiedDefinition<?> getToOrRoute() {
198        return toOrRoute;
199    }
200
201    public void setToOrRoute(OptionalIdentifiedDefinition<?> toOrRoute) {
202        this.toOrRoute = toOrRoute;
203    }
204
205    // Fluent API
206    // -------------------------------------------------------------------------
207
208    public RestDefinition get() {
209        return rest.get();
210    }
211
212    public RestDefinition get(String uri) {
213        return rest.get(uri);
214    }
215
216    public RestDefinition post() {
217        return rest.post();
218    }
219
220    public RestDefinition post(String uri) {
221        return rest.post(uri);
222    }
223
224    public RestDefinition put() {
225        return rest.put();
226    }
227
228    public RestDefinition put(String uri) {
229        return rest.put(uri);
230    }
231
232    public RestDefinition delete() {
233        return rest.delete();
234    }
235
236    public RestDefinition delete(String uri) {
237        return rest.delete(uri);
238    }
239
240    public RestDefinition head() {
241        return rest.head();
242    }
243
244    public RestDefinition head(String uri) {
245        return rest.head(uri);
246    }
247
248    public RestDefinition verb(String verb) {
249        return rest.verb(verb);
250    }
251
252    public RestDefinition verb(String verb, String uri) {
253        return rest.verb(verb, uri);
254    }
255
256    public String asVerb() {
257        // we do not want the jaxb model to repeat itself, by outputting <get method="get">
258        // so we defer the verb from the instance type
259        if (this instanceof GetVerbDefinition) {
260            return "get";
261        } else if (this instanceof PostVerbDefinition) {
262            return "post";
263        } else if (this instanceof PutVerbDefinition) {
264            return "put";
265        } else if (this instanceof DeleteVerbDefinition) {
266            return "delete";
267        } else if (this instanceof HeadVerbDefinition) {
268            return "head";
269        } else {
270            return method;
271        }
272    }
273
274}