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 2024/02/21 14:56:16 UTC

(camel) branch http-jolokia created (now 6d2eda00706)

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

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


      at 6d2eda00706 CAMEL-20447: camel-platform-http-main should not depend on jolokia but load it via the plugin.

This branch includes the following new commits:

     new 6d2eda00706 CAMEL-20447: camel-platform-http-main should not depend on jolokia but load it via the plugin.

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-20447: camel-platform-http-main should not depend on jolokia but load it via the plugin.

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

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

commit 6d2eda0070640a840fe981aa99ee7846e5b49e3f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Feb 21 15:56:01 2024 +0100

    CAMEL-20447: camel-platform-http-main should not depend on jolokia but load it via the plugin.
---
 components/camel-platform-http-jolokia/pom.xml     | 12 +++
 .../plugin/DefaultJolokiaPlatformHttpPlugin.java   | 88 ++++++++++++++++++++--
 .../DefaultJolokiaPlatformHttpPluginTest.java      |  3 +-
 .../platform/http/main/MainHttpServer.java         | 68 +----------------
 components/camel-platform-http/pom.xml             |  7 --
 .../http/plugin/JolokiaPlatformHttpPlugin.java     |  6 +-
 6 files changed, 99 insertions(+), 85 deletions(-)

diff --git a/components/camel-platform-http-jolokia/pom.xml b/components/camel-platform-http-jolokia/pom.xml
index 974b51fc0b2..ebc337e233f 100644
--- a/components/camel-platform-http-jolokia/pom.xml
+++ b/components/camel-platform-http-jolokia/pom.xml
@@ -47,6 +47,18 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-platform-http</artifactId>
         </dependency>
+        <dependency>
+            <groupId>io.vertx</groupId>
+            <artifactId>vertx-web</artifactId>
+            <version>${vertx-version}</version>
+        </dependency>
+
+        <!-- jolokia -->
+        <dependency>
+            <groupId>org.jolokia</groupId>
+            <artifactId>jolokia-agent-jvm</artifactId>
+            <version>${jolokia-version}</version>
+        </dependency>
 
         <!-- json -->
         <dependency>
diff --git a/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java b/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java
index 7701a57b6fd..6cc3a4ae9c4 100644
--- a/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java
+++ b/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java
@@ -17,7 +17,22 @@
 package org.apache.camel.component.platform.http.plugin;
 
 import java.io.IOException;
-
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.RuntimeMBeanException;
+
+import io.netty.buffer.ByteBufInputStream;
+import io.vertx.core.Handler;
+import io.vertx.core.MultiMap;
+import io.vertx.core.http.HttpHeaders;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.http.HttpServerRequest;
+import io.vertx.core.impl.Arguments;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.impl.Utils;
 import org.apache.camel.CamelContext;
 import org.apache.camel.spi.annotations.JdkService;
 import org.apache.camel.support.service.ServiceSupport;
@@ -34,6 +49,8 @@ import org.jolokia.server.core.service.api.Restrictor;
 import org.jolokia.server.core.util.NetworkUtil;
 import org.jolokia.service.jmx.LocalRequestHandler;
 import org.jolokia.service.serializer.JolokiaSerializer;
+import org.json.simple.JSONAware;
+import org.json.simple.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,13 +59,11 @@ public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements
 
     private static final Logger LOG = LoggerFactory.getLogger(DefaultJolokiaPlatformHttpPlugin.class);
 
+    private CamelContext camelContext;
     private final JolokiaServiceManager serviceManager;
-
-    private HttpRequestHandler requestHandler;
-
     private final LogHandler jolokiaLogHandler;
-
-    private CamelContext camelContext;
+    private HttpRequestHandler requestHandler;
+    private Handler<RoutingContext> handler;
 
     public DefaultJolokiaPlatformHttpPlugin() {
         var config = new StaticConfiguration(ConfigKey.AGENT_ID, NetworkUtil.getAgentId(hashCode(), "vertx"));
@@ -69,6 +84,7 @@ public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements
     public void doStart() {
         var jolokiaContext = serviceManager.start();
         requestHandler = new HttpRequestHandler(jolokiaContext);
+        handler = createVertxHandler();
     }
 
     @Override
@@ -79,7 +95,11 @@ public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements
     }
 
     @Override
-    public HttpRequestHandler getRequestHandler() {
+    public Object getHandler() {
+        return handler;
+    }
+
+    public HttpRequestHandler getJolokiaRequestHandler() {
         return requestHandler;
     }
 
@@ -139,4 +159,58 @@ public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements
         }
     }
 
+    private Handler<RoutingContext> createVertxHandler() {
+        return routingContext -> {
+            HttpServerRequest req = routingContext.request();
+            String remainingPath = Utils.pathOffset(req.path(), routingContext);
+
+            JSONAware json = null;
+            try {
+                requestHandler.checkAccess(req.remoteAddress().host(), req.remoteAddress().host(), getOriginOrReferer(req));
+                if (req.method() == HttpMethod.GET) {
+                    json = requestHandler.handleGetRequest(req.uri(), remainingPath, getParams(req.params()));
+                } else {
+                    Arguments.require(routingContext.body() != null, "Missing body");
+                    InputStream inputStream = new ByteBufInputStream(routingContext.body().buffer().getByteBuf());
+                    json = requestHandler.handlePostRequest(req.uri(), inputStream, StandardCharsets.UTF_8.name(),
+                            getParams(req.params()));
+                }
+            } catch (Throwable exp) {
+                json = requestHandler.handleThrowable(
+                        exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp);
+            } finally {
+                if (json == null)
+                    json = requestHandler.handleThrowable(new Exception("Internal error while handling an exception"));
+
+                routingContext.response()
+                        .setStatusCode(getStatusCode(json))
+                        .putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
+                        .end(json.toJSONString());
+            }
+        };
+    }
+
+    private Map<String, String[]> getParams(MultiMap params) {
+        Map<String, String[]> response = new HashMap<>();
+        for (String name : params.names()) {
+            response.put(name, params.getAll(name).toArray(new String[0]));
+        }
+        return response;
+    }
+
+    private String getOriginOrReferer(HttpServerRequest req) {
+        String origin = req.getHeader(HttpHeaders.ORIGIN);
+        if (origin == null) {
+            origin = req.getHeader(HttpHeaders.REFERER);
+        }
+        return origin != null ? origin.replaceAll("[\\n\\r]*", "") : null;
+    }
+
+    private int getStatusCode(JSONAware json) {
+        if (json instanceof JSONObject && ((JSONObject) json).get("status") instanceof Integer) {
+            return (Integer) ((JSONObject) json).get("status");
+        }
+        return 200;
+    }
+
 }
diff --git a/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java b/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java
index 6b7f6caf9cc..730c1504e10 100644
--- a/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java
+++ b/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java
@@ -19,7 +19,6 @@ package org.apache.camel.component.platform.http.plugin;
 import java.util.HashMap;
 import java.util.Optional;
 
-import com.fasterxml.jackson.core.util.VersionUtil;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.component.platform.http.spi.PlatformHttpPluginRegistry;
 import org.apache.camel.support.ResolverHelper;
@@ -41,7 +40,7 @@ public class DefaultJolokiaPlatformHttpPluginTest extends ContextTestSupport {
         DefaultJolokiaPlatformHttpPlugin plugin = registry
                 .resolvePluginById("jolokia", DefaultJolokiaPlatformHttpPlugin.class).orElseThrow();
         Assertions.assertNotNull(plugin);
-        HttpRequestHandler handler = plugin.getRequestHandler();
+        HttpRequestHandler handler = plugin.getJolokiaRequestHandler();
 
         JSONAware json = handler.handleGetRequest("", "/", new HashMap<>());
 
diff --git a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java
index e573fb55254..18909f9a94c 100644
--- a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java
+++ b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java
@@ -18,10 +18,8 @@ package org.apache.camel.component.platform.http.main;
 
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -34,21 +32,13 @@ import java.util.Set;
 import java.util.TreeSet;
 import java.util.stream.Collectors;
 
-import javax.management.RuntimeMBeanException;
-
-import io.netty.buffer.ByteBufInputStream;
 import io.vertx.core.Handler;
-import io.vertx.core.MultiMap;
-import io.vertx.core.http.HttpHeaders;
 import io.vertx.core.http.HttpMethod;
-import io.vertx.core.http.HttpServerRequest;
-import io.vertx.core.impl.Arguments;
 import io.vertx.ext.web.RequestBody;
 import io.vertx.ext.web.Route;
 import io.vertx.ext.web.RoutingContext;
 import io.vertx.ext.web.handler.BodyHandler;
 import io.vertx.ext.web.impl.BlockingHandlerDecorator;
-import io.vertx.ext.web.impl.Utils;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
@@ -80,9 +70,6 @@ import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.json.JsonObject;
-import org.jolokia.server.core.http.HttpRequestHandler;
-import org.json.simple.JSONAware;
-import org.json.simple.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -467,37 +454,7 @@ public class MainHttpServer extends ServiceSupport implements CamelContextAware,
         jolokia.method(HttpMethod.GET);
         jolokia.method(HttpMethod.POST);
 
-        Handler<RoutingContext> handler = routingContext -> {
-            HttpRequestHandler requestHandler = jolokiaPlugin.getRequestHandler();
-
-            HttpServerRequest req = routingContext.request();
-            String remainingPath = Utils.pathOffset(req.path(), routingContext);
-
-            JSONAware json = null;
-            try {
-                requestHandler.checkAccess(req.remoteAddress().host(), req.remoteAddress().host(), getOriginOrReferer(req));
-                if (req.method() == HttpMethod.GET) {
-                    json = requestHandler.handleGetRequest(req.uri(), remainingPath, getParams(req.params()));
-                } else {
-                    Arguments.require(routingContext.body() != null, "Missing body");
-                    InputStream inputStream = new ByteBufInputStream(routingContext.body().buffer().getByteBuf());
-                    json = requestHandler.handlePostRequest(req.uri(), inputStream, StandardCharsets.UTF_8.name(),
-                            getParams(req.params()));
-                }
-            } catch (Throwable exp) {
-                json = requestHandler.handleThrowable(
-                        exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp);
-            } finally {
-                if (json == null)
-                    json = requestHandler.handleThrowable(new Exception("Internal error while handling an exception"));
-
-                routingContext.response()
-                        .setStatusCode(getStatusCode(json))
-                        .putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
-                        .end(json.toJSONString());
-            }
-        };
-
+        Handler<RoutingContext> handler = (Handler<RoutingContext>) jolokiaPlugin.getHandler();
         jolokia.handler(new BlockingHandlerDecorator(handler, true));
 
         platformHttpComponent.addHttpEndpoint("/q/jolokia", null, null);
@@ -512,29 +469,6 @@ public class MainHttpServer extends ServiceSupport implements CamelContextAware,
                 "Cannot create PlatformHttpPluginRegistry. Make sure camel-platform-http JAR is on classpath."));
     }
 
-    private Map<String, String[]> getParams(MultiMap params) {
-        Map<String, String[]> response = new HashMap<>();
-        for (String name : params.names()) {
-            response.put(name, params.getAll(name).toArray(new String[0]));
-        }
-        return response;
-    }
-
-    private String getOriginOrReferer(HttpServerRequest req) {
-        String origin = req.getHeader(HttpHeaders.ORIGIN);
-        if (origin == null) {
-            origin = req.getHeader(HttpHeaders.REFERER);
-        }
-        return origin != null ? origin.replaceAll("[\\n\\r]*", "") : null;
-    }
-
-    protected int getStatusCode(JSONAware json) {
-        if (json instanceof JSONObject && ((JSONObject) json).get("status") instanceof Integer) {
-            return (Integer) ((JSONObject) json).get("status");
-        }
-        return 200;
-    }
-
     @Override
     protected void doStop() throws Exception {
         ServiceHelper.stopAndShutdownService(server);
diff --git a/components/camel-platform-http/pom.xml b/components/camel-platform-http/pom.xml
index 5d567f0d69b..b1b5f3961f3 100644
--- a/components/camel-platform-http/pom.xml
+++ b/components/camel-platform-http/pom.xml
@@ -42,13 +42,6 @@
             <artifactId>camel-util-json</artifactId>
         </dependency>
 
-        <!-- jolokia -->
-        <dependency>
-            <groupId>org.jolokia</groupId>
-            <artifactId>jolokia-agent-jvm</artifactId>
-            <version>${jolokia-version}</version>
-        </dependency>
-
         <!-- test infra -->
         <dependency>
             <groupId>org.apache.camel</groupId>
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java
index 04393c1491d..6adc9cdd4d4 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java
@@ -17,7 +17,6 @@
 package org.apache.camel.component.platform.http.plugin;
 
 import org.apache.camel.component.platform.http.spi.PlatformHttpPlugin;
-import org.jolokia.server.core.http.HttpRequestHandler;
 
 /**
  * Plugin for Jolokia
@@ -26,5 +25,8 @@ public interface JolokiaPlatformHttpPlugin extends PlatformHttpPlugin {
 
     String NAME = "jolokia";
 
-    HttpRequestHandler getRequestHandler();
+    /**
+     * The VertX Handler (io.vertx.core.Handler) that handles the HTTP requests to service Jolokia
+     */
+    Object getHandler();
 }