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

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

commit ae1e85b4bb8afe31c699670bdd5e595fc12f37f0
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Feb 28 15:02:29 2024 +0100

    CAMEL-20481: camel-core - Rest DSL should resolve placeholder for 
context-path and others
---
 .../apache/camel/model/RouteDefinitionHelper.java  |  7 ++-
 .../apache/camel/model/rest/RestDefinition.java    | 50 ++++++++--------
 .../rest/FromRestPathPlaceholderTest.java          | 70 ++++++++++++++++++++++
 3 files changed, 100 insertions(+), 27 deletions(-)

diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
index a372f229340..327a210e697 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
@@ -148,7 +148,7 @@ public final class RouteDefinitionHelper {
             } else {
                 RestDefinition rest = route.getRestDefinition();
                 if (rest != null && route.isRest()) {
-                    VerbDefinition verb = findVerbDefinition(rest, 
route.getInput().getEndpointUri());
+                    VerbDefinition verb = findVerbDefinition(context, rest, 
route.getInput().getEndpointUri());
                     if (verb != null) {
                         String id = 
context.resolvePropertyPlaceholders(verb.getId());
                         if (verb.hasCustomIdAssigned() && 
ObjectHelper.isNotEmpty(id) && !customIds.contains(id)) {
@@ -216,12 +216,13 @@ public final class RouteDefinitionHelper {
     /**
      * Find verb associated with the route by mapping uri
      */
-    private static VerbDefinition findVerbDefinition(RestDefinition rest, 
String endpointUri) throws Exception {
+    private static VerbDefinition findVerbDefinition(CamelContext 
camelContext, RestDefinition rest, String endpointUri)
+            throws Exception {
         VerbDefinition ret = null;
         String preVerbUri = "";
         String target = URISupport.normalizeUri(endpointUri);
         for (VerbDefinition verb : rest.getVerbs()) {
-            String verbUri = URISupport.normalizeUri(rest.buildFromUri(verb));
+            String verbUri = 
URISupport.normalizeUri(rest.buildFromUri(camelContext, verb));
             if (target.startsWith(verbUri) && preVerbUri.length() < 
verbUri.length()) {
                 // if there are multiple verb uri match, select the most 
specific one
                 // for example if the endpoint Uri is
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 dc55acc2edb..37e0f7234af 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
@@ -49,6 +49,8 @@ import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 
+import static org.apache.camel.support.CamelContextHelper.parseText;
+
 /**
  * Defines a rest service using the rest-dsl
  */
@@ -722,8 +724,8 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
     /**
      * Build the from endpoint uri for the verb
      */
-    public String buildFromUri(VerbDefinition verb) {
-        return "rest:" + verb.asVerb() + ":" + buildUri(verb);
+    public String buildFromUri(CamelContext camelContext, VerbDefinition verb) 
{
+        return "rest:" + verb.asVerb() + ":" + buildUri(camelContext, verb);
     }
 
     // Implementation
@@ -895,50 +897,50 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
                 throw new IllegalArgumentException("Rest service: " + verb + " 
must have to endpoint configured.");
             }
             if (verb.getRouteId() != null) {
-                route.routeId(verb.getRouteId());
+                route.routeId(parseText(camelContext, verb.getRouteId()));
             }
             route.getOutputs().add(verb.getTo());
 
             // add the binding
             RestBindingDefinition binding = new RestBindingDefinition();
             binding.setComponent(component);
-            binding.setType(verb.getType());
+            binding.setType(parseText(camelContext, verb.getType()));
             binding.setTypeClass(verb.getTypeClass());
-            binding.setOutType(verb.getOutType());
+            binding.setOutType(parseText(camelContext, verb.getOutType()));
             binding.setOutTypeClass(verb.getOutTypeClass());
             // verb takes precedence over configuration on rest
             if (verb.getConsumes() != null) {
-                binding.setConsumes(verb.getConsumes());
+                binding.setConsumes(parseText(camelContext, 
verb.getConsumes()));
             } else {
                 binding.setConsumes(getConsumes());
             }
             if (verb.getProduces() != null) {
-                binding.setProduces(verb.getProduces());
+                binding.setProduces(parseText(camelContext, 
verb.getProduces()));
             } else {
                 binding.setProduces(getProduces());
             }
             if (verb.getBindingMode() != null) {
-                binding.setBindingMode(verb.getBindingMode());
+                binding.setBindingMode(parseText(camelContext, 
verb.getBindingMode()));
             } else {
                 binding.setBindingMode(getBindingMode());
             }
             if (verb.getSkipBindingOnErrorCode() != null) {
-                
binding.setSkipBindingOnErrorCode(verb.getSkipBindingOnErrorCode());
+                binding.setSkipBindingOnErrorCode(parseText(camelContext, 
verb.getSkipBindingOnErrorCode()));
             } else {
                 binding.setSkipBindingOnErrorCode(getSkipBindingOnErrorCode());
             }
             if (verb.getClientRequestValidation() != null) {
-                
binding.setClientRequestValidation(verb.getClientRequestValidation());
+                binding.setClientRequestValidation(parseText(camelContext, 
verb.getClientRequestValidation()));
             } else {
                 
binding.setClientRequestValidation(getClientRequestValidation());
             }
             if (verb.getEnableCORS() != null) {
-                binding.setEnableCORS(verb.getEnableCORS());
+                binding.setEnableCORS(parseText(camelContext, 
verb.getEnableCORS()));
             } else {
                 binding.setEnableCORS(getEnableCORS());
             }
             if (verb.getEnableNoContentResponse() != null) {
-                
binding.setEnableNoContentResponse(verb.getEnableNoContentResponse());
+                binding.setEnableNoContentResponse(parseText(camelContext, 
verb.getEnableNoContentResponse()));
             } else {
                 
binding.setEnableNoContentResponse(getEnableNoContentResponse());
             }
@@ -947,7 +949,7 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
                 RestParamType type = param.getType();
                 if ((RestParamType.query == type || RestParamType.header == 
type)
                         && ObjectHelper.isNotEmpty(param.getDefaultValue())) {
-                    binding.addDefaultValue(param.getName(), 
param.getDefaultValue());
+                    binding.addDefaultValue(param.getName(), 
parseText(camelContext, param.getDefaultValue()));
                 }
                 // register which parameters are required
                 Boolean required = param.getRequired();
@@ -968,12 +970,12 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
             Map<String, Object> options = new HashMap<>();
             // verb takes precedence over configuration on rest
             if (verb.getConsumes() != null) {
-                options.put("consumes", verb.getConsumes());
+                options.put("consumes", parseText(camelContext, 
verb.getConsumes()));
             } else if (getConsumes() != null) {
                 options.put("consumes", getConsumes());
             }
             if (verb.getProduces() != null) {
-                options.put("produces", verb.getProduces());
+                options.put("produces", parseText(camelContext, 
verb.getProduces()));
             } else if (getProduces() != null) {
                 options.put("produces", getProduces());
             }
@@ -1007,19 +1009,19 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
                 description = getDescriptionText();
             }
             if (description != null) {
-                options.put("description", description);
+                options.put("description", parseText(camelContext, 
description));
             }
 
-            String path = getPath();
+            String path = parseText(camelContext, getPath());
             String s1 = FileUtil.stripTrailingSeparator(path);
-            String s2 = FileUtil.stripLeadingSeparator(verb.getPath());
+            String s2 = FileUtil.stripLeadingSeparator(parseText(camelContext, 
verb.getPath()));
             String allPath;
             if (s1 != null && s2 != null) {
                 allPath = s1 + "/" + s2;
             } else if (path != null) {
                 allPath = path;
             } else {
-                allPath = verb.getPath();
+                allPath = parseText(camelContext, verb.getPath());
             }
 
             // each {} is a parameter (url templating)
@@ -1037,7 +1039,7 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
             }
 
             if (verb.getType() != null) {
-                String bodyType = verb.getType();
+                String bodyType = parseText(camelContext, verb.getType());
                 if (bodyType.endsWith("[]")) {
                     bodyType = "List[" + bodyType.substring(0, 
bodyType.length() - 2) + "]";
                 }
@@ -1052,7 +1054,7 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
             }
 
             // create the from endpoint uri which is using the rest component
-            String from = buildFromUri(verb);
+            String from = buildFromUri(camelContext, verb);
 
             // rebuild uri without these query parameters
             if (toRemove != null && !toRemove.isEmpty()) {
@@ -1136,13 +1138,13 @@ public class RestDefinition extends 
OptionalIdentifiedDefinition<RestDefinition>
         return params;
     }
 
-    private String buildUri(VerbDefinition verb) {
+    private String buildUri(CamelContext camelContext, VerbDefinition verb) {
         if (path != null && verb.getPath() != null) {
-            return path + ":" + verb.getPath();
+            return path + ":" + parseText(camelContext, verb.getPath());
         } else if (path != null) {
             return path;
         } else if (verb.getPath() != null) {
-            return verb.getPath();
+            return parseText(camelContext, verb.getPath());
         } else {
             return "";
         }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestPathPlaceholderTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestPathPlaceholderTest.java
new file mode 100644
index 00000000000..0cdb9acdecd
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestPathPlaceholderTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.rest;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class FromRestPathPlaceholderTest extends ContextTestSupport {
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        Registry jndi = super.createRegistry();
+        jndi.bind("dummy-rest", new DummyRestConsumerFactory());
+        return jndi;
+    }
+
+    protected int getExpectedNumberOfRoutes() {
+        // routes are inlined
+        return 1;
+    }
+
+    @Test
+    public void testPlaceholder() throws Exception {
+        assertEquals(getExpectedNumberOfRoutes(), context.getRoutes().size());
+
+        RestDefinition rest = context.getRestDefinitions().get(0);
+        assertNotNull(rest);
+        assertEquals("/say/{{mypath}}", rest.getPath());
+
+        // placeholder should be resolved, so we can find the rest endpoint 
that is a dummy (via seda)
+        assertNotNull(context.hasEndpoint("seda://get-say-hello"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.getPropertiesComponent().addInitialProperty("mypath", 
"hello");
+
+                restConfiguration().host("localhost");
+
+                rest("/say/{{mypath}}").get().to("direct:hello");
+
+                from("direct:hello").log("Hello");
+
+            }
+        };
+    }
+}

Reply via email to