import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
+import java.util.zip.GZIPInputStream;
/**
* This component downloads HTTP files
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);
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;
}
}
- 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;
}
}
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;
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 {
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);