camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/4] camel git commit: Camel catalog - Validate uris with time pattern is now supported
Date Sun, 03 Jan 2016 11:15:34 GMT
Camel catalog - Validate uris with time pattern is now supported


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

Branch: refs/heads/master
Commit: ec91dc5afaffbfbdebabbeabf5fb0f3d4230571f
Parents: 5633d3f
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Jan 3 12:13:53 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Jan 3 12:13:53 2016 +0100

----------------------------------------------------------------------
 .../camel/catalog/DefaultCamelCatalog.java      | 26 ++++--
 .../camel/catalog/TimePatternConverter.java     | 93 ++++++++++++++++++++
 .../apache/camel/catalog/CamelCatalogTest.java  |  4 +
 3 files changed, 117 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ec91dc5a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
index fc63bd0..c4322f0 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
@@ -817,12 +817,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
                 // is integer
                 if (!placeholder && isPropertyInteger(rows, name)) {
                     // value must be an integer
-                    boolean valid = false;
-                    try {
-                        valid = Integer.valueOf(value) != null;
-                    } catch (Exception e) {
-                        // ignore
-                    }
+                    boolean valid = validateInteger(value);
                     if (!valid) {
                         result.addInvalidInteger(name, value);
                     }
@@ -862,6 +857,25 @@ public class DefaultCamelCatalog implements CamelCatalog {
         return result;
     }
 
+    private static boolean validateInteger(String value) {
+        boolean valid = false;
+        try {
+            valid = Integer.valueOf(value) != null;
+        } catch (Exception e) {
+            // ignore
+        }
+        if (!valid) {
+            // it may be a time pattern, such as 5s for 5 seconds = 5000
+            try {
+                TimePatternConverter.toMilliSeconds(value);
+                valid = true;
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+        return valid;
+    }
+
     @Override
     public Map<String, String> endpointProperties(String uri) throws URISyntaxException
{
         // NOTICE: This logic is similar to org.apache.camel.util.EndpointHelper#endpointProperties

http://git-wip-us.apache.org/repos/asf/camel/blob/ec91dc5a/platforms/catalog/src/main/java/org/apache/camel/catalog/TimePatternConverter.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/TimePatternConverter.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/TimePatternConverter.java
new file mode 100644
index 0000000..e49ee89
--- /dev/null
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/TimePatternConverter.java
@@ -0,0 +1,93 @@
+/**
+ * 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.catalog;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * This class is a copy from camel-core so we can use it independent to validate uris with
time patterns
+ */
+public final class TimePatternConverter {
+
+    private static final String NUMBERS_ONLY_STRING_PATTERN = "^[-]?(\\d)+$";
+    private static final String REPLACEMENT_PATTERN = "[our|inute|econd](s)?";
+    private static final String HOUR_REGEX_PATTERN = "((\\d)*(\\d))[h|H]";
+    private static final String MINUTES_REGEX_PATTERN = "((\\d)*(\\d))[m|M]";
+    private static final String SECONDS_REGEX_PATTERN = "((\\d)*(\\d))[s|S]";
+
+    /**
+     * Utility classes should not have a public constructor.
+     */
+    private TimePatternConverter() {
+    }
+
+    public static long toMilliSeconds(String source) throws IllegalArgumentException {
+        long milliseconds = 0;
+        boolean foundFlag = false;
+        Matcher matcher;
+
+        matcher = createMatcher(NUMBERS_ONLY_STRING_PATTERN, source);
+        if (matcher.find()) {
+            // Note: This will also be used for regular numeric strings.
+            //       This String -> long converter will be used for all strings.
+            milliseconds = Long.valueOf(source);
+        } else {
+            matcher = createMatcher(REPLACEMENT_PATTERN, source);
+            String replacedSource = matcher.replaceAll("");
+
+            matcher = createMatcher(HOUR_REGEX_PATTERN, replacedSource);
+            if (matcher.find()) {
+                milliseconds = milliseconds + (3600000 * Long.valueOf(matcher.group(1)));
+                foundFlag = true;
+            }
+
+            matcher = createMatcher(MINUTES_REGEX_PATTERN, replacedSource);
+            if (matcher.find()) {
+                long minutes = Long.valueOf(matcher.group(1));
+                if ((minutes > 59) && foundFlag) {
+                    throw new IllegalArgumentException("Minutes should contain a valid value
between 0 and 59: " + source);
+                }
+                foundFlag = true;
+                milliseconds = milliseconds + (60000 * minutes);
+            }
+
+            matcher = createMatcher(SECONDS_REGEX_PATTERN, replacedSource);
+            if (matcher.find()) {
+                long seconds = Long.valueOf(matcher.group(1));
+                if ((seconds > 59) && foundFlag) {
+                    throw new IllegalArgumentException("Seconds should contain a valid value
between 0 and 59: " + source);
+                }
+                foundFlag = true;
+                milliseconds = milliseconds + (1000 * seconds);
+            }
+
+            // No pattern matched... initiating fallback check and conversion (if required).
+            // The source at this point may contain illegal values or special characters
+            if (!foundFlag) {
+                milliseconds = Long.valueOf(source);
+            }
+        }
+
+        return milliseconds;
+    }
+
+    private static Matcher createMatcher(String regexPattern, String source) {
+        Pattern pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
+        return pattern.matcher(source);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ec91dc5a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index b58ec15..4387374 100644
--- a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++ b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -460,6 +460,10 @@ public class CamelCatalogTest {
         // dataset
         result = catalog.validateEndpointProperties("dataset:foo?minRate=50");
         assertTrue(result.isSuccess());
+
+        // time pattern
+        result = catalog.validateEndpointProperties("timer://foo?fixedRate=true&delay=0&period=2s");
+        assertTrue(result.isSuccess());
     }
 
     @Test


Mime
View raw message