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 2010/01/06 22:28:17 UTC
svn commit: r896677 - in
/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services:
ResourceStreamerImpl.java VirtualAssetStreamerImpl.java
Author: hlship
Date: Wed Jan 6 21:28:16 2010
New Revision: 896677
URL: http://svn.apache.org/viewvc?rev=896677&view=rev
Log:
TAP5-917: Don't set Expires header when tapestry.production-mode is false
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ResourceStreamerImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/VirtualAssetStreamerImpl.java
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ResourceStreamerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ResourceStreamerImpl.java?rev=896677&r1=896676&r2=896677&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ResourceStreamerImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ResourceStreamerImpl.java Wed Jan 6 21:28:16 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
//
// 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
+// 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,
@@ -46,20 +46,25 @@
private final int compressionCutoff;
+ private final boolean productionMode;
+
public ResourceStreamerImpl(Request request,
- Response response,
+ Response response,
+
+ Context context,
- Context context,
+ ResourceCache resourceCache,
- ResourceCache resourceCache,
+ Map<String, String> configuration,
- Map<String, String> configuration,
+ ResponseCompressionAnalyzer analyzer,
- ResponseCompressionAnalyzer analyzer,
+ @Symbol(SymbolConstants.MIN_GZIP_SIZE)
+ int compressionCutoff,
- @Symbol(SymbolConstants.MIN_GZIP_SIZE)
- int compressionCutoff)
+ @Symbol(SymbolConstants.PRODUCTION_MODE)
+ boolean productionMode)
{
this.request = request;
this.response = response;
@@ -68,6 +73,7 @@
this.configuration = configuration;
this.analyzer = analyzer;
this.compressionCutoff = compressionCutoff;
+ this.productionMode = productionMode;
}
public void streamResource(Resource resource) throws IOException
@@ -81,13 +87,15 @@
long lastModified = streamble.getLastModified();
response.setDateHeader("Last-Modified", lastModified);
- response.setDateHeader("Expires", lastModified + InternalConstants.TEN_YEARS);
+
+ if (productionMode)
+ response.setDateHeader("Expires", lastModified + InternalConstants.TEN_YEARS);
String contentType = identifyContentType(resource, streamble);
- boolean compress = analyzer.isGZipSupported() &&
- streamble.getSize(false) >= compressionCutoff &&
- analyzer.isCompressable(contentType);
+ boolean compress = analyzer.isGZipSupported()
+ && streamble.getSize(false) >= compressionCutoff
+ && analyzer.isCompressable(contentType);
int contentLength = streamble.getSize(compress);
@@ -95,7 +103,8 @@
response.setContentLength(contentLength);
if (compress)
- response.setHeader(InternalConstants.CONTENT_ENCODING_HEADER, InternalConstants.GZIP_CONTENT_ENCODING);
+ response.setHeader(InternalConstants.CONTENT_ENCODING_HEADER,
+ InternalConstants.GZIP_CONTENT_ENCODING);
InputStream is = null;
@@ -123,17 +132,21 @@
return identifyContentType(resource, resourceCache.getStreamableResource(resource));
}
- private String identifyContentType(Resource resource, StreamableResource streamble) throws IOException
+ private String identifyContentType(Resource resource, StreamableResource streamble)
+ throws IOException
{
String contentType = streamble.getContentType();
- if ("content/unknown".equals(contentType)) contentType = null;
+ if ("content/unknown".equals(contentType))
+ contentType = null;
- if (contentType != null) return contentType;
+ if (contentType != null)
+ return contentType;
contentType = context.getMimeType(resource.getPath());
- if (contentType != null) return contentType;
+ if (contentType != null)
+ return contentType;
String file = resource.getFile();
int dotx = file.lastIndexOf('.');
@@ -145,8 +158,6 @@
contentType = configuration.get(extension);
}
- return contentType != null
- ? contentType
- : "application/octet-stream";
+ return contentType != null ? contentType : "application/octet-stream";
}
}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/VirtualAssetStreamerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/VirtualAssetStreamerImpl.java?rev=896677&r1=896676&r2=896677&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/VirtualAssetStreamerImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/VirtualAssetStreamerImpl.java Wed Jan 6 21:28:16 2010
@@ -1,10 +1,10 @@
-// Copyright 2009 The Apache Software Foundation
+// Copyright 2009, 2010 The Apache Software Foundation
//
// 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
+// 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,
@@ -14,21 +14,35 @@
package org.apache.tapestry5.internal.services;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.Map;
+import java.util.zip.GZIPOutputStream;
+
+import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.internal.InternalConstants;
import org.apache.tapestry5.internal.TapestryInternalUtils;
import org.apache.tapestry5.ioc.Resource;
+import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.json.JSONArray;
-import org.apache.tapestry5.services.*;
-
-import java.io.*;
-import java.util.Map;
-import java.util.zip.GZIPOutputStream;
+import org.apache.tapestry5.services.ClientDataEncoder;
+import org.apache.tapestry5.services.InvalidationListener;
+import org.apache.tapestry5.services.Request;
+import org.apache.tapestry5.services.Response;
+import org.apache.tapestry5.services.ResponseCompressionAnalyzer;
/**
- * This implementation uses {@link org.apache.tapestry5.internal.services.ResourceCache}, but is also a listener of
- * invalidation events from ResourceCache. When a Asset resource changes, any cached data in this service is discarded.
- *
+ * This implementation uses {@link org.apache.tapestry5.internal.services.ResourceCache}, but is
+ * also a listener of
+ * invalidation events from ResourceCache. When a Asset resource changes, any cached data in this
+ * service is discarded.
+ *
* @since 5.1.0.2
*/
public class VirtualAssetStreamerImpl implements VirtualAssetStreamer, InvalidationListener
@@ -46,18 +60,25 @@
private final Response response;
/**
- * Cache keyed on client data (encoding the Asset paths to combine), value is the assembled virtual asset.
+ * Cache keyed on client data (encoding the Asset paths to combine), value is the assembled
+ * virtual asset.
*/
private final Map<String, ByteArrayOutputStream> cache = CollectionFactory.newConcurrentMap();
/**
* Cached keyed on client data ... value is the GZIP compressed value for the virtual asset.
*/
- private final Map<String, ByteArrayOutputStream> compressedCache = CollectionFactory.newConcurrentMap();
+ private final Map<String, ByteArrayOutputStream> compressedCache = CollectionFactory
+ .newConcurrentMap();
- public VirtualAssetStreamerImpl(ResourceCache resourceCache, ResponseCompressionAnalyzer compressionAnalyzer,
- ClientDataEncoder clientDataEncoder, AssetResourceLocator resourceLocator,
- Request request, Response response)
+ private final boolean productionMode;
+
+ public VirtualAssetStreamerImpl(ResourceCache resourceCache,
+ ResponseCompressionAnalyzer compressionAnalyzer, ClientDataEncoder clientDataEncoder,
+ AssetResourceLocator resourceLocator, Request request, Response response,
+
+ @Symbol(SymbolConstants.PRODUCTION_MODE)
+ boolean productionMode)
{
this.resourceCache = resourceCache;
this.compressionAnalyzer = compressionAnalyzer;
@@ -65,6 +86,7 @@
this.resourceLocator = resourceLocator;
this.request = request;
this.response = response;
+ this.productionMode = productionMode;
}
public void streamVirtualAsset(String clientData) throws IOException
@@ -79,16 +101,20 @@
long lastModified = System.currentTimeMillis();
response.setDateHeader("Last-Modified", lastModified);
- response.setDateHeader("Expires", lastModified + InternalConstants.TEN_YEARS);
+
+ if (productionMode)
+ response.setDateHeader("Expires", lastModified + InternalConstants.TEN_YEARS);
response.setContentLength(stream.size());
if (compress)
- response.setHeader(InternalConstants.CONTENT_ENCODING_HEADER, InternalConstants.GZIP_CONTENT_ENCODING);
+ response.setHeader(InternalConstants.CONTENT_ENCODING_HEADER,
+ InternalConstants.GZIP_CONTENT_ENCODING);
request.setAttribute(InternalConstants.SUPPRESS_COMPRESSION, true);
- // CSS support is problematic, because of relative URLs inside the CSS files. For the moment, only
+ // CSS support is problematic, because of relative URLs inside the CSS files. For the
+ // moment, only
// JavaScript is supported.
OutputStream output = response.getOutputStream("text/javascript");
@@ -98,7 +124,8 @@
output.close();
}
- private ByteArrayOutputStream getVirtualStream(String clientData, boolean compress) throws IOException
+ private ByteArrayOutputStream getVirtualStream(String clientData, boolean compress)
+ throws IOException
{
return compress ? getCompressedVirtualStream(clientData) : getVirtualStream(clientData);
}
@@ -133,9 +160,10 @@
}
/**
- * This is where it gets tricky. We need to take the clientData and turn it back into a bytestream, then read the
+ * This is where it gets tricky. We need to take the clientData and turn it back into a
+ * bytestream, then read the
* UTF strings for the paths out of it and add those all together.
- *
+ *
* @param clientData
* @return
*/
@@ -153,8 +181,7 @@
return result;
}
- private ByteArrayOutputStream constructVirtualAssetStream(String clientData)
- throws IOException
+ private ByteArrayOutputStream constructVirtualAssetStream(String clientData) throws IOException
{
ByteArrayOutputStream result = new ByteArrayOutputStream();