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 2014/08/27 18:18:06 UTC

[2/2] git commit: TAP5-2344: Asset/module responses are sent without charset

TAP5-2344: Asset/module responses are sent without charset


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

Branch: refs/heads/master
Commit: 000b51e266b2d2b2057b9078f957292999d60f72
Parents: 5066a21
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed Aug 27 09:17:13 2014 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed Aug 27 09:17:13 2014 -0700

----------------------------------------------------------------------
 54_RELEASE_NOTES.md                             |  5 ++
 .../assets/JavaScriptStackAssemblerImpl.java    |  2 +-
 .../services/assets/StreamableResourceImpl.java |  6 +++
 .../services/assets/UTF8ForTextAssets.java      | 51 ++++++++++++++++++++
 .../apache/tapestry5/modules/AssetsModule.java  | 12 +++++
 .../services/assets/StreamableResource.java     |  8 +++
 6 files changed, 83 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/000b51e2/54_RELEASE_NOTES.md
----------------------------------------------------------------------
diff --git a/54_RELEASE_NOTES.md b/54_RELEASE_NOTES.md
index 00e3dcb..d2c0d5a 100644
--- a/54_RELEASE_NOTES.md
+++ b/54_RELEASE_NOTES.md
@@ -140,6 +140,11 @@ The `Alert` class and the `Alerts` component now support rendering raw HTML inst
 
 # Breaking Changes:
 
+## Charset for Assets
+
+Tapestry now assumes that all text/* assets use the utf-8 character set. 
+This is generally the case for JavaScript files (the primary text asset). 
+
 ## tapestry-yuicompressor is replaced by tapestry-webresources
 
 The tapestry-yuicompressor module has been removed and superseded by tapestry-webresources.

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/000b51e2/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/JavaScriptStackAssemblerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/JavaScriptStackAssemblerImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/JavaScriptStackAssemblerImpl.java
index 0b5c4f1..db242cc 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/JavaScriptStackAssemblerImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/JavaScriptStackAssemblerImpl.java
@@ -32,7 +32,7 @@ import java.util.regex.Pattern;
 
 public class JavaScriptStackAssemblerImpl implements JavaScriptStackAssembler
 {
-    private static final String JAVASCRIPT_CONTENT_TYPE = "text/javascript";
+    private static final String JAVASCRIPT_CONTENT_TYPE = "text/javascript;charset=utf-8";
 
     private ThreadLocale threadLocale;
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/000b51e2/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/StreamableResourceImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/StreamableResourceImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/StreamableResourceImpl.java
index d057d9f..e245f64 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/StreamableResourceImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/StreamableResourceImpl.java
@@ -123,6 +123,12 @@ public class StreamableResourceImpl implements StreamableResource
         return responseCustomizer;
     }
 
+    @Override
+    public StreamableResource withContentType(String newContentType)
+    {
+        return new StreamableResourceImpl(description, newContentType, compression, lastModified, bytestreamCache, assetChecksumGenerator, responseCustomizer);
+    }
+
     private StreamableResourceImpl withNewResourceCustomizer(ResponseCustomizer customizer)
     {
         return new StreamableResourceImpl(description, contentType, compression, lastModified, bytestreamCache, assetChecksumGenerator, customizer);

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/000b51e2/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/UTF8ForTextAssets.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/UTF8ForTextAssets.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/UTF8ForTextAssets.java
new file mode 100644
index 0000000..3bf3413
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/UTF8ForTextAssets.java
@@ -0,0 +1,51 @@
+// Licensed 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.tapestry5.internal.services.assets;
+
+import org.apache.tapestry5.ioc.Resource;
+import org.apache.tapestry5.services.assets.ResourceDependencies;
+import org.apache.tapestry5.services.assets.StreamableResource;
+import org.apache.tapestry5.services.assets.StreamableResourceProcessing;
+import org.apache.tapestry5.services.assets.StreamableResourceSource;
+
+import java.io.IOException;
+
+/**
+ * Adds ;charset=utf-8 for text/* resources with no specific character set. This assumes that all test resources are
+ * in UTF-8.
+ *
+ * @since 5.4
+ */
+public class UTF8ForTextAssets extends DelegatingSRS
+{
+    public UTF8ForTextAssets(StreamableResourceSource delegate)
+    {
+        super(delegate);
+    }
+
+
+    @Override
+    public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies) throws IOException
+    {
+        StreamableResource resource = delegate.getStreamableResource(baseResource, processing, dependencies);
+
+        if (resource.getContentType().startsWith("text/")
+                && !resource.getContentType().contains(";")
+                && processing != StreamableResourceProcessing.FOR_AGGREGATION)
+        {
+            return resource.withContentType(resource.getContentType() + ";charset=utf-8");
+        }
+
+        return resource;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/000b51e2/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 702f8ed..b070ff8 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
@@ -161,6 +161,18 @@ public class AssetsModule
     }
 
     /**
+     * Ensures that all "text/*" assets are given the UTF-8 charset.
+     *
+     * @since 5.4
+     */
+    @Decorate(id = "TextUTF8", serviceInterface = StreamableResourceSource.class)
+    @Order("after:CSSURLRewrite")
+    public StreamableResourceSource setupTextAssetsAsUTF8(StreamableResourceSource delegate)
+    {
+        return new UTF8ForTextAssets(delegate);
+    }
+
+    /**
      * Adds content types:
      * <dl>
      * <dt>css</dt>

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/000b51e2/tapestry-core/src/main/java/org/apache/tapestry5/services/assets/StreamableResource.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/assets/StreamableResource.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/assets/StreamableResource.java
index bfa3eb6..9f52fd6 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/assets/StreamableResource.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/assets/StreamableResource.java
@@ -95,4 +95,12 @@ public interface StreamableResource
      * @since 5.4
      */
     ResponseCustomizer getResponseCustomizer();
+
+    /**
+     * Returns a new StreamableResource instance with the new content type.
+     *
+     * @param newContentType
+     * @since 5.4
+     */
+    StreamableResource withContentType(String newContentType);
 }