You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/09/14 01:55:59 UTC

svn commit: r1170395 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/services/ test/app1/ test/groovy/org/apache/tapestry5/integration/ test/groovy/org/apache/tapestry5/integration/app1/ test/groovy/org/apache/...

Author: hlship
Date: Tue Sep 13 23:55:59 2011
New Revision: 1170395

URL: http://svn.apache.org/viewvc?rev=1170395&view=rev
Log:
TAP5-1643: Fix AssetDispatcher to handle paths that include folder names

Add test to prove it works

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/GroovyTapestryCoreTestCase.groovy
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/LibraryTests.groovy
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/pages/
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/tapestry.png
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AssetDispatcher.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/appfolder/AppFolderTests.groovy
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AssetDispatcher.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AssetDispatcher.java?rev=1170395&r1=1170394&r2=1170395&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AssetDispatcher.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AssetDispatcher.java Tue Sep 13 23:55:59 2011
@@ -17,6 +17,7 @@ package org.apache.tapestry5.internal.se
 import org.apache.tapestry5.SymbolConstants;
 import org.apache.tapestry5.ioc.annotations.Symbol;
 import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.services.ClasspathAssetAliasManager;
 import org.apache.tapestry5.services.Dispatcher;
 import org.apache.tapestry5.services.Request;
@@ -25,6 +26,9 @@ import org.apache.tapestry5.services.ass
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -38,7 +42,15 @@ import java.util.Map;
 @UsesMappedConfiguration(AssetRequestHandler.class)
 public class AssetDispatcher implements Dispatcher
 {
-    private final Map<String, AssetRequestHandler> configuration;
+    /**
+     * Keyed on extended path name, which includes the pathPrefix first and a trailing slash.
+     */
+    private final Map<String, AssetRequestHandler> pathToHandler = CollectionFactory.newMap();
+
+    /**
+     * List of path prefixes in the pathToHandler, sorted be descending length.
+     */
+    private final List<String> assetPaths = CollectionFactory.newList();
 
     private final String pathPrefix;
 
@@ -49,11 +61,28 @@ public class AssetDispatcher implements 
 
                            @Symbol(SymbolConstants.APPLICATION_FOLDER) String applicationFolder)
     {
-        this.configuration = configuration;
-
         String folder = applicationFolder.equals("") ? "" : "/" + applicationFolder;
 
         this.pathPrefix = folder + RequestConstants.ASSET_PATH_PREFIX + applicationVersion + "/";
+
+        for (String path : configuration.keySet())
+        {
+            String extendedPath = this.pathPrefix + path + "/";
+
+            pathToHandler.put(extendedPath, configuration.get(path));
+
+            assetPaths.add(extendedPath);
+        }
+
+        // Sort by descending length
+
+        Collections.sort(assetPaths, new Comparator<String>()
+        {
+            public int compare(String o1, String o2)
+            {
+                return o2.length() - o1.length();
+            }
+        });
     }
 
     public boolean dispatch(Request request, Response response) throws IOException
@@ -64,25 +93,26 @@ public class AssetDispatcher implements 
         // looking for the asset path prefix right off the bat.
 
         if (!path.startsWith(pathPrefix))
+        {
             return false;
+        }
 
-        String virtualPath = path.substring(pathPrefix.length());
-
-        int slashx = virtualPath.indexOf('/');
-
-        String virtualFolder = virtualPath.substring(0, slashx);
-
-        AssetRequestHandler handler = configuration.get(virtualFolder);
-
-        if (handler != null)
+        for (String extendedPath : assetPaths)
         {
 
-            String extraPath = virtualPath.substring(slashx + 1);
-
-            boolean handled = handler.handleAssetRequest(request, response, extraPath);
-
-            if (handled)
-                return true;
+            if (path.startsWith(extendedPath))
+            {
+                AssetRequestHandler handler = pathToHandler.get(extendedPath);
+
+                String extraPath = path.substring(extendedPath.length());
+
+                boolean handled = handler.handleAssetRequest(request, response, extraPath);
+
+                if (handled)
+                {
+                    return true;
+                }
+            }
         }
 
         response.sendError(HttpServletResponse.SC_NOT_FOUND, path);

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml?rev=1170395&r1=1170394&r2=1170395&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml Tue Sep 13 23:55:59 2011
@@ -187,6 +187,10 @@
     <li>
         <a href="music/details2">Missing page activation context</a>
     </li>
+    <li>
+        <a href="lib/alpha/root">Alpha Library Root</a> -- demonstrates libraries contributed with a slash in the name
+    </li>
+
 </ul>
 
 </html>

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/GroovyTapestryCoreTestCase.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/GroovyTapestryCoreTestCase.groovy?rev=1170395&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/GroovyTapestryCoreTestCase.groovy (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/GroovyTapestryCoreTestCase.groovy Tue Sep 13 23:55:59 2011
@@ -0,0 +1,19 @@
+package org.apache.tapestry5.integration
+
+import org.apache.tapestry5.test.TapestryTestConstants
+
+class GroovyTapestryCoreTestCase extends TapestryCoreTestCase
+{
+
+    protected final assertDownloadedAsset(String assetURL, String path)
+    {
+
+        URL url = new URL(getBaseURL() + assetURL.substring(1))
+
+        byte[] downloaded = url.bytes
+
+        byte[] actual = new File(TapestryTestConstants.MODULE_BASE_DIR, path).bytes
+
+        assertEquals downloaded, actual, "Contents of $path do not match"
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/LibraryTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/LibraryTests.groovy?rev=1170395&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/LibraryTests.groovy (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/LibraryTests.groovy Tue Sep 13 23:55:59 2011
@@ -0,0 +1,25 @@
+package org.apache.tapestry5.integration.app1
+
+import org.apache.tapestry5.integration.GroovyTapestryCoreTestCase
+import org.testng.annotations.Test
+
+class LibraryTests extends GroovyTapestryCoreTestCase
+{
+
+    /**
+     * Tests case where a library is mapped to a subfolder (i.e., "lib/alpha"). This was not allowed in 5.2,
+     * but is added back in for 5.3.
+     */
+    @Test
+    void access_to_assets_in_library_with_subfolder()
+    {
+        openLinks "Alpha Library Root"
+
+        String assetURL = getAttribute("//img[@id='t5logo']/@src")
+
+        assert assetURL.contains("lib/alpha/pages")
+
+        assertDownloadedAsset assetURL, "src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/tapestry.png"
+    }
+
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/appfolder/AppFolderTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/appfolder/AppFolderTests.groovy?rev=1170395&r1=1170394&r2=1170395&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/appfolder/AppFolderTests.groovy (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/appfolder/AppFolderTests.groovy Tue Sep 13 23:55:59 2011
@@ -1,10 +1,9 @@
 package org.apache.tapestry5.integration.appfolder
 
-import org.apache.tapestry5.integration.TapestryCoreTestCase
+import org.apache.tapestry5.integration.GroovyTapestryCoreTestCase
 import org.testng.annotations.Test
-import org.apache.tapestry5.test.TapestryTestConstants
 
-class AppFolderTests extends TapestryCoreTestCase
+class AppFolderTests extends GroovyTapestryCoreTestCase
 {
 
     /**
@@ -48,7 +47,8 @@ class AppFolderTests extends TapestryCor
     }
 
     @Test
-    void asset_access() {
+    void asset_access()
+    {
         openLinks "t5app/"
 
         // Ony one image on page
@@ -56,12 +56,6 @@ class AppFolderTests extends TapestryCor
 
         assert assetURL.startsWith("/t5app/assets/")
 
-        URL url = new URL(getBaseURL() + assetURL.substring(1))
-
-        byte[] downloaded = url.bytes
-
-        byte[] actual = new File(TapestryTestConstants.MODULE_BASE_DIR, "src/test/appfolder/images/t5-logo.png").bytes
-
-        assertEquals downloaded, actual, "Contents of t5-logo.png do not match"
+        assertDownloadedAsset assetURL, "src/test/appfolder/images/t5-logo.png"
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java?rev=1170395&r1=1170394&r2=1170395&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java Tue Sep 13 23:55:59 2011
@@ -281,4 +281,11 @@ public class AppModule
     {
         configuration.add("properties");
     }
+
+    @Contribute(ComponentClassResolver.class)
+    public static void setupAlphaLibrary(Configuration<LibraryMapping> configuration)
+    {
+        configuration.add(new LibraryMapping("lib/alpha", "org.apache.tapestry5.integration.locallib.alpha"));
+    }
+
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.java?rev=1170395&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.java Tue Sep 13 23:55:59 2011
@@ -0,0 +1,8 @@
+package org.apache.tapestry5.integration.locallib.alpha.pages;
+
+/**
+ *
+ */
+public class AlphaRoot
+{
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.tml?rev=1170395&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/AlphaRoot.tml Tue Sep 13 23:55:59 2011
@@ -0,0 +1,8 @@
+<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
+
+<h1>Alpha Library Root</h1>
+
+<img id="t5logo" src="${asset:tapestry.png}"/>
+
+</html>
+

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/tapestry.png
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/tapestry.png?rev=1170395&view=auto
==============================================================================
Files tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/tapestry.png (added) and tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/locallib/alpha/pages/tapestry.png Tue Sep 13 23:55:59 2011 differ