SLING-6327 ResourceResolverImpl.isResourceType() should compare relative resource types (and ignore any search path prefixes)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1771302 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/api/resource/Resource.java b/src/main/java/org/apache/sling/api/resource/Resource.java
index c3809ed..75efa18 100644
--- a/src/main/java/org/apache/sling/api/resource/Resource.java
+++ b/src/main/java/org/apache/sling/api/resource/Resource.java
@@ -179,16 +179,8 @@
boolean hasChildren();
/**
- * Returns <code>true</code> if the resource type or any of the resource's
- * super type(s) equals the given resource type.
- *
- * @param resourceType The resource type to check this resource against.
- * @return <code>true</code> if the resource type or any of the resource's
- * super type(s) equals the given resource type. <code>false</code>
- * is also returned if <code>resourceType</code> is
- * <code>null</code>.
- * @throws IllegalStateException if this resource resolver has already been
- * {@link ResourceResolver#close() closed}.
+ * Is just a shortcut for {@code getResourceResolver().isResourceType(this, resourceType)}.
+ * @see ResourceResolver#isResourceType(Resource, String)
* @since 2.1.0 (Sling API Bundle 2.1.0)
*/
boolean isResourceType(String resourceType);
diff --git a/src/main/java/org/apache/sling/api/resource/ResourceResolver.java b/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
index 98a7064..50a19a7 100644
--- a/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
+++ b/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
@@ -698,6 +698,11 @@
/**
* Returns <code>true</code> if the resource type or any of the resource's
* super type(s) equals the given resource type.
+ *
+ * In case the type of the given resource or the given resource type is absolute (i.e. start with a "/")
+ * and starts with one of the resource resolver's search paths
+ * it is converted to relative resource types by stripping off the resource resolver's search path
+ * before doing the comparison.
*
* @param resource The resource to check
* @param resourceType The resource type to check this resource against.
diff --git a/src/main/java/org/apache/sling/api/resource/ResourceUtil.java b/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
index 81e0da6..e3fdb4f 100644
--- a/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
+++ b/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
@@ -475,6 +475,41 @@
}
/**
+ * Returns <code>true</code> if the given resource type are equal.
+ *
+ * In case the value of any of the given resource types
+ * starts with one of the resource resolver's search paths
+ * it is converted to a relative resource type by stripping off
+ * the resource resolver's search path before doing the comparison.
+ *
+ * @param resourceType A resource type
+ * @param anotherResourceType Another resource type to compare with {@link resourceType}.
+ * @return <code>true</code> if the resource type equals the given resource type.
+ * @since 2.10.0
+ */
+ public static boolean areResourceTypesEqual(@Nonnull String resourceType, @Nonnull String anotherResourceType, @Nonnull String[] searchPath) {
+ return relativizeResourceType(resourceType, searchPath).equals(relativizeResourceType(anotherResourceType, searchPath));
+ }
+
+ /**
+ * Makes the given resource type relative by stripping off any prefix which equals one of the given search paths.
+ * In case the given resource type does not start with any of the given search paths it is returned unmodified.
+ * @param resourceType the resourceType to relativize.
+ * @param searchPath the search paths to strip off from the given resource type.
+ * @return the relative resource type
+ */
+ public static String relativizeResourceType(@Nonnull String resourceType, @Nonnull String[] searchPath) {
+ if (resourceType.startsWith("/")) {
+ for (String prefix : searchPath) {
+ if (resourceType.startsWith(prefix)) {
+ return resourceType.substring(prefix.length());
+ }
+ }
+ }
+ return resourceType;
+ }
+
+ /**
* Return an iterator for objects of the specified type. A new iterator is
* returned which tries to adapt the provided resources to the given type
* (using {@link Resource#adaptTo(Class)}. If a resource in the original
diff --git a/src/main/java/org/apache/sling/api/resource/package-info.java b/src/main/java/org/apache/sling/api/resource/package-info.java
index 0c4fc5a..83de2d9 100644
--- a/src/main/java/org/apache/sling/api/resource/package-info.java
+++ b/src/main/java/org/apache/sling/api/resource/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("2.9.2")
+@Version("2.10.0")
package org.apache.sling.api.resource;
import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java b/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
index 99526ce..e5fdf83 100644
--- a/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
+++ b/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
@@ -416,4 +416,21 @@
@Test public void testFindResourceSuperType() {
assertNull(ResourceUtil.findResourceSuperType(null));
}
+
+ @Test public void testAreResourceTypesEqual() {
+ assertTrue(ResourceUtil.areResourceTypesEqual("some/type", "/apps/some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceUtil.areResourceTypesEqual("/apps/some/type", "some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceUtil.areResourceTypesEqual("/apps/some/type", "/apps/some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceUtil.areResourceTypesEqual("some/type", "some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceUtil.areResourceTypesEqual("/apps/some/type", "/libs/some/type", new String[]{"/apps/", "/libs/"}));
+ assertFalse(ResourceUtil.areResourceTypesEqual("/apps/some/type", "/libs/some/type", new String[]{}));
+ }
+
+ @Test public void testRelativizeResourceType() {
+ assertEquals("relative/type", ResourceUtil.relativizeResourceType("relative/type", new String[]{"/apps/", "/libs/"}));
+ assertEquals("relative/type", ResourceUtil.relativizeResourceType("/apps/relative/type", new String[]{"/apps/", "/libs/"}));
+ assertEquals("relative/type", ResourceUtil.relativizeResourceType("/libs/relative/type", new String[]{"/apps/", "/libs/"}));
+ assertEquals("", ResourceUtil.relativizeResourceType("/apps/", new String[]{"/apps/", "/libs/"}));
+ assertEquals("/some/prefix/type", ResourceUtil.relativizeResourceType("/some/prefix/type", new String[]{"/apps/", "/libs/"}));
+ }
}