]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4601 Make sure the servlet GZip filter is really used when downloading the...
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 29 Aug 2013 12:31:25 +0000 (14:31 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 29 Aug 2013 12:32:21 +0000 (14:32 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java

index bfcb689b2c7cf576e25fa7423b3145df9865b17e..6d6e75d2ed4e0267e4bab5ce2587fdaf2ef77ef9 100644 (file)
@@ -51,6 +51,7 @@ import java.net.URI;
 import java.nio.charset.Charset;
 import java.util.List;
 import java.util.Map;
+import java.util.zip.GZIPInputStream;
 
 /**
  * This component downloads HTTP files
@@ -250,6 +251,8 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo
         LoggerFactory.getLogger(getClass()).debug("Download: " + uri + " (" + getProxySynthesis(uri, ProxySelector.getDefault()) + ")");
 
         HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+        // allow both GZip and Deflate (ZLib) encodings
+        connection.setRequestProperty("Accept-Encoding", "gzip");
         if (!Strings.isNullOrEmpty(login)) {
           String encoded = new String(Base64.encodeBase64((login + ":" + password).getBytes()));
           connection.setRequestProperty("Authorization", "Basic " + encoded);
@@ -260,6 +263,12 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo
         connection.setInstanceFollowRedirects(true);
         connection.setRequestProperty("User-Agent", userAgent);
 
+        // establish connection, get response headers
+        connection.connect();
+
+        // obtain the encoding returned by the server
+        String encoding = connection.getContentEncoding();
+
         int responseCode = connection.getResponseCode();
         if (responseCode >= 400) {
           InputStream errorResponse = null;
@@ -276,7 +285,15 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo
           }
         }
 
-        return connection.getInputStream();
+        InputStream resultingInputStream = null;
+        // create the appropriate stream wrapper based on the encoding type
+        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
+          resultingInputStream = new GZIPInputStream(connection.getInputStream());
+        }
+        else {
+          resultingInputStream = connection.getInputStream();
+        }
+        return resultingInputStream;
       }
     }
 
index 7a752ab52645f0d8da762ded5f486bd95d1a474d..71c7712c1a47a2224305beda18a1e71265f2d61f 100644 (file)
@@ -47,6 +47,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Arrays;
 import java.util.Properties;
+import java.util.zip.GZIPOutputStream;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.any;
@@ -81,7 +82,15 @@ public class HttpDownloaderTest {
                 e.printStackTrace();
               }
             }
-            resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));
+            if (req.getPath().getPath().contains("/gzip/")) {
+              resp.set("Content-Encoding", "gzip");
+              GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
+              gzipOutputStream.write("GZIP response".getBytes());
+              gzipOutputStream.close();
+            }
+            else {
+              resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));
+            }
           }
         } catch (IOException e) {
         } finally {
@@ -116,6 +125,12 @@ public class HttpDownloaderTest {
     assertThat(text.length()).isGreaterThan(10);
   }
 
+  @Test
+  public void readGzipString() throws URISyntaxException {
+    String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/gzip/"), Charsets.UTF_8);
+    assertThat(text).isEqualTo("GZIP response");
+  }
+
   @Test
   public void readStringWithDefaultTimeout() throws URISyntaxException {
     String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8);