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 06e351d491c CAMEL-21071: camel-rest-openapi - Auto detect 
RestApiConsumerFactory from classpath (#15090)
06e351d491c is described below

commit 06e351d491cdb69379208d38b692b084c6ed8b52
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Aug 10 12:34:26 2024 +0200

    CAMEL-21071: camel-rest-openapi - Auto detect RestApiConsumerFactory from 
classpath (#15090)
---
 .../DefaultRestOpenapiProcessorStrategy.java       | 10 +++++----
 .../rest/openapi/RestOpenApiProcessor.java         |  3 ---
 .../camel/component/rest/RestApiEndpoint.java      | 24 ++++++++++++++++++++++
 .../apache/camel/component/rest/RestEndpoint.java  | 22 ++++++--------------
 .../java/org/apache/camel/main/KameletMain.java    |  7 +------
 .../DependencyDownloaderComponentResolver.java     |  2 +-
 6 files changed, 38 insertions(+), 30 deletions(-)

diff --git 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
index 5021ca826b0..7d62f6a169c 100644
--- 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
+++ 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
@@ -395,10 +395,12 @@ public class DefaultRestOpenapiProcessorStrategy extends 
ServiceSupport
     protected void doStop() throws Exception {
         ServiceHelper.stopService(producerCache);
 
-        PlatformHttpComponent phc = camelContext.getComponent("platform-http", 
PlatformHttpComponent.class);
-        if (phc != null) {
-            uris.forEach(phc::removeHttpEndpoint);
-            uris.clear();
+        if (camelContext != null) {
+            PlatformHttpComponent phc = (PlatformHttpComponent) 
camelContext.hasComponent("platform-http");
+            if (phc != null) {
+                uris.forEach(phc::removeHttpEndpoint);
+                uris.clear();
+            }
         }
     }
 
diff --git 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
index eee89ee80d5..45d3b8e48b8 100644
--- 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
+++ 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
@@ -127,9 +127,6 @@ public class RestOpenApiProcessor extends 
DelegateAsyncProcessor implements Came
         if (m instanceof RestOpenApiConsumerPath rcp) {
             Operation o = rcp.getConsumer();
 
-            // binding mode
-            RestConfiguration config = camelContext.getRestConfiguration();
-
             // map path-parameters from operation to camel headers
             HttpHelper.evalPlaceholders(exchange.getMessage().getHeaders(), 
uri, rcp.getConsumerPath());
 
diff --git 
a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
 
b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
index ca80cdb8443..547e18c9f1a 100644
--- 
a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
+++ 
b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
@@ -38,6 +38,8 @@ import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultEndpoint;
 import org.apache.camel.util.HostUtils;
 import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Expose OpenAPI Specification of the REST services defined using Camel REST 
DSL.
@@ -46,9 +48,14 @@ import org.apache.camel.util.ObjectHelper;
              remote = false, consumerOnly = true, category = { Category.CORE, 
Category.REST }, lenientProperties = true)
 public class RestApiEndpoint extends DefaultEndpoint {
 
+    public static final String[] DEFAULT_REST_API_CONSUMER_COMPONENTS
+            = new String[] { "platform-http", "servlet", "jetty", "undertow", 
"netty-http" };
+
     public static final String DEFAULT_API_COMPONENT_NAME = "openapi";
     public static final String RESOURCE_PATH = 
"META-INF/services/org/apache/camel/restapi/";
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(RestApiEndpoint.class);
+
     @UriPath
     @Metadata(required = true)
     private String path;
@@ -246,6 +253,23 @@ public class RestApiEndpoint extends DefaultEndpoint {
                 factory = factories.iterator().next();
             }
         }
+        // no explicit factory found then try to see if we can find any of the 
default rest consumer components
+        if (factory == null) {
+            RestApiConsumerFactory found = null;
+            String foundName = null;
+            for (String name : DEFAULT_REST_API_CONSUMER_COMPONENTS) {
+                Object comp = getCamelContext().getComponent(name, true);
+                if (comp instanceof RestApiConsumerFactory) {
+                    found = (RestApiConsumerFactory) comp;
+                    foundName = name;
+                    break;
+                }
+            }
+            if (found != null) {
+                LOG.debug("Auto discovered {} as RestApiConsumerFactory", 
foundName);
+                factory = found;
+            }
+        }
 
         if (factory != null) {
             // calculate the url to the rest API service
diff --git 
a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
 
b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
index da08c031b7c..d50aaf315df 100644
--- 
a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
+++ 
b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
@@ -399,20 +399,15 @@ public class RestEndpoint extends DefaultEndpoint {
         }
 
         // no explicit factory found then try to see if we can find any of the 
default rest producer components
-        // and there must only be exactly one so we safely can pick this one
         if (factory == null) {
             RestProducerFactory found = null;
             String foundName = null;
             for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
                 Object comp = setupComponent(name, getCamelContext(), 
(Map<String, Object>) parameters.get("component"));
                 if (comp instanceof RestProducerFactory) {
-                    if (found == null) {
-                        found = (RestProducerFactory) comp;
-                        foundName = name;
-                    } else {
-                        throw new IllegalArgumentException(
-                                "Multiple RestProducerFactory found on 
classpath. Configure explicit which component to use");
-                    }
+                    found = (RestProducerFactory) comp;
+                    foundName = name;
+                    break;
                 }
             }
             if (found != null) {
@@ -506,20 +501,15 @@ public class RestEndpoint extends DefaultEndpoint {
         }
 
         // no explicit factory found then try to see if we can find any of the 
default rest consumer components
-        // and there must only be exactly one so we safely can pick this one
         if (factory == null) {
             RestConsumerFactory found = null;
             String foundName = null;
             for (String name : DEFAULT_REST_CONSUMER_COMPONENTS) {
                 Object comp = getCamelContext().getComponent(name, true);
                 if (comp instanceof RestConsumerFactory) {
-                    if (found == null) {
-                        found = (RestConsumerFactory) comp;
-                        foundName = name;
-                    } else {
-                        throw new IllegalArgumentException(
-                                "Multiple RestConsumerFactory found on 
classpath. Configure explicit which component to use");
-                    }
+                    found = (RestConsumerFactory) comp;
+                    foundName = name;
+                    break;
                 }
             }
             if (found != null) {
diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 3536430974a..22290636038 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -790,12 +790,7 @@ public class KameletMain extends MainCommandLineSupport {
      */
     protected void configureInitialProperties(String location) {
         // optional configuration if these components are in-use
-        addInitialProperty("camel.component.?kamelet.location", location);
-        addInitialProperty("camel.component.?rest-api.consumerComponentName", 
"platform-http");
-        addInitialProperty("camel.component.?rest.consumerComponentName", 
"platform-http");
-        addInitialProperty("camel.component.?rest.producerComponentName", 
"vertx-http");
-        // make it easy to load mock-data from file without having to add 
camel-mock to classpath
-        addInitialProperty("camel.component.?rest-openapi.mockIncludePattern", 
"file:camel-mock/**,classpath:camel-mock/**");
+        addInitialProperty("camel.component.kamelet.location", location);
     }
 
     protected String startupInfo() {
diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java
index a6ea02a17e6..392919fe4ca 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java
@@ -34,7 +34,7 @@ import org.apache.camel.tooling.model.ComponentModel;
 public final class DependencyDownloaderComponentResolver extends 
DefaultComponentResolver {
 
     private static final String ACCEPTED_STUB_NAMES
-            = 
"stub,bean,class,direct,kamelet,log,platform-http,rest,rest-api,seda,vertx-http";
+            = 
"stub,bean,class,direct,kamelet,log,platform-http,rest,seda,vertx-http";
 
     private final CamelCatalog catalog = new DefaultCamelCatalog();
     private final CamelContext camelContext;

Reply via email to