You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:00:07 UTC

[sling-org-apache-sling-resourceresolver] 37/47: SLING-2626 : NPE in ResourceResolverImpl.getResource. Apply slightly modifed version of patch from Radu Cotescu

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.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git

commit 607628347573379605118b760fb52b2d17acf207
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Oct 22 12:20:23 2012 +0000

    SLING-2626 :  NPE in ResourceResolverImpl.getResource. Apply slightly modifed version of patch from Radu Cotescu
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/resourceresolver@1400860 13f79535-47bb-0310-9956-ffa450edef68
---
 .../impl/ResourceResolverImpl.java                 | 43 +++++++++++-----------
 .../impl/ResourceResolverImplTest.java             | 35 +++++++++++-------
 2 files changed, 44 insertions(+), 34 deletions(-)

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 20a87b2..9eddf6b 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
@@ -563,31 +563,32 @@ public class ResourceResolverImpl extends SlingAdaptable implements ResourceReso
     public Resource getResource(String path) {
         checkClosed();
 
-        // if the path is absolute, normalize . and .. segements and get res
-        if (path.startsWith("/")) {
-            path = ResourceUtil.normalize(path);
-            Resource result = (path != null) ? getResourceInternal(path) : null;
-            if (result != null) {
-                result = this.factory.getResourceDecoratorTracker().decorate(result);
-                return result;
-            }
-            return null;
-        }
+        Resource result = null;
+        if ( path != null ) {
+            // if the path is absolute, normalize . and .. segments and get res
+            if (path.startsWith("/")) {
+                path = ResourceUtil.normalize(path);
+                result = (path != null) ? getResourceInternal(path) : null;
+                if (result != null) {
+                    result = this.factory.getResourceDecoratorTracker().decorate(result);
+                }
+            } else {
 
-        // otherwise we have to apply the search path
-        // (don't use this.getSearchPath() to save a few cycle for not cloning)
-        final String[] paths = factory.getSearchPath();
-        if (paths != null) {
-            for (final String prefix : factory.getSearchPath()) {
-                final Resource res = getResource(prefix + path);
-                if (res != null) {
-                    return res;
+                // otherwise we have to apply the search path
+                // (don't use this.getSearchPath() to save a few cycle for not cloning)
+                final String[] paths = factory.getSearchPath();
+                if (paths != null) {
+                    for (final String prefix : factory.getSearchPath()) {
+                        result = getResource(prefix + path);
+                        if (result != null) {
+                            break;
+                        }
+                    }
                 }
             }
         }
 
-        // no resource found, if we get here
-        return null;
+        return result;
     }
 
     /**
@@ -597,7 +598,7 @@ public class ResourceResolverImpl extends SlingAdaptable implements ResourceReso
     public Resource getResource(final Resource base, String path) {
         checkClosed();
 
-        if (!path.startsWith("/") && base != null) {
+        if (path != null && !path.startsWith("/") && base != null) {
             path = base.getPath() + "/" + path;
         }
 
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 3b2f9a9..860e70f 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
@@ -176,26 +176,26 @@ public class ResourceResolverImplTest {
         final Resource res00 = resResolver.resolve((String) null);
         assertNotNull(res00);
         assertTrue("Resource must be NonExistingResource",
-                        res00 instanceof NonExistingResource);
+                res00 instanceof NonExistingResource);
         assertEquals("Null path is expected to return root", "/",
-            res00.getPath());
+                res00.getPath());
 
         // relative paths are treated as if absolute
         final String path01 = "relPath/relPath";
         final Resource res01 = resResolver.resolve(path01);
         assertNotNull(res01);
         assertEquals("Expecting absolute path for relative path", "/" + path01,
-            res01.getPath());
+                res01.getPath());
         assertTrue("Resource must be NonExistingResource",
-            res01 instanceof NonExistingResource);
+                res01 instanceof NonExistingResource);
 
         final String no_resource_path = "/no_resource/at/this/location";
         final Resource res02 = resResolver.resolve(no_resource_path);
         assertNotNull(res02);
         assertEquals("Expecting absolute path for relative path",
-            no_resource_path, res02.getPath());
+                no_resource_path, res02.getPath());
         assertTrue("Resource must be NonExistingResource",
-            res01 instanceof NonExistingResource);
+                res01 instanceof NonExistingResource);
 
         try {
             resResolver.resolve((HttpServletRequest) null);
@@ -207,25 +207,34 @@ public class ResourceResolverImplTest {
         final Resource res0 = resResolver.resolve(null, no_resource_path);
         assertNotNull("Expecting resource if resolution fails", res0);
         assertTrue("Resource must be NonExistingResource",
-            res0 instanceof NonExistingResource);
+                res0 instanceof NonExistingResource);
         assertEquals("Path must be the original path", no_resource_path,
-            res0.getPath());
+                res0.getPath());
 
         final HttpServletRequest req1 = new ResourceResolverTestRequest(
-            no_resource_path);
+                no_resource_path);
         final Resource res1 = resResolver.resolve(req1);
         assertNotNull("Expecting resource if resolution fails", res1);
         assertTrue("Resource must be NonExistingResource",
-            res1 instanceof NonExistingResource);
+                res1 instanceof NonExistingResource);
         assertEquals("Path must be the original path", no_resource_path,
-            res1.getPath());
+                res1.getPath());
 
         final HttpServletRequest req2 = new ResourceResolverTestRequest(null);
         final Resource res2 = resResolver.resolve(req2);
         assertNotNull("Expecting resource if resolution fails", res2);
         assertTrue("Resource must be NonExistingResource",
-            res2 instanceof NonExistingResource);
+                res2 instanceof NonExistingResource);
         assertEquals("Path must be the the root path", "/", res2.getPath());
+
+        final Resource res3 = resResolver.getResource(null);
+        assertNull("Expected null resource for null path", res3);
+
+        final Resource res4 = resResolver.getResource(null, null);
+        assertNull("Expected null resource for null path", res4);
+
+        final Resource res5 = resResolver.getResource(res01, null);
+        assertNull("Expected null resource for null path", res5);
     }
 
     @Test public void test_clone_based_on_anonymous() throws Exception {
@@ -297,7 +306,7 @@ public class ResourceResolverImplTest {
         assertTrue(validNames.remove(names.next()));
         assertFalse("Expect no more names", names.hasNext());
         assertTrue("Expect validNames set to be empty now",
-            validNames.isEmpty());
+                validNames.isEmpty());
 
         rr.close();
     }

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