You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/06/22 12:47:48 UTC

[camel-spring-boot] branch main updated: CAMEL-18149: camel-spring-boot - Add camel developer console as actuator that can be enabled.

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-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new fb1ded8c653 CAMEL-18149: camel-spring-boot - Add camel developer console as actuator that can be enabled.
fb1ded8c653 is described below

commit fb1ded8c653338bff8cd3a37c6fb7b3fb5564675
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jun 22 14:40:35 2022 +0200

    CAMEL-18149: camel-spring-boot - Add camel developer console as actuator that can be enabled.
---
 core/camel-spring-boot/pom.xml                     |  4 +
 .../src/main/docs/spring-boot.json                 | 19 +++++
 .../console/CamelDevConsoleAutoConfiguration.java  | 49 ++++++++++++
 .../actuate/console/CamelDevConsoleEndpoint.java   | 91 ++++++++++++++++++++++
 .../src/main/resources/META-INF/spring.factories   |  1 +
 5 files changed, 164 insertions(+)

diff --git a/core/camel-spring-boot/pom.xml b/core/camel-spring-boot/pom.xml
index d083545c59a..9a5079f00fd 100644
--- a/core/camel-spring-boot/pom.xml
+++ b/core/camel-spring-boot/pom.xml
@@ -74,6 +74,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-spring-main</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-util-json</artifactId>
+        </dependency>
         <!-- requires xml routes loader that is jaxb based -->
         <dependency>
             <groupId>org.apache.camel</groupId>
diff --git a/core/camel-spring-boot/src/main/docs/spring-boot.json b/core/camel-spring-boot/src/main/docs/spring-boot.json
index 37df44069a6..0e7a635413c 100644
--- a/core/camel-spring-boot/src/main/docs/spring-boot.json
+++ b/core/camel-spring-boot/src/main/docs/spring-boot.json
@@ -129,6 +129,11 @@
       "type": "org.apache.camel.spring.boot.vault.GcpVaultConfigurationProperties",
       "sourceType": "org.apache.camel.spring.boot.vault.GcpVaultConfigurationProperties"
     },
+    {
+      "name": "management.endpoint.camel",
+      "type": "org.apache.camel.spring.boot.actuate.console.CamelDevConsoleEndpoint",
+      "sourceType": "org.apache.camel.spring.boot.actuate.console.CamelDevConsoleEndpoint"
+    },
     {
       "name": "management.endpoint.camelroutecontroller",
       "type": "org.apache.camel.spring.boot.actuate.endpoint.CamelRouteControllerEndpoint",
@@ -1398,6 +1403,20 @@
       "sourceType": "org.apache.camel.spring.boot.vault.GcpVaultConfigurationProperties",
       "defaultValue": false
     },
+    {
+      "name": "management.endpoint.camel.cache.time-to-live",
+      "type": "java.time.Duration",
+      "description": "Maximum time that a response can be cached.",
+      "sourceType": "org.apache.camel.spring.boot.actuate.console.CamelDevConsoleEndpoint",
+      "defaultValue": "0ms"
+    },
+    {
+      "name": "management.endpoint.camel.enabled",
+      "type": "java.lang.Boolean",
+      "description": "Whether to enable the camel endpoint.",
+      "sourceType": "org.apache.camel.spring.boot.actuate.console.CamelDevConsoleEndpoint",
+      "defaultValue": true
+    },
     {
       "name": "management.endpoint.camelroutecontroller.cache.time-to-live",
       "type": "java.time.Duration",
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/console/CamelDevConsoleAutoConfiguration.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/console/CamelDevConsoleAutoConfiguration.java
new file mode 100644
index 00000000000..20ba3633ac8
--- /dev/null
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/console/CamelDevConsoleAutoConfiguration.java
@@ -0,0 +1,49 @@
+/*
+ * 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.spring.boot.actuate.console;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.spring.boot.actuate.endpoint.CamelRouteControllerEndpoint;
+import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/*
+ * Auto configuration for the {@link CamelDevConsoleEndpoint}.
+ */
+@Configuration(proxyBeanMethods = false)
+@ConditionalOnClass(name = "org.apache.camel.impl.console.DefaultDevConsoleRegistry")
+@ConditionalOnBean(CamelAutoConfiguration.class)
+@AutoConfigureAfter(CamelAutoConfiguration.class)
+public class CamelDevConsoleAutoConfiguration {
+
+    @Bean
+    @ConditionalOnClass(CamelContext.class)
+    @ConditionalOnMissingBean
+    public CamelDevConsoleEndpoint devConsoleEndpoint(CamelContext camelContext) {
+        // turn on dev console
+        camelContext.setDevConsole(true);
+        return new CamelDevConsoleEndpoint(camelContext);
+    }
+
+
+}
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/console/CamelDevConsoleEndpoint.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/console/CamelDevConsoleEndpoint.java
new file mode 100644
index 00000000000..8b2c13edb48
--- /dev/null
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/console/CamelDevConsoleEndpoint.java
@@ -0,0 +1,91 @@
+/*
+ * 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.spring.boot.actuate.console;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.console.DevConsole;
+import org.apache.camel.console.DevConsoleRegistry;
+import org.apache.camel.util.json.JsonObject;
+import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
+import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
+import org.springframework.boot.actuate.endpoint.annotation.Selector;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/*
+ * Camel Developer Console
+ */
+@Endpoint(id = "camel")
+public class CamelDevConsoleEndpoint {
+
+    private CamelContext camelContext;
+
+    public CamelDevConsoleEndpoint(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @ReadOperation
+    public JsonObject getConsoles() {
+        DevConsoleRegistry dcr = camelContext.getExtension(DevConsoleRegistry.class);
+        if (dcr == null || !dcr.isEnabled()) {
+            return null;
+        }
+
+        JsonObject root = new JsonObject();
+        dcr.stream().forEach(c -> {
+            JsonObject jo = new JsonObject();
+            jo.put("id", c.getId());
+            jo.put("displayName", c.getDisplayName());
+            jo.put("description", c.getDescription());
+            root.put(c.getId(), jo);
+        });
+
+        return root;
+    }
+
+    @ReadOperation
+    public JsonObject getConsoleById(@Selector String id) {
+        DevConsoleRegistry dcr = camelContext.getExtension(DevConsoleRegistry.class);
+        if (dcr == null || !dcr.isEnabled()) {
+            return null;
+        }
+
+        Map<String, Object> params = new HashMap<>();
+        params.put(Exchange.HTTP_PATH, id);
+        JsonObject root = new JsonObject();
+
+        // sort according to index by given id
+        dcr.stream().sorted((o1, o2) -> {
+            int p1 = id.indexOf(o1.getId());
+            int p2 = id.indexOf(o2.getId());
+            return Integer.compare(p1, p2);
+        }).forEach(c -> {
+            boolean include = "all".equals(id) || id.contains(c.getId());
+            if (include && c.supportMediaType(DevConsole.MediaType.JSON)) {
+                Object out = c.call(DevConsole.MediaType.JSON, params);
+                if (out != null) {
+                    root.put(c.getId(), out);
+                }
+            }
+        });
+
+        return root;
+    }
+
+}
diff --git a/core/camel-spring-boot/src/main/resources/META-INF/spring.factories b/core/camel-spring-boot/src/main/resources/META-INF/spring.factories
index 0a29f189e6e..e43ce29074f 100644
--- a/core/camel-spring-boot/src/main/resources/META-INF/spring.factories
+++ b/core/camel-spring-boot/src/main/resources/META-INF/spring.factories
@@ -17,6 +17,7 @@
 
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 org.apache.camel.spring.boot.CamelAutoConfiguration,\
+org.apache.camel.spring.boot.actuate.console.CamelDevConsoleAutoConfiguration,\
 org.apache.camel.spring.boot.actuate.endpoint.CamelRouteControllerEndpointAutoConfiguration,\
 org.apache.camel.spring.boot.actuate.endpoint.CamelRoutesEndpointAutoConfiguration,\
 org.apache.camel.spring.boot.actuate.health.CamelHealthCheckAutoConfiguration,\