You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2007/12/04 14:39:05 UTC

svn commit: r600949 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/scripting/ main/java/org/apache/sling/microsling/scripting/helpers/ test/java/org/apache/sling/microsling/scripting/helpers/

Author: bdelacretaz
Date: Tue Dec  4 05:39:04 2007
New Revision: 600949

URL: http://svn.apache.org/viewvc?rev=600949&view=rev
Log:
SLING-118 - ScriptSearchPathsBuilder factored out of MicroslingScriptResolver, more understandable and testable (and that exposed a bug ;-)

Added:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java   (with props)
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java   (with props)
Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java?rev=600949&r1=600948&r2=600949&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java Tue Dec  4 05:39:04 2007
@@ -22,7 +22,6 @@
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.util.HashMap;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -46,6 +45,7 @@
 import org.apache.sling.microsling.resource.JcrNodeResource;
 import org.apache.sling.microsling.scripting.helpers.ScriptFilenameBuilder;
 import org.apache.sling.microsling.scripting.helpers.ScriptHelper;
+import org.apache.sling.microsling.scripting.helpers.ScriptSearchPathsBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -72,6 +72,7 @@
     public static final String JCR_ENCODING = "jcr:encoding";
 
     private final ScriptFilenameBuilder scriptFilenameBuilder = new ScriptFilenameBuilder();
+    private final ScriptSearchPathsBuilder scriptSearchPathsBuilder = new ScriptSearchPathsBuilder();
 
     private Map<String, SlingScriptEngine> scriptEngines;
 
@@ -167,32 +168,16 @@
         final Session s = (Session)request.getAttribute(Session.class.getName());
         MicroslingScript result = null;
 
-        String scriptFilename = scriptFilenameBuilder.buildScriptFilename(
+        final String scriptFilename = scriptFilenameBuilder.buildScriptFilename(
             request.getMethod(),
             request.getRequestPathInfo().getExtension(), 
             "*");
-        String scriptPath = scriptFilenameBuilder.buildScriptPath(r);
         
         // this is the location of the trailing asterisk
         final int scriptExtensionOffset = scriptFilename.length() - 1;
 
-        // if there are selectors A and B, look for a script first at
-        // A/B/scriptFilename, then A/scriptFilename, then scriptFilename
-        final List<String> possiblePaths = new LinkedList<String> ();
-        final String [] selectors = request.getRequestPathInfo().getSelectors();
-        if(selectors!=null) {
-            final StringBuffer sb = new StringBuffer();
-            sb.append(scriptPath);
-            for(int i=selectors.length - 1; i >= 0; i--) {
-                for(int j=0; j <= i; j++) {
-                    sb.append("/");
-                    sb.append(selectors[j]);
-                }
-                possiblePaths.add(sb.toString());
-            }
-        }
-        possiblePaths.add(scriptPath);
-        
+        final List<String> possiblePaths = scriptSearchPathsBuilder.getScriptSearchPaths(
+                request.getResource(), request.getRequestPathInfo().getSelectors());
         for(String currentPath : possiblePaths) {
             
             if(result != null) {
@@ -201,7 +186,7 @@
             
             if (log.isDebugEnabled()) {
                 log.debug("Looking for script with filename=" + scriptFilename
-                    + " under " + scriptPath);
+                    + " under " + currentPath);
             }
             
             if (s.itemExists(currentPath)) {
@@ -239,8 +224,9 @@
                 result.getScriptResource().getURI(), r);
         } else {
             log.debug(
-                "nt:file script node not found at path={} for Resource={}",
-                scriptPath, r);
+                "nt:file script node not found under path={} for Resource={}",
+                possiblePaths.get(possiblePaths.size() - 1)
+            );
         }
 
         return result;

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java?rev=600949&r1=600948&r2=600949&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java Tue Dec  4 05:39:04 2007
@@ -18,8 +18,6 @@
  */
 package org.apache.sling.microsling.scripting.helpers;
 
-import org.apache.sling.api.SlingException;
-import org.apache.sling.api.resource.Resource;
 import org.apache.sling.microsling.helpers.constants.HttpConstants;
 
 /** Builds the names of script files based on the current input:
@@ -71,17 +69,5 @@
             sb.append(scriptExtension.toLowerCase());
         }
         return sb.toString();
-    }
-
-    /** Returns the script root path constructed from the {@link #SCRIPT_BASE_PATH}
-     * and the resource type, where all backslashes and colons are replaced by
-     * forward slashes.
-     */
-    public String buildScriptPath(Resource resource) throws SlingException {
-        if(resource.getResourceType() == null) {
-            throw new SlingException("resource.getResourceType()==null, cannot build script path");
-        }
-        String typePath = resource.getResourceType().replaceAll("\\:","/");
-        return SCRIPT_BASE_PATH + "/" + typePath.trim();
     }
 }

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java?rev=600949&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java (added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java Tue Dec  4 05:39:04 2007
@@ -0,0 +1,71 @@
+/*
+ * 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.microsling.scripting.helpers;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.resource.Resource;
+
+/** Compute the paths under which to look for scripts for a given
+ *  request.
+ *  See {#link ScriptSearchPathsBuilderTest} for examples.
+ */
+public class ScriptSearchPathsBuilder {
+
+    public static final String SCRIPT_BASE_PATH = "/sling/scripts";
+
+    /** Returns the list of paths where scripts can be found, in order, 
+     *  for the given request.
+     *  The paths are based on the Request's Resource type, and if the 
+     *  request contains selectors, they are used to build subpaths of
+     *  the main path, which are searched first. 
+     */
+    public List<String> getScriptSearchPaths(Resource resource, String [] selectors) throws SlingException {
+        if(resource==null) {
+            throw new SlingException("Resource is null, cannot build script path");
+        }
+        if(resource.getResourceType() == null) {
+            throw new SlingException("resource.getResourceType()==null, cannot build script path");
+        }
+        
+        // base path
+        final String typePath = resource.getResourceType().replaceAll("\\:","/");
+        final String basePath = SCRIPT_BASE_PATH + "/" + typePath.trim();
+        
+        // if there are selectors A and B, look for a script first under
+        // basePath/A/B, then basePath/A, then basePath
+        final List<String> result = new LinkedList<String> ();
+        if(selectors!=null) {
+            for(int i=selectors.length - 1; i >= 0; i--) {
+                final StringBuffer sb = new StringBuffer();
+                sb.append(basePath);
+                for(int j=0; j <= i; j++) {
+                    sb.append("/");
+                    sb.append(selectors[j]);
+                }
+                result.add(sb.toString());
+            }
+        }
+        result.add(basePath);
+        
+        return result;
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java?rev=600949&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java (added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java Tue Dec  4 05:39:04 2007
@@ -0,0 +1,98 @@
+/*
+ * 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.microsling.scripting.helpers;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+
+/** Test the ScriptSearchPathsBuilder class */
+public class ScriptSearchPathsBuilderTest extends TestCase {
+    
+    private final ScriptSearchPathsBuilder builder = new ScriptSearchPathsBuilder();
+    
+    private void testBuilder(String resourceType,String [] selectors, String [] paths) throws SlingException {
+        final Resource r = new MockResource(resourceType);
+        
+        final List<String> actual = builder.getScriptSearchPaths(r, selectors);
+        
+        final List<String> expected = new LinkedList<String>();
+        if(paths != null) {
+            for(String path : paths) {
+                expected.add(path);
+            }
+        }
+        assertEquals(expected,actual);
+    }
+    
+    public void testNoSelectorsA() throws SlingException {
+        final String [] selectors = null;
+        final String [] expected = { "/sling/scripts/rt" }; 
+        testBuilder("rt", selectors, expected);
+    }
+    
+    public void testNoSelectorsB() throws SlingException {
+        final String [] selectors = null;
+        final String [] expected = { "/sling/scripts/rt/something" }; 
+        testBuilder("rt/something", selectors, expected);
+    }
+    
+    public void testWithSelectorsA() throws SlingException {
+        final String [] selectors = { "a4" };
+        final String [] expected = { "/sling/scripts/rt/a4", "/sling/scripts/rt" }; 
+        testBuilder("rt", selectors, expected);
+    }
+    
+    public void testWithSelectorsB() throws SlingException {
+        final String [] selectors = { "a4", "print" };
+        final String [] expected = { "/sling/scripts/rt/x/a4/print", "/sling/scripts/rt/x/a4", "/sling/scripts/rt/x" }; 
+        testBuilder("rt/x", selectors, expected);
+    }
+    
+    static class MockResource implements Resource {
+
+        private final String resourceType;
+        
+        MockResource(String resourceType) {
+            this.resourceType = resourceType;
+        }
+
+        public String getResourceType() {
+            return resourceType;
+        }
+
+        public String getURI() {
+            throw new Error("MockResource does not implement this method");
+        }
+
+        public ResourceMetadata getResourceMetadata() {
+            throw new Error("MockResource does not implement this method");
+        }
+        
+        public <Type> Type adaptTo(Class<Type> type) {
+            throw new Error("MockResource does not implement this method");
+        }
+    }
+}
+

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL