This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new b6f65ecacdb Camel 20567 make it possible to define restconfiguration 
in xml-io-dsl and xml-jaxb-dsl (#13488)
b6f65ecacdb is described below

commit b6f65ecacdbc48ea90f0e9695704cb26ccf839ae
Author: John Poth <poth.j...@gmail.com>
AuthorDate: Mon Mar 18 11:25:42 2024 +0100

    Camel 20567 make it possible to define restconfiguration in xml-io-dsl and 
xml-jaxb-dsl (#13488)
    
    * CAMEL-20257: make it possible to define restConfiguration ins xml-io-dsl
    
    * CAMEL-20257: make it possible to define restConfiguration ins xml-jaxb-dsl
---
 .../java/org/apache/camel/xml/jaxb/JaxbHelper.java | 24 +++++++++
 .../camel/dsl/xml/io/XmlRoutesBuilderLoader.java   | 13 +++++
 .../apache/camel/dsl/xml/io/XmlLoadAppTest.java    | 35 +++++++++++++
 .../org/apache/camel/dsl/xml/io/camel-app12.xml    | 26 +++++++++
 .../LoadRestConfigurationFromXmlTest.java          | 61 ++++++++++++++++++++++
 .../dsl/xml/jaxb/definition/restConfiguration.xml  | 24 +++++++++
 .../dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java   |  9 ++++
 7 files changed, 192 insertions(+)

diff --git 
a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java 
b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java
index 41099653e46..712f2d6d4be 100644
--- 
a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java
+++ 
b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java
@@ -57,6 +57,7 @@ import org.apache.camel.model.TemplatedRouteDefinition;
 import org.apache.camel.model.TemplatedRoutesDefinition;
 import org.apache.camel.model.ToDynamicDefinition;
 import org.apache.camel.model.language.ExpressionDefinition;
+import org.apache.camel.model.rest.RestConfigurationDefinition;
 import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.RestsDefinition;
 import org.apache.camel.spi.NamespaceAware;
@@ -549,6 +550,29 @@ public final class JaxbHelper {
         return answer;
     }
 
+    public static RestConfigurationDefinition 
loadRestConfigurationDefinition(CamelContext context, InputStream inputStream)
+            throws Exception {
+        // load rest configuration using JAXB
+        Document dom = newXmlConverter(context).toDOMDocument(inputStream, 
null);
+
+        if (!CAMEL_NS.equals(dom.getDocumentElement().getNamespaceURI())) {
+            addNamespaceToDom(dom);
+        }
+        Unmarshaller unmarshaller = 
getJAXBContext(context).createUnmarshaller();
+        Object result = unmarshaller.unmarshal(dom);
+
+        if (result == null) {
+            throw new IOException("Cannot unmarshal to rest configuration 
using JAXB from input stream: " + inputStream);
+        }
+
+        if (result instanceof RestConfigurationDefinition) {
+            return (RestConfigurationDefinition) result;
+        } else {
+            // ignore not supported type
+            return null;
+        }
+    }
+
     private static void removeNoiseFromUris(Element element) {
         final NamedNodeMap attrs = element.getAttributes();
 
diff --git 
a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
 
b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
index e19fb750a58..d8e0e8e8e0d 100644
--- 
a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
+++ 
b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
@@ -51,6 +51,7 @@ import org.apache.camel.model.TemplatedRouteDefinition;
 import org.apache.camel.model.TemplatedRoutesDefinition;
 import org.apache.camel.model.app.BeansDefinition;
 import org.apache.camel.model.app.RegistryBeanDefinition;
+import org.apache.camel.model.rest.RestConfigurationDefinition;
 import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.RestsDefinition;
 import org.apache.camel.spi.ExchangeFactory;
@@ -195,6 +196,18 @@ public class XmlRoutesBuilderLoader extends 
RouteBuilderLoaderSupport {
                 // 
org.apache.camel.main.BaseMainSupport.postProcessCamelRegistry() (if given Main 
implementation
                 // decides to do so)
 
+                if (app.getRestConfigurations().size() > 1) {
+                    throw new RuntimeException("There should only be one 
<restConfiguration>");
+                }
+                if (app.getRestConfigurations().size() == 1) {
+                    RestConfigurationDefinition config = 
app.getRestConfigurations().get(0);
+                    try {
+                        config.asRestConfiguration(getCamelContext(), 
getCamelContext().getRestConfiguration());
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+
                 app.getRests().forEach(r -> {
                     r.setResource(getResource());
                     List<RestDefinition> list = new ArrayList<>();
diff --git 
a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
 
b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
index 2422651160e..c93a55aa48d 100644
--- 
a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
+++ 
b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
@@ -16,11 +16,16 @@
  */
 package org.apache.camel.dsl.xml.io;
 
+import java.util.Map;
+
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.rest.DummyRestConsumerFactory;
+import org.apache.camel.component.rest.DummyRestProcessorFactory;
 import org.apache.camel.dsl.xml.io.beans.GreeterMessage;
 import org.apache.camel.dsl.xml.io.beans.MyDestroyBean;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.RoutesLoader;
 import org.apache.camel.support.PluginHelper;
 import org.junit.jupiter.api.Test;
@@ -301,4 +306,34 @@ public class XmlLoadAppTest {
             context.stop();
         }
     }
+
+    @Test
+    public void testLoadCamelAppRestConfiguration() throws Exception {
+        try (DefaultCamelContext context = new DefaultCamelContext()) {
+            
context.getCamelContextExtension().getRegistry().bind("dummy-rest", new 
DummyRestConsumerFactory());
+            
context.getCamelContextExtension().getRegistry().bind("dummy-rest", new 
DummyRestProcessorFactory());
+
+            context.start();
+
+            Resource resource = 
PluginHelper.getResourceLoader(context).resolveResource(
+                    "/org/apache/camel/dsl/xml/io/camel-app12.xml");
+
+            RoutesLoader routesLoader = PluginHelper.getRoutesLoader(context);
+            routesLoader.preParseRoute(resource, false);
+            routesLoader.loadRoutes(resource);
+
+            RestConfiguration restConfiguration = 
context.getRestConfiguration();
+            assertNotNull(restConfiguration, "There should be a rest 
configuration");
+            assertEquals("dummy-rest", restConfiguration.getApiComponent());
+            assertEquals("dummy-rest", restConfiguration.getComponent());
+            assertEquals("api", restConfiguration.getContextPath());
+            assertEquals("api-doc", restConfiguration.getApiContextPath());
+
+            Map<String, Object> apiProperties = 
restConfiguration.getApiProperties();
+            assertEquals("test", apiProperties.get("api.title"));
+            assertEquals("3.0", apiProperties.get("openapi.version"));
+
+            context.stop();
+        }
+    }
 }
diff --git 
a/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app12.xml
 
b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app12.xml
new file mode 100644
index 00000000000..37392431201
--- /dev/null
+++ 
b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app12.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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 xmlns="http://camel.apache.org/schema/spring";>
+    <restConfiguration contextPath="api" apiContextPath="api-doc"
+                       apiComponent="dummy-rest" component="dummy-rest" 
bindingMode="auto">
+        <apiProperty key="api.title" value="test"/>
+        <apiProperty key="openapi.version" value="3.0"/>
+    </restConfiguration>
+</camel>
diff --git 
a/dsl/camel-xml-jaxb-dsl-test/definition/src/test/java/org/apache/camel/dsl/xml/jaxb/definition/LoadRestConfigurationFromXmlTest.java
 
b/dsl/camel-xml-jaxb-dsl-test/definition/src/test/java/org/apache/camel/dsl/xml/jaxb/definition/LoadRestConfigurationFromXmlTest.java
new file mode 100644
index 00000000000..36a281bc0e3
--- /dev/null
+++ 
b/dsl/camel-xml-jaxb-dsl-test/definition/src/test/java/org/apache/camel/dsl/xml/jaxb/definition/LoadRestConfigurationFromXmlTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.dsl.xml.jaxb.definition;
+
+import java.util.Map;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.rest.DummyRestConsumerFactory;
+import org.apache.camel.component.rest.DummyRestProcessorFactory;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RestConfiguration;
+import org.apache.camel.support.PluginHelper;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class LoadRestConfigurationFromXmlTest extends ContextTestSupport {
+
+    @Override
+    protected Registry createCamelRegistry() throws Exception {
+        Registry jndi = super.createCamelRegistry();
+        jndi.bind("dummy-rest", new DummyRestConsumerFactory());
+        jndi.bind("dummy-rest-api", new DummyRestProcessorFactory());
+        return jndi;
+    }
+
+    @Test
+    public void testLoadRestsDefinitionFromXml() throws Exception {
+        // load rest configuration from XML and add it to the existing camel 
context
+        Resource resource = PluginHelper.getResourceLoader(context)
+                
.resolveResource("org/apache/camel/dsl/xml/jaxb/definition/restConfiguration.xml");
+        PluginHelper.getRoutesLoader(context).loadRoutes(resource);
+
+        RestConfiguration restConfiguration = context.getRestConfiguration();
+        assertNotNull(restConfiguration, "There should be a rest 
configuration");
+        assertEquals("dummy-rest", restConfiguration.getApiComponent());
+        assertEquals("dummy-rest", restConfiguration.getComponent());
+        assertEquals("api", restConfiguration.getContextPath());
+        assertEquals("api-doc", restConfiguration.getApiContextPath());
+
+        Map<String, Object> apiProperties = 
restConfiguration.getApiProperties();
+        assertEquals("test", apiProperties.get("api.title"));
+        assertEquals("3.0", apiProperties.get("openapi.version"));
+    }
+}
diff --git 
a/dsl/camel-xml-jaxb-dsl-test/definition/src/test/resources/org/apache/camel/dsl/xml/jaxb/definition/restConfiguration.xml
 
b/dsl/camel-xml-jaxb-dsl-test/definition/src/test/resources/org/apache/camel/dsl/xml/jaxb/definition/restConfiguration.xml
new file mode 100644
index 00000000000..70a8e1cc416
--- /dev/null
+++ 
b/dsl/camel-xml-jaxb-dsl-test/definition/src/test/resources/org/apache/camel/dsl/xml/jaxb/definition/restConfiguration.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<restConfiguration contextPath="api" apiContextPath="api-doc"
+                   apiComponent="dummy-rest" component="dummy-rest" 
bindingMode="auto">
+    <apiProperty key="api.title" value="test"/>
+    <apiProperty key="openapi.version" value="3.0"/>
+</restConfiguration>
diff --git 
a/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
 
b/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
index ae9ccc5adf0..639a8ff0ed8 100644
--- 
a/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
+++ 
b/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
@@ -29,10 +29,12 @@ import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RouteTemplatesDefinition;
 import org.apache.camel.model.RoutesDefinition;
 import org.apache.camel.model.TemplatedRoutesDefinition;
+import org.apache.camel.model.rest.RestConfigurationDefinition;
 import org.apache.camel.model.rest.RestsDefinition;
 import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.annotations.RoutesLoader;
 
+import static 
org.apache.camel.xml.jaxb.JaxbHelper.loadRestConfigurationDefinition;
 import static org.apache.camel.xml.jaxb.JaxbHelper.loadRestsDefinition;
 import static 
org.apache.camel.xml.jaxb.JaxbHelper.loadRouteConfigurationsDefinition;
 import static 
org.apache.camel.xml.jaxb.JaxbHelper.loadRouteTemplatesDefinition;
@@ -90,6 +92,13 @@ public class JaxbXmlRoutesBuilderLoader extends 
RouteBuilderLoaderSupport {
                         }
                     }
                 }
+
+                try (InputStream is = resourceInputStream(resource)) {
+                    RestConfigurationDefinition config = 
loadRestConfigurationDefinition(getCamelContext(), is);
+                    if (config != null) {
+                        config.asRestConfiguration(getCamelContext(), 
getCamelContext().getRestConfiguration());
+                    }
+                }
             }
 
             @Override

Reply via email to