Camel catalog should allow to turn off encode uri when building a uri from options.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e6395632 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e6395632 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e6395632 Branch: refs/heads/camel-2.16.x Commit: e6395632b9497c44db7f1f09ffeecaf6341e6b86 Parents: af94e78 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Nov 11 10:58:36 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Nov 11 11:00:46 2015 +0100 ---------------------------------------------------------------------- .../org/apache/camel/catalog/CamelCatalog.java | 8 ++-- .../camel/catalog/DefaultCamelCatalog.java | 24 ++++++------ .../org/apache/camel/catalog/URISupport.java | 18 ++++++--- .../apache/camel/catalog/CamelCatalogTest.java | 39 +++++++++++++------- 4 files changed, 55 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/e6395632/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java ---------------------------------------------------------------------- diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java index ed8c317..bf34b0a 100644 --- a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java +++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java @@ -178,7 +178,7 @@ public interface CamelCatalog { * @return the constructed endpoint uri * @throws java.net.URISyntaxException is thrown if there is encoding error */ - String asEndpointUri(String scheme, String json) throws URISyntaxException; + String asEndpointUri(String scheme, String json, boolean encode) throws URISyntaxException; /** * Creates an endpoint uri in XML style (eg escape & as &l;) from the information in the json schema @@ -188,7 +188,7 @@ public interface CamelCatalog { * @return the constructed endpoint uri * @throws java.net.URISyntaxException is thrown if there is encoding error */ - String asEndpointUriXml(String scheme, String json) throws URISyntaxException; + String asEndpointUriXml(String scheme, String json, boolean encode) throws URISyntaxException; /** * Creates an endpoint uri in Java style from the information from the properties @@ -198,7 +198,7 @@ public interface CamelCatalog { * @return the constructed endpoint uri * @throws java.net.URISyntaxException is thrown if there is encoding error */ - String asEndpointUri(String scheme, Map<String, String> properties) throws URISyntaxException; + String asEndpointUri(String scheme, Map<String, String> properties, boolean encode) throws URISyntaxException; /** * Creates an endpoint uri in XML style (eg escape & as &l;) from the information from the properties @@ -208,7 +208,7 @@ public interface CamelCatalog { * @return the constructed endpoint uri * @throws java.net.URISyntaxException is thrown if there is encoding error */ - String asEndpointUriXml(String scheme, Map<String, String> properties) throws URISyntaxException; + String asEndpointUriXml(String scheme, Map<String, String> properties, boolean encode) throws URISyntaxException; /** * Lists all the components summary details in JSon http://git-wip-us.apache.org/repos/asf/camel/blob/e6395632/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 0cf52f0..01766fc 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 @@ -607,16 +607,16 @@ public class DefaultCamelCatalog implements CamelCatalog { } @Override - public String asEndpointUri(String scheme, String json) throws URISyntaxException { - return doAsEndpointUri(scheme, json, "&"); + public String asEndpointUri(String scheme, String json, boolean encode) throws URISyntaxException { + return doAsEndpointUri(scheme, json, "&", encode); } @Override - public String asEndpointUriXml(String scheme, String json) throws URISyntaxException { - return doAsEndpointUri(scheme, json, "&"); + public String asEndpointUriXml(String scheme, String json, boolean encode) throws URISyntaxException { + return doAsEndpointUri(scheme, json, "&", encode); } - private String doAsEndpointUri(String scheme, String json, String ampersand) throws URISyntaxException { + private String doAsEndpointUri(String scheme, String json, String ampersand, boolean encode) throws URISyntaxException { List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("properties", json, true); Map<String, String> copy = new HashMap<String, String>(); @@ -649,20 +649,20 @@ public class DefaultCamelCatalog implements CamelCatalog { } } - return doAsEndpointUri(scheme, copy, ampersand); + return doAsEndpointUri(scheme, copy, ampersand, encode); } @Override - public String asEndpointUri(String scheme, Map<String, String> properties) throws URISyntaxException { - return doAsEndpointUri(scheme, properties, "&"); + public String asEndpointUri(String scheme, Map<String, String> properties, boolean encode) throws URISyntaxException { + return doAsEndpointUri(scheme, properties, "&", encode); } @Override - public String asEndpointUriXml(String scheme, Map<String, String> properties) throws URISyntaxException { - return doAsEndpointUri(scheme, properties, "&"); + public String asEndpointUriXml(String scheme, Map<String, String> properties, boolean encode) throws URISyntaxException { + return doAsEndpointUri(scheme, properties, "&", encode); } - private String doAsEndpointUri(String scheme, Map<String, String> properties, String ampersand) throws URISyntaxException { + private String doAsEndpointUri(String scheme, Map<String, String> properties, String ampersand, boolean encode) throws URISyntaxException { String json = componentJSonSchema(scheme); if (json == null) { throw new IllegalArgumentException("Cannot find endpoint with scheme " + scheme); @@ -760,7 +760,7 @@ public class DefaultCamelCatalog implements CamelCatalog { if (!copy.isEmpty()) { // the last option may already contain a ? char, if so we should use & instead of ? sb.append(hasQuestionmark ? ampersand : '?'); - String query = createQueryString(copy, ampersand); + String query = createQueryString(copy, ampersand, encode); // we do not want to use %23 for # syntax query = query.replaceAll("\\=\\%23", "=#"); sb.append(query); http://git-wip-us.apache.org/repos/asf/camel/blob/e6395632/platforms/catalog/src/main/java/org/apache/camel/catalog/URISupport.java ---------------------------------------------------------------------- diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/URISupport.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/URISupport.java index 2742d0c..e0c16b7 100644 --- a/platforms/catalog/src/main/java/org/apache/camel/catalog/URISupport.java +++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/URISupport.java @@ -283,7 +283,7 @@ public final class URISupport { * @throws URISyntaxException is thrown if uri has invalid syntax. */ @SuppressWarnings("unchecked") - public static String createQueryString(Map<String, String> options, String ampersand) throws URISyntaxException { + public static String createQueryString(Map<String, String> options, String ampersand, boolean encode) throws URISyntaxException { try { if (options.size() > 0) { StringBuilder rc = new StringBuilder(); @@ -300,7 +300,7 @@ public final class URISupport { // use the value as a String String s = value != null ? value.toString() : null; - appendQueryStringParameter(key, s, rc); + appendQueryStringParameter(key, s, rc, encode); } return rc.toString(); } else { @@ -313,8 +313,12 @@ public final class URISupport { } } - private static void appendQueryStringParameter(String key, String value, StringBuilder rc) throws UnsupportedEncodingException { - rc.append(URLEncoder.encode(key, CHARSET)); + private static void appendQueryStringParameter(String key, String value, StringBuilder rc, boolean encode) throws UnsupportedEncodingException { + if (encode) { + rc.append(URLEncoder.encode(key, CHARSET)); + } else { + rc.append(key); + } // only append if value is not null if (value != null) { rc.append("="); @@ -322,7 +326,11 @@ public final class URISupport { // do not encode RAW parameters rc.append(value); } else { - rc.append(URLEncoder.encode(value, CHARSET)); + if (encode) { + rc.append(URLEncoder.encode(value, CHARSET)); + } else { + rc.append(value); + } } } } http://git-wip-us.apache.org/repos/asf/camel/blob/e6395632/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 eb7e8bf..f9a7c66 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 @@ -118,10 +118,10 @@ public class CamelCatalogTest { map.put("noop", "true"); map.put("delay", "5000"); - String uri = catalog.asEndpointUri("file", map); + String uri = catalog.asEndpointUri("file", map, true); assertEquals("file:src/data/inbox?delay=5000&noop=true", uri); - String uri2 = catalog.asEndpointUriXml("file", map); + String uri2 = catalog.asEndpointUriXml("file", map, true); assertEquals("file:src/data/inbox?delay=5000&noop=true", uri2); } @@ -133,10 +133,10 @@ public class CamelCatalogTest { map.put("directoryName", "foo"); map.put("connectTimeout", "5000"); - String uri = catalog.asEndpointUri("ftp", map); + String uri = catalog.asEndpointUri("ftp", map, true); assertEquals("ftp:someserver:21/foo?connectTimeout=5000", uri); - String uri2 = catalog.asEndpointUriXml("ftp", map); + String uri2 = catalog.asEndpointUriXml("ftp", map, true); assertEquals("ftp:someserver:21/foo?connectTimeout=5000", uri2); } @@ -146,7 +146,7 @@ public class CamelCatalogTest { map.put("destinationType", "queue"); map.put("destinationName", "foo"); - String uri = catalog.asEndpointUri("jms", map); + String uri = catalog.asEndpointUri("jms", map, true); assertEquals("jms:queue:foo", uri); } @@ -159,14 +159,14 @@ public class CamelCatalogTest { map.put("path", "foo/bar"); map.put("disconnect", "true"); - String uri = catalog.asEndpointUri("netty4-http", map); + String uri = catalog.asEndpointUri("netty4-http", map, true); assertEquals("netty4-http:http:localhost:8080/foo/bar?disconnect=true", uri); // lets set a query parameter in the path map.put("path", "foo/bar?verbose=true"); map.put("disconnect", "true"); - uri = catalog.asEndpointUri("netty4-http", map); + uri = catalog.asEndpointUri("netty4-http", map, true); assertEquals("netty4-http:http:localhost:8080/foo/bar?verbose=true&disconnect=true", uri); } @@ -176,10 +176,23 @@ public class CamelCatalogTest { map.put("timerName", "foo"); map.put("period", "5000"); - String uri = catalog.asEndpointUri("timer", map); + String uri = catalog.asEndpointUri("timer", map, true); assertEquals("timer:foo?period=5000", uri); } + @Test + public void testAsEndpointUriPropertiesPlaceholders() throws Exception { + Map<String, String> map = new HashMap<String, String>(); + map.put("timerName", "foo"); + map.put("period", "{{howoften}}"); + map.put("repeatCount", "5"); + + String uri = catalog.asEndpointUri("timer", map, true); + assertEquals("timer:foo?period=%7B%7Bhowoften%7D%7D&repeatCount=5", uri); + + uri = catalog.asEndpointUri("timer", map, false); + assertEquals("timer:foo?period={{howoften}}&repeatCount=5", uri); + } @Test public void testAsEndpointUriBeanLookup() throws Exception { @@ -187,7 +200,7 @@ public class CamelCatalogTest { map.put("resourceUri", "foo.xslt"); map.put("converter", "#myConverter"); - String uri = catalog.asEndpointUri("xslt", map); + String uri = catalog.asEndpointUri("xslt", map, true); assertEquals("xslt:foo.xslt?converter=#myConverter", uri); } @@ -195,23 +208,23 @@ public class CamelCatalogTest { public void testAsEndpointUriMapJmsRequiredOnly() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("destinationName", "foo"); - String uri = catalog.asEndpointUri("jms", map); + String uri = catalog.asEndpointUri("jms", map, true); assertEquals("jms:foo", uri); map.put("deliveryPersistent", "false"); map.put("allowNullBody", "true"); - uri = catalog.asEndpointUri("jms", map); + uri = catalog.asEndpointUri("jms", map, true); assertEquals("jms:foo?allowNullBody=true&deliveryPersistent=false", uri); - String uri2 = catalog.asEndpointUriXml("jms", map); + String uri2 = catalog.asEndpointUriXml("jms", map, true); assertEquals("jms:foo?allowNullBody=true&deliveryPersistent=false", uri2); } @Test public void testAsEndpointUriJson() throws Exception { String json = loadText(CamelCatalogTest.class.getClassLoader().getResourceAsStream("sample.json")); - String uri = catalog.asEndpointUri("ftp", json); + String uri = catalog.asEndpointUri("ftp", json, true); assertEquals("ftp:someserver:21/foo?connectTimeout=5000", uri); }