incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1384308 - in /sling/trunk/bundles/resourceresolver/src: main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
Date Thu, 13 Sep 2012 12:40:23 GMT
Author: fmeschbe
Date: Thu Sep 13 12:40:23 2012
New Revision: 1384308

URL: http://svn.apache.org/viewvc?rev=1384308&view=rev
Log:
SLING-2560 Support reverse mapping with regular expressions:
  - sling:match may be a replacement pattern (with $1 etc. references)
  - sling:internalRedirect may be a regular expression
  - sling:internalRedirect regaular expressions ignored for the resolution map

Modified:
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java?rev=1384308&r1=1384307&r2=1384308&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
(original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
Thu Sep 13 12:40:23 2012
@@ -18,8 +18,6 @@
  */
 package org.apache.sling.resourceresolver.impl.mapping;
 
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -35,7 +33,7 @@ import org.slf4j.LoggerFactory;
  * The <code>MapEntry</code> class represents a mapping entry in the mapping
  * configuration tree at <code>/etc/map</code>.
  * <p>
- * 
+ *
  * @see "http://cwiki.apache.org/SLING/flexible-resource-resolution.html"
  */
 public class MapEntry implements Comparable<MapEntry> {
@@ -72,7 +70,7 @@ public class MapEntry implements Compara
     /**
      * Returns a string used for matching map entries against the given request
      * or URI parts.
-     * 
+     *
      * @param scheme
      *            The URI scheme
      * @param host
@@ -110,16 +108,17 @@ public class MapEntry implements Compara
         return uriPath;
     }
 
-    public static URI toURI(final String uriPath) {
+    /**
+     * Converts the resolution path of the form http/host.77/the/path into an
+     * URI of the form http://host:77/the/path where any potential default port
+     * (80 for http and 443 for https) is actually removed. If the path is just
+     * a regular path such as /the/path, this method returns <code>null</code>.
+     */
+    public static String toURI(final String uriPath) {
         for (int i = 0; i < PATH_TO_URL_MATCH.length; i++) {
             final Matcher m = PATH_TO_URL_MATCH[i].matcher(uriPath);
             if (m.find()) {
-                final String newUriPath = m.replaceAll(PATH_TO_URL_REPLACEMENT[i]);
-                try {
-                    return new URI(newUriPath);
-                } catch (final URISyntaxException use) {
-                    // ignore, just don't return the uri as such
-                }
+                return m.replaceAll(PATH_TO_URL_REPLACEMENT[i]);
             }
         }
 
@@ -143,9 +142,9 @@ public class MapEntry implements Compara
                 return new MapEntry(url, status, trailingSlash, redirect);
             }
 
-            final String[] internalRedirect = props
-                            .get(ResourceResolverImpl.PROP_REDIRECT_INTERNAL,
-                                            String[].class);
+            final String[] internalRedirectProps = props.get(ResourceResolverImpl.PROP_REDIRECT_INTERNAL,
+                String[].class);
+            final String[] internalRedirect = filterRegExp(internalRedirectProps);
             if (internalRedirect != null) {
                 return new MapEntry(url, -1, trailingSlash, internalRedirect);
             }
@@ -196,12 +195,11 @@ public class MapEntry implements Compara
                                             String[].class);
             if (internalRedirect != null) {
 
+                // check whether the url is considered external or internal
                 int status = -1;
-                final URI extPathPrefix = toURI(url);
-                if (extPathPrefix != null) {
-                    url = getURI(extPathPrefix.getScheme(),
-                                    extPathPrefix.getHost(), extPathPrefix.getPort(),
-                                    extPathPrefix.getPath());
+                final String pathUri = toURI(url);
+                if (pathUri != null) {
+                    url = pathUri;
                     status = 302;
                 }
 
@@ -323,7 +321,7 @@ public class MapEntry implements Compara
     /**
      * Returns <code>true</code> if the string contains unescaped regular
      * expression special characters '+', '*', '?', '|', '(', '), '[', and ']'
-     * 
+     *
      * @param string
      * @return
      */
@@ -338,4 +336,26 @@ public class MapEntry implements Compara
         }
         return false;
     }
+
+    /**
+     * Returns an array of strings copied from the given strings where any
+     * regular expressions in the array are removed. If the input is
+     * <code>null</code> or no strings are provided <code>null</code>
is
+     * returned. <code>null</code> is also returned if the strings only contain
+     * regular expressions.
+     */
+    private static String[] filterRegExp(final String... strings) {
+        if (strings == null || strings.length == 0) {
+            return null;
+        }
+
+        ArrayList<String> list = new ArrayList<String>(strings.length);
+        for (String string : strings) {
+            if (!isRegExp(string)) {
+                list.add(string);
+            }
+        }
+
+        return list.isEmpty() ? null : (String[]) list.toArray(new String[list.size()]);
+    }
 }

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java?rev=1384308&r1=1384307&r2=1384308&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
(original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
Thu Sep 13 12:40:23 2012
@@ -23,8 +23,6 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.fail;
 
 import java.lang.reflect.Method;
-import java.net.URI;
-
 import junit.framework.TestCase;
 
 import org.apache.sling.resourceresolver.impl.mapping.MapEntry;
@@ -149,8 +147,39 @@ public class MapEntryTest {
         TestCase.assertTrue(isRegExp("http/[^.]+.www.example.com.8080/bla"));
     }
 
+    @Test public void test_filterRegExp() {
+        TestCase.assertNull(filterRegExp((String[]) null));
+        TestCase.assertNull(filterRegExp(new String[0]));
+
+        String aString = "plain";
+        String aString2 = "plain2";
+        String aPattern = "http/[^.]+.www.example.com.8080/bla";
+
+        TestCase.assertNull(filterRegExp(aPattern));
+
+        String[] res = filterRegExp(aString);
+        TestCase.assertNotNull(res);
+        TestCase.assertEquals(1, res.length);
+        TestCase.assertEquals(aString, res[0]);
+
+        res = filterRegExp(aString, aPattern);
+        TestCase.assertNotNull(res);
+        TestCase.assertEquals(1, res.length);
+        TestCase.assertEquals(aString, res[0]);
+
+        res = filterRegExp(aPattern, aString, aPattern);
+        TestCase.assertNotNull(res);
+        TestCase.assertEquals(1, res.length);
+        TestCase.assertEquals(aString, res[0]);
+
+        res = filterRegExp(aPattern, aString);
+        TestCase.assertNotNull(res);
+        TestCase.assertEquals(1, res.length);
+        TestCase.assertEquals(aString, res[0]);
+    }
+
     private void assertEqualUri(String expected, String uriPath) {
-        URI uri = MapEntry.toURI(uriPath);
+        String uri = MapEntry.toURI(uriPath);
         assertNotNull("Failed converting " + uriPath, uri);
         assertEquals(expected, uri.toString());
     }
@@ -171,4 +200,17 @@ public class MapEntryTest {
             return false; // quiesc compiler
         }
     }
+
+    private String[] filterRegExp(final String... strings) {
+        try {
+            Method m = MapEntry.class.getDeclaredMethod("filterRegExp", String[].class);
+            m.setAccessible(true);
+            return (String[]) m.invoke(null, new Object[] {
+                strings
+            });
+        } catch (Exception e) {
+            fail(e.toString());
+            return null; // quiesc compiler
+        }
+    }
 }



Mime
View raw message