Skip to content
This repository has been archived by the owner on Apr 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #45 from peet/gzip
Browse files Browse the repository at this point in the history
Added support to deflate gzip compressed responses
  • Loading branch information
beders committed Dec 5, 2015
2 parents 06fe594 + f807571 commit 198bd73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/us/monoid/web/AbstractResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,15 +32,26 @@ 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)
// if an HttpURLConnection was used clear the errorStream and close it
// 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 {
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/compression/test/CompressionTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit 198bd73

Please sign in to comment.