Skip to content

Commit 1c5f7a5

Browse files
MichalFoksas-gromov
authored andcommitted
Support response in gzip.
1 parent adb5629 commit 1c5f7a5

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[SNAPSHOT]
2+
* Support response in gzip.
3+
14
[2.2.2]
25
* make all APIs to be extentable (have protected constructors, useful for testing)
36

scribejava-core/src/main/java/com/github/scribejava/core/model/Response.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public Response(int code, String message, Map<String, String> headers, String bo
3838
}
3939

4040
private String parseBodyContents() {
41-
body = StreamUtils.getStreamContents(getStream());
41+
if ("gzip".equals(getHeader("Content-Encoding"))) {
42+
body = StreamUtils.getGzipStreamContents(getStream());
43+
} else {
44+
body = StreamUtils.getStreamContents(getStream());
45+
}
4246
return body;
4347
}
4448

scribejava-core/src/main/java/com/github/scribejava/core/utils/StreamUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.InputStream;
55
import java.io.InputStreamReader;
66
import java.io.Reader;
7+
import java.util.zip.GZIPInputStream;
78

89
/**
910
* Utils to deal with Streams.
@@ -37,4 +38,20 @@ public static String getStreamContents(InputStream is) {
3738
throw new IllegalStateException("Error while reading response body", ioe);
3839
}
3940
}
41+
42+
/**
43+
* Return String content from a gzip stream
44+
*
45+
* @param is input stream
46+
* @return string contents
47+
*/
48+
public static String getGzipStreamContents(InputStream is) {
49+
Preconditions.checkNotNull(is, "Cannot get String from a null object");
50+
try {
51+
final GZIPInputStream gis = new GZIPInputStream(is);
52+
return getStreamContents(gis);
53+
} catch (IOException ioe) {
54+
throw new IllegalStateException("Error while reading response body", ioe);
55+
}
56+
}
4057
}

0 commit comments

Comments
 (0)