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 638aadf20e2ab76deb91a2fc561e0ffdd5104d5e Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon May 2 16:56:21 2022 +0200 CAMEL-18026: camel-rest - Rest DSL - Output more fine grained http endpoints that are available --- .../component/platform/http/HttpEndpointModel.java | 84 ++++++++++++++++++++++ .../platform/http/PlatformHttpComponent.java | 22 +++--- .../platform/http/PlatformHttpEndpoint.java | 4 +- .../component/platform/http/PlatformHttpTest.java | 6 +- .../org/apache/camel/main/VertxHttpServer.java | 18 +++-- 5 files changed, 115 insertions(+), 19 deletions(-) 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 new file mode 100644 index 00000000000..b89db34c204 --- /dev/null +++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/HttpEndpointModel.java @@ -0,0 +1,84 @@ +/* + * 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 org.apache.camel.util.StringHelper; + +import java.util.Locale; +import java.util.Objects; + +/** + * Model of available http endpoints. + */ +public class HttpEndpointModel implements Comparable<HttpEndpointModel> { + + private final String uri; + private String verbs; + + public HttpEndpointModel(String uri) { + this(uri, null); + } + + public HttpEndpointModel(String uri, String verbs) { + this.uri = uri; + addVerb(verbs); + } + + public String getUri() { + return uri; + } + + public String getVerbs() { + return verbs; + } + + public void addVerb(String verb) { + if (verb != null) { + if (this.verbs == null) { + this.verbs = ""; + } + if (!StringHelper.containsIgnoreCase(this.verbs, verb)) { + if (!this.verbs.isEmpty()) { + this.verbs += ","; + } + this.verbs += verb.toUpperCase(Locale.US); + } + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + HttpEndpointModel that = (HttpEndpointModel) o; + return uri.equals(that.uri); + } + + @Override + public int hashCode() { + return Objects.hash(uri); + } + + @Override + public int compareTo(HttpEndpointModel o) { + return uri.compareTo(o.uri); + } +} 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 e382d9decad..9dc2775bc90 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 @@ -53,7 +53,7 @@ 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 final Set<HttpEndpointModel> httpEndpoints = new TreeSet<>(); private volatile boolean localEngine; @@ -84,7 +84,7 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu // reuse the createConsumer method we already have. The api need to use GET and match on uri prefix Consumer consumer = doCreateConsumer(camelContext, processor, "GET", contextPath, null, null, null, configuration, parameters, true); - addHttpEndpoint(contextPath); + addHttpEndpoint(contextPath, "GET"); return consumer; } @@ -98,9 +98,9 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu = doCreateConsumer(camelContext, processor, verb, basePath, uriTemplate, consumes, produces, configuration, parameters, false); if (uriTemplate != null) { - addHttpEndpoint(basePath + "/" + uriTemplate); + addHttpEndpoint(basePath + "/" + uriTemplate, verb); } else { - addHttpEndpoint(basePath); + addHttpEndpoint(basePath, verb); } return consumer; } @@ -108,21 +108,27 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu /** * Adds a known http endpoint managed by this component. */ - public void addHttpEndpoint(String uri) { - httpEndpoints.add(uri); + public void addHttpEndpoint(String uri, String verbs) { + HttpEndpointModel model = httpEndpoints.stream().filter(e -> e.getUri().equals(uri)).findFirst().orElse(null); + if (model == null) { + model = new HttpEndpointModel(uri, verbs); + httpEndpoints.add(model); + } else { + model.addVerb(verbs); + } } /** * Removes a known http endpoint managed by this component. */ public void removeHttpEndpoint(String uri) { - httpEndpoints.remove(uri); + httpEndpoints.stream().filter(e -> e.getUri().equals(uri)).findFirst().ifPresent(httpEndpoints::remove); } /** * Lists the known http endpoints managed by this component. The endpoints are without host:port/[context-path] */ - public Set<String> getHttpEndpoints() { + public Set<HttpEndpointModel> getHttpEndpoints() { return Collections.unmodifiableSet(httpEndpoints); } 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 c0615147672..db5ca28e7fe 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 @@ -106,7 +106,7 @@ public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoi protected void doStart() throws Exception { super.doStart(); ServiceHelper.startService(delegatedConsumer); - getComponent().addHttpEndpoint(getPath()); + getComponent().addHttpEndpoint(getPath(), httpMethodRestrict); } @Override @@ -205,6 +205,6 @@ public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoi PlatformHttpEngine getOrCreateEngine() { return platformHttpEngine != null ? platformHttpEngine - : ((PlatformHttpComponent) getComponent()).getOrCreateEngine(); + : getComponent().getOrCreateEngine(); } } 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 cf3e9511f36..a7e0ad0f018 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 @@ -52,9 +52,9 @@ public class PlatformHttpTest extends AbstractPlatformHttpTest { 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()); + Iterator<HttpEndpointModel> it = phc.getHttpEndpoints().iterator(); + assertEquals("/get", it.next().getUri()); + assertEquals("/post", it.next().getUri()); } @Override diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java index 7312e52e516..877bf93c816 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java @@ -35,6 +35,7 @@ import io.vertx.ext.web.RoutingContext; import org.apache.camel.CamelContext; import org.apache.camel.RuntimeCamelException; import org.apache.camel.StartupListener; +import org.apache.camel.component.platform.http.HttpEndpointModel; import org.apache.camel.component.platform.http.PlatformHttpComponent; import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpRouter; import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServer; @@ -100,7 +101,7 @@ public final class VertxHttpServer { public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception { camelContext.getManagementStrategy().addEventNotifier(new SimpleEventNotifierSupport() { - private Set<String> last; + private Set<HttpEndpointModel> last; @Override public boolean isEnabled(CamelEvent event) { @@ -119,7 +120,7 @@ public final class VertxHttpServer { } } - Set<String> endpoints = phc.getHttpEndpoints(); + Set<HttpEndpointModel> endpoints = phc.getHttpEndpoints(); if (endpoints.isEmpty()) { return; } @@ -127,8 +128,13 @@ public final class VertxHttpServer { // log only if changed if (last == null || last.size() != endpoints.size() || !last.containsAll(endpoints)) { LOG.info("HTTP endpoints summary"); - for (String u : endpoints) { - LOG.info(" http://0.0.0.0:" + port + u); + for (HttpEndpointModel u : endpoints) { + String v = u.getVerbs(); + String line = "http://0.0.0.0:" + port + u.getUri(); + if (u.getVerbs() != null) { + line += " (" + u.getVerbs() + ")"; + } + LOG.info(" {}", line); } } @@ -183,7 +189,7 @@ public final class VertxHttpServer { } } }); - phc.addHttpEndpoint("/q/dev"); + phc.addHttpEndpoint("/q/dev", null); } public static void registerHealthCheck(CamelContext camelContext) { @@ -254,7 +260,7 @@ public final class VertxHttpServer { live.handler(handler); ready.handler(handler); - phc.addHttpEndpoint("/q/health"); + phc.addHttpEndpoint("/q/health", null); } private static void healthCheckStatus(StringBuilder sb, boolean up) {