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.spi;
018
019import java.util.Map;
020
021/**
022 * Configuration use by {@link org.apache.camel.spi.RestConsumerFactory} and {@link org.apache.camel.spi.RestApiConsumerFactory}
023 * for Camel components to support the Camel {@link org.apache.camel.model.rest.RestDefinition rest} DSL.
024 */
025public class RestConfiguration {
026
027    public static final String CORS_ACCESS_CONTROL_ALLOW_ORIGIN = "*";
028    public static final String CORS_ACCESS_CONTROL_ALLOW_METHODS = "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH";
029    public static final String CORS_ACCESS_CONTROL_MAX_AGE = "3600";
030    public static final String CORS_ACCESS_CONTROL_ALLOW_HEADERS = "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers";
031
032    public enum RestBindingMode {
033        auto, off, json, xml, json_xml
034    }
035
036    public enum RestHostNameResolver {
037        allLocalIp, localIp, localHostName
038    }
039
040    private String component;
041    private String apiComponent;
042    private String producerComponent;
043    private String producerApiDoc;
044    private String scheme;
045    private String host;
046    private int port;
047    private String contextPath;
048    private String apiContextPath;
049    private String apiContextRouteId;
050    private String apiContextIdPattern;
051    private boolean apiContextListing;
052    private RestHostNameResolver restHostNameResolver = RestHostNameResolver.allLocalIp;
053    private RestBindingMode bindingMode = RestBindingMode.off;
054    private boolean skipBindingOnErrorCode = true;
055    private boolean enableCORS;
056    private String jsonDataFormat;
057    private String xmlDataFormat;
058    private Map<String, Object> componentProperties;
059    private Map<String, Object> endpointProperties;
060    private Map<String, Object> consumerProperties;
061    private Map<String, Object> dataFormatProperties;
062    private Map<String, Object> apiProperties;
063    private Map<String, String> corsHeaders;
064
065    /**
066     * Gets the name of the Camel component to use as the REST consumer
067     *
068     * @return the component name, or <tt>null</tt> to let Camel search the {@link Registry} to find suitable implementation
069     */
070    public String getComponent() {
071        return component;
072    }
073
074    /**
075     * Sets the name of the Camel component to use as the REST consumer
076     *
077     * @param componentName the name of the component (such as restlet, spark-rest, etc.)
078     */
079    public void setComponent(String componentName) {
080        this.component = componentName;
081    }
082
083    /**
084     * Gets the name of the Camel component to use as the REST API (such as swagger)
085     *
086     * @return the component name, or <tt>null</tt> to let Camel use the default name <tt>swagger</tt>
087     */
088    public String getApiComponent() {
089        return apiComponent;
090    }
091
092    /**
093     * Sets the name of the Camel component to use as the REST API (such as swagger)
094     *
095     * @param apiComponent the name of the component (such as swagger)
096     */
097    public void setApiComponent(String apiComponent) {
098        this.apiComponent = apiComponent;
099    }
100
101    /**
102     * Gets the name of the Camel component to use as the REST producer
103     *
104     * @return the component name, or <tt>null</tt> to let Camel search the {@link Registry} to find suitable implementation
105     */
106    public String getProducerComponent() {
107        return producerComponent;
108    }
109
110    /**
111     * Sets the name of the Camel component to use as the REST producer
112     *
113     * @param componentName the name of the component (such as restlet, jetty, etc.)
114     */
115    public void setProducerComponent(String componentName) {
116        this.producerComponent = componentName;
117    }
118
119    /**
120     * Gets the location of the api document (swagger api) the REST producer will use
121     * to validate the REST uri and query parameters are valid accordingly to the api document.
122     */
123    public String getProducerApiDoc() {
124        return producerApiDoc;
125    }
126
127    /**
128     * Sets the location of the api document (swagger api) the REST producer will use
129     * to validate the REST uri and query parameters are valid accordingly to the api document.
130     * This requires adding camel-swagger-java to the classpath, and any miss configuration
131     * will let Camel fail on startup and report the error(s).
132     * <p/>
133     * The location of the api document is loaded from classpath by default, but you can use
134     * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url.
135     */
136    public void setProducerApiDoc(String producerApiDoc) {
137        this.producerApiDoc = producerApiDoc;
138    }
139
140    /**
141     * Gets the hostname to use by the REST consumer
142     *
143     * @return the hostname, or <tt>null</tt> to use default hostname
144     */
145    public String getHost() {
146        return host;
147    }
148
149    /**
150     * Sets the hostname to use by the REST consumer
151     *
152     * @param host the hostname
153     */
154    public void setHost(String host) {
155        this.host = host;
156    }
157
158    /**
159     * Gets the scheme to use by the REST consumer
160     *
161     * @return the scheme, or <tt>null</tt> to use default scheme
162     */
163    public String getScheme() {
164        return scheme;
165    }
166
167    /**
168     * Sets the scheme to use by the REST consumer
169     *
170     * @param scheme the scheme
171     */
172    public void setScheme(String scheme) {
173        this.scheme = scheme;
174    }
175
176    /**
177     * Gets the port to use by the REST consumer
178     *
179     * @return the port, or <tt>0</tt> or <tt>-1</tt> to use default port
180     */
181    public int getPort() {
182        return port;
183    }
184
185    /**
186     * Sets the port to use by the REST consumer
187     *
188     * @param port the port number
189     */
190    public void setPort(int port) {
191        this.port = port;
192    }
193
194    /**
195     * Gets the configured context-path
196     *
197     * @return the context path, or <tt>null</tt> if none configured.
198     */
199    public String getContextPath() {
200        return contextPath;
201    }
202
203    /**
204     * Sets a leading context-path the REST services will be using.
205     * <p/>
206     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
207     * is deployed using a context-path. Or for components such as <tt>camel-jetty</tt> or <tt>camel-netty4-http</tt>
208     * that includes a HTTP server.
209     *
210     * @param contextPath the context path
211     */
212    public void setContextPath(String contextPath) {
213        this.contextPath = contextPath;
214    }
215
216    public String getApiContextPath() {
217        return apiContextPath;
218    }
219
220    /**
221     * Sets a leading API context-path the REST API services will be using.
222     * <p/>
223     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
224     * is deployed using a context-path.
225     *
226     * @param contextPath the API context path
227     */
228    public void setApiContextPath(String contextPath) {
229        this.apiContextPath = contextPath;
230    }
231
232    public String getApiContextRouteId() {
233        return apiContextRouteId;
234    }
235
236    /**
237     * Sets the route id to use for the route that services the REST API.
238     * <p/>
239     * The route will by default use an auto assigned route id.
240     *
241     * @param apiContextRouteId  the route id
242     */
243    public void setApiContextRouteId(String apiContextRouteId) {
244        this.apiContextRouteId = apiContextRouteId;
245    }
246
247    public String getApiContextIdPattern() {
248        return apiContextIdPattern;
249    }
250
251    /**
252     * Optional CamelContext id pattern to only allow Rest APIs from rest services within CamelContext's which name matches the pattern.
253     * <p/>
254     * The pattern <tt>#name#</tt> refers to the CamelContext name, to match on the current CamelContext only.
255     * For any other value, the pattern uses the rules from {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)}
256     *
257     * @param apiContextIdPattern  the pattern
258     */
259    public void setApiContextIdPattern(String apiContextIdPattern) {
260        this.apiContextIdPattern = apiContextIdPattern;
261    }
262
263    public boolean isApiContextListing() {
264        return apiContextListing;
265    }
266
267    /**
268     * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover
269     * these contexts, if <tt>false</tt> then only the current CamelContext is in use.
270     */
271    public void setApiContextListing(boolean apiContextListing) {
272        this.apiContextListing = apiContextListing;
273    }
274
275    /**
276     * Gets the resolver to use for resolving hostname
277     *
278     * @return the resolver
279     */
280    public RestHostNameResolver getRestHostNameResolver() {
281        return restHostNameResolver;
282    }
283
284    /**
285     * Sets the resolver to use for resolving hostname
286     *
287     * @param restHostNameResolver the resolver
288     */
289    public void setRestHostNameResolver(RestHostNameResolver restHostNameResolver) {
290        this.restHostNameResolver = restHostNameResolver;
291    }
292
293    /**
294     * Sets the resolver to use for resolving hostname
295     *
296     * @param restHostNameResolver the resolver
297     */
298    public void setRestHostNameResolver(String restHostNameResolver) {
299        this.restHostNameResolver = RestHostNameResolver.valueOf(restHostNameResolver);
300    }
301
302    /**
303     * Gets the binding mode used by the REST consumer
304     *
305     * @return the binding mode
306     */
307    public RestBindingMode getBindingMode() {
308        return bindingMode;
309    }
310
311    /**
312     * Sets the binding mode to be used by the REST consumer
313     *
314     * @param bindingMode the binding mode
315     */
316    public void setBindingMode(RestBindingMode bindingMode) {
317        this.bindingMode = bindingMode;
318    }
319
320    /**
321     * Sets the binding mode to be used by the REST consumer
322     *
323     * @param bindingMode the binding mode
324     */
325    public void setBindingMode(String bindingMode) {
326        this.bindingMode = RestBindingMode.valueOf(bindingMode);
327    }
328
329    /**
330     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
331     * <p/>
332     * This option is default <tt>true</tt>.
333     *
334     * @return whether to skip binding on error code
335     */
336    public boolean isSkipBindingOnErrorCode() {
337        return skipBindingOnErrorCode;
338    }
339
340    /**
341     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
342     * <p/>
343     * This option is default <tt>true</tt>.
344     *
345     * @param skipBindingOnErrorCode whether to skip binding on error code
346     */
347    public void setSkipBindingOnErrorCode(boolean skipBindingOnErrorCode) {
348        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
349    }
350
351    /**
352     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
353     * <p/>
354     * This option is default <tt>false</tt>
355     *
356     * @return whether CORS is enabled or not
357     */
358    public boolean isEnableCORS() {
359        return enableCORS;
360    }
361
362    /**
363     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
364     * <p/>
365     * This option is default <tt>false</tt>
366     *
367     * @param enableCORS <tt>true</tt> to enable CORS
368     */
369    public void setEnableCORS(boolean enableCORS) {
370        this.enableCORS = enableCORS;
371    }
372
373    /**
374     * Gets the name of the json data format.
375     * <p/>
376     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
377     *
378     * @return the name, or <tt>null</tt> to use default
379     */
380    public String getJsonDataFormat() {
381        return jsonDataFormat;
382    }
383
384    /**
385     * Sets a custom json data format to be used
386     * <p/>
387     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
388     *
389     * @param name name of the data format
390     */
391    public void setJsonDataFormat(String name) {
392        this.jsonDataFormat = name;
393    }
394
395    /**
396     * Gets the name of the xml data format.
397     * <p/>
398     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
399     *
400     * @return the name, or <tt>null</tt> to use default
401     */
402    public String getXmlDataFormat() {
403        return xmlDataFormat;
404    }
405
406    /**
407     * Sets a custom xml data format to be used.
408     * <p/>
409     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
410     *
411     * @param name name of the data format
412     */
413    public void setXmlDataFormat(String name) {
414        this.xmlDataFormat = name;
415    }
416
417    /**
418     * Gets additional options on component level
419     *
420     * @return additional options
421     */
422    public Map<String, Object> getComponentProperties() {
423        return componentProperties;
424    }
425
426    /**
427     * Sets additional options on component level
428     *
429     * @param componentProperties the options
430     */
431    public void setComponentProperties(Map<String, Object> componentProperties) {
432        this.componentProperties = componentProperties;
433    }
434
435    /**
436     * Gets additional options on endpoint level
437     *
438     * @return additional options
439     */
440    public Map<String, Object> getEndpointProperties() {
441        return endpointProperties;
442    }
443
444    /**
445     * Sets additional options on endpoint level
446     *
447     * @param endpointProperties the options
448     */
449    public void setEndpointProperties(Map<String, Object> endpointProperties) {
450        this.endpointProperties = endpointProperties;
451    }
452
453    /**
454     * Gets additional options on consumer level
455     *
456     * @return additional options
457     */
458    public Map<String, Object> getConsumerProperties() {
459        return consumerProperties;
460    }
461
462    /**
463     * Sets additional options on consumer level
464     *
465     * @param consumerProperties the options
466     */
467    public void setConsumerProperties(Map<String, Object> consumerProperties) {
468        this.consumerProperties = consumerProperties;
469    }
470
471    /**
472     * Gets additional options on data format level
473     *
474     * @return additional options
475     */
476    public Map<String, Object> getDataFormatProperties() {
477        return dataFormatProperties;
478    }
479
480    /**
481     * Sets additional options on data format level
482     *
483     * @param dataFormatProperties the options
484     */
485    public void setDataFormatProperties(Map<String, Object> dataFormatProperties) {
486        this.dataFormatProperties = dataFormatProperties;
487    }
488
489    public Map<String, Object> getApiProperties() {
490        return apiProperties;
491    }
492
493    /**
494     * Sets additional options on api level
495     *
496     * @param apiProperties the options
497     */
498    public void setApiProperties(Map<String, Object> apiProperties) {
499        this.apiProperties = apiProperties;
500    }
501
502    /**
503     * Gets the CORS headers to use if CORS has been enabled.
504     *
505     * @return the CORS headers
506     */
507    public Map<String, String> getCorsHeaders() {
508        return corsHeaders;
509    }
510
511    /**
512     * Sets the CORS headers to use if CORS has been enabled.
513     *
514     * @param corsHeaders the CORS headers
515     */
516    public void setCorsHeaders(Map<String, String> corsHeaders) {
517        this.corsHeaders = corsHeaders;
518    }
519}