camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lburgazz...@apache.org
Subject [camel] branch master updated: CAMEL-13631: PropertyBindingSupport to support key filtering and transformation
Date Tue, 11 Jun 2019 10:09:34 GMT
This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d2b91b  CAMEL-13631: PropertyBindingSupport to support key filtering and transformation
0d2b91b is described below

commit 0d2b91bb96d0cd89969d2b1a65a0a2265acfea05
Author: lburgazzoli <lburgazzoli@gmail.com>
AuthorDate: Mon Jun 10 18:50:24 2019 +0200

    CAMEL-13631: PropertyBindingSupport to support key filtering and transformation
---
 .../camel/support/PropertyBindingSupportTest.java  | 25 ++++++++
 .../camel/support/PropertyBindingSupport.java      | 73 +++++++++++++++++++---
 2 files changed, 91 insertions(+), 7 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index 694abfb..f92edb0 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -70,6 +70,31 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
     }
 
     @Test
+    public void testBindPropertiesWithOptionPrefix() throws Exception {
+        Foo foo = new Foo();
+
+        Map<String, Object> prop = new HashMap<>();
+        prop.put("my.prefix.name", "James");
+        prop.put("my.prefix.bar.age", "33");
+        prop.put("my.prefix.bar.{{committer}}", "true");
+        prop.put("my.prefix.bar.gold-customer", "true");
+        prop.put("my.prefix.bar.work.id", "123");
+        prop.put("my.prefix.bar.work.name", "{{companyName}}");
+        prop.put("my.other.prefix.something", "test");
+
+        PropertyBindingSupport.bindProperties(context, foo, prop, "my.prefix.");
+
+        assertEquals("James", foo.getName());
+        assertEquals(33, foo.getBar().getAge());
+        assertTrue(foo.getBar().isRider());
+        assertTrue(foo.getBar().isGoldCustomer());
+        assertEquals(123, foo.getBar().getWork().getId());
+        assertEquals("Acme", foo.getBar().getWork().getName());
+        assertTrue(prop.containsKey("my.other.prefix.something"));
+        assertEquals(1, prop.size());
+    }
+
+    @Test
     public void testNested() throws Exception {
         Foo foo = new Foo();
 
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 468ef3e..be3ee8f 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -47,7 +47,6 @@ import static org.apache.camel.util.ObjectHelper.isNotEmpty;
  * This implementations reuses parts of {@link IntrospectionSupport}.
  */
 public final class PropertyBindingSupport {
-
     /**
      * To use a fluent builder style to configure this property binding support.
      */
@@ -58,6 +57,7 @@ public final class PropertyBindingSupport {
         private boolean reference = true;
         private boolean placeholder = true;
         private boolean fluentBuilder = true;
+        private String optionPrefix;
 
         /**
          * Whether nesting is in use
@@ -101,6 +101,15 @@ public final class PropertyBindingSupport {
         }
 
         /**
+         * Whether properties should be filtered by prefix.         *
+         * Note that the prefix is removed from the key before the property is bound.
+         */
+        public Builder withOptionPrefix(String optionPrefix) {
+            this.optionPrefix = optionPrefix;
+            return this;
+        }
+
+        /**
          * Binds the properties to the target object, and removes the property that was bound
from properties.
          *
          * @param camelContext  the camel context
@@ -113,7 +122,7 @@ public final class PropertyBindingSupport {
             org.apache.camel.util.ObjectHelper.notNull(target, "target");
             org.apache.camel.util.ObjectHelper.notNull(properties, "properties");
 
-            return bindProperties(camelContext, target, properties, nesting, deepNesting,
fluentBuilder, reference, placeholder);
+            return bindProperties(camelContext, target, properties, optionPrefix, nesting,
deepNesting, fluentBuilder, reference, placeholder);
         }
 
     }
@@ -258,15 +267,51 @@ public final class PropertyBindingSupport {
      * @return              true if one or more properties was bound
      */
     public static boolean bindProperties(CamelContext camelContext, Object target, Map<String,
Object> properties) {
-        return bindProperties(camelContext, target, properties, true, true, true, true, true);
+        return bindProperties(camelContext, target, properties, null);
     }
 
     /**
-     * Binds the properties to the target object, and removes the property that was bound
from properties.
+     * Binds the properties with the given prefix to the target object, and removes the property
that was bound from properties.
+     * Note that the prefix is removed from the key before the property is bound.
+     *
+     * @param camelContext  the camel context
+     * @param target        the target object
+     * @param properties    the properties where the bound properties will be removed from
+     * @param optionPrefix  the prefix used to filter properties
+     * @return              true if one or more properties was bound
+     */
+    public static boolean bindProperties(CamelContext camelContext, Object target, Map<String,
Object> properties, String optionPrefix) {
+        return bindProperties(camelContext, target, properties, optionPrefix, true, true,
true, true, true);
+    }
+
+    /**
+     * Binds the properties with the given prefix to the target object, and removes the property
that was bound from properties.
+     *
+     * @param camelContext  the camel context
+     * @param target        the target object
+     * @param properties    the properties where the bound properties will be removed from
+     * @param nesting       whether nesting is in use
+     * @param deepNesting   whether deep nesting is in use, where Camel will attempt to walk
as deep as possible by creating new objects in the OGNL graph if
+     *                      a property has a setter and the object can be created from a
default no-arg constructor.
+     * @param fluentBuilder whether fluent builder is allowed as a valid getter/setter
+     * @param reference     whether reference parameter (syntax starts with #) is in use
+     * @param placeholder   whether to use Camels property placeholder to resolve placeholders
on keys and values
+     * @return              true if one or more properties was bound
+     */
+    public static boolean bindProperties(CamelContext camelContext, Object target, Map<String,
Object> properties,
+                                         boolean nesting, boolean deepNesting, boolean fluentBuilder,
boolean reference, boolean placeholder) {
+
+        return bindProperties(camelContext, target, properties, null, nesting, deepNesting,
fluentBuilder, reference, placeholder);
+    }
+
+    /**
+     * Binds the properties with the given prefix to the target object, and removes the property
that was bound from properties.
+     * Note that the prefix is removed from the key before the property is bound.
      *
      * @param camelContext  the camel context
      * @param target        the target object
      * @param properties    the properties where the bound properties will be removed from
+     * @param optionPrefix  the prefix used to filter properties
      * @param nesting       whether nesting is in use
      * @param deepNesting   whether deep nesting is in use, where Camel will attempt to walk
as deep as possible by creating new objects in the OGNL graph if
      *                      a property has a setter and the object can be created from a
default no-arg constructor.
@@ -276,6 +321,7 @@ public final class PropertyBindingSupport {
      * @return              true if one or more properties was bound
      */
     public static boolean bindProperties(CamelContext camelContext, Object target, Map<String,
Object> properties,
+                                         String optionPrefix,
                                          boolean nesting, boolean deepNesting, boolean fluentBuilder,
boolean reference, boolean placeholder) {
         org.apache.camel.util.ObjectHelper.notNull(camelContext, "camelContext");
         org.apache.camel.util.ObjectHelper.notNull(target, "target");
@@ -287,9 +333,22 @@ public final class PropertyBindingSupport {
 
         for (Iterator<Map.Entry<String, Object>> iter = properties.entrySet().iterator();
iter.hasNext();) {
             Map.Entry<String, Object> entry = iter.next();
-            if (bindProperty(camelContext, target, entry.getKey(), entry.getValue(), nesting,
deepNesting, fluentBuilder, reference, placeholder)) {
-                iter.remove();
-                rc = true;
+            String key = entry.getKey();
+            Object value = entry.getValue();
+
+            if (isNotEmpty(optionPrefix)) {
+                if (!key.startsWith(optionPrefix)) {
+                    continue;
+                }
+
+                key = key.substring(optionPrefix.length());
+            }
+
+            if (entry != null) {
+                if (bindProperty(camelContext, target, key, value, nesting, deepNesting,
fluentBuilder, reference, placeholder)) {
+                    iter.remove();
+                    rc = true;
+                }
             }
         }
 


Mime
View raw message