sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1628637 - in /sling/trunk/contrib/extensions/rewriter: ./ src/main/java/org/apache/sling/rewriter/impl/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/sling/ src/test/java/org/apache/sling/re...
Date Wed, 01 Oct 2014 08:23:59 GMT
Author: sseifert
Date: Wed Oct  1 08:23:59 2014
New Revision: 1628637

URL: http://svn.apache.org/r1628637
Log:
SLING-3511 Support selectors for pipeline configuration (patch supplied by Dirk Rudolph)

Added:
    sling/trunk/contrib/extensions/rewriter/src/test/
    sling/trunk/contrib/extensions/rewriter/src/test/java/
    sling/trunk/contrib/extensions/rewriter/src/test/java/org/
    sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/
    sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/
    sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/
    sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/
    sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java
  (with props)
Modified:
    sling/trunk/contrib/extensions/rewriter/pom.xml
    sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
    sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java

Modified: sling/trunk/contrib/extensions/rewriter/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/pom.xml?rev=1628637&r1=1628636&r2=1628637&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/rewriter/pom.xml (original)
+++ sling/trunk/contrib/extensions/rewriter/pom.xml Wed Oct  1 08:23:59 2014
@@ -142,5 +142,24 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
+
+        <!-- testing -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.testing</artifactId>
+            <version>2.0.16</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java?rev=1628637&r1=1628636&r2=1628637&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
(original)
+++ sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
Wed Oct  1 08:23:59 2014
@@ -64,6 +64,9 @@ public class FactoryCache {
     /** The optional property for the content types the component should apply to */
     private static final String PROPERTY_CONTENT_TYPES = "pipeline.contentTypes";
 
+    /** The optional property for the selectors the component should apply to */
+    private static final String PROPERTY_SELECTORS = "pipeline.selectors";
+
     /** The optional property for the resource types the component should apply to */
     private static final String PROPERTY_RESOURCE_TYPES = "pipeline.resourceTypes";
 
@@ -430,12 +433,14 @@ public class FactoryCache {
             final String[] extensions = OsgiUtil.toStringArray(ref.getProperty(PROPERTY_EXTENSIONS),
null);
             final String[] contentTypes = OsgiUtil.toStringArray(ref.getProperty(PROPERTY_CONTENT_TYPES),
null);
             final String[] resourceTypes = OsgiUtil.toStringArray(ref.getProperty(PROPERTY_RESOURCE_TYPES),
null);
+            final String[] selectors = OsgiUtil.toStringArray(ref.getProperty(PROPERTY_SELECTORS),
null);
             final boolean noCheckRequired = (paths == null || paths.length == 0) &&
                                    (extensions == null || extensions.length == 0) &&
                                    (contentTypes == null || contentTypes.length == 0) &&
-                                   (resourceTypes == null || resourceTypes.length == 0);
+                                   (resourceTypes == null || resourceTypes.length == 0) &&
+                                   (selectors == null || selectors.length == 0);
             if ( !noCheckRequired ) {
-                this.configuration = new ProcessorConfigurationImpl(contentTypes, paths,
extensions, resourceTypes);
+                this.configuration = new ProcessorConfigurationImpl(contentTypes, paths,
extensions, resourceTypes, selectors);
             } else {
                 this.configuration = null;
             }

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=1628637&r1=1628636&r2=1628637&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
Wed Oct  1 08:23:59 2014
@@ -18,7 +18,9 @@ package org.apache.sling.rewriter.impl;
 
 import java.io.PrintWriter;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
@@ -47,6 +49,8 @@ public class ProcessorConfigurationImpl 
 
     private static final String PROPERTY_RESOURCE_TYPES = "resourceTypes";
 
+    private static final String PROPERTY_SELECTORS = "selectors";
+
     private static final String PROPERTY_TRANFORMERS = "transformerTypes";
 
     private static final String PROPERTY_GENERATOR = "generatorType";
@@ -70,6 +74,9 @@ public class ProcessorConfigurationImpl 
     /** For which resource types should this processor be applied. */
     private final String[] resourceTypes;
 
+    /** For which selectors should this processor be applied. */
+    private final String[] selectors;
+
     /** The order of this processor */
     private final int order;
 
@@ -105,6 +112,7 @@ public class ProcessorConfigurationImpl 
                                       String[] paths,
                                       String[] extensions,
                                       String[] resourceTypes,
+                                      String[] selectors,
                                       int      order,
                                       ProcessingComponentConfiguration generatorConfig,
                                       ProcessingComponentConfiguration[] transformerConfigs,
@@ -112,6 +120,7 @@ public class ProcessorConfigurationImpl 
                                       boolean processErrorResponse) {
         this.contentTypes = contentTypes;
         this.resourceTypes = resourceTypes;
+        this.selectors = selectors;
         this.paths = paths;
         this.extensions = extensions;
         this.order = order;
@@ -132,8 +141,9 @@ public class ProcessorConfigurationImpl 
     public ProcessorConfigurationImpl(String[] contentTypes,
                                       String[] paths,
                                       String[] extensions,
-                                      String[] resourceTypes) {
-        this(contentTypes, paths, extensions, resourceTypes, 0, null, null, null, false);
+                                      String[] resourceTypes,
+                                      String[] selectors) {
+        this(contentTypes, paths, extensions, resourceTypes, selectors, 0, null, null, null,
false);
     }
 
     /**
@@ -144,6 +154,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.selectors = properties.get(PROPERTY_SELECTORS, String[].class);
         this.paths = properties.get(PROPERTY_PATHS, String[].class);
         this.extensions = properties.get(PROPERTY_EXTENSIONS, String[].class);
 
@@ -185,6 +196,10 @@ public class ProcessorConfigurationImpl 
             pw.print("Resource Types : ");
             pw.println(Arrays.toString(this.resourceTypes));
         }
+        if ( this.selectors != null ) {
+            pw.print("Selectors : ");
+            pw.println(Arrays.toString(this.selectors));
+        }
         if ( this.paths != null ) {
             pw.print("Paths : ");
             pw.println(Arrays.toString(this.paths));
@@ -235,13 +250,18 @@ public class ProcessorConfigurationImpl 
         if ( this.contentTypes != null ) {
             sb.append("contentTypes=");
             sb.append(Arrays.toString(this.contentTypes));
-            sb.append(',');
+            sb.append(", ");
         }
         if ( this.resourceTypes != null ) {
             sb.append("resourceTypes=");
             sb.append(Arrays.toString(this.resourceTypes));
             sb.append(", ");
         }
+        if ( this.selectors != null ) {
+            sb.append("selectors=");
+            sb.append(Arrays.toString(this.selectors));
+            sb.append(", ");
+        }
         if ( this.paths != null ) {
             sb.append("paths=");
             sb.append(Arrays.toString(this.paths));
@@ -417,6 +437,31 @@ public class ProcessorConfigurationImpl 
                 return false;
             }
         }
+
+        // now check for selectors
+        if( this.selectors != null && this.selectors.length > 0 ) {
+            final String selectorString = processContext.getRequest().getRequestPathInfo().getSelectorString();
+            if ( selectorString == null || "".equals(selectorString )) {
+                // selectors required but not set
+                return false;
+            }
+
+            final Set<String> selectors = new HashSet<String>(Arrays.asList(selectorString.split("\\.")));
+            int index = 0;
+            boolean found = false;
+            while ( !found && index < this.selectors.length ) {
+                final String selector = this.selectors[index];
+                if( selectors.contains(selector) ) {
+                    found = true;
+                }
+                index++;
+            }
+
+            if( !found ) {
+                return false;
+            }
+        }
+
         return true;
     }
 

Added: 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=1628637&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java
(added)
+++ sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java
Wed Oct  1 08:23:59 2014
@@ -0,0 +1,162 @@
+/*
+ * 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.rewriter.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.commons.testing.sling.MockResourceResolver;
+import org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest;
+import org.apache.sling.rewriter.ProcessingContext;
+import org.apache.sling.rewriter.ProcessorConfiguration;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author diru
+ */
+public class ProcessorConfigurationImplTest {
+
+    private MockSlingHttpServletRequest mockRequestWithSelector;
+    private MockSlingHttpServletRequest mockRequestWithoutSelector;
+
+    @Before
+    public void setup() {
+        this.mockRequestWithSelector = new MockSlingHttpServletRequest("/content/path", "sel1.sel2",
"xml", null, null);
+        this.mockRequestWithoutSelector = new MockSlingHttpServletRequest("/content/path",
null, "xml", null, null);
+        // mock the resource resolver and create also a mocked resource to prevent NPE in
ResourceUtil.
+        final MockResourceResolver mockResourceResolver = new MockResourceResolver();
+        mockResourceResolver.setSearchPath("/libs");
+        this.mockRequestWithSelector.setResourceResolver(mockResourceResolver);
+        this.mockRequestWithoutSelector.setResourceResolver(mockResourceResolver);
+    }
+
+    @Test
+    public void testMatchContentTypeMismatch() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(new
String[] {"text/html",
+            "text/plain" }, null, null, null, null), true);
+    }
+
+    @Test
+    public void testMatchAtLeastOneContentType() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(new
String[] {"text/html",
+                "text/xml" }, null, null, null, null));
+    }
+
+    @Test
+    public void testMatchAnyContentType() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(
+                new String[] {"text/html", "*" }, null, null, null, null));
+    }
+
+    @Test
+    public void testMatchExtensionMismatch() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
null, new String[] {
+            "html", "txt" }, null, null), true);
+    }
+
+    @Test
+    public void testMatchAtLeastOneExtension() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
null, new String[] {
+                "html", "xml" }, null, null));
+    }
+
+    @Test
+    public void testMatchResourceTypeMismatch() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
null, null, new String[] {
+            "a/b/c" }, null), true);
+    }
+
+    @Test
+    public void testMatchAtLeastOneResourceType() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
null, null, new String[] {
+                "a/b/c", MockSlingHttpServletRequest.RESOURCE_TYPE }, null));
+    }
+
+    @Test
+    public void testMatchPathMismatch() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
new String[] {"/apps",
+            "/var" }, null, null, null), true);
+    }
+
+    @Test
+    public void testMatchAtLeastOnePath() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
new String[] {"/libs",
+                "/content" }, null, null, null));
+    }
+
+    @Test
+    public void testMatchAnyPath() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
new String[] {"/libs",
+                "*" }, null, null, null));
+    }
+
+    @Test
+    public void testMatchSelectorRequired() {
+        this.doTestAgainstProcessingContextWithoutSelector(new ProcessorConfigurationImpl(null,
null, null, null,
+                new String[] {"sel" }), true);
+    }
+
+    @Test
+    public void testMatchSelectorMismatch() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
null, null, null,
+            new String[] {"sel3" }), true);
+    }
+
+    @Test
+    public void testMatchAtLeastOneSelector() {
+        this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null,
null, null, null,
+            new String[] {"sel1" }));
+    }
+
+    private void doTestAgainstProcessingContextWithSelector(ProcessorConfiguration configuration)
{
+        this.doTestAgainstProcessingContextWithSelector(configuration, false);
+    }
+
+    private void doTestAgainstProcessingContextWithSelector(ProcessorConfiguration configuration,
boolean negate) {
+        // setup processing context
+        ProcessingContext context = createProcessingContext(this.mockRequestWithSelector);
+        // test the given configuration
+        doTest(configuration, context, negate);
+    }
+
+    private void doTestAgainstProcessingContextWithoutSelector(ProcessorConfiguration configuration,
boolean negate) {
+        // setup processing context
+        ProcessingContext context = createProcessingContext(this.mockRequestWithoutSelector);
+        // test the given configuration
+        doTest(configuration, context, negate);
+    }
+
+    private void doTest(ProcessorConfiguration configuration, ProcessingContext context,
boolean negate) {
+        if (!negate) {
+            assertTrue(configuration.match(context));
+        } else {
+            assertFalse(configuration.match(context));
+        }
+    }
+
+    private ProcessingContext createProcessingContext(SlingHttpServletRequest request) {
+        final ProcessingContext context = mock(ProcessingContext.class);
+        when(context.getContentType()).thenReturn("text/xml");
+        when(context.getRequest()).thenReturn(request);
+
+        return context;
+    }
+}

Propchange: sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Oct  1 08:23:59 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message