This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new f8c19cb CAMEL-14723: Adding a restConfiguration() make all rest properties to be ignored (#3639) f8c19cb is described below commit f8c19cbecf3d81dc7efbfe710968689dcc4eb648 Author: Luca Burgazzoli <lburgazz...@users.noreply.github.com> AuthorDate: Tue Mar 17 06:18:27 2020 +0100 CAMEL-14723: Adding a restConfiguration() make all rest properties to be ignored (#3639) --- .../org/apache/camel/builder/RouteBuilder.java | 9 +-- .../model/rest/RestConfigurationDefinition.java | 66 +++++++++---------- .../core/xml/AbstractCamelContextFactoryBean.java | 6 +- .../apache/camel/main/MainWithRestConfigTest.java | 77 ++++++++++++++++++++++ 4 files changed, 116 insertions(+), 42 deletions(-) diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java index 807f2df..e2e7268 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java @@ -530,12 +530,9 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild // setup rest configuration before adding the rests if (getRestConfigurations() != null) { for (Map.Entry<String, RestConfigurationDefinition> entry : getRestConfigurations().entrySet()) { - RestConfiguration config = entry.getValue().asRestConfiguration(getContext()); - if ("".equals(entry.getKey())) { - camelContext.setRestConfiguration(config); - } else { - camelContext.addRestConfiguration(config); - } + entry.getValue().asRestConfiguration( + getContext(), + camelContext.getRestConfiguration(entry.getKey(), true)); } } // cannot add rests as routes yet as we need to initialize this diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java index 95303b9..6ef8277 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java @@ -876,85 +876,85 @@ public class RestConfigurationDefinition { // ------------------------------------------------------------------------- /** - * Creates a {@link org.apache.camel.spi.RestConfiguration} instance based + * Configured an instance of a {@link org.apache.camel.spi.RestConfiguration} instance based * on the definition * * @param context the camel context + * @param target the {@link org.apache.camel.spi.RestConfiguration} target * @return the configuration * @throws Exception is thrown if error creating the configuration */ - public RestConfiguration asRestConfiguration(CamelContext context) throws Exception { - RestConfiguration answer = new RestConfiguration(); + public RestConfiguration asRestConfiguration(CamelContext context, RestConfiguration target) throws Exception { if (component != null) { - answer.setComponent(CamelContextHelper.parseText(context, component)); + target.setComponent(CamelContextHelper.parseText(context, component)); } if (apiComponent != null) { - answer.setApiComponent(CamelContextHelper.parseText(context, apiComponent)); + target.setApiComponent(CamelContextHelper.parseText(context, apiComponent)); } if (producerComponent != null) { - answer.setProducerComponent(CamelContextHelper.parseText(context, producerComponent)); + target.setProducerComponent(CamelContextHelper.parseText(context, producerComponent)); } if (scheme != null) { - answer.setScheme(CamelContextHelper.parseText(context, scheme)); + target.setScheme(CamelContextHelper.parseText(context, scheme)); } if (host != null) { - answer.setHost(CamelContextHelper.parseText(context, host)); + target.setHost(CamelContextHelper.parseText(context, host)); } if (useXForwardHeaders != null) { - answer.setUseXForwardHeaders(useXForwardHeaders); + target.setUseXForwardHeaders(useXForwardHeaders); } if (apiHost != null) { - answer.setApiHost(CamelContextHelper.parseText(context, apiHost)); + target.setApiHost(CamelContextHelper.parseText(context, apiHost)); } if (port != null) { - answer.setPort(CamelContextHelper.parseInteger(context, port)); + target.setPort(CamelContextHelper.parseInteger(context, port)); } if (producerApiDoc != null) { - answer.setProducerApiDoc(CamelContextHelper.parseText(context, producerApiDoc)); + target.setProducerApiDoc(CamelContextHelper.parseText(context, producerApiDoc)); } if (apiContextPath != null) { - answer.setApiContextPath(CamelContextHelper.parseText(context, apiContextPath)); + target.setApiContextPath(CamelContextHelper.parseText(context, apiContextPath)); } if (apiContextRouteId != null) { - answer.setApiContextRouteId(CamelContextHelper.parseText(context, apiContextRouteId)); + target.setApiContextRouteId(CamelContextHelper.parseText(context, apiContextRouteId)); } if (apiContextIdPattern != null) { // special to allow #name# to refer to itself if ("#name#".equals(apiComponent)) { - answer.setApiContextIdPattern(context.getName()); + target.setApiContextIdPattern(context.getName()); } else { - answer.setApiContextIdPattern(CamelContextHelper.parseText(context, apiContextIdPattern)); + target.setApiContextIdPattern(CamelContextHelper.parseText(context, apiContextIdPattern)); } } if (apiContextListing != null) { - answer.setApiContextListing(apiContextListing); + target.setApiContextListing(apiContextListing); } if (apiVendorExtension != null) { - answer.setApiVendorExtension(apiVendorExtension); + target.setApiVendorExtension(apiVendorExtension); } if (contextPath != null) { - answer.setContextPath(CamelContextHelper.parseText(context, contextPath)); + target.setContextPath(CamelContextHelper.parseText(context, contextPath)); } if (hostNameResolver != null) { - answer.setHostNameResolver(hostNameResolver.name()); + target.setHostNameResolver(hostNameResolver.name()); } if (bindingMode != null) { - answer.setBindingMode(bindingMode.name()); + target.setBindingMode(bindingMode.name()); } if (skipBindingOnErrorCode != null) { - answer.setSkipBindingOnErrorCode(skipBindingOnErrorCode); + target.setSkipBindingOnErrorCode(skipBindingOnErrorCode); } if (clientRequestValidation != null) { - answer.setClientRequestValidation(clientRequestValidation); + target.setClientRequestValidation(clientRequestValidation); } if (enableCORS != null) { - answer.setEnableCORS(enableCORS); + target.setEnableCORS(enableCORS); } if (jsonDataFormat != null) { - answer.setJsonDataFormat(jsonDataFormat); + target.setJsonDataFormat(jsonDataFormat); } if (xmlDataFormat != null) { - answer.setXmlDataFormat(xmlDataFormat); + target.setXmlDataFormat(xmlDataFormat); } if (!componentProperties.isEmpty()) { Map<String, Object> props = new HashMap<>(); @@ -963,7 +963,7 @@ public class RestConfigurationDefinition { String value = CamelContextHelper.parseText(context, prop.getValue()); props.put(key, value); } - answer.setComponentProperties(props); + target.setComponentProperties(props); } if (!endpointProperties.isEmpty()) { Map<String, Object> props = new HashMap<>(); @@ -972,7 +972,7 @@ public class RestConfigurationDefinition { String value = CamelContextHelper.parseText(context, prop.getValue()); props.put(key, value); } - answer.setEndpointProperties(props); + target.setEndpointProperties(props); } if (!consumerProperties.isEmpty()) { Map<String, Object> props = new HashMap<>(); @@ -981,7 +981,7 @@ public class RestConfigurationDefinition { String value = CamelContextHelper.parseText(context, prop.getValue()); props.put(key, value); } - answer.setConsumerProperties(props); + target.setConsumerProperties(props); } if (!dataFormatProperties.isEmpty()) { Map<String, Object> props = new HashMap<>(); @@ -990,7 +990,7 @@ public class RestConfigurationDefinition { String value = CamelContextHelper.parseText(context, prop.getValue()); props.put(key, value); } - answer.setDataFormatProperties(props); + target.setDataFormatProperties(props); } if (!apiProperties.isEmpty()) { Map<String, Object> props = new HashMap<>(); @@ -999,7 +999,7 @@ public class RestConfigurationDefinition { String value = CamelContextHelper.parseText(context, prop.getValue()); props.put(key, value); } - answer.setApiProperties(props); + target.setApiProperties(props); } if (!corsHeaders.isEmpty()) { Map<String, String> props = new HashMap<>(); @@ -1008,9 +1008,9 @@ public class RestConfigurationDefinition { String value = CamelContextHelper.parseText(context, prop.getValue()); props.put(key, value); } - answer.setCorsHeaders(props); + target.setCorsHeaders(props); } - return answer; + return target; } } diff --git a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java index 454f407..97dc676 100644 --- a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java +++ b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java @@ -685,11 +685,11 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex PropertiesParser.class); pc.setPropertiesParser(parser); } - + if (def.getDefaultFallbackEnabled() != null) { pc.setDefaultFallbackEnabled(def.getDefaultFallbackEnabled()); } - + if (def.getFunctions() != null && !def.getFunctions().isEmpty()) { for (CamelPropertyPlaceholderFunctionDefinition function : def.getFunctions()) { String ref = function.getRef(); @@ -972,7 +972,7 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex context.getTypeConverterRegistry().setTypeConverterExistsLoggingLevel(getTypeConverterExistsLoggingLevel()); } if (getRestConfiguration() != null) { - context.setRestConfiguration(getRestConfiguration().asRestConfiguration(context)); + getRestConfiguration().asRestConfiguration(context, context.getRestConfiguration()); } if (getDefaultServiceCallConfiguration() != null) { context.setServiceCallConfiguration(getDefaultServiceCallConfiguration()); diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainWithRestConfigTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainWithRestConfigTest.java new file mode 100644 index 0000000..437af91 --- /dev/null +++ b/core/camel-main/src/test/java/org/apache/camel/main/MainWithRestConfigTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.main; + +import java.util.Properties; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Assert; +import org.junit.Test; + +public class MainWithRestConfigTest extends Assert { + @Test + public void testRestConfigurationPropagation() { + Properties properties = new Properties(); + properties.setProperty("camel.rest.port", "8989"); + properties.setProperty("camel.rest.component", "undertow"); + + Main main = new Main(); + try { + main.addRoutesBuilder(new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration(); + } + }); + main.setOverrideProperties(properties); + main.setDefaultPropertyPlaceholderLocation("false"); + main.start(); + + assertEquals(8989, main.getCamelContext().getRestConfiguration().getPort()); + assertEquals("undertow", main.getCamelContext().getRestConfiguration().getComponent()); + } finally { + main.stop(); + } + } + + @Test + public void testRestConfigurationOverride() { + Properties properties = new Properties(); + properties.setProperty("camel.rest.port", "8989"); + properties.setProperty("camel.rest.component", "undertow"); + + Main main = new Main(); + try { + main.addRoutesBuilder(new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration() + .component("jetty"); + } + }); + main.setOverrideProperties(properties); + main.setDefaultPropertyPlaceholderLocation("false"); + main.start(); + + assertEquals(8989, main.getCamelContext().getRestConfiguration().getPort()); + assertEquals("jetty", main.getCamelContext().getRestConfiguration().getComponent()); + } finally { + main.stop(); + } + } +} +