incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r602100 - 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/integration/ test/java/org/a...
Date Fri, 07 Dec 2007 13:56:05 GMT
Author: bdelacretaz
Date: Fri Dec  7 05:56:03 2007
New Revision: 602100

URL: http://svn.apache.org/viewvc?rev=602100&view=rev
Log:
SLING-125 - Allow multiple script search paths + search path based on the resource path

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
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.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=602100&r1=602099&r2=602100&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
Fri Dec  7 05:56:03 2007
@@ -57,8 +57,9 @@
  * <pre>
  *      /sling/scripts/some/type/get.html.js
  * </pre>
- *
- * in the repository.
+ * 
+ * in the repository. In the above example, "/sling/scripts" is a script search path,
+ * which is provided by {#ScriptSearchPathsBuilder} 
  */
 public class MicroslingScriptResolver implements SlingScriptResolver {
 
@@ -182,9 +183,19 @@
                 log.debug("Looking for script with filename=" + scriptFilename
                     + " under " + currentPath);
             }
+            
+            // do not throw exceptions if path is invalid, that might happen
+            // depending on the resource type / search path values
+            boolean pathExists = false;
+            try {
+                pathExists = s.itemExists(currentPath);
+            } catch(Exception e) {
+                if(log.isDebugEnabled()) {
+                    log.debug("itemExists(" + currentPath + ") call fails, exception ignored:
" + e);
+                }
+            }
 
-            if (s.itemExists(currentPath)) {
-
+            if (pathExists) {
                 // get the item and ensure it is a node
                 final Item i = s.getItem(currentPath);
                 if (i.isNode()) {

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=602100&r1=602099&r2=602100&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
Fri Dec  7 05:56:03 2007
@@ -36,8 +36,6 @@
  */
 public class ScriptFilenameBuilder {
 
-    public static final String SCRIPT_BASE_PATH = "/sling/scripts";
-
     /** @return a name like "html.js" or "print/a4/html.vlt" or "POST.js" */
     public String buildScriptFilename(String methodName,String requestExtension,String scriptExtension)
{
         final StringBuffer sb = new StringBuffer();

Modified: 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=602100&r1=602099&r2=602100&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilder.java
Fri Dec  7 05:56:03 2007
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.microsling.scripting.helpers;
 
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -30,7 +31,8 @@
  */
 public class ScriptSearchPathsBuilder {
 
-    public static final String SCRIPT_BASE_PATH = "/sling/scripts";
+    /** hardcoded for now, these could be configurable */ 
+    public static final String [] DEFAULT_SCRIPT_SEARCH_PATHS = { "/sling/scripts", "/apps"
};
 
     /** Returns the list of paths where scripts can be found, in order, 
      *  for the given request.
@@ -46,26 +48,53 @@
             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]);
+        
+        // use the default script search paths + the path provided by getDynamicDefaultScriptPath
+        final List<String> paths = new ArrayList<String>();
+        for(String defaultPath : DEFAULT_SCRIPT_SEARCH_PATHS) {
+            paths.add(defaultPath + "/" + typePath.trim());
+        }
+        // the last path in the list does not take the resource type into account (SLING-125)
+        paths.add(getDynamicDefaultScriptPath(resource));
+
+        // and add selector-based paths, which take precedence over the default ones
+        for(String basePath : paths) {
+            
+            // if there are selectors A and B, look for a script first under
+            // basePath/A/B, then basePath/A, then basePath
+            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(sb.toString());
             }
+            result.add(basePath);
+            
         }
-        result.add(basePath);
         
+        return result;
+    }
+    
+    /** Transform the Resource URI into a search path for scripts.
+     *  For example, a resource with URI=/content/blog/a/b/c returns
+     *  /apps/blog - this allows for simple path-based mapping of
+     *  resources to scripts
+     */
+    protected String getDynamicDefaultScriptPath(Resource r) {
+        String result = null;
+        if(r!=null && r.getURI() != null) {
+            final String [] pathFolders = r.getURI().split("/");
+            if(pathFolders.length >= 3) {
+                result ="/apps/" + pathFolders[2];
+            }
+        }
         return result;
     }
 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java?rev=602100&r1=602099&r2=602100&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
Fri Dec  7 05:56:03 2007
@@ -26,6 +26,8 @@
  */
 public class NodetypeRenderingTest extends RenderingTestBase {
 
+    private String secondFolderOfContentPath;
+    
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -34,7 +36,8 @@
         testText = "This is a test " + System.currentTimeMillis();
 
         // create the test node, under a path that's specific to this class to allow collisions
-        final String url = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "." + System.currentTimeMillis();
+        secondFolderOfContentPath = "" + System.currentTimeMillis();
+        final String url = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "/" + secondFolderOfContentPath
+ "/*";
         final Map<String,String> props = new HashMap<String,String>();
         props.put("text", testText);
         displayUrl = testClient.createNode(url, props);
@@ -69,6 +72,27 @@
         final String toDelete = uploadTestScript("rendering-test.esp","xml.esp");
         try {
             final String content = getContent(displayUrl + ".xml", CONTENT_TYPE_XML);
+            assertTrue("Content includes ESP marker",content.contains("ESP template"));
+            assertTrue("Content contains formatted test text",content.contains("<p>"
+ testText + "</p>"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+    
+    public void testEspHtmlWithContentBasedPath() throws IOException {
+        
+        // make sure there's no leftover rendering script
+        {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertFalse("Content must not include ESP marker before test",content.contains("ESP
template"));
+        }
+        
+        // put our script in the /apps/<second folder level of content> (SLING-125)
+        final String path = "/apps/" + secondFolderOfContentPath;
+        testClient.mkdirs(WEBDAV_BASE_URL, path);
+        final String toDelete = uploadTestScript(path,"rendering-test.esp","html.esp");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
             assertTrue("Content includes ESP marker",content.contains("ESP template"));
             assertTrue("Content contains formatted test text",content.contains("<p>"
+ testText + "</p>"));
         } finally {

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java?rev=602100&r1=602099&r2=602100&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
Fri Dec  7 05:56:03 2007
@@ -27,6 +27,7 @@
 public class SlingResourceTypeRenderingTest extends RenderingTestBase {
 
     private String slingResourceType;
+    private String secondFolderOfContentPath;
 
     @Override
     protected void setUp() throws Exception {
@@ -37,7 +38,8 @@
         testText = "This is a test " + System.currentTimeMillis();
 
         // create the test node, under a path that's specific to this class to allow collisions
-        final String url = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "_" + System.currentTimeMillis()
+ "/*";
+        secondFolderOfContentPath = "" + System.currentTimeMillis();
+        final String url = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "/" + secondFolderOfContentPath
+ "/*";
         final Map<String,String> props = new HashMap<String,String>();
         props.put("sling:resourceType", slingResourceType);
         props.put("text", testText);
@@ -60,6 +62,47 @@
 
     public void testEspHtml() throws IOException {
         final String toDelete = uploadTestScript("rendering-test.esp","html.esp");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes ESP marker",content.contains("ESP template"));
+            assertTrue("Content contains formatted test text",content.contains("<p>"
+ testText + "</p>"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
+    public void testEspHtmlInAppsFolder() throws IOException {
+        // make sure there's no leftover rendering script
+        {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertFalse("Content must not include ESP marker before test",content.contains("ESP
template"));
+        }
+
+        // put our script under /apps/<resource type>
+        final String path = "/apps/" + slingResourceType;
+        testClient.mkdirs(WEBDAV_BASE_URL, path);
+        final String toDelete = uploadTestScript(path,"rendering-test.esp","html.esp");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes ESP marker",content.contains("ESP template"));
+            assertTrue("Content contains formatted test text",content.contains("<p>"
+ testText + "</p>"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
+    public void testEspHtmlWithContentBasedPath() throws IOException {
+        
+        // make sure there's no leftover rendering script
+        {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertFalse("Content must not include ESP marker before test",content.contains("ESP
template"));
+        }
+        
+        // put our script in the /apps/<second folder level of content> (SLING-125)
+        final String path = "/apps/" + secondFolderOfContentPath;
+        testClient.mkdirs(WEBDAV_BASE_URL, path);
+        final String toDelete = uploadTestScript(path,"rendering-test.esp","html.esp");
         try {
             final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
             assertTrue("Content includes ESP marker",content.contains("ESP template"));

Modified: 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=602100&r1=602099&r2=602100&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptSearchPathsBuilderTest.java
Fri Dec  7 05:56:03 2007
@@ -32,8 +32,8 @@
     
     private final ScriptSearchPathsBuilder builder = new ScriptSearchPathsBuilder();
     
-    private void testBuilder(String resourceType,String [] selectors, String [] paths) throws
SlingException {
-        final Resource r = new MockResource(resourceType);
+    private void testBuilder(String resourceType,String resourcePath, String [] selectors,
String [] paths) throws SlingException {
+        final Resource r = new MockResource(resourceType,resourcePath);
         
         final List<String> actual = builder.getScriptSearchPaths(r, selectors);
         
@@ -48,34 +48,44 @@
     
     public void testNoSelectorsA() throws SlingException {
         final String [] selectors = null;
-        final String [] expected = { "/sling/scripts/rt" }; 
-        testBuilder("rt", selectors, expected);
+        final String [] expected = { "/sling/scripts/rt", "/apps/rt", "/apps/blog" }; 
+        testBuilder("rt", "/content/blog", selectors, expected);
     }
     
     public void testNoSelectorsB() throws SlingException {
         final String [] selectors = null;
-        final String [] expected = { "/sling/scripts/rt/something" }; 
-        testBuilder("rt/something", selectors, expected);
+        final String [] expected = { "/sling/scripts/rt/something", "/apps/rt/something",
"/apps/blog" }; 
+        testBuilder("rt/something", "/content/blog/a/b/c", 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);
+        final String [] expected = { 
+                "/sling/scripts/rt/a4", "/sling/scripts/rt", 
+                "/apps/rt/a4", "/apps/rt", 
+                "/apps/blog/a4", "/apps/blog", 
+                }; 
+        testBuilder("rt", "/content/blog", 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);
+        final String [] expected = { 
+                "/sling/scripts/rt/x/a4/print", "/sling/scripts/rt/x/a4", "/sling/scripts/rt/x",

+                "/apps/rt/x/a4/print", "/apps/rt/x/a4", "/apps/rt/x", 
+                "/apps/banking/a4/print", "/apps/banking/a4", "/apps/banking", 
+        }; 
+        testBuilder("rt/x", "/somewhere/banking/blog", selectors, expected);
     }
     
     static class MockResource implements Resource {
 
         private final String resourceType;
+        private final String path;
         
-        MockResource(String resourceType) {
+        MockResource(String resourceType, String path) {
             this.resourceType = resourceType;
+            this.path = path;
         }
 
         public String getResourceType() {
@@ -83,7 +93,7 @@
         }
 
         public String getURI() {
-            throw new Error("MockResource does not implement this method");
+            return path;
         }
 
         public ResourceMetadata getResourceMetadata() {



Mime
View raw message