Author: jkuhnert
Date: Tue Dec 27 20:25:04 2005
New Revision: 359389
URL: http://svn.apache.org/viewcvs?rev=359389&view=rev
Log:
Integrated RequestMatcher with AssetService
Modified:
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/asset/AssetService.java
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/engine/encoders/AssetEncoder.java
Modified:
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/asset/AssetService.java
URL:
http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/asset/AssetService.java?rev=359389&r1=359388&r2=359389&view=diff
==============================================================================
---
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/asset/AssetService.java
(original)
+++
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/asset/AssetService.java
Tue Dec 27 20:25:04 2005
@@ -15,6 +15,7 @@
package org.apache.tapestry.asset;
import java.io.BufferedInputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -193,10 +194,11 @@
{
String path = cycle.getParameter(PATH);
String md5Digest = cycle.getParameter(DIGEST);
-
+ boolean checkDigest = !_unprotectedMatcher.containsResource(path);
try
{
- if (!_digestSource.getDigestForResource(path).equals(md5Digest))
+ if (checkDigest
+ &&
!_digestSource.getDigestForResource(path).equals(md5Digest))
{
_response.sendError(HttpServletResponse.SC_FORBIDDEN,
AssetMessages
.md5Mismatch(path));
@@ -204,9 +206,9 @@
}
// If they were vended an asset in the past then it must be up-to
date.
- // Asset URIs change if the underlying file is modified.
+ // Asset URIs change if the underlying file is modified. (unless
unprotected)
- if (_request.getHeader("If-Modified-Since") != null)
+ if (checkDigest && _request.getHeader("If-Modified-Since") != null)
{
_response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
@@ -217,6 +219,11 @@
if (resourceURL == null)
throw new
ApplicationRuntimeException(AssetMessages.noSuchResource(path));
+ //check caching for unprotected resources
+
+ if (!checkDigest && cachedResource(resourceURL))
+ return;
+
URLConnection resourceConnection = resourceURL.openConnection();
writeAssetContent(cycle, path, resourceConnection);
@@ -227,7 +234,34 @@
}
}
-
+
+ /**
+ * Checks if the resource contained within the specified URL
+ * has a modified time greater than the request header value
+ * of <code>If-Modified-Since</code>. If it doesn't then the
+ * response status is set to [EMAIL PROTECTED]
HttpServletResponse#SC_NOT_MODIFIED}.
+ *
+ * @param resourceURL Resource being checked
+ * @return True if resource should be cached and response header was set.
+ * @since 4.1
+ */
+
+ protected boolean cachedResource(URL resourceURL)
+ {
+ File resource = new File(resourceURL.getFile());
+ if (!resource.exists()) return false;
+
+ //even if it doesn't exist in header the value will be -1,
+ //which means we need to write out the contents of the resource
+
+ long modify = Long.parseLong(_request.getHeader("If-Modified-Since"));
+ if (resource.lastModified() > modify)
+ return false;
+
+ _response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+ return true;
+ }
+
/** @since 2.2 */
private void writeAssetContent(IRequestCycle cycle, String resourcePath,
Modified:
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/engine/encoders/AssetEncoder.java
URL:
http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/engine/encoders/AssetEncoder.java?rev=359389&r1=359388&r2=359389&view=diff
==============================================================================
---
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/engine/encoders/AssetEncoder.java
(original)
+++
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/engine/encoders/AssetEncoder.java
Tue Dec 27 20:25:04 2005
@@ -43,11 +43,11 @@
String path = encoding.getParameterValue(AssetService.PATH);
String digest = encoding.getParameterValue(AssetService.DIGEST);
-
+
// _path ends with a slash, path starts with one.
-
+
String fullPath = _path + "/" + digest + path;
-
+
encoding.setServletPath(fullPath);
encoding.setParameterValue(AssetService.PATH, null);
encoding.setParameterValue(AssetService.DIGEST, null);
@@ -58,13 +58,13 @@
{
if (!encoding.getServletPath().equals(_path))
return;
-
+
String pathInfo = encoding.getPathInfo();
-
+
// The lead character is a slash, so find the next slash (the divider
between the
// digest and the path).
int slashx = pathInfo.indexOf('/', 1);
-
+
encoding.setParameterValue(ServiceConstants.SERVICE,
Tapestry.ASSET_SERVICE);
encoding.setParameterValue(AssetService.DIGEST, pathInfo.substring(1,
slashx));
encoding.setParameterValue(AssetService.PATH,
pathInfo.substring(slashx));
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]