changeset 1631:02e02b5a01c0

Support pack200/gzip compression in Netx 2009-07-09 Omair Majid <omajid@redhat.com> * rt/net/sourceforge/jnlp/cache/ResourceTracker.java: (downloadResource): Accept and download content with pack200 or gzip compression and uncompress it on the fly. (initializeResource): Accept content with pack200 or gzip compression. (getVersionedResourceURL): Add javadoc.
author Omair Majid <omajid@redhat.com>
date Thu, 09 Jul 2009 17:18:02 -0400
parents d0694a669da3
children 7acbff01007f
files ChangeLog rt/net/sourceforge/jnlp/cache/ResourceTracker.java
diffstat 2 files changed, 103 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Jul 09 15:28:30 2009 -0400
+++ b/ChangeLog	Thu Jul 09 17:18:02 2009 -0400
@@ -1,3 +1,11 @@
+2009-07-09  Omair Majid  <omajid@redhat.com>
+
+	* rt/net/sourceforge/jnlp/cache/ResourceTracker.java:
+	(downloadResource): Accept and download content with pack200 or gzip
+	compression and uncompress it on the fly.
+	(initializeResource): Accept content with pack200 or gzip compression.
+	(getVersionedResourceURL): Add javadoc.
+
 2009-07-09  Deepak Bhole  <dbhole@redhat.com>
 
 	* IcedTeaPlugin.cc: Add suppport for cookie info requests from applets.
--- a/rt/net/sourceforge/jnlp/cache/ResourceTracker.java	Thu Jul 09 15:28:30 2009 -0400
+++ b/rt/net/sourceforge/jnlp/cache/ResourceTracker.java	Thu Jul 09 17:18:02 2009 -0400
@@ -17,14 +17,30 @@
 
 package net.sourceforge.jnlp.cache;
 
-import java.io.*;
-import java.net.*;
-import java.util.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Pack200;
+import java.util.jar.Pack200.Unpacker;
+import java.util.zip.GZIPInputStream;
 
-import net.sourceforge.jnlp.*;
-import net.sourceforge.jnlp.event.*;
-import net.sourceforge.jnlp.runtime.*;
-import net.sourceforge.jnlp.util.*;
+import net.sourceforge.jnlp.Version;
+import net.sourceforge.jnlp.event.DownloadEvent;
+import net.sourceforge.jnlp.event.DownloadListener;
+import net.sourceforge.jnlp.runtime.JNLPRuntime;
+import net.sourceforge.jnlp.util.WeakList;
 
 /**
  * This class tracks the downloading of various resources of a
@@ -598,7 +614,9 @@
     }
 
     /**
-     * Downloads an resource to a file.
+     * Downloads a resource to a file, uncompressing it if required
+     * 
+     * @param resource the resource to download
      */
     private void downloadResource(Resource resource) {
         resource.fireDownloadEvent(); // fire DOWNLOADING
@@ -606,9 +624,34 @@
         try {
             // create out second in case in does not exist
             URLConnection con = getVersionedResourceURL(resource).openConnection();
+            con.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
+            
+            con.connect();
+
+            /*
+             * We dont really know what we are downloading. If we ask for
+             * foo.jar, the server might send us foo.jar.pack.gz or foo.jar.gz
+             * instead. So we save the file with the appropriate extension
+             */
+            URL downloadLocation = resource.location;
+
+            String contentEncoding = con.getContentEncoding();
+
+            if (JNLPRuntime.isDebug()) {
+                System.err.println("Content encoding for " + resource.location + ": "
+                        + contentEncoding);
+            }
+
+            if (contentEncoding != null) {
+                if (contentEncoding.equals("gzip")) {
+                    downloadLocation = new URL(downloadLocation.toString() + ".gz");
+                } else if (contentEncoding.equals("pack200-gzip")) {
+                    downloadLocation = new URL(downloadLocation.toString() + ".pack.gz");
+                }
+            }
 
             InputStream in = new BufferedInputStream(con.getInputStream());
-            OutputStream out = CacheUtil.getOutputStream(resource.location, resource.downloadVersion);
+            OutputStream out = CacheUtil.getOutputStream(downloadLocation, resource.downloadVersion);
             byte buf[] = new byte[1024];
             int rlen;
 
@@ -623,7 +666,45 @@
             // explicitly close the URLConnection.
             if (con instanceof HttpURLConnection)
                 ((HttpURLConnection)con).disconnect();
+            
+            /*
+             * If the file was compressed, uncompress it.
+             */
+            if (contentEncoding != null) {
+                if (contentEncoding.equals("gzip")) {
+                    GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(CacheUtil
+                            .getCacheFile(downloadLocation, resource.downloadVersion)));
+                    InputStream inputStream = new BufferedInputStream(gzInputStream);
 
+                    BufferedOutputStream outputStream = new BufferedOutputStream(
+                            new FileOutputStream(CacheUtil.getCacheFile(resource.location,
+                                    resource.downloadVersion)));
+
+                    while (-1 != (rlen = inputStream.read(buf))) {
+                        outputStream.write(buf, 0, rlen);
+                    }
+
+                    outputStream.close();
+                    inputStream.close();
+                    gzInputStream.close();
+                    
+                } else if (contentEncoding.equals("pack200-gzip")) {
+                    GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(
+                            CacheUtil.getCacheFile(downloadLocation, resource.downloadVersion)));
+                    InputStream inputStream = new BufferedInputStream(gzInputStream);
+
+                    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(
+                            CacheUtil.getCacheFile(resource.location, resource.downloadVersion)));
+
+                    Unpacker unpacker = Pack200.newUnpacker();
+                    unpacker.unpack(inputStream, outputStream);
+
+                    outputStream.close();
+                    inputStream.close();
+                    gzInputStream.close();
+                }
+            }
+            
             resource.changeStatus(DOWNLOADING, DOWNLOADED);
             synchronized(lock) {
                 lock.notifyAll(); // wake up wait's to check for completion
@@ -654,6 +735,7 @@
 
             // connect
             URLConnection connection = getVersionedResourceURL(resource).openConnection(); // this won't change so should be okay unsynchronized
+            connection.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
 
             int size = connection.getContentLength();
             boolean current = CacheUtil.isCurrent(resource.location, resource.requestVersion, connection) && resource.getUpdatePolicy() != UpdatePolicy.FORCE;
@@ -698,8 +780,10 @@
         }
     }
 
-    
- 
+    /**
+     * Returns the versioned url for a resource
+     * @param resource the resource to get the url for 
+     */
     private URL getVersionedResourceURL(Resource resource) {
         String actualLocation = resource.location.getProtocol() + "://"
                 + resource.location.getHost();