Author: sseifert Date: Thu Sep 10 10:08:53 2015 New Revision: 1702206 URL: http://svn.apache.org/r1702206 Log: SLING-5012 Sling Rewriter: Check unwrapped resources as well for resource type matching
Modified: sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java Modified: sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java?rev=1702206&r1=1702205&r2=1702206&view=diff ============================================================================== --- sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java (original) +++ sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java Thu Sep 10 10:08:53 2015 @@ -23,7 +23,9 @@ import java.util.Map; import java.util.Set; import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceUtil; +import org.apache.sling.api.resource.ResourceWrapper; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.rewriter.PipelineConfiguration; import org.apache.sling.rewriter.ProcessingComponentConfiguration; @@ -49,6 +51,8 @@ public class ProcessorConfigurationImpl static final String PROPERTY_RESOURCE_TYPES = "resourceTypes"; + static final String PROPERTY_UNWRAP_RESOURCES = "unwrapResources"; + static final String PROPERTY_SELECTORS = "selectors"; static final String PROPERTY_TRANFORMERS = "transformerTypes"; @@ -74,6 +78,9 @@ public class ProcessorConfigurationImpl /** For which resource types should this processor be applied. */ private final String[] resourceTypes; + /** Whether unwrapped resources should be validated as well when checking for resource types. */ + private final boolean unwrapResources; + /** For which selectors should this processor be applied. */ private final String[] selectors; @@ -112,6 +119,7 @@ public class ProcessorConfigurationImpl String[] paths, String[] extensions, String[] resourceTypes, + boolean unwrapResources, String[] selectors, int order, ProcessingComponentConfiguration generatorConfig, @@ -120,6 +128,7 @@ public class ProcessorConfigurationImpl boolean processErrorResponse) { this.contentTypes = contentTypes; this.resourceTypes = resourceTypes; + this.unwrapResources = unwrapResources; this.selectors = selectors; this.paths = paths; this.extensions = extensions; @@ -143,7 +152,7 @@ public class ProcessorConfigurationImpl String[] extensions, String[] resourceTypes, String[] selectors) { - this(contentTypes, paths, extensions, resourceTypes, selectors, 0, null, null, null, false); + this(contentTypes, paths, extensions, resourceTypes, false, selectors, 0, null, null, null, false); } /** @@ -154,6 +163,7 @@ public class ProcessorConfigurationImpl final ValueMap properties = ResourceUtil.getValueMap(resource); this.contentTypes = properties.get(PROPERTY_CONTENT_TYPES, String[].class); this.resourceTypes = properties.get(PROPERTY_RESOURCE_TYPES, String[].class); + this.unwrapResources = properties.get(PROPERTY_UNWRAP_RESOURCES, false); this.selectors = properties.get(PROPERTY_SELECTORS, String[].class); this.paths = properties.get(PROPERTY_PATHS, String[].class); this.extensions = properties.get(PROPERTY_EXTENSIONS, String[].class); @@ -405,13 +415,21 @@ public class ProcessorConfigurationImpl } // check resource types if ( this.resourceTypes != null && this.resourceTypes.length > 0 ) { + final ResourceResolver resourceResolver = processContext.getRequest().getResourceResolver(); final Resource resource = processContext.getRequest().getResource(); boolean found = false; int index = 0; while ( !found && index < this.resourceTypes.length ) { - if ( ResourceUtil.isA(resource, resourceTypes[index]) ) { + if ( resourceResolver.isResourceType(resource, resourceTypes[index]) ) { found = true; } + else if ( unwrapResources && resource instanceof ResourceWrapper ) { + // accept resource as well if type was overridden and unwrapped resource has a matching type + final Resource unwrappedResource = unwrap(resource); + if ( resourceResolver.isResourceType(unwrappedResource, resourceTypes[index]) ) { + found = true; + } + } index++; } if ( !found ) { @@ -466,6 +484,20 @@ public class ProcessorConfigurationImpl } /** + * Unwrap the resource and return the wrapped implementation. + * Copied from ResourceUtil.unwrap which is available in Sling API 2.7.0 and up. + * @param rsrc The resource to unwrap + * @return The unwrapped resource + */ + private static Resource unwrap(final Resource rsrc) { + Resource result = rsrc; + while (result instanceof ResourceWrapper) { + result = ((ResourceWrapper)result).getResource(); + } + return result; + } + + /** * The configuration for the generator. */ public ProcessingComponentConfiguration getGeneratorConfiguration() { Modified: sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java?rev=1702206&r1=1702205&r2=1702206&view=diff ============================================================================== --- sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java (original) +++ sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java Thu Sep 10 10:08:53 2015 @@ -21,6 +21,7 @@ import static org.apache.sling.rewriter. import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_PATHS; import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_RESOURCE_TYPES; import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_SELECTORS; +import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_UNWRAP_RESOURCES; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; @@ -28,6 +29,7 @@ import static org.mockito.Mockito.when; import java.util.Map; import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceWrapper; import org.apache.sling.rewriter.ProcessingContext; import org.apache.sling.testing.mock.sling.junit.SlingContext; import org.junit.Before; @@ -110,6 +112,35 @@ public class ProcessorConfigurationImplT } @Test + public void testMatchAtLeastOneResourceTypeWithResourceWrapper_UnwrapDisabled() { + Resource resource = context.create().resource("/content/test", ImmutableMap.<String, Object>of("sling:resourceType", "type/1")); + + // overwrite resource type via ResourceWrapper + Resource resourceWrapper = new ResourceWrapper(resource) { + @Override + public String getResourceType() { return "/type/override/1"; } + }; + context.currentResource(resourceWrapper); + + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/1","type/2"})); + } + + @Test + public void testMatchAtLeastOneResourceTypeWithResourceWrapper_UnwrapEnabled() { + Resource resource = context.create().resource("/content/test", ImmutableMap.<String, Object>of("sling:resourceType", "type/1")); + + // overwrite resource type via ResourceWrapper + Resource resourceWrapper = new ResourceWrapper(resource) { + @Override + public String getResourceType() { return "/type/override/1"; } + }; + context.currentResource(resourceWrapper); + + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/1","type/2"}, + PROPERTY_UNWRAP_RESOURCES, true)); + } + + @Test public void testMatchPathMismatch() { context.requestPathInfo().setResourcePath("/content/test"); assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","/var"}));