This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch rest-to in repository https://gitbox.apache.org/repos/asf/camel.git
commit c730e4bdc5f6ec7cb6a0946aa63251dc0a0e310a Author: Claus Ibsen <[email protected]> AuthorDate: Wed Sep 18 10:20:02 2024 +0200 CAMEL-21237: Add Endpoint DSL to rest-dsl --- .../camel/builder/EndpointConsumerBuilder.java | 1 + .../camel/builder/EndpointProducerBuilder.java | 1 + .../apache/camel/model/rest/RestDefinition.java | 43 ++++++++++++++- .../apache/camel/builder/endpoint/RestDslTest.java | 61 ++++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointConsumerBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointConsumerBuilder.java index cdb66b76221..ef1183639a8 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointConsumerBuilder.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointConsumerBuilder.java @@ -28,6 +28,7 @@ import org.apache.camel.Expression; * @see EndpointProducerBuilder */ public interface EndpointConsumerBuilder extends EndpointConsumerResolver { + /** * Builds the encoded url of this endpoint. This API is only intended for Camel internally. */ diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java index a6acd231946..571d24fa9a8 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java @@ -28,6 +28,7 @@ import org.apache.camel.Expression; * @see EndpointConsumerBuilder */ public interface EndpointProducerBuilder extends EndpointProducerResolver { + /** * Builds the encoded url of this endpoint. This API is only intended for Camel internally. */ diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java index dea48fda60f..9b20ec0165b 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java @@ -34,11 +34,14 @@ import jakarta.xml.bind.annotation.XmlRootElement; import jakarta.xml.bind.annotation.XmlTransient; import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.EndpointProducerBuilder; import org.apache.camel.model.OptionalIdentifiedDefinition; import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.StopDefinition; import org.apache.camel.model.ToDefinition; +import org.apache.camel.spi.AsEndpointUri; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.NodeIdFactory; import org.apache.camel.spi.Resource; @@ -777,6 +780,44 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> return this; } + /** + * Sends the exchange to the given endpoint + * + * @param endpoint the endpoint to send to + * @return the builder + */ + public RestDefinition to(Endpoint endpoint) { + // add to last verb + if (getVerbs().isEmpty()) { + throw new IllegalArgumentException(MISSING_VERB); + } + + ToDefinition to = new ToDefinition(endpoint); + + VerbDefinition verb = getVerbs().get(getVerbs().size() - 1); + verb.setTo(to); + return this; + } + + /** + * Sends the exchange to the given endpoint + * + * @param endpoint the endpoint to send to + * @return the builder + */ + public RestDefinition to(@AsEndpointUri EndpointProducerBuilder endpoint) { + // add to last verb + if (getVerbs().isEmpty()) { + throw new IllegalArgumentException(MISSING_VERB); + } + + ToDefinition to = new ToDefinition(endpoint); + + VerbDefinition verb = getVerbs().get(getVerbs().size() - 1); + verb.setTo(to); + return this; + } + /** * Build the from endpoint uri for the verb */ @@ -893,7 +934,7 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> for (VerbDefinition verb : verbs) { ToDefinition to = verb.getTo(); if (to != null) { - String uri = to.getUri(); + String uri = to.getEndpointUri(); if (uri.startsWith("direct:")) { if (!directs.add(uri)) { throw new IllegalArgumentException("Duplicate to in rest-dsl: " + uri); diff --git a/dsl/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/RestDslTest.java b/dsl/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/RestDslTest.java new file mode 100644 index 00000000000..72d8f8293a1 --- /dev/null +++ b/dsl/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/RestDslTest.java @@ -0,0 +1,61 @@ +/* + * 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.builder.endpoint; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServer; +import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServerConfiguration; +import org.apache.camel.test.AvailablePortFinder; +import org.junit.jupiter.api.Test; + +public class RestDslTest extends BaseEndpointDslTest { + + @Override + protected CamelContext createCamelContext() throws Exception { + int port = AvailablePortFinder.getNextAvailable(); + VertxPlatformHttpServerConfiguration conf = new VertxPlatformHttpServerConfiguration(); + conf.setBindPort(port); + + CamelContext context = super.createCamelContext(); + context.addService(new VertxPlatformHttpServer(conf)); + return context; + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testRestDsl() throws Exception { + context.start(); + + context.addRoutes(new EndpointRouteBuilder() { + @Override + public void configure() throws Exception { + rest("/api") + .get("name").to(direct("username").advanced().lazyStartProducer(true)); + + from(direct("username")) + .setBody(constant("scott")); + } + }); + + context.stop(); + } + +}
