You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/09/14 13:51:55 UTC

[camel] 01/02: CAMEL-13962: keep track of brackets that belong to a method in order to prevent false method splitting

This is an automated email from the ASF dual-hosted git repository.

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

commit 9e696d136331268cb6e8c09ddc7aed2f9b9815f5
Author: Michael Pätzold <mi...@aoe.com>
AuthorDate: Thu Sep 12 10:32:06 2019 +0200

    CAMEL-13962: keep track of brackets that belong to a method in order to prevent false method splitting
---
 .../java/org/apache/camel/util/OgnlHelper.java     | 23 ++++++++++---------
 .../java/org/apache/camel/util/OgnlHelperTest.java | 26 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
index df806ea..564df2a 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
@@ -204,8 +204,9 @@ public final class OgnlHelper {
         StringBuilder sb = new StringBuilder();
 
         int j = 0; // j is used as counter per method
-        boolean squareBracket = false; // special to keep track if we are inside a square bracket block, eg: [foo]
-        boolean parenthesisBracket = false; // special to keep track if we are inside a parenthesis block, eg: bar(${body}, ${header.foo})
+        int squareBracketCnt = 0; // special to keep track if and how deep we are inside a square bracket block, eg: [foo]
+        int parenthesisBracketCnt = 0; // special to keep track if and how deep we are inside a parenthesis block, eg: bar(${body}, ${header.foo})
+
         for (int i = 0; i < ognl.length(); i++) {
             char ch = ognl.charAt(i);
             // special for starting a new method
@@ -213,16 +214,16 @@ public final class OgnlHelper {
                     || (ch != '.' && ch != '?' && ch != ']')) {
                 sb.append(ch);
                 // special if we are doing square bracket
-                if (ch == '[' && !parenthesisBracket) {
-                    squareBracket = true;
+                if (ch == '[' && parenthesisBracketCnt == 0) {
+                    squareBracketCnt++;
                 } else if (ch == '(') {
-                    parenthesisBracket = true;
+                    parenthesisBracketCnt++;
                 } else if (ch == ')') {
-                    parenthesisBracket = false;
+                    parenthesisBracketCnt--;
                 }
                 j++; // advance
             } else {
-                if (ch == '.' && !squareBracket && !parenthesisBracket) {
+                if (ch == '.' && squareBracketCnt == 0 && parenthesisBracketCnt == 0) {
                     // only treat dot as a method separator if not inside a square bracket block
                     // as dots can be used in key names when accessing maps
 
@@ -243,7 +244,7 @@ public final class OgnlHelper {
 
                     // reset j to begin a new method
                     j = 0;
-                } else if (ch == ']' && !parenthesisBracket) {
+                } else if (ch == ']' && parenthesisBracketCnt == 0) {
                     // append ending ] to method name
                     sb.append(ch);
                     String s = sb.toString();
@@ -258,11 +259,11 @@ public final class OgnlHelper {
                     j = 0;
 
                     // no more square bracket
-                    squareBracket = false;
+                    squareBracketCnt--;
                 }
 
                 // and don't lose the char if its not an ] end marker (as we already added that)
-                if (ch != ']' || parenthesisBracket) {
+                if (ch != ']' || parenthesisBracketCnt > 0) {
                     sb.append(ch);
                 }
 
@@ -279,7 +280,7 @@ public final class OgnlHelper {
         }
 
         String last = methods.isEmpty() ? null : methods.get(methods.size() - 1);
-        if (parenthesisBracket && last != null) {
+        if (parenthesisBracketCnt>0 && last != null) {
             // there is an unclosed parenthesis bracket on the last method, so it should end with a parenthesis
             if (last.contains("(") && !last.endsWith(")")) {
                 throw new IllegalArgumentException("Method should end with parenthesis, was " + last);
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java b/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java
new file mode 100644
index 0000000..6d2874a
--- /dev/null
+++ b/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java
@@ -0,0 +1,26 @@
+package org.apache.camel.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class OgnlHelperTest extends Assert {
+
+    /**
+     * Tests correct splitting in case the OGNL expression contains method parameters with brackets.
+     */
+    @Test
+    public void splitOgnlWithRegexInMethod() {
+        String ognl = "header.cookie.replaceFirst(\".*;?iwanttoknow=([^;]+);?.*\", \"$1\")";
+        assertFalse(OgnlHelper.isInvalidValidOgnlExpression(ognl));
+        assertTrue(OgnlHelper.isValidOgnlExpression(ognl));
+
+        List<String> strings = OgnlHelper.splitOgnl(ognl);
+        assertEquals(3, strings.size());
+        assertEquals("header", strings.get(0));
+        assertEquals(".cookie", strings.get(1));
+        assertEquals(".replaceFirst(\".*;?iwanttoknow=([^;]+);?.*\", \"$1\")", strings.get(2));
+    }
+
+}