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

Added support do deflate gzip compressed responses #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ target/*
.project
bin/*
*~
.idea/*
resty.iml
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"));
}

}