You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/12/14 10:38:42 UTC

[2/2] camel git commit: CAMEL-10581: toD should safe split uris to avoid issues with RAW values.

CAMEL-10581: toD should safe split uris to avoid issues with RAW values.


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

Branch: refs/heads/camel-2.17.x
Commit: 3f8e0547cac72e37b628e04358aad33dbf53dae3
Parents: d16e902
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Dec 14 11:37:28 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Dec 14 11:38:25 2016 +0100

----------------------------------------------------------------------
 .../apache/camel/model/ToDynamicDefinition.java | 79 +++++++++++++++++++-
 .../camel/util/UnsafeUriCharactersEncoder.java  |  1 +
 .../processor/ToDynamicRawAndXPathTest.java     | 44 +++++++++++
 .../camel/processor/ToDynamicRawTest.java       | 42 +++++++++++
 4 files changed, 165 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3f8e0547/camel-core/src/main/java/org/apache/camel/model/ToDynamicDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/ToDynamicDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ToDynamicDefinition.java
index 956a73f..f6628b9 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ToDynamicDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ToDynamicDefinition.java
@@ -18,6 +18,8 @@ package org.apache.camel.model;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
@@ -47,6 +49,9 @@ import org.apache.camel.util.ObjectHelper;
 @XmlRootElement(name = "toD")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class ToDynamicDefinition extends NoOutputDefinition<ToDynamicDefinition> {
+
+    private static final Pattern RAW_PATTERN = Pattern.compile("RAW\\([^\\)]+\\)");
+
     @XmlAttribute @Metadata(required = "true")
     private String uri;
     @XmlAttribute
@@ -83,7 +88,8 @@ public class ToDynamicDefinition extends NoOutputDefinition<ToDynamicDefinition>
 
     protected Expression createExpression(RouteContext routeContext) {
         List<Expression> list = new ArrayList<Expression>();
-        String[] parts = uri.split("\\+");
+
+        String[] parts = safeSplitRaw(uri);
         for (String part : parts) {
             // the part may have optional language to use, so you can mix languages
             String value = ObjectHelper.after(part, "language:");
@@ -195,5 +201,76 @@ public class ToDynamicDefinition extends NoOutputDefinition<ToDynamicDefinition>
         this.ignoreInvalidEndpoint = ignoreInvalidEndpoint;
     }
 
+    // Utilities
+    // -------------------------------------------------------------------------
+
+    private static class Pair {
+        int left;
+        int right;
+        Pair(int left, int right) {
+            this.left = left;
+            this.right = right;
+        }
+    }
+
+    private static List<Pair> checkRAW(String s) {
+        Matcher matcher = RAW_PATTERN.matcher(s);
+        List<Pair> answer = new ArrayList<Pair>();
+        // Check all occurrences
+        while (matcher.find()) {
+            answer.add(new Pair(matcher.start(), matcher.end() - 1));
+        }
+        return answer;
+    }
+
+    private static boolean isRaw(int index, List<Pair>pairs) {
+        for (Pair pair : pairs) {
+            if (index < pair.left) {
+                return false;
+            } else {
+                if (index >= pair.left) {
+                    if (index <= pair.right) {
+                        return true;
+                    } else {
+                        continue;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * We need to split the string safely for each + sign, but avoid splitting within RAW(...).
+     */
+    private static String[] safeSplitRaw(String s) {
+        List<String> list = new ArrayList<>();
+
+        if (!s.contains("+")) {
+            // no plus sign so there is only one part, so no need to split
+            list.add(s);
+        } else {
+            // there is a plus sign so we need to split in a safe manner
+            List<Pair> rawPairs = checkRAW(s);
+            StringBuilder sb = new StringBuilder();
+            char chars[] = s.toCharArray();
+            for (int i = 0; i < chars.length; i++) {
+                char ch = chars[i];
+                if (ch != '+' || isRaw(i, rawPairs)) {
+                    sb.append(ch);
+                } else {
+                    list.add(sb.toString());
+                    sb.setLength(0);
+                }
+            }
+            // any leftover?
+            if (sb.length() > 0) {
+                list.add(sb.toString());
+                sb.setLength(0);
+            }
+        }
+
+        return list.toArray(new String[list.size()]);
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/3f8e0547/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java b/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
index 8c81be8..9f59442 100644
--- a/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
+++ b/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
@@ -99,6 +99,7 @@ public final class UnsafeUriCharactersEncoder {
         List<Pair> answer = new ArrayList<Pair>();
         // Check all occurrences
         while (matcher.find()) {
+            // TODO: should likely be matcher.end() - 1
             answer.add(new Pair(matcher.start(), matcher.end()));
         }
         return answer;

http://git-wip-us.apache.org/repos/asf/camel/blob/3f8e0547/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawAndXPathTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawAndXPathTest.java b/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawAndXPathTest.java
new file mode 100644
index 0000000..59eb92f5
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawAndXPathTest.java
@@ -0,0 +1,44 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class ToDynamicRawAndXPathTest extends ContextTestSupport {
+
+    public void testToDynamicRawAndPlus() throws Exception {
+        getMockEndpoint("mock:RAW(se+ret)foo").expectedBodiesReceived("<order uri=\"foo\"/>");
+        getMockEndpoint("mock:RAW(se+ret)bar").expectedBodiesReceived("<order uri=\"bar\"/>");
+
+        template.sendBody("direct:start", "<order uri=\"foo\"/>");
+        template.sendBody("direct:start", "<order uri=\"bar\"/>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .toD("mock:RAW(se+ret)+language:xpath:/order/@uri");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/3f8e0547/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawTest.java b/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawTest.java
new file mode 100644
index 0000000..284cecd
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/ToDynamicRawTest.java
@@ -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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class ToDynamicRawTest extends ContextTestSupport {
+
+    public void testToDynamicRaw() throws Exception {
+        getMockEndpoint("mock:RAW(se+ret)").expectedBodiesReceived("Hello Camel");
+
+        template.sendBody("direct:start", "Hello Camel");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .toD("mock:RAW(se+ret)");
+            }
+        };
+    }
+}