Repository: camel Updated Branches: refs/heads/camel-2.15.x 38e0233bc -> c80e57600 refs/heads/master eab3dee9c -> 59d281ad6
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/59d281ad Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/59d281ad Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/59d281ad Branch: refs/heads/master Commit: 59d281ad6131851d1a4ee2265d8595993fde1286 Parents: eab3dee Author: Claus Ibsen <[email protected]> Authored: Fri Aug 21 09:29:14 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 21 09:29:14 2015 +0200 ---------------------------------------------------------------------- ...rvletRestServletResolveConsumerStrategy.java | 48 +++++++++- .../rest/RestServletGetWildcardsTest.java | 98 ++++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/59d281ad/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 b7df2cd..64e90ca 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 @@ -65,18 +65,35 @@ public class ServletRestServletResolveConsumerStrategy extends HttpServletResolv } } - // 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 ServletRestServletResolveConsumerStrategy extends HttpServletResolv } /** + * 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 http://git-wip-us.apache.org/repos/asf/camel/blob/59d281ad/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 new file mode 100644 index 0000000..2a49836 --- /dev/null +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletGetWildcardsTest.java @@ -0,0 +1,98 @@ +/** + * 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.servlet.rest; + +import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebResponse; +import com.meterware.servletunit.ServletUnitClient; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +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 { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("myBinding", new ServletRestHttpBinding()); + return jndi; + } + + @Test + public void testServletProducerGet() throws Exception { + WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/users/123/basic"); + ServletUnitClient client = newClient(); + client.setExceptionsThrownOnErrorStatus(false); + WebResponse response = client.getResponse(req); + + assertEquals(200, response.getResponseCode()); + + assertEquals("123;Donald Duck", response.getText()); + } + + @Test + @Ignore + public void testServletProducerGetWildcards() throws Exception { + WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/users/456/name=g*"); + ServletUnitClient client = newClient(); + client.setExceptionsThrownOnErrorStatus(false); + WebResponse response = client.getResponse(req); + + assertEquals(200, response.getResponseCode()); + + assertEquals("456;Goofy", response.getText()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use servlet on localhost + restConfiguration().component("servlet").host("localhost").endpointProperty("httpBindingRef", "#myBinding"); + + // use the rest DSL to define the rest services + rest("/users/") + .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() + .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(); + } + }; + } + +}
