From commits-return-73383-archive-asf-public=cust-asf.ponee.io@camel.apache.org Tue Jun 11 10:09:35 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 3FA7C180627 for ; Tue, 11 Jun 2019 12:09:35 +0200 (CEST) Received: (qmail 47007 invoked by uid 500); 11 Jun 2019 10:09:34 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 46998 invoked by uid 99); 11 Jun 2019 10:09:34 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 11 Jun 2019 10:09:34 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 940B987AA3; Tue, 11 Jun 2019 10:09:34 +0000 (UTC) Date: Tue, 11 Jun 2019 10:09:34 +0000 To: "commits@camel.apache.org" Subject: [camel] branch master updated: CAMEL-13631: PropertyBindingSupport to support key filtering and transformation MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <156024777421.27487.4790603579208876870@gitbox.apache.org> From: lburgazzoli@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: camel X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: 1276d434ab68eb2e50fc942de1c1297f71d0f16a X-Git-Newrev: 0d2b91bb96d0cd89969d2b1a65a0a2265acfea05 X-Git-Rev: 0d2b91bb96d0cd89969d2b1a65a0a2265acfea05 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated 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 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 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 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 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 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 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> iter = properties.entrySet().iterator(); iter.hasNext();) { Map.Entry 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; + } } }