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/05/03 05:06:49 UTC
[camel] 02/02: CAMEL-18026: dev console for camel-platform-http
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
commit 5d746928e973fa0a3c7004532ac4da7851d35fd3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 3 07:05:12 2022 +0200
CAMEL-18026: dev console for camel-platform-http
---
.../http/vertx/VertxPlatformHttpEngine.java | 42 ++++++++++++++++-
.../http/vertx/VertxPlatformHttpRouter.java | 9 +++-
.../http/vertx/VertxPlatformHttpServer.java | 6 ++-
.../http/vertx/VertxPlatformHttpEngineTest.java | 7 +--
components/camel-platform-http/pom.xml | 5 ++
.../org/apache/camel/dev-console/platform-http | 2 +
.../component/platform/http/HttpEndpointModel.java | 4 +-
.../platform/http/PlatformHttpConsole.java | 54 ++++++++++++++++++++++
.../platform/http/spi/PlatformHttpEngine.java | 5 ++
.../http/JettyCustomPlatformHttpConsumer.java | 1 -
.../http/JettyCustomPlatformHttpEngine.java | 17 +++++++
.../component/platform/http/JettyServerTest.java | 18 ++++----
12 files changed, 152 insertions(+), 18 deletions(-)
diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
index 17c7246f811..325cd4d6507 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
@@ -22,25 +22,41 @@ import java.util.List;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.component.platform.http.PlatformHttpConstants;
import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.service.ServiceSupport;
/**
* Implementation of the {@link PlatformHttpEngine} based on Vert.x Web.
*/
@JdkService(PlatformHttpConstants.PLATFORM_HTTP_ENGINE_FACTORY)
-public class VertxPlatformHttpEngine extends ServiceSupport implements PlatformHttpEngine {
+public class VertxPlatformHttpEngine extends ServiceSupport implements PlatformHttpEngine, CamelContextAware {
+
+ private CamelContext camelContext;
private List<Handler<RoutingContext>> handlers;
+ private int port;
public VertxPlatformHttpEngine() {
this.handlers = Collections.emptyList();
}
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
public List<Handler<RoutingContext>> getHandlers() {
return Collections.unmodifiableList(handlers);
}
@@ -68,4 +84,28 @@ public class VertxPlatformHttpEngine extends ServiceSupport implements PlatformH
processor,
handlers);
}
+
+ @Override
+ public int getServerPort() {
+ if (port == 0) {
+ VertxPlatformHttpServer server = CamelContextHelper.findSingleByType(camelContext, VertxPlatformHttpServer.class);
+ if (server != null && server.getServer() != null) {
+ port = server.getServer().actualPort();
+ }
+ if (port == 0) {
+ VertxPlatformHttpServerConfiguration config
+ = CamelContextHelper.findSingleByType(camelContext, VertxPlatformHttpServerConfiguration.class);
+ if (config != null) {
+ port = config.getBindPort();
+ }
+ }
+ if (port == 0) {
+ VertxPlatformHttpRouter router = VertxPlatformHttpRouter.lookup(camelContext);
+ if (router != null && router.getServer() != null && router.getServer().getServer() != null) {
+ port = router.getServer().getServer().actualPort();
+ }
+ }
+ }
+ return port;
+ }
}
diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java
index 6cef545c347..0df41432f56 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java
@@ -22,6 +22,7 @@ import java.util.Map;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
+import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.AllowForwardHeaders;
import io.vertx.ext.web.Route;
@@ -35,11 +36,13 @@ import org.apache.camel.support.CamelContextHelper;
public class VertxPlatformHttpRouter implements Router {
public static final String PLATFORM_HTTP_ROUTER_NAME = PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME + "-router";
+ private final VertxPlatformHttpServer server;
private final Vertx vertx;
private final Router delegate;
private AllowForwardHeaders allowForward;
- public VertxPlatformHttpRouter(Vertx vertx, Router delegate) {
+ public VertxPlatformHttpRouter(VertxPlatformHttpServer server, Vertx vertx, Router delegate) {
+ this.server = server;
this.vertx = vertx;
this.delegate = delegate;
this.allowForward = AllowForwardHeaders.NONE;
@@ -49,6 +52,10 @@ public class VertxPlatformHttpRouter implements Router {
return vertx;
}
+ public VertxPlatformHttpServer getServer() {
+ return server;
+ }
+
@Override
public Route route() {
return delegate.route();
diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
index 9b3fa741bd2..9b3791eae6a 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
@@ -72,6 +72,10 @@ public class VertxPlatformHttpServer extends ServiceSupport implements CamelCont
this.context = context;
}
+ public HttpServer getServer() {
+ return server;
+ }
+
public Vertx getVertx() {
return vertx;
}
@@ -151,7 +155,7 @@ public class VertxPlatformHttpServer extends ServiceSupport implements CamelCont
context.getRegistry().bind(
VertxPlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME,
- new VertxPlatformHttpRouter(vertx, subRouter) {
+ new VertxPlatformHttpRouter(this, vertx, subRouter) {
@Override
public Handler<RoutingContext> bodyHandler() {
return createBodyHandler(configuration);
diff --git a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
index ca2cd0bc330..133e26b855f 100644
--- a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
+++ b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
@@ -38,6 +38,7 @@ import org.apache.camel.CamelContext;
import org.apache.camel.Message;
import org.apache.camel.attachment.AttachmentMessage;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.platform.http.HttpEndpointModel;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.rest.RestParamType;
@@ -150,9 +151,9 @@ public class VertxPlatformHttpEngineTest {
PlatformHttpComponent phc = context.getComponent("platform-http", PlatformHttpComponent.class);
assertEquals(2, phc.getHttpEndpoints().size());
- Iterator<String> it = phc.getHttpEndpoints().iterator();
- assertEquals("/get", it.next());
- assertEquals("/post", it.next());
+ Iterator<HttpEndpointModel> it = phc.getHttpEndpoints().iterator();
+ assertEquals("/get", it.next().getUri());
+ assertEquals("/post", it.next().getUri());
} finally {
context.stop();
diff --git a/components/camel-platform-http/pom.xml b/components/camel-platform-http/pom.xml
index b6a6e1fca07..81ab6a56781 100644
--- a/components/camel-platform-http/pom.xml
+++ b/components/camel-platform-http/pom.xml
@@ -37,6 +37,11 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-support</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-console</artifactId>
+ </dependency>
+
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
diff --git a/components/camel-platform-http/src/generated/resources/META-INF/services/org/apache/camel/dev-console/platform-http b/components/camel-platform-http/src/generated/resources/META-INF/services/org/apache/camel/dev-console/platform-http
new file mode 100644
index 00000000000..af7b2579928
--- /dev/null
+++ b/components/camel-platform-http/src/generated/resources/META-INF/services/org/apache/camel/dev-console/platform-http
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.platform.http.PlatformHttpConsole
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/HttpEndpointModel.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/HttpEndpointModel.java
index b89db34c204..5b53a7eadfd 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/HttpEndpointModel.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/HttpEndpointModel.java
@@ -16,11 +16,11 @@
*/
package org.apache.camel.component.platform.http;
-import org.apache.camel.util.StringHelper;
-
import java.util.Locale;
import java.util.Objects;
+import org.apache.camel.util.StringHelper;
+
/**
* Model of available http endpoints.
*/
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java
new file mode 100644
index 00000000000..50d5aadd363
--- /dev/null
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java
@@ -0,0 +1,54 @@
+/*
+ * 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.component.platform.http;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.impl.console.AbstractDevConsole;
+import org.apache.camel.spi.annotations.DevConsole;
+
+@DevConsole("platform-http")
+public class PlatformHttpConsole extends AbstractDevConsole {
+
+ public PlatformHttpConsole() {
+ super("camel", "platform-http", "Platform HTTP", "Embedded HTTP Server");
+ }
+
+ @Override
+ protected Object doCall(MediaType mediaType, Map<String, Object> options) {
+ // only text is supported
+ StringBuilder sb = new StringBuilder();
+
+ PlatformHttpComponent http = getCamelContext().getComponent("platform-http", PlatformHttpComponent.class);
+ if (http != null) {
+ int port = http.getEngine().getServerPort();
+ String server = "http://0.0.0.0:" + port;
+ Set<HttpEndpointModel> models = http.getHttpEndpoints();
+ for (HttpEndpointModel model : models) {
+ if (model.getVerbs() != null) {
+ sb.append(String.format(" %s%s (%s)\n", server, model.getUri(), model.getVerbs()));
+ } else {
+ sb.append(String.format(" %s%s\n", server, model.getUri()));
+ }
+ }
+ }
+
+ return sb.toString();
+ }
+
+}
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpEngine.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpEngine.java
index 89e3d4790a9..af5e7cc57b2 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpEngine.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpEngine.java
@@ -34,4 +34,9 @@ public interface PlatformHttpEngine {
*/
Consumer createConsumer(PlatformHttpEndpoint platformHttpEndpoint, Processor processor);
+ /**
+ * The port number the HTTP server is using
+ */
+ int getServerPort();
+
}
diff --git a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpConsumer.java b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpConsumer.java
index 182de854753..ec3bc5c0ce2 100644
--- a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpConsumer.java
+++ b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpConsumer.java
@@ -53,7 +53,6 @@ public class JettyCustomPlatformHttpConsumer extends DefaultConsumer {
ContextHandler contextHandler = createHandler(endpoint, path);
// add handler after starting server.
jettyServerTest.addHandler(contextHandler);
-
}
private ContextHandler createHandler(PlatformHttpEndpoint endpoint, String path) {
diff --git a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpEngine.java b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpEngine.java
index c5301f00150..93c5811a34c 100644
--- a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpEngine.java
+++ b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyCustomPlatformHttpEngine.java
@@ -19,11 +19,28 @@ package org.apache.camel.component.platform.http;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
+import org.apache.camel.support.CamelContextHelper;
public class JettyCustomPlatformHttpEngine implements PlatformHttpEngine {
+ private int port;
+
@Override
public Consumer createConsumer(PlatformHttpEndpoint platformHttpEndpoint, Processor processor) {
+ if (port == 0) {
+ JettyServerTest jettyServerTest = CamelContextHelper.mandatoryLookup(
+ platformHttpEndpoint.getCamelContext(),
+ JettyServerTest.JETTY_SERVER_NAME,
+ JettyServerTest.class);
+
+ port = jettyServerTest.getServerPort();
+ }
+
return new JettyCustomPlatformHttpConsumer(platformHttpEndpoint, processor);
}
+
+ @Override
+ public int getServerPort() {
+ return port;
+ }
}
diff --git a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyServerTest.java b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyServerTest.java
index b4c974eb66a..7af9f15a84b 100644
--- a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyServerTest.java
+++ b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/JettyServerTest.java
@@ -24,12 +24,14 @@ public class JettyServerTest {
public static final String JETTY_SERVER_NAME = "JettyServerTest";
private Server server;
+ private int port;
private HandlerCollection contextHandlerCollection;
public JettyServerTest(int port) {
- server = new Server(port);
- contextHandlerCollection = new HandlerCollection(true);
- server.setHandler(contextHandlerCollection);
+ this.server = new Server(port);
+ this.port = port;
+ this.contextHandlerCollection = new HandlerCollection(true);
+ this.server.setHandler(contextHandlerCollection);
}
public void start() throws Exception {
@@ -40,14 +42,12 @@ public class JettyServerTest {
server.stop();
}
- /**
- * adds a context handler and starts it
- *
- * @param contextHandler
- * @throws Exception
- */
public void addHandler(ContextHandler contextHandler) throws Exception {
contextHandlerCollection.addHandler(contextHandler);
contextHandler.start();
}
+
+ public int getServerPort() {
+ return port;
+ }
}