incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1337417 - in /sling/trunk/bundles/jcr/resource/src: main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
Date Fri, 11 May 2012 23:47:43 GMT
Author: fmeschbe
Date: Fri May 11 23:47:43 2012
New Revision: 1337417

URL: http://svn.apache.org/viewvc?rev=1337417&view=rev
Log:
SLING-2475 Don't create mapping entry for url which is a regular expression. Log an INFO message
if a mapping entry is not created for an external redirect or regular expression url.

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java?rev=1337417&r1=1337416&r2=1337417&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
Fri May 11 23:47:43 2012
@@ -29,6 +29,7 @@ import java.util.regex.Pattern;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.jcr.resource.internal.JcrResourceResolver;
+import org.slf4j.LoggerFactory;
 
 /**
  * The <code>MapEntry</code> class represents a mapping entry in the mapping
@@ -155,6 +156,18 @@ public class MapEntry implements Compara
                 JcrResourceResolver.PROP_REDIRECT_EXTERNAL, String.class);
             if (redirect != null) {
                 // ignoring external redirects for mapping
+                LoggerFactory.getLogger(MapEntry.class).info(
+                    "createMapEntry: Configuration has external redirect to {}; not creating
mapping for configuration in {}",
+                    redirect, resource.getPath());
+                return null;
+            }
+
+            // ignore potential regular expression url
+            if (isRegExp(url)) {
+                LoggerFactory.getLogger(MapEntry.class).info(
+                    "createMapEntry: URL {} contains a regular expression; not creating mapping
for configuration in {}",
+                    url, resource.getPath());
+
                 return null;
             }
 
@@ -295,4 +308,24 @@ public class MapEntry implements Compara
         }
         return buf.toString();
     }
+
+    //---------- helper
+
+    /**
+     * Returns <code>true</code> if the string contains unescaped regular
+     * expression special characters '+', '*', '?', '|', '(', '), '[', and ']'
+     * @param string
+     * @return
+     */
+    private static boolean isRegExp(final String string) {
+        for (int i=0; i < string.length(); i++) {
+            char c = string.charAt(i);
+            if (c == '\\') {
+                i++; // just skip
+            } else if ("+*?|()[]".indexOf(c) >= 0) {
+                return true; // assume an unescaped pattern character
+            }
+        }
+        return false;
+    }
 }

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java?rev=1337417&r1=1337416&r2=1337417&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
Fri May 11 23:47:43 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.jcr.resource.internal.helper;
 
+import java.lang.reflect.Method;
 import java.net.URI;
 
 import junit.framework.TestCase;
@@ -133,6 +134,14 @@ public class MapEntryTest extends TestCa
 
     }
 
+    public void test_isRegExp() {
+        TestCase.assertFalse(isRegExp("http/www.example.com.8080/bla"));
+        TestCase.assertTrue(isRegExp("http/.+\\.www.example.com.8080/bla"));
+        TestCase.assertTrue(isRegExp("http/(.+)\\.www.example.com.8080/bla"));
+        TestCase.assertTrue(isRegExp("http/(.+)\\.www.example.com.8080/bla"));
+        TestCase.assertTrue(isRegExp("http/[^.]+.www.example.com.8080/bla"));
+    }
+
     private void assertEqualUri(String expected, String uriPath) {
         URI uri = MapEntry.toURI(uriPath);
         assertNotNull("Failed converting " + uriPath, uri);
@@ -144,4 +153,15 @@ public class MapEntryTest extends TestCa
         assertNotNull(fixed);
         assertEquals(expected, fixed);
     }
+
+    private boolean isRegExp(final String string) {
+        try {
+            Method m = MapEntry.class.getDeclaredMethod("isRegExp", String.class);
+            m.setAccessible(true);
+            return (Boolean) m.invoke(null, string);
+        } catch (Exception e) {
+            fail(e.toString());
+            return false; // quiesc compiler
+        }
+    }
 }



Mime
View raw message