diff --git a/.gitignore b/.gitignore index 867d20b..abc26d1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ target/* .project bin/* *~ +.idea/* +resty.iml \ No newline at end of file diff --git a/src/main/java/us/monoid/web/AbstractResource.java b/src/main/java/us/monoid/web/AbstractResource.java index d710fe8..475cbeb 100644 --- a/src/main/java/us/monoid/web/AbstractResource.java +++ b/src/main/java/us/monoid/web/AbstractResource.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Map; import java.util.logging.Logger; +import java.util.zip.GZIPInputStream; /** * Abstract base class for all resource handlers you want to use with Resty. @@ -31,7 +32,12 @@ public AbstractResource(Option... options) { void fill(URLConnection anUrlConnection) throws IOException { urlConnection = anUrlConnection; try { - inputStream = anUrlConnection.getInputStream(); + if ("gzip".equals(anUrlConnection.getContentEncoding())) { + inputStream = new GZIPInputStream(anUrlConnection.getInputStream()); + } + else { + inputStream = anUrlConnection.getInputStream(); + } } catch (IOException e) { // Per http://docs.oracle.com/javase/1.5.0/docs/guide/net/http-keepalive.html // (comparable documentation exists for later java versions) @@ -39,7 +45,13 @@ void fill(URLConnection anUrlConnection) throws IOException { // so that keep alive can keep doing its work if (anUrlConnection instanceof HttpURLConnection) { HttpURLConnection conn = (HttpURLConnection) anUrlConnection; - InputStream es = new BufferedInputStream(conn.getErrorStream()); + InputStream es; + if ("gzip".equals(conn.getContentEncoding())) { + es = new BufferedInputStream(new GZIPInputStream(conn.getErrorStream())); + } + else { + es = new BufferedInputStream(conn.getErrorStream()); + } ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { diff --git a/src/test/java/compression/test/CompressionTest.java b/src/test/java/compression/test/CompressionTest.java new file mode 100644 index 0000000..973dc02 --- /dev/null +++ b/src/test/java/compression/test/CompressionTest.java @@ -0,0 +1,31 @@ +package compression.test; + +import org.junit.Test; +import us.monoid.web.Resty; + +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + +/** + * Test that Resty deflates a compressed stream if gzip'd content is requested via the Accept-Encoding header + */ +public class CompressionTest { + + @Test + public void testResponseDeflated() throws IOException { + // no compression + Resty r = new Resty(); + String responseBody = r.text("http://www.whatsmyip.org/http-compression-test/").toString(); + assertTrue("response body expected to contain message that NOT compressed", + responseBody.contains("No, Your Browser is Not Requesting Compressed Content")); + + // with compression + r = new Resty(); + r.withHeader("Accept-Encoding", "gzip"); + responseBody = r.text("http://www.whatsmyip.org/http-compression-test/").toString(); + assertTrue("response body expected to contain message that it WAS compressed", + responseBody.contains("Yes, Your Browser is Requesting Compressed Content")); + } + +}