You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2012/07/18 14:28:47 UTC

svn commit: r1362909 - in /sling/trunk/bundles: api/src/main/java/org/apache/sling/api/resource/ servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/

Author: cziegeler
Date: Wed Jul 18 12:28:46 2012
New Revision: 1362909

URL: http://svn.apache.org/viewvc?rev=1362909&view=rev
Log:
SLING-2457 : ResourceUtil.isA() fails if resource has a type, whose super type is not readable

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java?rev=1362909&r1=1362908&r2=1362909&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java Wed Jul 18 12:28:46 2012
@@ -112,6 +112,6 @@ public abstract class AbstractResource
         // the implementation) to prevent problems if there are implementations
         // of the pre-2.1.0 Resource interface in the framework.
         //
-        return ResourceUtil.isA(this, resourceType);
+        return ResourceUtil.internalIsA(this, resourceType);
     }
 }

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=1362909&r1=1362908&r2=1362909&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 Wed Jul 18 12:28:46 2012
@@ -447,11 +447,32 @@ public class ResourceUtil {
      *         <code>resourceType</code>.
      * @since 2.0.6
      */
-    public static boolean isA(final Resource resource, String resourceType) {
+    public static boolean isA(final Resource resource, final String resourceType) {
         if (resource == null || resourceType == null) {
             return false;
         }
 
+        try {
+            return resource.isResourceType(resourceType);
+        } catch (final AbstractMethodError ame) {
+            // this might occur with old pre 2.1.0 resource implementations (see SLING-2457)
+            return internalIsA(resource, resourceType);
+        }
+    }
+
+    /**
+     * 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.
+     *
+     * @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>.
+     *         Otherwise returns the result of calling
+     *         {@link Resource#isResourceType(String)} with the given
+     *         <code>resourceType</code>.
+     */
+    static boolean internalIsA(final Resource resource, final String resourceType) {
         if (resourceType.equals(resource.getResourceType())) {
             return true;
         }

Modified: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java?rev=1362909&r1=1362908&r2=1362909&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java (original)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java Wed Jul 18 12:28:46 2012
@@ -60,9 +60,12 @@ import org.apache.sling.api.request.Requ
 import org.apache.sling.api.request.RequestUtil;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceDecorator;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.ResourceWrapper;
 import org.apache.sling.api.resource.SyntheticResource;
 import org.apache.sling.api.scripting.SlingScript;
 import org.apache.sling.api.scripting.SlingScriptResolver;
@@ -97,10 +100,11 @@ import org.slf4j.LoggerFactory;
  */
 @Component(name="org.apache.sling.servlets.resolver.SlingServletResolver", metatype=true,
            label="%servletresolver.name", description="%servletresolver.description")
-@Service(value={ServletResolver.class, SlingScriptResolver.class, ErrorHandler.class})
+@Service(value={ServletResolver.class, SlingScriptResolver.class, ErrorHandler.class, ResourceDecorator.class})
 @Properties({
     @Property(name="service.description", value="Sling Servlet Resolver and Error Handler"),
     @Property(name="service.vendor", value="The Apache Software Foundation"),
+    @Property(name="service.ranking", intValue=10000), // for the resource decorator
     @Property(name="event.topics", propertyPrivate=true,
          value={"org/apache/sling/api/resource/Resource/*",
                     "org/apache/sling/api/resource/ResourceProvider/*",
@@ -109,7 +113,12 @@ import org.slf4j.LoggerFactory;
 })
 @Reference(name="Servlet", referenceInterface=javax.servlet.Servlet.class,
            cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
-public class SlingServletResolver implements ServletResolver, SlingScriptResolver, ErrorHandler, EventHandler {
+public class SlingServletResolver
+    implements ServletResolver,
+               SlingScriptResolver,
+               ErrorHandler,
+               EventHandler,
+               ResourceDecorator {
 
     /**
      * The default servlet root is the first search path (which is usally /apps)
@@ -1106,4 +1115,28 @@ public class SlingServletResolver implem
             this.registration = sr;
         }
     }
+
+    /**
+     * @see org.apache.sling.api.resource.ResourceDecorator#decorate(org.apache.sling.api.resource.Resource)
+     */
+    public Resource decorate(final Resource resource) {
+        return new ResourceWrapper(resource) {
+            public boolean isResourceType(final String type) {
+                return ResourceUtil.isA(new ResourceWrapper(getResource()) {
+                    public ResourceResolver getResourceResolver() {
+                        return scriptResolver;
+                    }
+                }, type);
+            }
+        };
+     }
+
+    /**
+     * @see org.apache.sling.api.resource.ResourceDecorator#decorate(org.apache.sling.api.resource.Resource, javax.servlet.http.HttpServletRequest)
+     */
+    @SuppressWarnings("javadoc")
+    public Resource decorate(final Resource resource, final HttpServletRequest request) {
+        // this is deprecated, but we just delegate anyway
+        return this.decorate(resource);
+    }
 }