From 36027423eee058404da750c47eeaee5845999d4b Mon Sep 17 00:00:00 2001 From: "J.D. Mullin" Date: Thu, 27 Feb 2014 14:30:15 -0700 Subject: [PATCH 1/3] Modified to support gzip compressed server responses Modified AbstractResource.fill to support deflating gzip compressed content from the server. To request compressed content in the first place, use restyInstance.withHeader("Accept-Encoding", "gzip") --- .gitignore | 2 ++ .../java/us/monoid/web/AbstractResource.java | 8 ++++- .../compression/test/CompressionTest.java | 31 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/test/java/compression/test/CompressionTest.java 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..8f3e525 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) diff --git a/src/test/java/compression/test/CompressionTest.java b/src/test/java/compression/test/CompressionTest.java new file mode 100644 index 0000000..c3bb29b --- /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")); + } + +} From 97dec3ef6e1949f441d21946b86b7ef4aa36ec3e Mon Sep 17 00:00:00 2001 From: "J.D. Mullin" Date: Thu, 27 Feb 2014 14:37:34 -0700 Subject: [PATCH 2/3] Replaced spaces in last checkin with tabs Just noticed project uses tabs so updated my compression fix. --- .../java/us/monoid/web/AbstractResource.java | 12 ++++---- .../compression/test/CompressionTest.java | 28 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/us/monoid/web/AbstractResource.java b/src/main/java/us/monoid/web/AbstractResource.java index 8f3e525..885ca30 100644 --- a/src/main/java/us/monoid/web/AbstractResource.java +++ b/src/main/java/us/monoid/web/AbstractResource.java @@ -32,12 +32,12 @@ public AbstractResource(Option... options) { void fill(URLConnection anUrlConnection) throws IOException { urlConnection = anUrlConnection; try { - if ("gzip".equals(anUrlConnection.getContentEncoding())) { - inputStream = new GZIPInputStream(anUrlConnection.getInputStream()); - } - else { - 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) diff --git a/src/test/java/compression/test/CompressionTest.java b/src/test/java/compression/test/CompressionTest.java index c3bb29b..973dc02 100644 --- a/src/test/java/compression/test/CompressionTest.java +++ b/src/test/java/compression/test/CompressionTest.java @@ -12,20 +12,20 @@ */ 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")); + @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")); - } + // 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")); + } } From 5205bc67e3f477366d483a45c501863db63c9cec Mon Sep 17 00:00:00 2001 From: "J.D. Mullin" Date: Thu, 27 Feb 2014 16:16:03 -0700 Subject: [PATCH 3/3] Modified AbstractResource.fill to handle compressed error stream too Added check to exception handler in AbstractResource.fill to handle a compressed error stream. --- src/main/java/us/monoid/web/AbstractResource.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/us/monoid/web/AbstractResource.java b/src/main/java/us/monoid/web/AbstractResource.java index 885ca30..475cbeb 100644 --- a/src/main/java/us/monoid/web/AbstractResource.java +++ b/src/main/java/us/monoid/web/AbstractResource.java @@ -45,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 {