camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] camel git commit: Camel catalog - Add support for multi valued options
Date Tue, 26 Jan 2016 15:31:30 GMT
Camel catalog - Add support for multi valued options


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/985331d9
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/985331d9
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/985331d9

Branch: refs/heads/camel-2.16.x
Commit: 985331d9726c28c9ba9d53c5e6aed6e19fc4ba53
Parents: 83b9c51
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Jan 26 16:30:38 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Jan 26 16:31:16 2016 +0100

----------------------------------------------------------------------
 .../camel/catalog/CollectionStringBuffer.java   | 57 ++++++++++++++++++++
 .../camel/catalog/DefaultCamelCatalog.java      | 23 +++++++-
 .../org/apache/camel/catalog/URISupport.java    | 18 +++++++
 .../apache/camel/catalog/CamelCatalogTest.java  | 10 ++++
 4 files changed, 107 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/985331d9/platforms/catalog/src/main/java/org/apache/camel/catalog/CollectionStringBuffer.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/CollectionStringBuffer.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CollectionStringBuffer.java
new file mode 100644
index 0000000..2844ca9
--- /dev/null
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/CollectionStringBuffer.java
@@ -0,0 +1,57 @@
+/**
+ * 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.catalog;
+
+public class CollectionStringBuffer {
+    private final StringBuilder buffer = new StringBuilder();
+    private String separator;
+    private boolean first = true;
+
+    public CollectionStringBuffer() {
+        this(", ");
+    }
+
+    public CollectionStringBuffer(String separator) {
+        this.separator = separator;
+    }
+
+    @Override
+    public String toString() {
+        return buffer.toString();
+    }
+
+    public void append(Object value) {
+        if (first) {
+            first = false;
+        } else {
+            buffer.append(separator);
+        }
+        buffer.append(value);
+    }
+
+    public String getSeparator() {
+        return separator;
+    }
+
+    public void setSeparator(String separator) {
+        this.separator = separator;
+    }
+
+    public boolean isEmpty() {
+        return first;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/985331d9/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 f1e013a..b3478cc 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
@@ -1177,10 +1177,31 @@ public class DefaultCamelCatalog implements CamelCatalog {
         Map<String, Object> parameters = URISupport.parseParameters(u);
 
         // and covert the values to String so its JMX friendly
-        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
+        while (!parameters.isEmpty()) {
+            Map.Entry<String, Object> entry = parameters.entrySet().iterator().next();
             String key = entry.getKey();
             String value = entry.getValue() != null ? entry.getValue().toString() : "";
+
+            boolean multiValued = isPropertyMultiValue(rows, key);
+            if (multiValued) {
+                String prefix = getPropertyPrefix(rows, key);
+                // extra all the multi valued options
+                Map<String, Object> values = URISupport.extractProperties(parameters,
prefix);
+                // build a string with the extra multi valued options with the prefix and
& as separator
+                CollectionStringBuffer csb = new CollectionStringBuffer("&");
+                for (Map.Entry<String, Object> multi : values.entrySet()) {
+                    String line = prefix + multi.getKey() + "=" + (multi.getValue() != null
? multi.getValue().toString() : "");
+                    csb.append(line);
+                }
+                // append the extra multi-values to the existing (which contains the first
multi value)
+                if (!csb.isEmpty()) {
+                    value = value + "&" + csb.toString();
+                }
+            }
+
             answer.put(key, value);
+            // remove the parameter as we run in a while loop until no more parameters
+            parameters.remove(key);
         }
 
         return answer;

http://git-wip-us.apache.org/repos/asf/camel/blob/985331d9/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 e0c16b7..a62523e 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
@@ -22,6 +22,7 @@ import java.net.URISyntaxException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -51,6 +52,23 @@ public final class URISupport {
         return new URI(UnsafeUriCharactersEncoder.encode(uri, true));
     }
 
+    public static Map<String, Object> extractProperties(Map<String, Object> properties,
String optionPrefix) {
+        Map<String, Object> rc = new LinkedHashMap<String, Object>(properties.size());
+
+        for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator();
it.hasNext();) {
+            Map.Entry<String, Object> entry = it.next();
+            String name = entry.getKey();
+            if (name.startsWith(optionPrefix)) {
+                Object value = properties.get(name);
+                name = name.substring(optionPrefix.length());
+                rc.put(name, value);
+                it.remove();
+            }
+        }
+
+        return rc;
+    }
+
     /**
      * Strips the query parameters from the uri
      *

http://git-wip-us.apache.org/repos/asf/camel/blob/985331d9/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 57ad517..0589521 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
@@ -393,6 +393,16 @@ public class CamelCatalogTest {
     }
 
     @Test
+    public void testEndpointPropertiesMultiValued() throws Exception {
+        Map<String, String> map = catalog.endpointProperties("http:helloworld?httpClientOptions=httpClient.foo=123&httpClient.bar=456");
+        assertNotNull(map);
+        assertEquals(2, map.size());
+
+        assertEquals("helloworld", map.get("httpUri"));
+        assertEquals("httpClient.foo=123&httpClient.bar=456", map.get("httpClientOptions"));
+    }
+
+    @Test
     public void testEndpointPropertiesSshWithUserInfo() throws Exception {
         Map<String, String> map = catalog.endpointProperties("ssh:localhost:8101?username=scott&password=tiger");
         assertNotNull(map);


Mime
View raw message