You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2016/11/28 08:47:27 UTC
svn commit: r1771688 - in /sling/trunk/bundles:
api/src/main/java/org/apache/sling/api/resource/
api/src/test/java/org/apache/sling/api/resource/
resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/
resourceresolver/src/test/java/org/...
Author: kwin
Date: Mon Nov 28 08:47:26 2016
New Revision: 1771688
URL: http://svn.apache.org/viewvc?rev=1771688&view=rev
Log:
SLING-6327 move the resource type util methods to a private Utils class (and removed it from Sling API)
further clarifications on the javadoc
Added:
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtil.java (with props)
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtilTest.java (with props)
Modified:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java?rev=1771688&r1=1771687&r2=1771688&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java Mon Nov 28 08:47:26 2016
@@ -699,9 +699,8 @@ public interface ResourceResolver extend
* 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
+ * In case the type of the given resource or the given resource type 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 resource The resource to check
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=1771688&r1=1771687&r2=1771688&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java Mon Nov 28 08:47:26 2016
@@ -455,6 +455,10 @@ public class ResourceUtil {
* resource type of the resource, then its super resource type and continues
* to go up the resource super type hierarchy.
*
+ * In case the type of the given resource or the given resource type 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 resource the resource to check
* @param resourceType the resource type to check the resource against
* @return <code>false</code> if <code>resource</code> is <code>null</code>.
@@ -475,41 +479,6 @@ public class ResourceUtil {
}
/**
- * 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
Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=1771688&r1=1771687&r2=1771688&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java (original)
+++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java Mon Nov 28 08:47:26 2016
@@ -416,21 +416,4 @@ public class ResourceUtilTest {
@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/"}));
- }
}
Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java?rev=1771688&r1=1771687&r2=1771688&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java Mon Nov 28 08:47:26 2016
@@ -1311,13 +1311,13 @@ public class ResourceResolverImpl extend
// 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 (ResourceUtil.areResourceTypesEqual(resourceType, resource.getResourceType(), getSearchPath())) {
+ if (ResourceTypeUtil.areResourceTypesEqual(resourceType, resource.getResourceType(), getSearchPath())) {
result = true;
} else {
Set<String> superTypesChecked = new HashSet<String>();
String superType = this.getParentResourceType(resource);
while (!result && superType != null) {
- if (ResourceUtil.areResourceTypesEqual(resourceType, superType, getSearchPath())) {
+ if (ResourceTypeUtil.areResourceTypesEqual(resourceType, superType, getSearchPath())) {
result = true;
} else {
superTypesChecked.add(superType);
Added: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtil.java?rev=1771688&view=auto
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtil.java (added)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtil.java Mon Nov 28 08:47:26 2016
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.resourceresolver.impl;
+
+import javax.annotation.Nonnull;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+
+/**
+ * Some helper methods for doing comparisons on resource types.
+ * This class is private the resource resolver bundle.
+ * Consumers should rely on {@link Resource#isResourceType(String)} or {@link ResourceResolver#isResourceType(Resource, String)} instead.
+ */
+public class ResourceTypeUtil {
+
+ /**
+ * 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.
+ */
+ 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;
+ }
+
+}
Propchange: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtil.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtilTest.java?rev=1771688&view=auto
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtilTest.java (added)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtilTest.java Mon Nov 28 08:47:26 2016
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.resourceresolver.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class ResourceTypeUtilTest {
+
+ @Test public void testAreResourceTypesEqual() {
+ assertTrue(ResourceTypeUtil.areResourceTypesEqual("some/type", "/apps/some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceTypeUtil.areResourceTypesEqual("/apps/some/type", "some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceTypeUtil.areResourceTypesEqual("/apps/some/type", "/apps/some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceTypeUtil.areResourceTypesEqual("some/type", "some/type", new String[]{"/apps/", "/libs/"}));
+ assertTrue(ResourceTypeUtil.areResourceTypesEqual("/apps/some/type", "/libs/some/type", new String[]{"/apps/", "/libs/"}));
+ assertFalse(ResourceTypeUtil.areResourceTypesEqual("/apps/some/type", "/libs/some/type", new String[]{}));
+ }
+
+ @Test public void testRelativizeResourceType() {
+ assertEquals("relative/type", ResourceTypeUtil.relativizeResourceType("relative/type", new String[]{"/apps/", "/libs/"}));
+ assertEquals("relative/type", ResourceTypeUtil.relativizeResourceType("/apps/relative/type", new String[]{"/apps/", "/libs/"}));
+ assertEquals("relative/type", ResourceTypeUtil.relativizeResourceType("/libs/relative/type", new String[]{"/apps/", "/libs/"}));
+ assertEquals("", ResourceTypeUtil.relativizeResourceType("/apps/", new String[]{"/apps/", "/libs/"}));
+ assertEquals("/some/prefix/type", ResourceTypeUtil.relativizeResourceType("/some/prefix/type", new String[]{"/apps/", "/libs/"}));
+ }
+}
Propchange: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceTypeUtilTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain