sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r985108 - in /sling/trunk/bundles/jcr/resource/src: main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
Date Fri, 13 Aug 2010 06:49:54 GMT
Author: fmeschbe
Date: Fri Aug 13 06:49:54 2010
New Revision: 985108

URL: http://svn.apache.org/viewvc?rev=985108&view=rev
Log:
SLING-1332 Add support for multi-value sling:alias properties for resource resolution. Mapping
a resource will always use the first value of a multi-value sling:alias property.

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java?rev=985108&r1=985107&r2=985108&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
Fri Aug 13 06:49:54 2010
@@ -1103,12 +1103,16 @@ public class JcrResourceResolver
         Iterator<Resource> children = listChildren(parent);
         while (children.hasNext()) {
             child = children.next();
-            String alias = getProperty(child, PROP_ALIAS);
-            if (childName.equals(alias)) {
-                LOGGER.debug(
-                    "getChildInternal: Found Resource {} with alias {} to use",
-                    child, childName);
-                return child;
+            String[] aliases = getProperty(child, PROP_ALIAS, String[].class);
+            if (aliases != null) {
+                for (String alias : aliases) {
+                    if (childName.equals(alias)) {
+                        LOGGER.debug(
+                            "getChildInternal: Found Resource {} with alias {} to use",
+                            child, childName);
+                        return child;
+                    }
+                }
             }
         }
 
@@ -1135,11 +1139,16 @@ public class JcrResourceResolver
     }
 
     public String getProperty(Resource res, String propName) {
+        return getProperty(res, propName, String.class);
+    }
+
+    public <Type> Type getProperty(Resource res, String propName,
+            Class<Type> type) {
 
         // check the property in the resource itself
         ValueMap props = res.adaptTo(ValueMap.class);
         if (props != null) {
-            String prop = props.get(propName, String.class);
+            Type prop = props.get(propName, type);
             if (prop != null) {
                 LOGGER.debug("getProperty: Resource {} has property {}={}",
                     new Object[] { res, propName, prop });
@@ -1147,11 +1156,11 @@ public class JcrResourceResolver
             }
             // otherwise, check it in the jcr:content child resource
             // This is a special case checking for JCR based resources
-            // we directly use the deep resolving of properties of the
+            // we directly use the deep resolution of properties of the
             // JCR value map implementation - this does not work
-            // in none JCR environments, however in non JCR envs there
+            // in non JCR environments, however in non JCR envs there
             // is usually no "jcr:content" child node anyway
-            prop = props.get("jcr:content/" + propName, String.class);
+            prop = props.get("jcr:content/" + propName, type);
             return prop;
         }
 

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java?rev=985108&r1=985107&r2=985108&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
Fri Aug 13 06:49:54 2010
@@ -1740,6 +1740,85 @@ public class JcrResourceResolverTest ext
         assertEquals(grandchild.getPath(), resNodeEnkel.getPath());
     }
 
+    public void test_resolve_with_sling_alias_multi_value() throws Exception {
+
+        Node child = rootNode.addNode("child");
+        child.setProperty(JcrResourceResolver.PROP_ALIAS, new String[] {
+            "kind", "enfant" });
+        session.save();
+
+        // expect kind due to alias and no parent due to mapping
+        // the rootPath onto root
+        String path = "/kind";
+        String mapped = resResolver.map(child.getPath());
+        assertEquals(path, mapped);
+
+        Resource res = resResolver.resolve(null, path);
+        assertNotNull(res);
+        assertEquals(rootNode.getPath() + "/kind",
+            res.getResourceMetadata().getResolutionPath());
+        assertEquals("", res.getResourceMetadata().getResolutionPathInfo());
+
+        Node resNode = res.adaptTo(Node.class);
+        assertNotNull(resNode);
+
+        assertEquals(child.getPath(), resNode.getPath());
+
+        // expect enfant due to alias and no parent due to mapping
+        // the rootPath onto root
+        String pathEnfant = "/enfant";
+        String mappedEnfant = resResolver.map(child.getPath());
+        assertEquals(path, mappedEnfant); // map always selects first alias
+
+        Resource resEnfant = resResolver.resolve(null, pathEnfant);
+        assertNotNull(resEnfant);
+        assertEquals(rootNode.getPath() + "/enfant",
+            resEnfant.getResourceMetadata().getResolutionPath());
+        assertEquals("", resEnfant.getResourceMetadata().getResolutionPathInfo());
+
+        Node resNodeEnfant = resEnfant.adaptTo(Node.class);
+        assertNotNull(resNodeEnfant);
+
+        assertEquals(child.getPath(), resNodeEnfant.getPath());
+
+        // second level alias
+        Node grandchild = child.addNode("grandchild");
+        grandchild.setProperty(JcrResourceResolver.PROP_ALIAS, "enkel");
+        session.save();
+
+        // expect kind/enkel due to alias and no parent due to mapping
+        // the rootPath onto root
+        String pathEnkel = "/kind/enkel";
+        String mappedEnkel = resResolver.map(grandchild.getPath());
+        assertEquals(pathEnkel, mappedEnkel);
+
+        Resource resEnkel = resResolver.resolve(null, pathEnkel);
+        assertNotNull(resEnkel);
+        assertEquals(rootNode.getPath() + "/kind/enkel",
+            resEnkel.getResourceMetadata().getResolutionPath());
+        assertEquals("", resEnkel.getResourceMetadata().getResolutionPathInfo());
+
+        Node resNodeEnkel = resEnkel.adaptTo(Node.class);
+        assertNotNull(resNodeEnkel);
+        assertEquals(grandchild.getPath(), resNodeEnkel.getPath());
+
+        // expect kind/enkel due to alias and no parent due to mapping
+        // the rootPath onto root
+        String pathEnfantEnkel = "/enfant/enkel";
+        String mappedEnfantEnkel = resResolver.map(grandchild.getPath());
+        assertEquals(pathEnkel, mappedEnfantEnkel); // map always selects first alias
+
+        Resource resEnfantEnkel = resResolver.resolve(null, pathEnfantEnkel);
+        assertNotNull(resEnfantEnkel);
+        assertEquals(rootNode.getPath() + "/enfant/enkel",
+            resEnfantEnkel.getResourceMetadata().getResolutionPath());
+        assertEquals("", resEnfantEnkel.getResourceMetadata().getResolutionPathInfo());
+
+        Node resNodeEnfantEnkel = resEnfantEnkel.adaptTo(Node.class);
+        assertNotNull(resNodeEnfantEnkel);
+        assertEquals(grandchild.getPath(), resNodeEnfantEnkel.getPath());
+    }
+
     public void test_resolve_with_sling_alias_extension() throws Exception {
 
         final String selExt = ".html";



Mime
View raw message