Repository: camel Updated Branches: refs/heads/master 59d281ad6 -> 927627061
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/92762706 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/92762706 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/92762706 Branch: refs/heads/master Commit: 927627061154a40514f5dbb0279345a6c8ef146d Parents: 59d281a Author: Claus Ibsen <[email protected]> Authored: Fri Aug 21 09:42:54 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 21 09:42:54 2015 +0200 ---------------------------------------------------------------------- ...JettyRestServletResolveConsumerStrategy.java | 50 ++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/92762706/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 ba8672b..3da7c29 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 @@ -27,7 +27,7 @@ import org.apache.camel.http.common.HttpConsumer; import org.apache.camel.http.common.HttpServletResolveConsumerStrategy; /** - * A {@link org.apache.camel.component.common.http.HttpServletResolveConsumerStrategy} that supports the Rest DSL. + * A {@link org.apache.camel.http.common.HttpServletResolveConsumerStrategy} that supports the Rest DSL. */ public class JettyRestServletResolveConsumerStrategy extends HttpServletResolveConsumerStrategy { @@ -65,18 +65,35 @@ public class JettyRestServletResolveConsumerStrategy extends HttpServletResolveC } } - // then match by non wildcard path + // then match by wildcard path if (answer == null) { it = candidates.iterator(); while (it.hasNext()) { HttpConsumer consumer = it.next(); String consumerPath = consumer.getPath(); + // filter non matching paths if (!matchRestPath(path, consumerPath, true)) { it.remove(); } } - // there should only be one + // if there is multiple candidates then pick anyone with the least number of wildcards + int best = -1; + if (candidates.size() > 1) { + it = candidates.iterator(); + while (it.hasNext()) { + HttpConsumer consumer = it.next(); + String consumerPath = consumer.getPath(); + int wildcards = countWildcards(consumerPath); + if (best != -1 && wildcards >= best) { + it.remove(); + } else { + best = wildcards; + } + } + } + + // if there is one left then its our answer if (candidates.size() == 1) { answer = candidates.get(0); } @@ -141,6 +158,33 @@ public class JettyRestServletResolveConsumerStrategy extends HttpServletResolveC } /** + * 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 + */ + public 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; + } + + /** * Matches the given request HTTP method with the configured HTTP method of the consumer * * @param method the request HTTP method
