/hg/icedtea-web: Fix caching of compressed content

omajid at icedtea.classpath.org omajid at icedtea.classpath.org
Mon May 12 16:59:21 UTC 2014


changeset d150e4453b0b in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=d150e4453b0b
author: Omair Majid <omajid at redhat.com>
date: Mon May 12 12:55:21 2014 -0400

	Fix caching of compressed content

	To decide if an item is cached, icedtea-web does a HEAD request and
	compares the Content-Length with the size of the cache entry. This
	works fine when the jars are not compressed. When the jars are
	fetched compressed and then uncompressed on disk, there is a
	mismatch between the cache entry size and the remote Content-Length
	and icedtea-web decides the content is not cached.

	Store the original (compressed) size in the cache info file and use
	that instead of the actual file size in the size-comparison

	2014-05-12  Omair Majid  <omajid at redhat.com>

	        * netx/net/sourceforge/jnlp/cache/CacheEntry.java: Add
	        KEY_CONTENT_ORIGINAL_LENGTH, LENGTH_UNKNOWN.
	        (getOriginalContentLength, setOriginalContentLength)
	        (getLongKey(String,long)): New methods.
	        (isCached): Check if the original content length is recorded and use it,
	        if available, as the content length.
	        * netx/net/sourceforge/jnlp/cache/ResourceTracker.java (downloadResource):
	        If the content was compressed, store original content length in the cache
	        entry.
	        * tests/netx/unit/net/sourceforge/jnlp/cache/CacheEntryTest.java
	        (testOriginalContentLengthIsSetCorrectly)
	        (verifyCachedIfOriginalContentLengthsAreSame): New method.


diffstat:

 ChangeLog                                                      |  15 ++++
 netx/net/sourceforge/jnlp/cache/CacheEntry.java                |  33 +++++++++-
 netx/net/sourceforge/jnlp/cache/ResourceTracker.java           |   3 +
 tests/netx/unit/net/sourceforge/jnlp/cache/CacheEntryTest.java |  23 ++++++
 4 files changed, 73 insertions(+), 1 deletions(-)

diffs (143 lines):

diff -r 461840384bee -r d150e4453b0b ChangeLog
--- a/ChangeLog	Mon May 12 11:42:08 2014 -0400
+++ b/ChangeLog	Mon May 12 12:55:21 2014 -0400
@@ -1,3 +1,18 @@
+2014-05-12  Omair Majid  <omajid at redhat.com>
+
+	* netx/net/sourceforge/jnlp/cache/CacheEntry.java: Add
+	KEY_CONTENT_ORIGINAL_LENGTH and LENGTH_UNKNOWN.
+	(getOriginalContentLength, setOriginalContentLength)
+	(getLongKey(String,long)): New methods.
+	(isCached): Check if the original content length is recorded and use it,
+	if available, as the content length.
+	* netx/net/sourceforge/jnlp/cache/ResourceTracker.java (downloadResource):
+	If the content was compressed, store original content length in the cache
+	entry.
+	* tests/netx/unit/net/sourceforge/jnlp/cache/CacheEntryTest.java
+	(testOriginalContentLengthIsSetCorrectly)
+	(verifyCachedIfOriginalContentLengthsAreSame): New method.
+
 2014-05-12  Omair Majid  <omajid at redhat.com>
 
 	* netx/net/sourceforge/jnlp/cache/CacheEntry.java: Use
diff -r 461840384bee -r d150e4453b0b netx/net/sourceforge/jnlp/cache/CacheEntry.java
--- a/netx/net/sourceforge/jnlp/cache/CacheEntry.java	Mon May 12 11:42:08 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/CacheEntry.java	Mon May 12 12:55:21 2014 -0400
@@ -33,7 +33,10 @@
  */
 public class CacheEntry {
 
+    public static final long LENGTH_UNKNOWN = -1;
+
     private static final String KEY_CONTENT_LENGTH = "content-length";
+    private static final String KEY_CONTENT_ORIGINAL_LENGTH = "content-original-length";
     private static final String KEY_LAST_MODIFIED = "last-modified";
     private static final String KEY_LAST_UPDATED = "last-updated";
 
@@ -96,6 +99,24 @@
         setLongKey(KEY_CONTENT_LENGTH, length);
     }
 
+    /**
+     * Return the length of the original content that was cached. May be different
+     * from the actual cache entry size due to (de)compression.
+     *
+     * @return the content length or {@link #LENGTH_UNKNOWN} if unknown.
+     */
+    public long getOriginalContentLength() {
+        return getLongKey(KEY_CONTENT_ORIGINAL_LENGTH, LENGTH_UNKNOWN);
+    }
+
+    /**
+     * Set the length of the original content that was cached. May be different
+     * from the actual cache entry size due to (de)compression.
+     */
+    public void setOriginalContentLength(long contentLength) {
+        setLongKey(KEY_CONTENT_ORIGINAL_LENGTH, contentLength);
+    }
+
     public long getLastModified() {
         return getLongKey(KEY_LAST_MODIFIED);
     }
@@ -105,10 +126,15 @@
     }
 
     private long getLongKey(String key) {
+        return getLongKey(key, 0);
+    }
+
+    private long getLongKey(String key, long defaultValue) {
         try {
             return Long.parseLong(properties.getProperty(key));
         } catch (Exception ex) {
-            return 0;
+            OutputController.getLogger().log(ex);
+            return defaultValue;
         }
     }
 
@@ -158,6 +184,11 @@
 
         try {
             long cachedLength = localFile.length();
+            String originalLength = properties.getProperty(KEY_CONTENT_ORIGINAL_LENGTH);
+            if (originalLength != null) {
+                cachedLength = Long.parseLong(originalLength);
+            }
+
             long remoteLength = Long.parseLong(properties.getProperty(KEY_CONTENT_LENGTH, "-1"));
 
             if (remoteLength >= 0 && cachedLength != remoteLength)
diff -r 461840384bee -r d150e4453b0b netx/net/sourceforge/jnlp/cache/ResourceTracker.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Mon May 12 11:42:08 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Mon May 12 12:55:21 2014 -0400
@@ -757,6 +757,9 @@
             }
 
             if (!downloadLocationFile.getPath().equals(finalFile.getPath())) {
+                origEntry.setOriginalContentLength(downloadEntry.getRemoteContentLength());
+                origEntry.store();
+
                 downloadEntry.markForDelete();
                 downloadEntry.store();
             }
diff -r 461840384bee -r d150e4453b0b tests/netx/unit/net/sourceforge/jnlp/cache/CacheEntryTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/cache/CacheEntryTest.java	Mon May 12 11:42:08 2014 -0400
+++ b/tests/netx/unit/net/sourceforge/jnlp/cache/CacheEntryTest.java	Mon May 12 12:55:21 2014 -0400
@@ -113,6 +113,16 @@
     }
 
     @Test
+    public void verifyOriginalContentLengthIsSetCorrectly() {
+        long ORIGINAL_CONTENT_LENGTH = 1000;
+
+        CacheEntry entry = new CacheEntry(url, version);
+        entry.setOriginalContentLength(ORIGINAL_CONTENT_LENGTH);
+
+        assertEquals(ORIGINAL_CONTENT_LENGTH, entry.getOriginalContentLength());
+    }
+
+    @Test
     public void verifyNotCachedIfFileIsAbsent() {
         File doesNotExist = new File("/foo/bar/baz/spam/eggs");
 
@@ -143,6 +153,19 @@
     }
 
     @Test
+    public void verifyCachedIfOriginalContentLengthsAreSame() throws IOException {
+        String contents = "FooDECOMPRESSED";
+        long compressedLength = 5;
+        File cachedFile = createCacheFile(contents);
+
+        CacheEntry entry = new TestCacheEntry(url, version, cachedFile);
+        entry.setRemoteContentLength(compressedLength);
+        entry.setOriginalContentLength(compressedLength);
+
+        assertTrue(entry.isCached());
+    }
+
+    @Test
     public void verifyCurrentWhenCacheEntryHasSameTimeStamp() throws IOException {
         long lastModified = 10;
         String contents = "Foo";


More information about the distro-pkg-dev mailing list