Author: maartenc
Date: Wed Jun  2 21:20:49 2010
New Revision: 950762

URL: http://svn.apache.org/viewvc?rev=950762&view=rev
Log:
IMPROVEMENT: added support for 'gzip' HTTP Content-Encoding (IVY-1194)

Modified:
    ant/ivy/core/trunk/CHANGES.txt
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java

Modified: ant/ivy/core/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=950762&r1=950761&r2=950762&view=diff
==============================================================================
--- ant/ivy/core/trunk/CHANGES.txt (original)
+++ ant/ivy/core/trunk/CHANGES.txt Wed Jun  2 21:20:49 2010
@@ -115,6 +115,7 @@ for detailed view of each issue, please 
 - NEW: Support ivy.xml parent mechanism (IVY-742) (thanks to Jason Trump and 
Jean-Louis Boudart)
 - NEW: Make ivy.xml <conf description> available (IVY-1158)
 
+- IMPROVEMENT: added support for 'gzip' HTTP Content-Encoding (IVY-1194)
 - IMPROVEMENT: retrieve doesn't retrive files if the current one is more 
recent (IVY-1044)
 - IMPROVEMENT: better diagnostics when reporting bad ivy file in cache 
(IVY-1190)
 - IMPROVEMENT: Support changing="true" for inline retrieve (IVY-1180)

Modified: 
ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java?rev=950762&r1=950761&r2=950762&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java 
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java 
Wed Jun  2 21:20:49 2010
@@ -28,6 +28,7 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.UnknownHostException;
+import java.util.zip.GZIPInputStream;
 
 import org.apache.ivy.Ivy;
 import org.apache.ivy.util.CopyProgressListener;
@@ -116,6 +117,7 @@ public class BasicURLHandler extends Abs
             url = normalizeToURL(url);
             conn = url.openConnection();
             conn.setRequestProperty("User-Agent", "Apache Ivy/" + 
Ivy.getIvyVersion());
+            conn.setRequestProperty("Accept-Encoding", "gzip");
             if (conn instanceof HttpURLConnection) {
                 HttpURLConnection httpCon = (HttpURLConnection) conn;
                 if (!checkStatusCode(url, httpCon)) {
@@ -124,7 +126,12 @@ public class BasicURLHandler extends Abs
                                 + " See log for more detail.");
                 }
             }
-            InputStream inStream  = conn.getInputStream();
+            InputStream inStream;
+            if ("gzip".equals(conn.getContentEncoding())) {
+                inStream = new GZIPInputStream(conn.getInputStream());
+            } else {
+                inStream = conn.getInputStream();
+            }
             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 
             byte[] buffer = new byte[BUFFER_SIZE];
@@ -147,6 +154,7 @@ public class BasicURLHandler extends Abs
             src = normalizeToURL(src);
             srcConn = src.openConnection();
             srcConn.setRequestProperty("User-Agent", "Apache Ivy/" + 
Ivy.getIvyVersion());
+            srcConn.setRequestProperty("Accept-Encoding", "gzip");
             if (srcConn instanceof HttpURLConnection) {
                 HttpURLConnection httpCon = (HttpURLConnection) srcConn;
                 if (!checkStatusCode(src, httpCon)) {
@@ -156,7 +164,15 @@ public class BasicURLHandler extends Abs
                 }
             }
             int contentLength = srcConn.getContentLength();
-            FileUtil.copy(srcConn.getInputStream(), dest, l);
+
+            InputStream inStream;
+            if ("gzip".equals(srcConn.getContentEncoding())) {
+                inStream = new GZIPInputStream(srcConn.getInputStream());
+            } else {
+                inStream = srcConn.getInputStream();
+            }
+
+            FileUtil.copy(inStream, dest, l);
             if (dest.length() != contentLength && contentLength != -1) {
                 dest.delete();
                 throw new IOException(

Modified: 
ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java?rev=950762&r1=950761&r2=950762&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java 
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java 
Wed Jun  2 21:20:49 2010
@@ -28,6 +28,7 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.zip.GZIPInputStream;
 
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.Header;
@@ -46,6 +47,7 @@ import org.apache.commons.httpclient.met
 import org.apache.commons.httpclient.methods.HeadMethod;
 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
 import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.apache.ivy.Ivy;
 import org.apache.ivy.util.CopyProgressListener;
 import org.apache.ivy.util.FileUtil;
@@ -110,7 +112,16 @@ public class HttpClientHandler extends A
                 throw new IOException("The HTTP response code for " + src
                         + " did not indicate a success." + " See log for more 
detail.");
             }
-            FileUtil.copy(get.getResponseBodyAsStream(), dest, l);
+            
+            InputStream is;
+            Header[] contentEncodings = 
get.getResponseHeaders("Content-Encoding");
+            if ((contentEncodings.length > 0) && 
"gzip".equals(contentEncodings[0].getValue())) {
+                is = new GZIPInputStream(get.getResponseBodyAsStream());
+            } else {
+                is = get.getResponseBodyAsStream();
+            }
+
+            FileUtil.copy(is, dest, l);
             dest.setLastModified(getLastModified(get));
         } finally {
             get.releaseConnection();
@@ -244,6 +255,7 @@ public class HttpClientHandler extends A
 
         GetMethod get = new GetMethod(normalizeToString(url));
         get.setDoAuthentication(useAuthentication(url) || 
useProxyAuthentication());
+        get.setRequestHeader("Accept-Encoding", "gzip");
         client.executeMethod(get);
         return get;
     }
@@ -286,7 +298,7 @@ public class HttpClientHandler extends A
             }
 
             // user-agent
-            httpClient.getParams().setParameter("http.useragent",
+            httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,
                 "Apache Ivy/" + Ivy.getIvyVersion());
 
             // authentication
@@ -316,7 +328,13 @@ public class HttpClientHandler extends A
 
         private GETInputStream(GetMethod get) throws IOException {
             this.get = get;
-            is = get.getResponseBodyAsStream();
+            
+            Header[] contentEncodings = 
get.getResponseHeaders("Content-Encoding");
+            if ((contentEncodings.length > 0) && 
"gzip".equals(contentEncodings[0].getValue())) {
+                is = new GZIPInputStream(get.getResponseBodyAsStream());
+            } else {
+                is = get.getResponseBodyAsStream();
+            }
         }
 
         public int available() throws IOException {


Reply via email to