Repository: camel Updated Branches: refs/heads/camel-2.16.x 54f1ea076 -> 84842ded9
Fixed Camel catalog endpoint options parser Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/84842ded Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/84842ded Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/84842ded Branch: refs/heads/camel-2.16.x Commit: 84842ded9693171eae85e5ddbf508a6e2c5e0280 Parents: 54f1ea0 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Dec 15 10:40:07 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Dec 15 10:40:37 2015 +0100 ---------------------------------------------------------------------- .../camel/catalog/DefaultCamelCatalog.java | 67 ++++++++++++-------- .../apache/camel/catalog/CamelCatalogTest.java | 47 ++++++++++++-- 2 files changed, 85 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/84842ded/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java ---------------------------------------------------------------------- diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java index 3d99109..d5c34f5 100644 --- a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java +++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java @@ -492,7 +492,11 @@ public class DefaultCamelCatalog implements CamelCatalog { // clip the scheme from the syntax syntax = after(syntax, ":"); - // parse the syntax and find the same group in the uri + // clip the scheme from the uri + uri = after(uri, ":"); + String uriPath = stripQuery(uri); + + // parse the syntax and find the names of each option Matcher matcher = SYNTAX_PATTERN.matcher(syntax); List<String> word = new ArrayList<String>(); while (matcher.find()) { @@ -501,30 +505,39 @@ public class DefaultCamelCatalog implements CamelCatalog { word.add(s); } } + // parse the syntax and find each token between each option + String[] tokens = SYNTAX_PATTERN.split(syntax); - // clip the scheme from the uri - uri = after(uri, ":"); - String uriPath = stripQuery(uri); - - // if there is only one, then use uriPath as is + // find the position where each option start/end List<String> word2 = new ArrayList<String>(); + int prev = 0; + for (String token : tokens) { + if (token.isEmpty()) { + continue; + } - if (word.size() == 1) { - String s = uriPath; - s = URISupport.stripPrefix(s, scheme); - // strip any leading : or / after the scheme - while (s.startsWith(":") || s.startsWith("/")) { - s = s.substring(1); - } - word2.add(s); - } else { - Matcher matcher2 = SYNTAX_PATTERN.matcher(uriPath); - while (matcher2.find()) { - String s = matcher2.group(1); - if (!scheme.equals(s)) { - word2.add(s); - } + // special for some tokens where :// can be used also, eg http://foo + int idx = -1; + int len = 0; + if (":".equals(token)) { + idx = uriPath.indexOf("://", prev); + len = 3; } + if (idx == -1) { + idx = uriPath.indexOf(token, prev); + len = token.length(); + } + + if (idx > 0) { + String option = uriPath.substring(prev, idx); + word2.add(option); + prev = idx + len; + } + } + // special for last or if we did not add anyone + if (prev > 0 || word2.isEmpty()) { + String option = uriPath.substring(prev); + word2.add(option); } rows = JSonSchemaHelper.parseJsonSchema("properties", json, true); @@ -551,11 +564,15 @@ public class DefaultCamelCatalog implements CamelCatalog { // we have a little problem as we do not not have all options if (!required) { String value = defaultValue; - options.put(key, value); - defaultValueAdded = true; + if (value != null) { + options.put(key, value); + defaultValueAdded = true; + } } else { - String value = it.next(); - options.put(key, value); + String value = it.hasNext() ? it.next() : null; + if (value != null) { + options.put(key, value); + } } } } http://git-wip-us.apache.org/repos/asf/camel/blob/84842ded/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java ---------------------------------------------------------------------- diff --git a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java index 417daa3..3f002a3 100644 --- a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java +++ b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java @@ -262,8 +262,7 @@ public class CamelCatalogTest { } @Test - @Ignore("TODO: fix me later") - public void testEndpointPropertiesNetty4http() throws Exception { + public void testEndpointPropertiesNetty4Http() throws Exception { Map<String, String> map = catalog.endpointProperties("netty4-http:http:localhost:8080/foo/bar?disconnect=true&keepAlive=false"); assertNotNull(map); assertEquals(6, map.size()); @@ -271,8 +270,48 @@ public class CamelCatalogTest { assertEquals("http", map.get("protocol")); assertEquals("localhost", map.get("host")); assertEquals("8080", map.get("port")); - // TODO: fix me later - //assertEquals("foo/bar", map.get("path")); + assertEquals("foo/bar", map.get("path")); + assertEquals("true", map.get("disconnect")); + assertEquals("false", map.get("keepAlive")); + } + + @Test + public void testEndpointPropertiesNetty4HttpDefaultPort() throws Exception { + Map<String, String> map = catalog.endpointProperties("netty4-http:http:localhost/foo/bar?disconnect=true&keepAlive=false"); + assertNotNull(map); + assertEquals(5, map.size()); + + assertEquals("http", map.get("protocol")); + assertEquals("localhost", map.get("host")); + assertEquals("foo/bar", map.get("path")); + assertEquals("true", map.get("disconnect")); + assertEquals("false", map.get("keepAlive")); + } + + @Test + public void testEndpointPropertiesNetty4HttpPlaceholder() throws Exception { + Map<String, String> map = catalog.endpointProperties("netty4-http:http:{{myhost}}:{{myport}}/foo/bar?disconnect=true&keepAlive=false"); + assertNotNull(map); + assertEquals(6, map.size()); + + assertEquals("http", map.get("protocol")); + assertEquals("{{myhost}}", map.get("host")); + assertEquals("{{myport}}", map.get("port")); + assertEquals("foo/bar", map.get("path")); + assertEquals("true", map.get("disconnect")); + assertEquals("false", map.get("keepAlive")); + } + + @Test + public void testEndpointPropertiesNetty4HttpWithDoubleSlash() throws Exception { + Map<String, String> map = catalog.endpointProperties("netty4-http:http://localhost:8080/foo/bar?disconnect=true&keepAlive=false"); + assertNotNull(map); + assertEquals(6, map.size()); + + assertEquals("http", map.get("protocol")); + assertEquals("localhost", map.get("host")); + assertEquals("8080", map.get("port")); + assertEquals("foo/bar", map.get("path")); assertEquals("true", map.get("disconnect")); assertEquals("false", map.get("keepAlive")); }