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