CAMEL-11593: Global rest configuration gets overridden by default
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/26d70e72 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/26d70e72 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/26d70e72 Branch: refs/heads/camel-2.19.x Commit: 26d70e72ffd434ebe5ed9f425868e352f83689f3 Parents: eed0e8a Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jul 25 16:26:48 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jul 25 20:50:00 2017 +0200 ---------------------------------------------------------------------- .../camel/component/rest/RestComponent.java | 9 ++-- .../apache/camel/impl/DefaultCamelContext.java | 10 ++-- .../apache/camel/model/rest/RestDefinition.java | 13 ++++- .../spring/boot/CamelAutoConfiguration.java | 7 +++ .../springboot/geocoder/Application.java | 9 +--- .../springboot/geocoder/CamelGeocoderRoute.java | 47 +++++++++++++++++ .../springboot/geocoder/CamelRouter.java | 55 -------------------- .../src/main/resources/application.properties | 36 +++++++++++++ 8 files changed, 113 insertions(+), 73 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java index d1c1963..264750c 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java @@ -64,7 +64,7 @@ public class RestComponent extends DefaultComponent implements VerifiableCompone // if no explicit host was given, then fallback and use default configured host String h = getAndRemoveOrResolveReferenceParameter(parameters, "host", String.class, host); - if (h == null && config != null) { + if (h == null) { h = config.getHost(); int port = config.getPort(); // is there a custom port number @@ -118,7 +118,7 @@ public class RestComponent extends DefaultComponent implements VerifiableCompone answer.setUriTemplate(uriTemplate); // if no explicit component name was given, then fallback and use default configured component name - if (answer.getComponentName() == null && config != null) { + if (answer.getComponentName() == null) { String name = config.getProducerComponent(); if (name == null) { // fallback and use the consumer name @@ -127,7 +127,7 @@ public class RestComponent extends DefaultComponent implements VerifiableCompone answer.setComponentName(name); } // if no explicit producer api was given, then fallback and use default configured - if (answer.getApiDoc() == null && config != null) { + if (answer.getApiDoc() == null) { answer.setApiDoc(config.getProducerApiDoc()); } @@ -188,6 +188,9 @@ public class RestComponent extends DefaultComponent implements VerifiableCompone } private RestConfiguration mergeConfigurations(RestConfiguration conf, RestConfiguration from) throws Exception { + if (conf == from) { + return conf; + } if (from != null) { Map<String, Object> map = IntrospectionSupport.getNonNullProperties(from); http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 13cf55b..5a15441 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -2607,12 +2607,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon } public RestConfiguration getRestConfiguration() { - RestConfiguration config = restConfigurations.get(""); - if (config == null) { - config = new RestConfiguration(); - setRestConfiguration(config); - } - return config; + return restConfigurations.get(""); } public void setRestConfiguration(RestConfiguration restConfiguration) { @@ -2633,8 +2628,9 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon } RestConfiguration config = restConfigurations.get(component); if (config == null && defaultIfNotExist) { + // grab the default configuration config = getRestConfiguration(); - if (config != null && config.getComponent() != null && !config.getComponent().equals(component)) { + if (config == null || (config.getComponent() != null && !config.getComponent().equals(component))) { config = new RestConfiguration(); restConfigurations.put(component, config); } http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java index 6973f8c..b668f48 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java @@ -37,6 +37,7 @@ import org.apache.camel.model.ToDefinition; import org.apache.camel.model.ToDynamicDefinition; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.FileUtil; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; @@ -615,7 +616,17 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> List<RouteDefinition> answer = new ArrayList<RouteDefinition>(); if (camelContext.getRestConfigurations().isEmpty()) { - camelContext.getRestConfiguration(); + // make sure to initialize a rest configuration when its empty + // lookup a global which may have been setup via camel-spring-boot etc + RestConfiguration conf = CamelContextHelper.lookup(camelContext, RestConstants.DEFAULT_REST_CONFIGURATION_ID, RestConfiguration.class); + if (conf == null) { + conf = CamelContextHelper.findByType(camelContext, RestConfiguration.class); + } + if (conf != null) { + camelContext.setRestConfiguration(conf); + } else { + camelContext.setRestConfiguration(new RestConfiguration()); + } } for (RestConfiguration config : camelContext.getRestConfigurations()) { addRouteDefinition(camelContext, answer, config.getComponent()); http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java index c46e758..8eac55d 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java @@ -45,6 +45,7 @@ import org.apache.camel.spi.LifecycleStrategy; import org.apache.camel.spi.ManagementNamingStrategy; import org.apache.camel.spi.ManagementStrategy; import org.apache.camel.spi.ReloadStrategy; +import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RoutePolicyFactory; import org.apache.camel.spi.RuntimeEndpointRegistry; import org.apache.camel.spi.ShutdownStrategy; @@ -409,6 +410,12 @@ public class CamelAutoConfiguration { camelContext.setSSLContextParameters(sslContextParametersSupplier.get()); } + // rest-dsl global configuration + RestConfiguration restConfiguration = getSingleBeanOfType(applicationContext, RestConfiguration.class); + if (restConfiguration != null) { + camelContext.setRestConfiguration(restConfiguration); + } + // set the default thread pool profile if defined initThreadPoolProfiles(applicationContext, camelContext); } http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java index 25f5f1f..c4735ff 100644 --- a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java +++ b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java @@ -19,16 +19,10 @@ package org.apache.camel.example.springboot.geocoder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +// CHECKSTYLE:OFF @SpringBootApplication public class Application { - /* - * For PMD HideUtilityClassConstructorCheck - */ - private void noop() { - - } - /** * Main method to start the application. */ @@ -37,3 +31,4 @@ public class Application { } } +// CHECKSTYLE:ON http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelGeocoderRoute.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelGeocoderRoute.java b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelGeocoderRoute.java new file mode 100644 index 0000000..4ea6e93 --- /dev/null +++ b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelGeocoderRoute.java @@ -0,0 +1,47 @@ +/** + * 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.example.springboot.geocoder; + +import com.google.code.geocoder.model.GeocodeResponse; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.rest.RestBindingMode; +import org.springframework.stereotype.Component; + +import static org.apache.camel.model.rest.RestParamType.query; + +/** + * A simple Camel REST DSL route example using the Geocoder component and documented with Swagger + */ +@Component +public class CamelGeocoderRoute extends RouteBuilder { + + @Override + public void configure() throws Exception { + // rest-dsl is also configured in the application.properties file + + rest("/geocoder").description("Geocoder REST service") + .consumes("application/json") + .produces("application/json") + + .get().description("Geocoder address lookup").outType(GeocodeResponse.class) + .param().name("address").type(query).description("The address to lookup").dataType("string").endParam() + .responseMessage().code(200).message("Geocoder successful").endResponseMessage() + // call the geocoder to lookup details from the provided address + .toD("geocoder:address:${header.address}"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java deleted file mode 100644 index 3f0183a..0000000 --- a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.example.springboot.geocoder; - -import com.google.code.geocoder.model.GeocodeResponse; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.model.rest.RestBindingMode; -import org.springframework.stereotype.Component; - -import static org.apache.camel.model.rest.RestParamType.query; - -/** - * A simple Camel REST DSL route example using the Geocoder component and documented with Swagger - * - */ -@Component -public class CamelRouter extends RouteBuilder { - - @Override - public void configure() throws Exception { - restConfiguration() - .component("servlet") - .bindingMode(RestBindingMode.json) - .dataFormatProperty("prettyPrint", "true") - .apiContextPath("/api-doc") - .apiProperty("api.title", "Geocoder API").apiProperty("api.version", "1.0.0") - .apiProperty("cors", "true"); - - - rest("/geocoder").description("Geocoder REST service") - .consumes("application/json") - .produces("application/json") - - .get().description("Geocoder address lookup").outType(GeocodeResponse.class) - .param().name("address").type(query).description("The address to lookup").dataType("string").endParam() - .responseMessage().code(200).message("Geocoder successful").endResponseMessage() - .toD("geocoder:address:${header.address}"); - - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/26d70e72/examples/camel-example-spring-boot-geocoder/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-geocoder/src/main/resources/application.properties b/examples/camel-example-spring-boot-geocoder/src/main/resources/application.properties new file mode 100644 index 0000000..221789b --- /dev/null +++ b/examples/camel-example-spring-boot-geocoder/src/main/resources/application.properties @@ -0,0 +1,36 @@ +## --------------------------------------------------------------------------- +## 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. + +camel.springboot.name=Geocoder + +# use servlet as the component +# (this can be omitted as camel will lookup on the classpath and discover it automatic) +camel.rest.component=servlet + +# turn on json binding +camel.rest.binding-mode=json + +# output in pretty print mode +camel.rest.data-format-property.prettyPrint=true + +# context path for swagger api docs +camel.rest.api-context-path=/api-doc + +# swagger api properties +camel.rest.api-property.api.title=Geocoder API +camel.rest.api-property.api.version=1.0.0 +camel.rest.api-property.cors=true +