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.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlAttribute;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029
030import org.apache.camel.CamelContext;
031import org.apache.camel.spi.Metadata;
032import org.apache.camel.spi.RestConfiguration;
033import org.apache.camel.util.CamelContextHelper;
034
035/**
036 * To configure rest
037 */
038@Metadata(label = "rest")
039@XmlRootElement(name = "restConfiguration")
040@XmlAccessorType(XmlAccessType.FIELD)
041public class RestConfigurationDefinition {
042
043    @XmlAttribute
044    private String component;
045
046    @XmlAttribute @Metadata(label = "consumer", defaultValue = "swagger")
047    private String apiComponent;
048
049    @XmlAttribute @Metadata(label = "producer")
050    private String producerComponent;
051
052    @XmlAttribute
053    private String scheme;
054
055    @XmlAttribute
056    private String host;
057
058    @XmlAttribute
059    private String apiHost;
060
061    @XmlAttribute @Metadata(defaultValue = "true")
062    private Boolean useXForwardHeaders;
063
064    @XmlAttribute
065    private String port;
066
067    @XmlAttribute @Metadata(label = "producer")
068    private String producerApiDoc;
069
070    @XmlAttribute @Metadata(label = "consumer")
071    private String contextPath;
072
073    @XmlAttribute @Metadata(label = "consumer")
074    private String apiContextPath;
075
076    @XmlAttribute @Metadata(label = "consumer")
077    private String apiContextRouteId;
078
079    @XmlAttribute @Metadata(label = "consumer")
080    private String apiContextIdPattern;
081
082    @XmlAttribute @Metadata(label = "consumer")
083    private Boolean apiContextListing;
084
085    @XmlAttribute @Metadata(label = "consumer")
086    private Boolean apiVendorExtension;
087
088    @XmlAttribute @Metadata(label = "consumer")
089    private RestHostNameResolver hostNameResolver;
090
091    @XmlAttribute @Metadata(defaultValue = "off")
092    private RestBindingMode bindingMode;
093
094    @XmlAttribute
095    private Boolean skipBindingOnErrorCode;
096
097    @XmlAttribute
098    private Boolean clientRequestValidation;
099
100    @XmlAttribute @Metadata(label = "consumer")
101    private Boolean enableCORS;
102
103    @XmlAttribute
104    private String jsonDataFormat;
105
106    @XmlAttribute
107    private String xmlDataFormat;
108
109    @XmlElement(name = "componentProperty")
110    private List<RestPropertyDefinition> componentProperties = new ArrayList<>();
111
112    @XmlElement(name = "endpointProperty")
113    private List<RestPropertyDefinition> endpointProperties = new ArrayList<>();
114
115    @XmlElement(name = "consumerProperty") @Metadata(label = "consumer")
116    private List<RestPropertyDefinition> consumerProperties = new ArrayList<>();
117
118    @XmlElement(name = "dataFormatProperty")
119    private List<RestPropertyDefinition> dataFormatProperties = new ArrayList<>();
120
121    @XmlElement(name = "apiProperty") @Metadata(label = "consumer")
122    private List<RestPropertyDefinition> apiProperties = new ArrayList<>();
123
124    @XmlElement(name = "corsHeaders") @Metadata(label = "consumer")
125    private List<RestPropertyDefinition> corsHeaders = new ArrayList<>();
126
127    public String getComponent() {
128        return component;
129    }
130
131    /**
132     * The Camel Rest component to use for the REST transport (consumer), such as restlet, spark-rest.
133     * If no component has been explicit configured, then Camel will lookup if there is a Camel component
134     * that integrates with the Rest DSL, or if a org.apache.camel.spi.RestConsumerFactory is registered in the registry.
135     * If either one is found, then that is being used.
136     */
137    public void setComponent(String component) {
138        this.component = component;
139    }
140
141    public String getApiComponent() {
142        return apiComponent;
143    }
144
145    /**
146     * The name of the Camel component to use as the REST API (such as swagger)
147     */
148    public void setApiComponent(String apiComponent) {
149        this.apiComponent = apiComponent;
150    }
151
152    public String getProducerComponent() {
153        return producerComponent;
154    }
155
156    /**
157     * Sets the name of the Camel component to use as the REST producer
158     */
159    public void setProducerComponent(String producerComponent) {
160        this.producerComponent = producerComponent;
161    }
162
163    public String getScheme() {
164        return scheme;
165    }
166
167    /**
168     * The scheme to use for exposing the REST service. Usually http or https is supported.
169     * <p/>
170     * The default value is http
171     */
172    public void setScheme(String scheme) {
173        this.scheme = scheme;
174    }
175
176    public String getHost() {
177        return host;
178    }
179
180    /**
181     * The hostname to use for exposing the REST service.
182     */
183    public void setHost(String host) {
184        this.host = host;
185    }
186
187    public String getApiHost() {
188        return apiHost;
189    }
190
191    /**
192     * To use an specific hostname for the API documentation (eg swagger)
193     * <p/>
194     * This can be used to override the generated host with this configured hostname
195     */
196    public void setApiHost(String apiHost) {
197        this.apiHost = apiHost;
198    }
199
200    public String getPort() {
201        return port;
202    }
203
204    /**
205     * The port number to use for exposing the REST service.
206     * Notice if you use servlet component then the port number configured here does not apply,
207     * as the port number in use is the actual port number the servlet component is using.
208     * eg if using Apache Tomcat its the tomcat http port, if using Apache Karaf its the HTTP service in Karaf
209     * that uses port 8181 by default etc. Though in those situations setting the port number here,
210     * allows tooling and JMX to know the port number, so its recommended to set the port number
211     * to the number that the servlet engine uses.
212     */
213    public void setPort(String port) {
214        this.port = port;
215    }
216
217    public String getProducerApiDoc() {
218        return producerApiDoc;
219    }
220
221    /**
222     * Sets the location of the api document (swagger api) the REST producer will use
223     * to validate the REST uri and query parameters are valid accordingly to the api document.
224     * This requires adding camel-swagger-java to the classpath, and any miss configuration
225     * will let Camel fail on startup and report the error(s).
226     * <p/>
227     * The location of the api document is loaded from classpath by default, but you can use
228     * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url.
229     */
230    public void setProducerApiDoc(String producerApiDoc) {
231        this.producerApiDoc = producerApiDoc;
232    }
233
234    public String getContextPath() {
235        return contextPath;
236    }
237
238    /**
239     * Sets a leading context-path the REST services will be using.
240     * <p/>
241     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
242     * is deployed using a context-path. Or for components such as <tt>camel-jetty</tt> or <tt>camel-netty4-http</tt>
243     * that includes a HTTP server.
244     */
245    public void setContextPath(String contextPath) {
246        this.contextPath = contextPath;
247    }
248
249    public String getApiContextPath() {
250        return apiContextPath;
251    }
252
253    /**
254     * Sets a leading API context-path the REST API services will be using.
255     * <p/>
256     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
257     * is deployed using a context-path.
258     *
259     * @param contextPath the API context path
260     */
261    public void setApiContextPath(String contextPath) {
262        this.apiContextPath = contextPath;
263    }
264
265    public String getApiContextRouteId() {
266        return apiContextRouteId;
267    }
268
269    /**
270     * Sets the route id to use for the route that services the REST API.
271     * <p/>
272     * The route will by default use an auto assigned route id.
273     *
274     * @param apiContextRouteId  the route id
275     */
276    public void setApiContextRouteId(String apiContextRouteId) {
277        this.apiContextRouteId = apiContextRouteId;
278    }
279
280    public String getApiContextIdPattern() {
281        return apiContextIdPattern;
282    }
283
284    /**
285     * Sets an CamelContext id pattern to only allow Rest APIs from rest services within CamelContext's which name matches the pattern.
286     * <p/>
287     * The pattern <tt>#name#</tt> refers to the CamelContext name, to match on the current CamelContext only.
288     * For any other value, the pattern uses the rules from {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)}
289     *
290     * @param apiContextIdPattern  the pattern
291     */
292    public void setApiContextIdPattern(String apiContextIdPattern) {
293        this.apiContextIdPattern = apiContextIdPattern;
294    }
295
296    public Boolean getApiContextListing() {
297        return apiContextListing;
298    }
299
300    /**
301     * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover
302     * these contexts, if <tt>false</tt> then only the current CamelContext is in use.
303     */
304    public void setApiContextListing(Boolean apiContextListing) {
305        this.apiContextListing = apiContextListing;
306    }
307
308    public Boolean getApiVendorExtension() {
309        return apiVendorExtension;
310    }
311
312    /**
313     * Whether vendor extension is enabled in the Rest APIs. If enabled then Camel will include additional information
314     * as vendor extension (eg keys starting with x-) such as route ids, class names etc.
315     * Not all 3rd party API gateways and tools supports vendor-extensions when importing your API docs.
316     */
317    public void setApiVendorExtension(Boolean apiVendorExtension) {
318        this.apiVendorExtension = apiVendorExtension;
319    }
320
321    public RestHostNameResolver getHostNameResolver() {
322        return hostNameResolver;
323    }
324
325    /**
326     * If no hostname has been explicit configured, then this resolver is used to compute the hostname the REST service will be using.
327     */
328    public void setHostNameResolver(RestHostNameResolver hostNameResolver) {
329        this.hostNameResolver = hostNameResolver;
330    }
331
332    public RestBindingMode getBindingMode() {
333        return bindingMode;
334    }
335
336    /**
337     * Sets the binding mode to use.
338     * <p/>
339     * The default value is off
340     */
341    public void setBindingMode(RestBindingMode bindingMode) {
342        this.bindingMode = bindingMode;
343    }
344
345    public Boolean getSkipBindingOnErrorCode() {
346        return skipBindingOnErrorCode;
347    }
348
349    /**
350     * Whether to skip binding on output if there is a custom HTTP error code header.
351     * This allows to build custom error messages that do not bind to json / xml etc, as success messages otherwise will do.
352     */
353    public void setSkipBindingOnErrorCode(Boolean skipBindingOnErrorCode) {
354        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
355    }
356
357    public Boolean getClientRequestValidation() {
358        return clientRequestValidation;
359    }
360
361    /**
362     * Whether to enable validation of the client request to check whether the Content-Type and Accept headers from
363     * the client is supported by the Rest-DSL configuration of its consumes/produces settings.
364     * <p/>
365     * This can be turned on, to enable this check. In case of validation error, then HTTP Status codes 415 or 406 is returned.
366     * <p/>
367     * The default value is false.
368     */
369    public void setClientRequestValidation(Boolean clientRequestValidation) {
370        this.clientRequestValidation = clientRequestValidation;
371    }
372
373    public Boolean getEnableCORS() {
374        return enableCORS;
375    }
376
377    /**
378     * Whether to enable CORS headers in the HTTP response.
379     * <p/>
380     * The default value is false.
381     */
382    public void setEnableCORS(Boolean enableCORS) {
383        this.enableCORS = enableCORS;
384    }
385
386    public String getJsonDataFormat() {
387        return jsonDataFormat;
388    }
389
390    /**
391     * Name of specific json data format to use.
392     * By default json-jackson will be used.
393     * Important: This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
394     */
395    public void setJsonDataFormat(String jsonDataFormat) {
396        this.jsonDataFormat = jsonDataFormat;
397    }
398
399    public String getXmlDataFormat() {
400        return xmlDataFormat;
401    }
402
403    /**
404     * Name of specific XML data format to use.
405     * By default jaxb will be used.
406     * Important: This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
407     */
408    public void setXmlDataFormat(String xmlDataFormat) {
409        this.xmlDataFormat = xmlDataFormat;
410    }
411
412    public List<RestPropertyDefinition> getComponentProperties() {
413        return componentProperties;
414    }
415
416    /**
417     * Allows to configure as many additional properties for the rest component in use.
418     */
419    public void setComponentProperties(List<RestPropertyDefinition> componentProperties) {
420        this.componentProperties = componentProperties;
421    }
422
423    public List<RestPropertyDefinition> getEndpointProperties() {
424        return endpointProperties;
425    }
426
427    /**
428     * Allows to configure as many additional properties for the rest endpoint in use.
429     */
430    public void setEndpointProperties(List<RestPropertyDefinition> endpointProperties) {
431        this.endpointProperties = endpointProperties;
432    }
433
434    public List<RestPropertyDefinition> getConsumerProperties() {
435        return consumerProperties;
436    }
437
438    /**
439     * Allows to configure as many additional properties for the rest consumer in use.
440     */
441    public void setConsumerProperties(List<RestPropertyDefinition> consumerProperties) {
442        this.consumerProperties = consumerProperties;
443    }
444
445    public List<RestPropertyDefinition> getDataFormatProperties() {
446        return dataFormatProperties;
447    }
448
449    /**
450     * Allows to configure as many additional properties for the data formats in use.
451     * For example set property prettyPrint to true to have json outputted in pretty mode.
452     * The properties can be prefixed to denote the option is only for either JSON or XML and for either the IN or the OUT.
453     * The prefixes are:
454     * <ul>
455     *     <li>json.in.</li>
456     *     <li>json.out.</li>
457     *     <li>xml.in.</li>
458     *     <li>xml.out.</li>
459     * </ul>
460     * For example a key with value "xml.out.mustBeJAXBElement" is only for the XML data format for the outgoing.
461     * A key without a prefix is a common key for all situations.
462     */
463    public void setDataFormatProperties(List<RestPropertyDefinition> dataFormatProperties) {
464        this.dataFormatProperties = dataFormatProperties;
465    }
466
467    public List<RestPropertyDefinition> getApiProperties() {
468        return apiProperties;
469    }
470
471    /**
472     * Allows to configure as many additional properties for the api documentation (swagger).
473     * For example set property api.title to my cool stuff
474     */
475    public void setApiProperties(List<RestPropertyDefinition> apiProperties) {
476        this.apiProperties = apiProperties;
477    }
478
479    public List<RestPropertyDefinition> getCorsHeaders() {
480        return corsHeaders;
481    }
482
483    /**
484     * Allows to configure custom CORS headers.
485     */
486    public void setCorsHeaders(List<RestPropertyDefinition> corsHeaders) {
487        this.corsHeaders = corsHeaders;
488    }
489
490    // Fluent API
491    //-------------------------------------------------------------------------
492
493    /**
494     * To use a specific Camel rest component (consumer)
495     */
496    public RestConfigurationDefinition component(String componentId) {
497        setComponent(componentId);
498        return this;
499    }
500
501    /**
502     * To use a specific Camel rest API component
503     */
504    public RestConfigurationDefinition apiComponent(String componentId) {
505        setApiComponent(componentId);
506        return this;
507    }
508
509    /**
510     * To use a specific Camel rest component (producer)
511     */
512    public RestConfigurationDefinition producerComponent(String componentId) {
513        setProducerComponent(componentId);
514        return this;
515    }
516
517    /**
518     * To use a specific scheme such as http/https
519     */
520    public RestConfigurationDefinition scheme(String scheme) {
521        setScheme(scheme);
522        return this;
523    }
524
525    /**
526     * To define the host to use, such as 0.0.0.0 or localhost
527     */
528    public RestConfigurationDefinition host(String host) {
529        setHost(host);
530        return this;
531    }
532
533    /**
534     * To define a specific host to use for API documentation (eg swagger) instead
535     * of using a generated API hostname that is relative to the REST service host.
536     */
537    public RestConfigurationDefinition apiHost(String host) {
538        setApiHost(host);
539        return this;
540    }
541
542    /**
543     * To specify the port number to use for the REST service
544     */
545    public RestConfigurationDefinition port(int port) {
546        setPort("" + port);
547        return this;
548    }
549
550    /**
551     * To specify the port number to use for the REST service
552     */
553    public RestConfigurationDefinition port(String port) {
554        setPort(port);
555        return this;
556    }
557
558    /**
559     * Sets the location of the api document (swagger api) the REST producer will use
560     * to validate the REST uri and query parameters are valid accordingly to the api document.
561     * This requires adding camel-swagger-java to the classpath, and any miss configuration
562     * will let Camel fail on startup and report the error(s).
563     * <p/>
564     * The location of the api document is loaded from classpath by default, but you can use
565     * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url.
566     */
567    public RestConfigurationDefinition producerApiDoc(String apiDoc) {
568        setProducerApiDoc(apiDoc);
569        return this;
570    }
571
572    /**
573     * Sets a leading context-path the REST services will be using.
574     * <p/>
575     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
576     * is deployed using a context-path. Or for components such as <tt>camel-jetty</tt> or <tt>camel-netty4-http</tt>
577     * that includes a HTTP server.
578     */
579    public RestConfigurationDefinition apiContextPath(String contextPath) {
580        setApiContextPath(contextPath);
581        return this;
582    }
583
584    /**
585     * Sets the route id to use for the route that services the REST API.
586     */
587    public RestConfigurationDefinition apiContextRouteId(String routeId) {
588        setApiContextRouteId(routeId);
589        return this;
590    }
591
592    /**
593     * Sets an CamelContext id pattern to only allow Rest APIs from rest services within CamelContext's which name matches the pattern.
594     * <p/>
595     * The pattern uses the rules from {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)}
596     */
597    public RestConfigurationDefinition apiContextIdPattern(String pattern) {
598        setApiContextIdPattern(pattern);
599        return this;
600    }
601
602    /**
603     * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover
604     * these contexts, if <tt>false</tt> then only the current CamelContext is in use.
605     */
606    public RestConfigurationDefinition apiContextListing(boolean listing) {
607        setApiContextListing(listing);
608        return this;
609    }
610
611    /**
612     * Whether vendor extension is enabled in the Rest APIs. If enabled then Camel will include additional information
613     * as vendor extension (eg keys starting with x-) such as route ids, class names etc.
614     * Some API tooling may not support vendor extensions and this option can then be turned off.
615     */
616    public RestConfigurationDefinition apiVendorExtension(boolean vendorExtension) {
617        setApiVendorExtension(vendorExtension);
618        return this;
619    }
620
621    /**
622     * Sets a leading context-path the REST services will be using.
623     * <p/>
624     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
625     * is deployed using a context-path.
626     */
627    public RestConfigurationDefinition contextPath(String contextPath) {
628        setContextPath(contextPath);
629        return this;
630    }
631
632    /**
633     * To specify the hostname resolver
634     */
635    public RestConfigurationDefinition hostNameResolver(RestHostNameResolver hostNameResolver) {
636        setHostNameResolver(hostNameResolver);
637        return this;
638    }
639
640    /**
641     * To specify the binding mode
642     */
643    public RestConfigurationDefinition bindingMode(RestBindingMode bindingMode) {
644        setBindingMode(bindingMode);
645        return this;
646    }
647
648    /**
649     * To specify whether to skip binding output if there is a custom HTTP error code
650     */
651    public RestConfigurationDefinition skipBindingOnErrorCode(boolean skipBindingOnErrorCode) {
652        setSkipBindingOnErrorCode(skipBindingOnErrorCode);
653        return this;
654    }
655
656    /**
657     * Whether to enable validation of the client request to check whether the Content-Type and Accept headers from
658     * the client is supported by the Rest-DSL configuration of its consumes/produces settings.
659     */
660    public RestConfigurationDefinition clientRequestValidation(boolean clientRequestValidation) {
661        setClientRequestValidation(clientRequestValidation);
662        return this;
663    }
664
665    /**
666     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
667     */
668    public RestConfigurationDefinition enableCORS(boolean enableCORS) {
669        setEnableCORS(enableCORS);
670        return this;
671    }
672
673    /**
674     * To use a specific json data format
675     * <p/>
676     * <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.
677     *
678     * @param name  name of the data format to {@link org.apache.camel.CamelContext#resolveDataFormat(java.lang.String) resolve}
679     */
680    public RestConfigurationDefinition jsonDataFormat(String name) {
681        setJsonDataFormat(name);
682        return this;
683    }
684
685    /**
686     * To use a specific XML data format
687     * <p/>
688     * <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.
689     *
690     * @param name  name of the data format to {@link org.apache.camel.CamelContext#resolveDataFormat(java.lang.String) resolve}
691     */
692    public RestConfigurationDefinition xmlDataFormat(String name) {
693        setXmlDataFormat(name);
694        return this;
695    }
696
697    /**
698     * For additional configuration options on component level
699     * <p/>
700     * The value can use <tt>#</tt> to refer to a bean to lookup in the registry.
701     */
702    public RestConfigurationDefinition componentProperty(String key, String value) {
703        RestPropertyDefinition prop = new RestPropertyDefinition();
704        prop.setKey(key);
705        prop.setValue(value);
706        getComponentProperties().add(prop);
707        return this;
708    }
709
710    /**
711     * For additional configuration options on endpoint level
712     * <p/>
713     * The value can use <tt>#</tt> to refer to a bean to lookup in the registry.
714     */
715    public RestConfigurationDefinition endpointProperty(String key, String value) {
716        RestPropertyDefinition prop = new RestPropertyDefinition();
717        prop.setKey(key);
718        prop.setValue(value);
719        getEndpointProperties().add(prop);
720        return this;
721    }
722
723    /**
724     * For additional configuration options on consumer level
725     * <p/>
726     * The value can use <tt>#</tt> to refer to a bean to lookup in the registry.
727     */
728    public RestConfigurationDefinition consumerProperty(String key, String value) {
729        RestPropertyDefinition prop = new RestPropertyDefinition();
730        prop.setKey(key);
731        prop.setValue(value);
732        getConsumerProperties().add(prop);
733        return this;
734    }
735
736    /**
737     * For additional configuration options on data format level
738     * <p/>
739     * The value can use <tt>#</tt> to refer to a bean to lookup in the registry.
740     */
741    public RestConfigurationDefinition dataFormatProperty(String key, String value) {
742        RestPropertyDefinition prop = new RestPropertyDefinition();
743        prop.setKey(key);
744        prop.setValue(value);
745        getDataFormatProperties().add(prop);
746        return this;
747    }
748
749    /**
750     * For configuring an api property, such as <tt>api.title</tt>, or <tt>api.version</tt>.
751     */
752    public RestConfigurationDefinition apiProperty(String key, String value) {
753        RestPropertyDefinition prop = new RestPropertyDefinition();
754        prop.setKey(key);
755        prop.setValue(value);
756        getApiProperties().add(prop);
757        return this;
758    }
759
760    /**
761     * For configuring CORS headers
762     */
763    public RestConfigurationDefinition corsHeaderProperty(String key, String value) {
764        RestPropertyDefinition prop = new RestPropertyDefinition();
765        prop.setKey(key);
766        prop.setValue(value);
767        getCorsHeaders().add(prop);
768        return this;
769    }
770
771    /**
772     * Shortcut for setting the {@code Access-Control-Allow-Credentials} header.
773     */
774    public RestConfigurationDefinition corsAllowCredentials(boolean corsAllowCredentials) {
775        return corsHeaderProperty("Access-Control-Allow-Credentials", String.valueOf(corsAllowCredentials));
776    }
777
778    /**
779     * To specify whether to use X-Forward headers for Host and related setting
780     */
781    public RestConfigurationDefinition useXForwardHeaders(boolean useXForwardHeaders) {
782        setUseXForwardHeaders(useXForwardHeaders);
783        return this;
784    }
785
786
787    // Implementation
788    //-------------------------------------------------------------------------
789
790    /**
791     * Creates a {@link org.apache.camel.spi.RestConfiguration} instance based on the definition
792     *
793     * @param context     the camel context
794     * @return the configuration
795     * @throws Exception is thrown if error creating the configuration
796     */
797    public RestConfiguration asRestConfiguration(CamelContext context) throws Exception {
798        RestConfiguration answer = new RestConfiguration();
799        if (component != null) {
800            answer.setComponent(CamelContextHelper.parseText(context, component));
801        }
802        if (apiComponent != null) {
803            answer.setApiComponent(CamelContextHelper.parseText(context, apiComponent));
804        }
805        if (producerComponent != null) {
806            answer.setProducerComponent(CamelContextHelper.parseText(context, producerComponent));
807        }
808        if (scheme != null) {
809            answer.setScheme(CamelContextHelper.parseText(context, scheme));
810        }
811        if (host != null) {
812            answer.setHost(CamelContextHelper.parseText(context, host));
813        }
814        if (apiHost != null) {
815            answer.setApiHost(CamelContextHelper.parseText(context, apiHost));
816        }
817        if (port != null) {
818            answer.setPort(CamelContextHelper.parseInteger(context, port));
819        }
820        if (producerApiDoc != null) {
821            answer.setProducerApiDoc(CamelContextHelper.parseText(context, producerApiDoc));
822        }
823        if (apiContextPath != null) {
824            answer.setApiContextPath(CamelContextHelper.parseText(context, apiContextPath));
825        }
826        if (apiContextRouteId != null) {
827            answer.setApiContextRouteId(CamelContextHelper.parseText(context, apiContextRouteId));
828        }
829        if (apiContextIdPattern != null) {
830            // special to allow #name# to refer to itself
831            if ("#name#".equals(apiComponent)) {
832                answer.setApiContextIdPattern(context.getName());
833            } else {
834                answer.setApiContextIdPattern(CamelContextHelper.parseText(context, apiContextIdPattern));
835            }
836        }
837        if (apiContextListing != null) {
838            answer.setApiContextListing(apiContextListing);
839        }
840        if (apiVendorExtension != null) {
841            answer.setApiVendorExtension(apiVendorExtension);
842        }
843        if (contextPath != null) {
844            answer.setContextPath(CamelContextHelper.parseText(context, contextPath));
845        }
846        if (hostNameResolver != null) {
847            answer.setRestHostNameResolver(hostNameResolver.name());
848        }
849        if (bindingMode != null) {
850            answer.setBindingMode(bindingMode.name());
851        }
852        if (skipBindingOnErrorCode != null) {
853            answer.setSkipBindingOnErrorCode(skipBindingOnErrorCode);
854        }
855        if (clientRequestValidation != null) {
856            answer.setClientRequestValidation(clientRequestValidation);
857        }
858        if (enableCORS != null) {
859            answer.setEnableCORS(enableCORS);
860        }
861        if (jsonDataFormat != null) {
862            answer.setJsonDataFormat(jsonDataFormat);
863        }
864        if (xmlDataFormat != null) {
865            answer.setXmlDataFormat(xmlDataFormat);
866        }
867        if (!componentProperties.isEmpty()) {
868            Map<String, Object> props = new HashMap<>();
869            for (RestPropertyDefinition prop : componentProperties) {
870                String key = prop.getKey();
871                String value = CamelContextHelper.parseText(context, prop.getValue());
872                props.put(key, value);
873            }
874            answer.setComponentProperties(props);
875        }
876        if (!endpointProperties.isEmpty()) {
877            Map<String, Object> props = new HashMap<>();
878            for (RestPropertyDefinition prop : endpointProperties) {
879                String key = prop.getKey();
880                String value = CamelContextHelper.parseText(context, prop.getValue());
881                props.put(key, value);
882            }
883            answer.setEndpointProperties(props);
884        }
885        if (!consumerProperties.isEmpty()) {
886            Map<String, Object> props = new HashMap<>();
887            for (RestPropertyDefinition prop : consumerProperties) {
888                String key = prop.getKey();
889                String value = CamelContextHelper.parseText(context, prop.getValue());
890                props.put(key, value);
891            }
892            answer.setConsumerProperties(props);
893        }
894        if (!dataFormatProperties.isEmpty()) {
895            Map<String, Object> props = new HashMap<>();
896            for (RestPropertyDefinition prop : dataFormatProperties) {
897                String key = prop.getKey();
898                String value = CamelContextHelper.parseText(context, prop.getValue());
899                props.put(key, value);
900            }
901            answer.setDataFormatProperties(props);
902        }
903        if (!apiProperties.isEmpty()) {
904            Map<String, Object> props = new HashMap<>();
905            for (RestPropertyDefinition prop : apiProperties) {
906                String key = prop.getKey();
907                String value = CamelContextHelper.parseText(context, prop.getValue());
908                props.put(key, value);
909            }
910            answer.setApiProperties(props);
911        }
912        if (!corsHeaders.isEmpty()) {
913            Map<String, String> props = new HashMap<>();
914            for (RestPropertyDefinition prop : corsHeaders) {
915                String key = prop.getKey();
916                String value = CamelContextHelper.parseText(context, prop.getValue());
917                props.put(key, value);
918            }
919            answer.setCorsHeaders(props);
920        }
921        if (useXForwardHeaders != null) {
922            answer.setUseXForwardHeaders(useXForwardHeaders);
923        }
924        return answer;
925    }
926
927
928    public Boolean getUseXForwardHeaders() {
929        return useXForwardHeaders;
930    }
931
932    /**
933     * Whether to use X-Forward headers for Host and related setting.
934     * <p/>
935     * The default value is true.
936     */
937    public void setUseXForwardHeaders(Boolean useXForwardHeaders) {
938        this.useXForwardHeaders = useXForwardHeaders;
939    }
940
941}