Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,26 @@ public static List<String> splitOgnl(String ognl) {
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
if (j == 0 || (j == 1 && ognl.charAt(i - 1) == '?')
|| (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

Expand All @@ -243,7 +244,7 @@ public static List<String> splitOgnl(String ognl) {

// 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();
Expand All @@ -258,11 +259,11 @@ public static List<String> splitOgnl(String ognl) {
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);
}

Expand All @@ -279,7 +280,7 @@ public static List<String> splitOgnl(String ognl) {
}

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.util;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the licence header needs to be added here


import java.util.List;

import org.junit.Assert;
import org.junit.Test;

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));
}

}