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 2015/08/21 10:44:53 UTC

[3/4] camel git commit: CAMEL-9089: rest-dsl when multiple candidates then use the most specific one.

CAMEL-9089: rest-dsl when multiple candidates then use the most specific one.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/691c9754
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/691c9754
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/691c9754

Branch: refs/heads/master
Commit: 691c9754fd48f0fa849fb71e56c8fc0305a16021
Parents: b1c9132
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Aug 21 10:46:28 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Aug 21 10:53:03 2015 +0200

----------------------------------------------------------------------
 .../HttpServerMultiplexChannelHandler.java      |  8 ++-----
 .../HttpServerMultiplexChannelHandler.java      |  8 ++-----
 ...rvletRestServletResolveConsumerStrategy.java | 25 +++++++++++++-------
 .../rest/RestServletGetWildcardsTest.java       | 12 +++++-----
 4 files changed, 26 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/691c9754/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
index 747b242..de443d6 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
@@ -224,13 +224,9 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelUpstreamHand
                     String consumerPath = entry.getValue().getConsumer().getConfiguration().getPath();
                     int wildcards = countWildcards(consumerPath);
                     if (wildcards > 0) {
-                        if (best == null) {
+                        if (best == null || wildcards < bestWildcard) {
                             best = entry;
-                        } else {
-                            if (wildcards < bestWildcard) {
-                                bestWildcard = wildcards;
-                                best = entry;
-                            }
+                            bestWildcard = wildcards;
                         }
                     }
                 }

http://git-wip-us.apache.org/repos/asf/camel/blob/691c9754/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerMultiplexChannelHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerMultiplexChannelHandler.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerMultiplexChannelHandler.java
index 68bb656..30dae07 100644
--- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerMultiplexChannelHandler.java
+++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerMultiplexChannelHandler.java
@@ -221,13 +221,9 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelInboundHandl
                     String consumerPath = entry.getValue().getConsumer().getConfiguration().getPath();
                     int wildcards = countWildcards(consumerPath);
                     if (wildcards > 0) {
-                        if (best == null) {
+                        if (best == null || wildcards < bestWildcard) {
                             best = entry;
-                        } else {
-                            if (wildcards < bestWildcard) {
-                                bestWildcard = wildcards;
-                                best = entry;
-                            }
+                            bestWildcard = wildcards;
                         }
                     }
                 }

http://git-wip-us.apache.org/repos/asf/camel/blob/691c9754/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestServletResolveConsumerStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestServletResolveConsumerStrategy.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestServletResolveConsumerStrategy.java
index 64e90ca..71e3f96 100644
--- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestServletResolveConsumerStrategy.java
+++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestServletResolveConsumerStrategy.java
@@ -77,24 +77,31 @@ public class ServletRestServletResolveConsumerStrategy extends HttpServletResolv
                 }
             }
 
-            // if there is multiple candidates then pick anyone with the least number of wildcards
-            int best = -1;
+            // if there is multiple candidates with wildcards then pick anyone with the least number of wildcards
+            int bestWildcard = Integer.MAX_VALUE;
+            HttpConsumer best = null;
             if (candidates.size() > 1) {
                 it = candidates.iterator();
                 while (it.hasNext()) {
-                    HttpConsumer consumer = it.next();
-                    String consumerPath = consumer.getPath();
+                    HttpConsumer entry = it.next();
+                    String consumerPath = entry.getPath();
                     int wildcards = countWildcards(consumerPath);
-                    if (best != -1 && wildcards >= best) {
-                        it.remove();
-                    } else {
-                        best = wildcards;
+                    if (wildcards > 0) {
+                        if (best == null || wildcards < bestWildcard) {
+                            best = entry;
+                            bestWildcard = wildcards;
+                        }
                     }
                 }
+
+                if (best != null) {
+                    // pick the best among the wildcards
+                    answer = best;
+                }
             }
 
             // if there is one left then its our answer
-            if (candidates.size() == 1) {
+            if (answer == null && candidates.size() == 1) {
                 answer = candidates.get(0);
             }
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/691c9754/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletGetWildcardsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletGetWildcardsTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletGetWildcardsTest.java
index e382601..67a1b7a 100644
--- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletGetWildcardsTest.java
+++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletGetWildcardsTest.java
@@ -71,22 +71,22 @@ public class RestServletGetWildcardsTest extends ServletCamelRouterTestSupport {
                 
                 // use the rest DSL to define the rest services
                 rest("/users/")
-                    .get("{id}/{query}")
+                    .get("{id}/basic")
                         .route()
-                        .to("mock:query")
+                        .to("mock:input")
                         .process(new Processor() {
                             public void process(Exchange exchange) throws Exception {
                                 String id = exchange.getIn().getHeader("id", String.class);
-                                exchange.getOut().setBody(id + ";Goofy");
+                                exchange.getOut().setBody(id + ";Donald Duck");
                             }
                         }).endRest()
-                    .get("{id}/basic")
+                    .get("{id}/{query}")
                         .route()
-                        .to("mock:input")
+                        .to("mock:query")
                         .process(new Processor() {
                             public void process(Exchange exchange) throws Exception {
                                 String id = exchange.getIn().getHeader("id", String.class);
-                                exchange.getOut().setBody(id + ";Donald Duck");
+                                exchange.getOut().setBody(id + ";Goofy");
                             }
                         }).endRest();
             }