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/88b8e447
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/88b8e447
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/88b8e447

Branch: refs/heads/master
Commit: 88b8e447bc5b2a9a28048a170fc4d6dcf21ffce9
Parents: 691c975
Author: Claus Ibsen <[email protected]>
Authored: Fri Aug 21 10:51:29 2015 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Fri Aug 21 10:53:03 2015 +0200

----------------------------------------------------------------------
 ...JettyRestServletResolveConsumerStrategy.java | 25 +++---
 .../jetty/rest/RestJettyGetWildcardsTest.java   | 80 ++++++++++++++++++++
 2 files changed, 96 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/88b8e447/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyRestServletResolveConsumerStrategy.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyRestServletResolveConsumerStrategy.java
 
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyRestServletResolveConsumerStrategy.java
index 3da7c29..b49debb 100644
--- 
a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyRestServletResolveConsumerStrategy.java
+++ 
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyRestServletResolveConsumerStrategy.java
@@ -77,24 +77,31 @@ public class JettyRestServletResolveConsumerStrategy 
extends HttpServletResolveC
                 }
             }
 
-            // 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/88b8e447/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetWildcardsTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetWildcardsTest.java
 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetWildcardsTest.java
new file mode 100644
index 0000000..bde8864
--- /dev/null
+++ 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetWildcardsTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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.jetty.rest;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.component.jetty.JettyRestHttpBinding;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+
+public class RestJettyGetWildcardsTest extends BaseJettyTest {
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("mybinding", new JettyRestHttpBinding());
+        return jndi;
+    }
+
+    @Test
+    public void testJettyProducerGet() throws Exception {
+        String out = template.requestBody("http://localhost:"; + getPort() + 
"/users/123/basic", null, String.class);
+        assertEquals("123;Donald Duck", out);
+    }
+
+    @Test
+    public void testJettyProducerGetWildcards() throws Exception {
+        String out = template.requestBody("http://localhost:"; + getPort() + 
"/users/456/name=g*", null, String.class);
+        assertEquals("456;Goofy", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // configure to use jetty on localhost with the given port
+                
restConfiguration().component("jetty").host("localhost").port(getPort()).endpointProperty("httpBindingRef",
 "#mybinding");
+
+                // use the rest DSL to define the rest services
+                rest("/users/")
+                    .get("{id}/basic")
+                        .route()
+                        .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 + ";Donald Duck");
+                            }
+                        }).endRest()
+                    .get("{id}/{query}")
+                        .route()
+                        .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 + ";Goofy");
+                            }
+                        }).endRest();
+            }
+        };
+    }
+
+}

Reply via email to