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 2023/06/16 11:44:12 UTC

[camel] branch main updated: Revert "should servlet and rest throw exceptions when duplicate and ambiguous paths occur (#10400)"

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


The following commit(s) were added to refs/heads/main by this push:
     new 29e918f1cb8 Revert "should servlet and rest throw exceptions when duplicate and ambiguous paths occur (#10400)"
29e918f1cb8 is described below

commit 29e918f1cb8f0d24e0928823ee826ee25d12d1b1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jun 16 13:42:07 2023 +0200

    Revert "should servlet and rest throw exceptions when duplicate and ambiguous paths occur (#10400)"
    
    This reverts commit bd8f07cf2a84afb65fe0971b6d8a404ae98b8298.
---
 .../org/apache/camel/http/common/CamelServlet.java |  6 +-
 .../apache/camel/http/common/CamelServletTest.java | 61 -------------------
 .../support/RestConsumerContextPathMatcher.java    | 69 +++++++++-------------
 3 files changed, 28 insertions(+), 108 deletions(-)

diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
index 1dbd2039c12..55365ae9f5b 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
@@ -415,11 +415,7 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
     @Override
     public void connect(HttpConsumer consumer) {
         log.debug("Connecting consumer: {}", consumer);
-        String endpointUri = consumer.getEndpoint().getEndpointUri();
-        if (consumers.containsKey(endpointUri)) {
-            throw new IllegalStateException("Duplicate request path for " + endpointUri);
-        }
-        consumers.put(endpointUri, consumer);
+        consumers.put(consumer.getEndpoint().getEndpointUri(), consumer);
     }
 
     @Override
diff --git a/components/camel-http-common/src/test/java/org/apache/camel/http/common/CamelServletTest.java b/components/camel-http-common/src/test/java/org/apache/camel/http/common/CamelServletTest.java
deleted file mode 100644
index 1384628122e..00000000000
--- a/components/camel-http-common/src/test/java/org/apache/camel/http/common/CamelServletTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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.http.common;
-
-import org.apache.camel.Consumer;
-import org.apache.camel.Processor;
-import org.apache.camel.Producer;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
-public class CamelServletTest {
-
-    @Test
-    public void testDuplicatedServletPath() {
-        CamelServlet camelServlet = new CamelServlet();
-
-        HttpCommonEndpoint httpCommonEndpoint = new HttpCommonEndpoint() {
-
-            @Override
-            public Producer createProducer() throws Exception {
-                return null;
-            }
-
-            @Override
-            public Consumer createConsumer(Processor processor) throws Exception {
-                return null;
-            }
-        };
-
-        DefaultCamelContext dc = new DefaultCamelContext();
-
-        httpCommonEndpoint.setEndpointUriIfNotSpecified("rest:post://camel.apache.org");
-        httpCommonEndpoint.setCamelContext(dc);
-
-        HttpConsumer httpConsumer1 = new HttpConsumer(httpCommonEndpoint, null);
-        HttpConsumer httpConsumer2 = new HttpConsumer(httpCommonEndpoint, null);
-
-        camelServlet.connect(httpConsumer1);
-        IllegalStateException illegalStateException
-                = assertThrows(IllegalStateException.class, () -> camelServlet.connect(httpConsumer2));
-        assertEquals("Duplicate request path for rest:post://camel.apache.org",
-                illegalStateException.getMessage());
-    }
-}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java b/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java
index be6f23638dc..d028ce11026 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java
@@ -18,13 +18,9 @@ package org.apache.camel.support;
 
 import java.util.ArrayList;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
-import java.util.Map;
-import java.util.OptionalInt;
-import java.util.stream.Collectors;
 
 /**
  * A context path matcher when using rest-dsl that allows components to reuse the same matching logic.
@@ -165,54 +161,43 @@ public final class RestConsumerContextPathMatcher {
                     .sorted(Comparator.comparingInt(o -> -1 * o.getConsumerPath().length())).findFirst().orElse(null);
         }
 
-        if (answer != null) {
-            return answer;
-        }
-
         // then match by wildcard path
-        it = candidates.iterator();
-        while (it.hasNext()) {
-            ConsumerPath consumer = it.next();
-            // filter non matching paths
-            if (!matchRestPath(requestPath, consumer.getConsumerPath(), true)) {
-                it.remove();
-            }
-        }
-
-        // if there is multiple candidates with wildcards then pick anyone with the least number of wildcards
-        ConsumerPath best = null;
-        Map<Integer, List<ConsumerPath>> pathMap = new HashMap<>();
-        if (candidates.size() > 1) {
+        if (answer == null) {
             it = candidates.iterator();
             while (it.hasNext()) {
-                ConsumerPath entry = it.next();
-                int wildcards = countWildcards(entry.getConsumerPath());
-                if (wildcards > 0) {
-                    List<ConsumerPath> consumerPathsLst = pathMap.computeIfAbsent(wildcards, key -> new ArrayList<>());
-                    consumerPathsLst.add(entry);
+                ConsumerPath consumer = it.next();
+                // filter non matching paths
+                if (!matchRestPath(requestPath, consumer.getConsumerPath(), true)) {
+                    it.remove();
                 }
             }
 
-            OptionalInt max = pathMap.keySet().stream().mapToInt(Integer::intValue).max();
-            if (max.isPresent()) {
-                List<ConsumerPath> bestConsumerPaths = pathMap.get(max.getAsInt());
-                if (bestConsumerPaths.size() > 1) {
-                    String exceptionMsg = "Ambiguous paths " + bestConsumerPaths.stream().map(ConsumerPath::getConsumerPath)
-                            .collect(Collectors.joining(",")) + " for request path " + requestPath;
-                    throw new IllegalStateException(exceptionMsg);
+            // if there is multiple candidates with wildcards then pick anyone with the least number of wildcards
+            int bestWildcard = Integer.MAX_VALUE;
+            ConsumerPath best = null;
+            if (candidates.size() > 1) {
+                it = candidates.iterator();
+                while (it.hasNext()) {
+                    ConsumerPath entry = it.next();
+                    int wildcards = countWildcards(entry.getConsumerPath());
+                    if (wildcards > 0) {
+                        if (best == null || wildcards < bestWildcard) {
+                            best = entry;
+                            bestWildcard = wildcards;
+                        }
+                    }
                 }
-                best = bestConsumerPaths.get(0);
-            }
 
-            if (best != null) {
-                // pick the best among the wildcards
-                answer = best;
+                if (best != null) {
+                    // pick the best among the wildcards
+                    answer = best;
+                }
             }
-        }
 
-        // if there is one left then its our answer
-        if (answer == null && candidates.size() == 1) {
-            answer = candidates.get(0);
+            // if there is one left then its our answer
+            if (answer == null && candidates.size() == 1) {
+                answer = candidates.get(0);
+            }
         }
 
         return answer;