aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-08-29 14:31:25 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-08-29 14:32:21 +0200
commit46b50dd409556b90db50393bab9d53aa3be869a6 (patch)
treec357a5c8c67a410d9d12f6b01738e7e2cc41484c /sonar-plugin-api
parentd1e8b9e9c57728d36e703af5f6108d5f19883376 (diff)
downloadsonarqube-46b50dd409556b90db50393bab9d53aa3be869a6.tar.gz
sonarqube-46b50dd409556b90db50393bab9d53aa3be869a6.zip
SONAR-4601 Make sure the servlet GZip filter is really used when downloading the DryRun H2
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java19
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java17
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 {
@@ -117,6 +126,12 @@ public class HttpDownloaderTest {
}
@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);
assertThat(text.length()).isGreaterThan(10);