sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-resourceresolver] 04/24: SLING-2739 : Add methods for handling the resource type hierarchy to the resource resolver
Date Tue, 07 Nov 2017 10:00:48 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.resourceresolver-1.0.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git

commit 002fba30b1a8a9e091abf4e2239e148ece567e2e
Author: Carsten Ziegeler <cziegeler@apache.org>
AuthorDate: Fri Feb 22 09:09:59 2013 +0000

    SLING-2739 :  Add methods for handling the resource type hierarchy to the resource resolver
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/resourceresolver@1448950
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |   2 +-
 .../impl/ResourceResolverImpl.java                 |  68 ++++++++++++
 .../impl/ResourceResolverImplTest.java             | 121 ++++++++++++++++++++-
 .../impl/tree/ResourceProviderEntryTest.java       |  15 +++
 4 files changed, 204 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index cbfe2b1..650e7b2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -114,7 +114,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.3.0</version>
+            <version>2.3.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
index 36dddd5..706b7ff 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
@@ -1083,4 +1083,72 @@ public class ResourceResolverImpl extends SlingAdaptable implements
ResourceReso
     public boolean hasChanges() {
         return this.context.hasChanges(this);
     }
+
+    /**
+     * @see org.apache.sling.api.resource.ResourceResolver#getResourceSuperType(org.apache.sling.api.resource.Resource)
+     */
+    public String getResourceSuperType(final Resource resource) {
+        String resourceSuperType = null;
+        if ( resource != null ) {
+            resourceSuperType = resource.getResourceSuperType();
+            if (resourceSuperType == null) {
+                resourceSuperType = this.getResourceSuperType(resource.getResourceType());
+            }
+        }
+        return resourceSuperType;
+    }
+
+    /**
+     * @see org.apache.sling.api.resource.ResourceResolver#getResourceSuperType(java.lang.String)
+     */
+    public String getResourceSuperType(final String resourceType) {
+        // normalize resource type to a path string
+        final String rtPath = (resourceType == null ? null : ResourceUtil.resourceTypeToPath(resourceType));
+        // get the resource type resource and check its super type
+        String resourceSuperType = null;
+
+        if ( rtPath != null ) {
+            ResourceResolver adminResolver = null;
+            try {
+                adminResolver = this.factory.getAdministrativeResourceResolver(null);
+                final Resource rtResource = adminResolver.getResource(rtPath);
+                if (rtResource != null) {
+                    resourceSuperType = rtResource.getResourceSuperType();
+                }
+            } catch (final LoginException e) {
+                // we simply ignore this and return null
+            } finally {
+                if ( adminResolver != null ) {
+                    adminResolver.close();
+                }
+            }
+        }
+        return resourceSuperType;
+    }
+
+    /**
+     * @see org.apache.sling.api.resource.ResourceResolver#isResourceType(org.apache.sling.api.resource.Resource,
java.lang.String)
+     */
+    public boolean isResourceType(final Resource resource, final String resourceType) {
+        boolean result = false;
+        if ( resource != null && resourceType != null ) {
+             // Check if the resource is of the given type. This method first checks the
+             // resource type of the resource, then its super resource type and continues
+             //  to go up the resource super type hierarchy.
+             if (resourceType.equals(resource.getResourceType())) {
+                 result = true;
+             } else {
+                 String superType = this.getResourceSuperType(resource);
+                 while (!result && superType != null) {
+                     if (resourceType.equals(superType)) {
+                         result = true;
+                     } else {
+                         superType = this.getResourceSuperType(superType);
+                     }
+                 }
+             }
+
+        }
+        return result;
+    }
 }
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
b/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
index 860e70f..73e56cd 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
@@ -27,10 +27,12 @@ import static org.junit.Assert.fail;
 
 import java.io.BufferedReader;
 import java.security.Principal;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
@@ -42,11 +44,13 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
 import org.apache.sling.api.resource.AbstractResource;
+import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.SyntheticResource;
 import org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext;
 import org.junit.Before;
 import org.junit.Test;
@@ -338,7 +342,98 @@ public class ResourceResolverImplTest {
         }
     }
 
-    private static final class ResourceImpl extends AbstractResource {
+    @Test public void test_getResourceSuperType() {
+        // the resource resolver
+        final List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
+        final PathBasedResourceResolverImpl resolver = new PathBasedResourceResolverImpl(
+                new ResourceResolverFactoryImpl(new ResourceResolverFactoryActivator()) {
+
+                    @Override
+                    public ResourceResolver getAdministrativeResourceResolver(
+                            Map<String, Object> authenticationInfo)
+                            throws LoginException {
+                        return resolvers.get(0);
+                    }
+
+                },
+                new ResourceResolverContext(false, null));
+        resolvers.add(resolver);
+
+        // the resource to test
+        final Resource r = new ResourceImpl() {
+            @Override
+            public String getResourceType() {
+                return "a:b";
+            }
+        };
+        final Resource r2 = new ResourceImpl() {
+            @Override
+            public String getResourceType() {
+                return "a:c";
+            }
+        };
+        final Resource typeResource = new ResourceImpl() {
+            @Override
+            public String getResourceType() {
+                return "x:y";
+            }
+
+            @Override
+            public String getResourceSuperType() {
+                return "t:c";
+            }
+        };
+        resolver.setResource("/a", r);
+        resolver.setResource("/a/b", typeResource);
+
+        assertEquals("t:c", resolver.getResourceSuperType(r.getResourceType()));
+        assertNull(resolver.getResourceSuperType(r2.getResourceType()));
+    }
+
+    @Test public void test_isA() {
+        final Resource typeResource = new ResourceImpl() {
+
+            @Override
+            public String getResourceType() {
+                return "x:y";
+            }
+
+            @Override
+            public String getResourceSuperType() {
+                return "t:c";
+            }
+        };
+        final List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
+        final PathBasedResourceResolverImpl resolver = new PathBasedResourceResolverImpl(
+                new ResourceResolverFactoryImpl(new ResourceResolverFactoryActivator()) {
+
+                    @Override
+                    public ResourceResolver getAdministrativeResourceResolver(
+                            Map<String, Object> authenticationInfo)
+                            throws LoginException {
+                        return resolvers.get(0);
+                    }
+
+                },
+                new ResourceResolverContext(false, null));
+        resolvers.add(resolver);
+        final Resource r = new SyntheticResource(resolver, "/a", "a:b") {
+            @Override
+            public String getResourceSuperType() {
+                return "d:e";
+            }
+        };
+        resolver.setResource("/a", r);
+        resolver.setResource("/d/e", typeResource);
+
+        assertTrue(resolver.isResourceType(r, "a:b"));
+        assertTrue(resolver.isResourceType(r, "d:e"));
+        assertFalse(resolver.isResourceType(r, "x:y"));
+        assertTrue(resolver.isResourceType(r, "t:c"));
+        assertFalse(resolver.isResourceType(r, "h:p"));
+    }
+
+    private static class ResourceImpl extends AbstractResource {
 
         public String getPath() {
             return "/some";
@@ -605,4 +700,28 @@ public class ResourceResolverImplTest {
             return false;
         }
     }
+    private static class PathBasedResourceResolverImpl extends ResourceResolverImpl {
+
+        private final Map<String, Resource> resources = new HashMap<String, Resource>();
+
+        public PathBasedResourceResolverImpl(
+                ResourceResolverFactoryImpl factory, ResourceResolverContext ctx) {
+            super(factory, ctx);
+        }
+
+        public void setResource(final String path, final Resource r) {
+            this.resources.put(path, r);
+        }
+
+        @Override
+        public String[] getSearchPath() {
+            return new String[] {""};
+        }
+
+        @Override
+        public Resource getResource(final String path) {
+            final String p = (path.startsWith("/") ? path : "/" + path);
+            return this.resources.get(p);
+        }
+    }
 }
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
b/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
index 0b60d20..c9970a3 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
@@ -359,6 +359,21 @@ public class ResourceProviderEntryTest {
             // TODO Auto-generated method stub
             return false;
         }
+
+        public String getResourceSuperType(Resource resource) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public String getResourceSuperType(String resourceType) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public boolean isResourceType(Resource resource, String resourceType) {
+            // TODO Auto-generated method stub
+            return false;
+        }
     }
 
     private static class TestResource extends AbstractResource {

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message