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 2021/12/08 13:59:32 UTC
[camel] 01/04: CAMEL-17301: camel-platform-http - Keep list of managed http endpoints.
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 0b22ed3e3e3e0a0c9d309bd467a6c209a08c4c17
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 8 14:21:02 2021 +0100
CAMEL-17301: camel-platform-http - Keep list of managed http endpoints.
---
.../http/vertx/VertxPlatformHttpEngineTest.java | 8 ++++
.../platform/http/PlatformHttpComponent.java | 45 ++++++++++++++++++++--
.../platform/http/PlatformHttpEndpoint.java | 11 +++++-
.../platform/http/AbstractPlatformHttpTest.java | 4 ++
.../component/platform/http/PlatformHttpTest.java | 8 ++++
5 files changed, 71 insertions(+), 5 deletions(-)
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 216eeb3..8374ee3 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
@@ -20,6 +20,7 @@ import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import javax.activation.DataHandler;
@@ -54,6 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
public class VertxPlatformHttpEngineTest {
public static SSLContextParameters serverSSLParameters;
@@ -145,6 +147,12 @@ public class VertxPlatformHttpEngineTest {
.statusCode(200)
.body(equalTo("POST"));
+ 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());
+
} finally {
context.stop();
}
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java
index f67b023..e382d9d 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java
@@ -16,7 +16,10 @@
*/
package org.apache.camel.component.platform.http;
+import java.util.Collections;
import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
@@ -50,6 +53,8 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu
@Metadata(label = "advanced", description = "An HTTP Server engine implementation to serve the requests")
private volatile PlatformHttpEngine engine;
+ private final Set<String> httpEndpoints = new TreeSet<>();
+
private volatile boolean localEngine;
private final Object lock = new Object();
@@ -66,7 +71,7 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
PlatformHttpEndpoint endpoint = new PlatformHttpEndpoint(uri, remaining, this);
endpoint.setPlatformHttpEngine(engine);
-
+ setProperties(endpoint, parameters);
return endpoint;
}
@@ -75,8 +80,12 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu
CamelContext camelContext, Processor processor, String contextPath,
RestConfiguration configuration, Map<String, Object> parameters)
throws Exception {
+
// reuse the createConsumer method we already have. The api need to use GET and match on uri prefix
- return doCreateConsumer(camelContext, processor, "GET", contextPath, null, null, null, configuration, parameters, true);
+ Consumer consumer = doCreateConsumer(camelContext, processor, "GET", contextPath, null, null, null, configuration,
+ parameters, true);
+ addHttpEndpoint(contextPath);
+ return consumer;
}
@Override
@@ -85,8 +94,36 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu
String uriTemplate,
String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters)
throws Exception {
- return doCreateConsumer(camelContext, processor, verb, basePath, uriTemplate, consumes, produces, configuration,
- parameters, false);
+ Consumer consumer
+ = doCreateConsumer(camelContext, processor, verb, basePath, uriTemplate, consumes, produces, configuration,
+ parameters, false);
+ if (uriTemplate != null) {
+ addHttpEndpoint(basePath + "/" + uriTemplate);
+ } else {
+ addHttpEndpoint(basePath);
+ }
+ return consumer;
+ }
+
+ /**
+ * Adds a known http endpoint managed by this component.
+ */
+ public void addHttpEndpoint(String uri) {
+ httpEndpoints.add(uri);
+ }
+
+ /**
+ * Removes a known http endpoint managed by this component.
+ */
+ public void removeHttpEndpoint(String uri) {
+ httpEndpoints.remove(uri);
+ }
+
+ /**
+ * Lists the known http endpoints managed by this component. The endpoints are without host:port/[context-path]
+ */
+ public Set<String> getHttpEndpoints() {
+ return Collections.unmodifiableSet(httpEndpoints);
}
@Override
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java
index 5811774..41d6053 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java
@@ -80,13 +80,18 @@ public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoi
}
@Override
+ public PlatformHttpComponent getComponent() {
+ return (PlatformHttpComponent) super.getComponent();
+ }
+
+ @Override
public Producer createProducer() throws Exception {
throw new UnsupportedOperationException("Producer is not supported");
}
@Override
public Consumer createConsumer(Processor processor) throws Exception {
- return new DefaultConsumer(this, processor) {
+ Consumer consumer = new DefaultConsumer(this, processor) {
private Consumer delegatedConsumer;
@Override
@@ -105,11 +110,13 @@ public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoi
protected void doStart() throws Exception {
super.doStart();
ServiceHelper.startService(delegatedConsumer);
+ getComponent().addHttpEndpoint(getPath());
}
@Override
protected void doStop() throws Exception {
super.doStop();
+ getComponent().removeHttpEndpoint(getPath());
ServiceHelper.stopAndShutdownServices(delegatedConsumer);
}
@@ -125,6 +132,8 @@ public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoi
super.doSuspend();
}
};
+ configureConsumer(consumer);
+ return consumer;
}
@Override
diff --git a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/AbstractPlatformHttpTest.java b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/AbstractPlatformHttpTest.java
index ec8a56c..9262821 100644
--- a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/AbstractPlatformHttpTest.java
+++ b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/AbstractPlatformHttpTest.java
@@ -58,6 +58,10 @@ abstract class AbstractPlatformHttpTest {
};
}
+ protected CamelContext getContext() {
+ return ctx;
+ }
+
@AfterAll
public static void tearDown() throws Exception {
synchronized (LOCK) {
diff --git a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpTest.java b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpTest.java
index b674fb1..5474fe9 100644
--- a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpTest.java
+++ b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.platform.http;
+import java.util.Iterator;
+
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
@@ -47,6 +49,12 @@ public class PlatformHttpTest extends AbstractPlatformHttpTest {
int statusCode = response.getStatusCode();
assertEquals(200, statusCode);
assertEquals("TEST", response.body().asString().trim());
+
+ PlatformHttpComponent phc = getContext().getComponent("platform-http", PlatformHttpComponent.class);
+ assertEquals(2, phc.getHttpEndpoints().size());
+ Iterator<String> it = phc.getHttpEndpoints().iterator();
+ assertEquals("/get", it.next());
+ assertEquals("/post", it.next());
}
@Override