From 46b50dd409556b90db50393bab9d53aa3be869a6 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 29 Aug 2013 14:31:25 +0200 Subject: [PATCH] SONAR-4601 Make sure the servlet GZip filter is really used when downloading the DryRun H2 --- .../org/sonar/api/utils/HttpDownloader.java | 19 ++++++++++++++++++- .../sonar/api/utils/HttpDownloaderTest.java | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java index bfcb689b2c7..6d6e75d2ed4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java @@ -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; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java index 7a752ab5264..71c7712c1a4 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java @@ -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); -- 2.39.5