Repository: tapestry-5
Updated Branches:
  refs/heads/master 23330e4bf -> 5b311716e


TAP5-2187 : CSS relative URL rewriting isn't lenient enough


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/5b311716
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/5b311716
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/5b311716

Branch: refs/heads/master
Commit: 5b311716e65e5fde43cf925c0728b52e4e63cf32
Parents: 23330e4
Author: Thiago H. de Paula Figueiredo <[email protected]>
Authored: Fri May 30 20:41:57 2014 -0300
Committer: Thiago H. de Paula Figueiredo <[email protected]>
Committed: Fri May 30 20:41:57 2014 -0300

----------------------------------------------------------------------
 .../org/apache/tapestry5/SymbolConstants.java   |  8 +++
 .../services/assets/CSSURLRewriter.java         | 24 ++++++++-
 .../apache/tapestry5/modules/AssetsModule.java  |  5 +-
 .../tapestry5/modules/TapestryModule.java       |  3 ++
 .../services/assets/CSSURLRewriterTests.groovy  | 52 +++++++++++++++-----
 5 files changed, 76 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5b311716/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java 
b/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
index 83e6f9c..d2526ae 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
@@ -482,4 +482,12 @@ public class SymbolConstants
      */
     public static final String LENIENT_DATE_FORMAT = 
"tapestry.lenient-date-format";
 
+    /**
+     * Defines whether {@link CSSURLRewriter} will throw an exception when a 
CSS file
+     * references an URL which doesn't exist. The default value is 
<code>false</code>.
+     * 
+     * @since 5.4
+     */
+    public static final String STRICT_CSS_URL_REWRITING = 
"tapestry.strict-css-url-rewriting";
+
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5b311716/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
index ae6c3b5..d5053f5 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
@@ -15,11 +15,14 @@
 package org.apache.tapestry5.internal.services.assets;
 
 import org.apache.tapestry5.Asset;
+import org.apache.tapestry5.SymbolConstants;
 import org.apache.tapestry5.ioc.IOOperation;
 import org.apache.tapestry5.ioc.OperationTracker;
 import org.apache.tapestry5.ioc.Resource;
 import org.apache.tapestry5.services.AssetSource;
 import org.apache.tapestry5.services.assets.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -39,6 +42,7 @@ import java.util.regex.Pattern;
  * somewhat banking on the fact that referenced resources are non-compressable 
images.
  *
  * @since 5.4
+ * @see SymbolConstants#STRICT_CSS_URL_REWRITING
  */
 public class CSSURLRewriter extends DelegatingSRS
 {
@@ -64,13 +68,19 @@ public class CSSURLRewriter extends DelegatingSRS
     private final AssetSource assetSource;
 
     private final AssetChecksumGenerator checksumGenerator;
+    
+    private final Logger logger = 
LoggerFactory.getLogger(CSSURLRewriter.class);
+    
+    private final boolean strictCssUrlRewriting;
 
-    public CSSURLRewriter(StreamableResourceSource delegate, OperationTracker 
tracker, AssetSource assetSource, AssetChecksumGenerator checksumGenerator)
+    public CSSURLRewriter(StreamableResourceSource delegate, OperationTracker 
tracker, AssetSource assetSource, 
+            AssetChecksumGenerator checksumGenerator, boolean 
strictCssUrlRewriting)
     {
         super(delegate);
         this.tracker = tracker;
         this.assetSource = assetSource;
         this.checksumGenerator = checksumGenerator;
+        this.strictCssUrlRewriting = strictCssUrlRewriting;
     }
 
     @Override
@@ -169,6 +179,18 @@ public class CSSURLRewriter extends DelegatingSRS
                 didReplace = true;
                 
             }
+            else 
+            {
+                final String message = String.format("URL %s, referenced in 
file %s, doesn't exist.", url, baseResource.toURL(), baseResource);
+                if (strictCssUrlRewriting) 
+                {
+                    throw new RuntimeException(message);
+                }
+                else if (logger.isWarnEnabled()) 
+                {
+                    logger.warn(message);
+                }
+            }
             
         }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5b311716/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java 
b/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
index a97683d..b6a6b13 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
@@ -135,9 +135,10 @@ public class AssetsModule
     public StreamableResourceSource 
enableCSSURLRewriting(StreamableResourceSource delegate,
                                                           OperationTracker 
tracker,
                                                           AssetSource 
assetSource,
-                                                          
AssetChecksumGenerator checksumGenerator)
+                                                          
AssetChecksumGenerator checksumGenerator,
+                                                          
@Symbol(SymbolConstants.STRICT_CSS_URL_REWRITING) boolean strictCssUrlRewriting)
     {
-        return new CSSURLRewriter(delegate, tracker, assetSource, 
checksumGenerator);
+        return new CSSURLRewriter(delegate, tracker, assetSource, 
checksumGenerator, strictCssUrlRewriting);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5b311716/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java 
b/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
index e520b09..d13c974 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
@@ -2122,6 +2122,9 @@ public final class TapestryModule
         
         // TAP5-1998
         configuration.add(SymbolConstants.LENIENT_DATE_FORMAT, "false");
+        
+        // TAP5-2187
+        configuration.add(SymbolConstants.STRICT_CSS_URL_REWRITING, "false");
 
     }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5b311716/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
 
b/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
index d491d64..4b8752c 100644
--- 
a/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
+++ 
b/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
@@ -17,7 +17,7 @@ body {
 }
 '''
 
-        def rewriter = new CSSURLRewriter(null, null, null, null)
+        def rewriter = new CSSURLRewriter(null, null, null, null, true)
 
         assertNull rewriter.replaceURLs(input, null)
     }
@@ -43,7 +43,7 @@ body {
         replay()
 
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -78,7 +78,7 @@ body {
 
         replay()
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -112,7 +112,7 @@ body {
         replay()
 
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -134,7 +134,7 @@ body {
 }
 '''
 
-        def rewriter = new CSSURLRewriter(null, null, null, null)
+        def rewriter = new CSSURLRewriter(null, null, null, null, true)
 
         assertNull rewriter.replaceURLs(input, null)
     }
@@ -147,7 +147,7 @@ body {
 }
 '''
 
-        def rewriter = new CSSURLRewriter(null, null, null, null)
+        def rewriter = new CSSURLRewriter(null, null, null, null, true)
 
         assertNull rewriter.replaceURLs(input, null)
     }
@@ -178,7 +178,7 @@ body {
         replay()
 
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -199,7 +199,7 @@ span {
 }
 '''
 
-        def rewriter = new CSSURLRewriter(null, null, null, null)
+        def rewriter = new CSSURLRewriter(null, null, null, null, true)
 
         assertNull rewriter.replaceURLs(input, null)
     }
@@ -229,7 +229,7 @@ div.busy {
 
         replay()
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -269,7 +269,7 @@ div.busy {
 
         replay()
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -305,7 +305,8 @@ h1 {
         ).andReturn asset
 
         expect(asset.toClientURL()).andReturn "/ctx/images/back.png"
-
+        expect(resource.toURL()).andReturn new 
java.net.URL("file:/home/you/layout.css")
+        
         expect(
             assetSource.getAsset(resource, "images/i_dont_exist.png", null)
         ).andReturn null
@@ -313,7 +314,7 @@ h1 {
         replay()
 
 
-        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, false)
 
         def output = rewriter.replaceURLs input, resource
 
@@ -325,9 +326,34 @@ h1 {
   background: white url("images/i_dont_exist.png") attach-x;
 }
 '''
-
         verify()
 
     }
+    
+    // See TAP5-2187
+    @Test(expectedExceptions = RuntimeException.class)
+    void strict_css_url_rewriting() {
+        def input = '''
+h1 {
+  background: white url("images/i_dont_exist.png") attach-x;
+}
+'''
+
+        def assetSource = newMock AssetSource
+        def resource = newMock Resource
+
+        expect(
+            assetSource.getAsset(resource, "images/i_dont_exist.png", null)
+        ).andReturn null
+        expect(resource.toURL()).andReturn new 
java.net.URL("file:/home/you/layout.css")
+    
+        replay()
+
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null, true) 
+        
+        // should throw an exception here
+        rewriter.replaceURLs input, resource
+
+    }
 
 }

Reply via email to