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:51 UTC
[1/4] camel git commit: CAMEL-9089: rest-dsl when multiple candidates
then use the most specific one.
Repository: camel
Updated Branches:
refs/heads/master 19f27fd26 -> 88b8e447b
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/b1c9132c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b1c9132c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b1c9132c
Branch: refs/heads/master
Commit: b1c9132cf22de1c066a9869b2f54235c6ab535dd
Parents: 57f48e8
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Aug 21 10:24:41 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Aug 21 10:53:02 2015 +0200
----------------------------------------------------------------------
.../HttpServerMultiplexChannelHandler.java | 61 ++++++++++++++-
.../rest/RestNettyHttpGetWildcardsTest.java | 79 ++++++++++++++++++++
.../HttpServerMultiplexChannelHandler.java | 2 +-
3 files changed, 138 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/b1c9132c/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 2bcac76..747b242 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
@@ -203,18 +203,46 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelUpstreamHand
}
}
- // then match by non wildcard path
+ // then match by wildcard path
if (answer == null) {
Iterator<Map.Entry<ContextPathMatcher, HttpServerChannelHandler>> it = candidates.iterator();
while (it.hasNext()) {
Map.Entry<ContextPathMatcher, HttpServerChannelHandler> entry = it.next();
+ // filter non matching paths
if (!entry.getKey().matchesRest(path, true)) {
it.remove();
}
}
- // there should only be one
- if (candidates.size() == 1) {
+ // if there is multiple candidates with wildcards then pick anyone with the least number of wildcards
+ int bestWildcard = Integer.MAX_VALUE;
+ Map.Entry<ContextPathMatcher, HttpServerChannelHandler> best = null;
+ if (candidates.size() > 1) {
+ it = candidates.iterator();
+ while (it.hasNext()) {
+ Map.Entry<ContextPathMatcher, HttpServerChannelHandler> entry = it.next();
+ String consumerPath = entry.getValue().getConsumer().getConfiguration().getPath();
+ int wildcards = countWildcards(consumerPath);
+ if (wildcards > 0) {
+ if (best == null) {
+ best = entry;
+ } else {
+ if (wildcards < bestWildcard) {
+ bestWildcard = wildcards;
+ best = entry;
+ }
+ }
+ }
+ }
+
+ if (best != null) {
+ // pick the best among the wildcards
+ answer = best.getValue();
+ }
+ }
+
+ // if there is one left then its our answer
+ if (answer == null && candidates.size() == 1) {
answer = candidates.get(0).getValue();
}
}
@@ -232,6 +260,33 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelUpstreamHand
return answer;
}
+ /**
+ * Counts the number of wildcards in the path
+ *
+ * @param consumerPath the consumer path which may use { } tokens
+ * @return number of wildcards, or <tt>0</tt> if no wildcards
+ */
+ private static int countWildcards(String consumerPath) {
+ int wildcards = 0;
+
+ // remove starting/ending slashes
+ if (consumerPath.startsWith("/")) {
+ consumerPath = consumerPath.substring(1);
+ }
+ if (consumerPath.endsWith("/")) {
+ consumerPath = consumerPath.substring(0, consumerPath.length() - 1);
+ }
+
+ String[] consumerPaths = consumerPath.split("/");
+ for (String p2 : consumerPaths) {
+ if (p2.startsWith("{") && p2.endsWith("}")) {
+ wildcards++;
+ }
+ }
+
+ return wildcards;
+ }
+
private static String pathAsKey(String path) {
// cater for default path
if (path == null || path.equals("/")) {
http://git-wip-us.apache.org/repos/asf/camel/blob/b1c9132c/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/rest/RestNettyHttpGetWildcardsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/rest/RestNettyHttpGetWildcardsTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/rest/RestNettyHttpGetWildcardsTest.java
new file mode 100644
index 0000000..403bc8f
--- /dev/null
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/rest/RestNettyHttpGetWildcardsTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.netty.http.rest;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.netty.http.BaseNettyTest;
+import org.apache.camel.component.netty.http.RestNettyHttpBinding;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+
+public class RestNettyHttpGetWildcardsTest extends BaseNettyTest {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("mybinding", new RestNettyHttpBinding());
+ return jndi;
+ }
+
+ @Test
+ public void testProducerGet() throws Exception {
+ String out = template.requestBody("netty-http:http://localhost:{{port}}/users/123/basic", null, String.class);
+ assertEquals("123;Donald Duck", out);
+ }
+
+ @Test
+ public void testServletProducerGetWildcards() throws Exception {
+ String out = template.requestBody("netty-http:http://localhost:{{port}}/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 netty4-http on localhost with the given port
+ restConfiguration().component("netty-http").host("localhost").port(getPort()).endpointProperty("nettyHttpBinding", "#mybinding");
+
+ // use the rest DSL to define the rest services
+ rest("/users/")
+ .get("{id}/{query}")
+ .route()
+ .to("log: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()
+ .get("{id}/basic")
+ .route()
+ .to("log: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();
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/b1c9132c/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 32fd20c..68bb656 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
@@ -263,7 +263,7 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelInboundHandl
* @param consumerPath the consumer path which may use { } tokens
* @return number of wildcards, or <tt>0</tt> if no wildcards
*/
- private int countWildcards(String consumerPath) {
+ private static int countWildcards(String consumerPath) {
int wildcards = 0;
// remove starting/ending slashes
[4/4] camel git commit: CAMEL-9089: rest-dsl when multiple candidates
then use the most specific one.
Posted by da...@apache.org.
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 <da...@apache.org>
Authored: Fri Aug 21 10:51:29 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
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();
+ }
+ };
+ }
+
+}
[3/4] camel git commit: CAMEL-9089: rest-dsl when multiple candidates
then use the most specific one.
Posted by da...@apache.org.
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();
}
[2/4] camel git commit: CAMEL-9089: rest-dsl when multiple candidates
then use the most specific one.
Posted by da...@apache.org.
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/57f48e83
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/57f48e83
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/57f48e83
Branch: refs/heads/master
Commit: 57f48e8334c768ccd41864396343a13528fc630b
Parents: 19f27fd
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Aug 21 10:17:18 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Aug 21 10:53:02 2015 +0200
----------------------------------------------------------------------
.../HttpServerMultiplexChannelHandler.java | 61 ++++++++++++++-
.../rest/RestNettyHttpGetWildcardsTest.java | 80 ++++++++++++++++++++
.../rest/RestServletGetWildcardsTest.java | 4 +-
3 files changed, 139 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/57f48e83/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 5b3ad93..32fd20c 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
@@ -200,18 +200,46 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelInboundHandl
}
}
- // then match by non wildcard path
+ // then match by wildcard path
if (answer == null) {
Iterator<Map.Entry<ContextPathMatcher, HttpServerChannelHandler>> it = candidates.iterator();
while (it.hasNext()) {
Map.Entry<ContextPathMatcher, HttpServerChannelHandler> entry = it.next();
+ // filter non matching paths
if (!entry.getKey().matchesRest(path, true)) {
it.remove();
}
}
- // there should only be one
- if (candidates.size() == 1) {
+ // if there is multiple candidates with wildcards then pick anyone with the least number of wildcards
+ int bestWildcard = Integer.MAX_VALUE;
+ Map.Entry<ContextPathMatcher, HttpServerChannelHandler> best = null;
+ if (candidates.size() > 1) {
+ it = candidates.iterator();
+ while (it.hasNext()) {
+ Map.Entry<ContextPathMatcher, HttpServerChannelHandler> entry = it.next();
+ String consumerPath = entry.getValue().getConsumer().getConfiguration().getPath();
+ int wildcards = countWildcards(consumerPath);
+ if (wildcards > 0) {
+ if (best == null) {
+ best = entry;
+ } else {
+ if (wildcards < bestWildcard) {
+ bestWildcard = wildcards;
+ best = entry;
+ }
+ }
+ }
+ }
+
+ if (best != null) {
+ // pick the best among the wildcards
+ answer = best.getValue();
+ }
+ }
+
+ // if there is one left then its our answer
+ if (answer == null && candidates.size() == 1) {
answer = candidates.get(0).getValue();
}
}
@@ -229,6 +257,33 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelInboundHandl
return answer;
}
+ /**
+ * Counts the number of wildcards in the path
+ *
+ * @param consumerPath the consumer path which may use { } tokens
+ * @return number of wildcards, or <tt>0</tt> if no wildcards
+ */
+ private int countWildcards(String consumerPath) {
+ int wildcards = 0;
+
+ // remove starting/ending slashes
+ if (consumerPath.startsWith("/")) {
+ consumerPath = consumerPath.substring(1);
+ }
+ if (consumerPath.endsWith("/")) {
+ consumerPath = consumerPath.substring(0, consumerPath.length() - 1);
+ }
+
+ String[] consumerPaths = consumerPath.split("/");
+ for (String p2 : consumerPaths) {
+ if (p2.startsWith("{") && p2.endsWith("}")) {
+ wildcards++;
+ }
+ }
+
+ return wildcards;
+ }
+
private static String pathAsKey(String path) {
// cater for default path
if (path == null || path.equals("/")) {
http://git-wip-us.apache.org/repos/asf/camel/blob/57f48e83/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpGetWildcardsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpGetWildcardsTest.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpGetWildcardsTest.java
new file mode 100644
index 0000000..176c6e9
--- /dev/null
+++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpGetWildcardsTest.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.netty4.http.rest;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.netty4.http.BaseNettyTest;
+import org.apache.camel.component.netty4.http.RestNettyHttpBinding;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+
+public class RestNettyHttpGetWildcardsTest extends BaseNettyTest {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("mybinding", new RestNettyHttpBinding());
+ return jndi;
+ }
+
+ @Test
+ public void testProducerGet() throws Exception {
+ String out = template.requestBody("netty4-http:http://localhost:{{port}}/users/123/basic", null, String.class);
+ assertEquals("123;Donald Duck", out);
+ }
+
+ @Test
+ public void testServletProducerGetWildcards() throws Exception {
+ String out = template.requestBody("netty4-http:http://localhost:{{port}}/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 netty4-http on localhost with the given port
+ restConfiguration().component("netty4-http").host("localhost").port(getPort()).endpointProperty("nettyHttpBinding", "#mybinding");
+
+ // use the rest DSL to define the rest services
+ rest("/users/")
+ .get("{id}/{query}")
+ .route()
+ .to("log: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()
+ .get("{id}/basic")
+ .route()
+ .to("log: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();
+ }
+ };
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/57f48e83/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 2a49836..e382601 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
@@ -26,7 +26,6 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.servlet.ServletCamelRouterTestSupport;
import org.apache.camel.component.servlet.ServletRestHttpBinding;
import org.apache.camel.impl.JndiRegistry;
-import org.junit.Ignore;
import org.junit.Test;
public class RestServletGetWildcardsTest extends ServletCamelRouterTestSupport {
@@ -51,7 +50,6 @@ public class RestServletGetWildcardsTest extends ServletCamelRouterTestSupport {
}
@Test
- @Ignore
public void testServletProducerGetWildcards() throws Exception {
WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/users/456/name=g*");
ServletUnitClient client = newClient();
@@ -82,7 +80,7 @@ public class RestServletGetWildcardsTest extends ServletCamelRouterTestSupport {
exchange.getOut().setBody(id + ";Goofy");
}
}).endRest()
- .get("{id}/basic")
+ .get("{id}/basic")
.route()
.to("mock:input")
.process(new Processor() {