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 2026/05/05 12:44:29 UTC

(camel) branch rd created (now f18cb2de5d62)

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

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


      at f18cb2de5d62 CAMEL-23416: camel-jbang - Add route diagram web console to show route diagram when using --console

This branch includes the following new commits:

     new f18cb2de5d62 CAMEL-23416: camel-jbang - Add route diagram web console to show route diagram when using --console

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: CAMEL-23416: camel-jbang - Add route diagram web console to show route diagram when using --console

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f18cb2de5d62320402b0523b0878c6b00f1027db
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 5 14:35:48 2026 +0200

    CAMEL-23416: camel-jbang - Add route diagram web console to show route diagram when using --console
---
 .../platform/http/main/ManagementHttpServer.java   |   8 +-
 dsl/camel-jbang/camel-jbang-console/pom.xml        |   4 +
 .../apache/camel/dev-console/route-diagram.json    |  15 +++
 .../org/apache/camel/dev-console/route-diagram     |   2 +
 .../org/apache/camel/dev-consoles.properties       |   2 +-
 .../camel/jbang/console/DiagramDevConsole.java     | 139 +++++++++++++++++++++
 6 files changed, 167 insertions(+), 3 deletions(-)

diff --git a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/ManagementHttpServer.java b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/ManagementHttpServer.java
index fc722247736b..648f4e979236 100644
--- a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/ManagementHttpServer.java
+++ b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/ManagementHttpServer.java
@@ -1020,7 +1020,7 @@ public class ManagementHttpServer extends ServiceSupport implements CamelContext
                 final boolean json = pos2 < pos1;
                 final DevConsole.MediaType mediaType = json ? DevConsole.MediaType.JSON : DevConsole.MediaType.TEXT;
 
-                ctx.response().putHeader("content-type", "text/plain");
+                ctx.response().putHeader("content-type", "text/plain;charset=utf-8");
 
                 if (!camelContext.isDevConsole()) {
                     ctx.end("Developer Console is not enabled on CamelContext. Set camel.context.dev-console=true in application.properties");
@@ -1097,7 +1097,11 @@ public class ManagementHttpServer extends ServiceSupport implements CamelContext
                         boolean include = "all".equals(id) || c.getId().equalsIgnoreCase(id);
                         if (include && c.supportMediaType(mediaType)) {
                             Object out = c.call(mediaType, params);
-                            if (out != null && mediaType == DevConsole.MediaType.TEXT) {
+                            // special if returning html
+                            if (out instanceof String text && text.startsWith("<html>")) {
+                                ctx.response().putHeader("content-type", "text/html");
+                                sb.append(out);
+                            } else if (out != null && mediaType == DevConsole.MediaType.TEXT) {
                                 sb.append(c.getDisplayName()).append(":");
                                 sb.append("\n\n");
                                 sb.append(out);
diff --git a/dsl/camel-jbang/camel-jbang-console/pom.xml b/dsl/camel-jbang/camel-jbang-console/pom.xml
index 5c963b0702a2..671163c60ac8 100644
--- a/dsl/camel-jbang/camel-jbang-console/pom.xml
+++ b/dsl/camel-jbang/camel-jbang-console/pom.xml
@@ -50,6 +50,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-support</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-diagram</artifactId>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/org/apache/camel/dev-console/route-diagram.json b/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/org/apache/camel/dev-console/route-diagram.json
new file mode 100644
index 000000000000..12d04504b577
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/org/apache/camel/dev-console/route-diagram.json
@@ -0,0 +1,15 @@
+{
+  "console": {
+    "kind": "console",
+    "group": "camel-jbang",
+    "name": "route-diagram",
+    "title": "Route Diagram",
+    "description": "Visual route diagrams",
+    "deprecated": false,
+    "javaType": "org.apache.camel.jbang.console.DiagramDevConsole",
+    "groupId": "org.apache.camel",
+    "artifactId": "camel-jbang-console",
+    "version": "4.21.0-SNAPSHOT"
+  }
+}
+
diff --git a/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-diagram b/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-diagram
new file mode 100644
index 000000000000..9489d2910671
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-diagram
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.jbang.console.DiagramDevConsole
diff --git a/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties b/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
index 952b9e2c8844..ec45d06d2d45 100644
--- a/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
+++ b/dsl/camel-jbang/camel-jbang-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
@@ -1,5 +1,5 @@
 # Generated by camel build tools - do NOT edit this file!
-dev-consoles=jbang source-dir
+dev-consoles=jbang route-diagram source-dir
 groupId=org.apache.camel
 artifactId=camel-jbang-console
 version=4.21.0-SNAPSHOT
diff --git a/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/DiagramDevConsole.java b/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/DiagramDevConsole.java
new file mode 100644
index 000000000000..31fb187b907a
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/DiagramDevConsole.java
@@ -0,0 +1,139 @@
+/*
+ * 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.jbang.console;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import java.util.StringJoiner;
+
+import javax.imageio.ImageIO;
+
+import org.apache.camel.console.DevConsoleRegistry;
+import org.apache.camel.diagram.RouteDiagramHelper;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine;
+import org.apache.camel.diagram.RouteDiagramRenderer;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole(name = "route-diagram", group = "camel-jbang", displayName = "Route Diagram", description = "Visual route diagrams")
+public class DiagramDevConsole extends AbstractDevConsole {
+
+    /**
+     * Theme to use: dark, light, or text
+     */
+    public static final String THEME = "theme";
+
+    public DiagramDevConsole() {
+        super("camel-jbang", "route-diagram", "Route Diagram", "Visual route diagrams");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        final StringJoiner sj = new StringJoiner("\n");
+
+        String theme = (String) options.get(THEME);
+
+        org.apache.camel.console.DevConsole dc
+                = getCamelContext().getCamelContextExtension().getContextPlugin(DevConsoleRegistry.class)
+                        .resolveById("route-structure");
+        JsonObject root = (JsonObject) dc.call(MediaType.JSON);
+        // parseRoutes expect a JsonArray and not ArrayList
+        JsonArray arr = new JsonArray(root.getCollection("routes"));
+        root.put("routes", arr);
+        var routes = RouteDiagramHelper.parseRoutes(root);
+        if ("text".equals(theme)) {
+            RouteDiagramRenderer renderer = new RouteDiagramRenderer();
+            var lines = renderer.printTextDiagram(routes);
+            lines.forEach(sj::add);
+            sj.add("");
+        } else {
+            try {
+                BufferedImage image = renderImage(routes, theme);
+                String base64 = imageToBase64(image, "png");
+                // For HTML embedding:
+                String html = "<html>%n<body>%n<img src=\"data:image/png;base64,%s\" alt=\"Route Diagram\"%n</body>%n</html>>"
+                        .formatted(base64);
+                sj.add(html);
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+        return sj.toString();
+    }
+
+    @Override
+    protected Map<String, Object> doCallJson(Map<String, Object> options) {
+        String theme = (String) options.get(THEME);
+
+        org.apache.camel.console.DevConsole dc
+                = getCamelContext().getCamelContextExtension().getContextPlugin(DevConsoleRegistry.class)
+                        .resolveById("route-structure");
+        JsonObject root = (JsonObject) dc.call(MediaType.JSON);
+        // parseRoutes expect a JsonArray and not ArrayList
+        JsonArray arr = new JsonArray(root.getCollection("routes"));
+        root.put("routes", arr);
+        var routes = RouteDiagramHelper.parseRoutes(root);
+
+        root = new JsonObject();
+        try {
+            BufferedImage image = renderImage(routes, theme);
+            String base64 = imageToBase64(image, "png");
+            root.put("image", base64);
+        } catch (Exception e) {
+            // ignore
+        }
+
+        return root;
+    }
+
+    private static BufferedImage renderImage(List<RouteDiagramLayoutEngine.RouteInfo> routes, String theme) {
+        RouteDiagramRenderer renderer = new RouteDiagramRenderer();
+        RouteDiagramLayoutEngine engine = new RouteDiagramLayoutEngine();
+
+        List<RouteDiagramLayoutEngine.LayoutRoute> layoutRoutes = new ArrayList<>();
+        int currentY = RouteDiagramLayoutEngine.PADDING;
+        for (RouteDiagramLayoutEngine.RouteInfo route : routes) {
+            RouteDiagramLayoutEngine.LayoutRoute lr = engine.layoutRoute(route, currentY);
+            layoutRoutes.add(lr);
+            currentY = lr.maxY + RouteDiagramLayoutEngine.V_GAP;
+        }
+        if (theme == null) {
+            theme = "transparent";
+        }
+        RouteDiagramRenderer.DiagramColors colors = RouteDiagramRenderer.DiagramColors.parse(theme);
+        return renderer.renderDiagram(layoutRoutes, currentY, colors);
+    }
+
+    private static String imageToBase64(BufferedImage image, String format) throws Exception {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ImageIO.write(image, format, baos);
+        baos.flush();
+        try {
+            byte[] imageBytes = baos.toByteArray();
+            return Base64.getEncoder().encodeToString(imageBytes);
+        } finally {
+            IOHelper.close(baos);
+        }
+    }
+}