Skip to content

Commit 3ad9567

Browse files
ajaaymsduskis
authored andcommitted
Add option to return raw stream from response (#579)
* Add option to return raw stream * fix formatting
1 parent 970e998 commit 3ad9567

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
224224
/** OpenCensus tracing component. */
225225
private final Tracer tracer = OpenCensusUtils.getTracer();
226226

227+
/**
228+
* Determines whether {@link HttpResponse#getContent()} of this request should return raw
229+
* input stream or not.
230+
*
231+
* <p>
232+
* It is {@code false} by default.
233+
* </p>
234+
*/
235+
private boolean responseReturnRawInputStream = false;
236+
227237
/**
228238
* @param transport HTTP transport
229239
* @param requestMethod HTTP request method or {@code null} for none
@@ -835,6 +845,31 @@ public HttpRequest setSuppressUserAgentSuffix(boolean suppressUserAgentSuffix) {
835845
return this;
836846
}
837847

848+
/**
849+
* Returns whether {@link HttpResponse#getContent()} should return raw input stream for this
850+
* request.
851+
*
852+
* @since 1.29
853+
*/
854+
public boolean getResponseReturnRawInputStream() {
855+
return responseReturnRawInputStream;
856+
}
857+
858+
/**
859+
* Sets whether {@link HttpResponse#getContent()} should return raw input stream for this
860+
* request.
861+
*
862+
* <p>
863+
* The default value is {@code false}.
864+
* </p>
865+
*
866+
* @since 1.29
867+
*/
868+
public HttpRequest setResponseReturnRawInputStream(boolean responseReturnRawInputStream) {
869+
this.responseReturnRawInputStream = responseReturnRawInputStream;
870+
return this;
871+
}
872+
838873
/**
839874
* Execute the HTTP request and returns the HTTP response.
840875
*

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public final class HttpResponse {
8282
/** HTTP request. */
8383
private final HttpRequest request;
8484

85+
/** Whether {@link #getContent()} should return raw input stream. */
86+
private final boolean returnRawInputStream;
87+
8588
/**
8689
* Determines the limit to the content size that will be logged during {@link #getContent()}.
8790
*
@@ -118,6 +121,7 @@ public final class HttpResponse {
118121

119122
HttpResponse(HttpRequest request, LowLevelHttpResponse response) throws IOException {
120123
this.request = request;
124+
this.returnRawInputStream = request.getResponseReturnRawInputStream();
121125
contentLoggingLimit = request.getContentLoggingLimit();
122126
loggingEnabled = request.isLoggingEnabled();
123127
this.response = response;
@@ -359,7 +363,8 @@ public InputStream getContent() throws IOException {
359363
try {
360364
// gzip encoding (wrap content with GZipInputStream)
361365
String contentEncoding = this.contentEncoding;
362-
if (contentEncoding != null && contentEncoding.contains("gzip")) {
366+
if (!returnRawInputStream && contentEncoding != null && contentEncoding
367+
.contains("gzip")) {
363368
lowLevelResponseContent = new GZIPInputStream(lowLevelResponseContent);
364369
}
365370
// logging (wrap content with LoggingInputStream)

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.text.NumberFormat;
3030
import java.util.Arrays;
3131
import java.util.logging.Level;
32+
import java.util.zip.GZIPInputStream;
3233
import junit.framework.TestCase;
3334

3435
/**
@@ -400,4 +401,26 @@ public LowLevelHttpResponse execute() throws IOException {
400401
transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL);
401402
request.execute().getContent();
402403
}
404+
405+
public void testGetContent_gzipEncoding_ReturnRawStream() throws IOException {
406+
HttpTransport transport = new MockHttpTransport() {
407+
@Override
408+
public LowLevelHttpRequest buildRequest(String method, final String url) throws IOException {
409+
return new MockLowLevelHttpRequest() {
410+
@Override
411+
public LowLevelHttpResponse execute() throws IOException {
412+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
413+
result.setContent("");
414+
result.setContentEncoding("gzip");
415+
result.setContentType("text/plain");
416+
return result;
417+
}
418+
};
419+
}
420+
};
421+
HttpRequest request =
422+
transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL);
423+
request.setResponseReturnRawInputStream(true);
424+
assertFalse("it should not decompress stream", request.execute().getContent() instanceof GZIPInputStream);
425+
}
403426
}

0 commit comments

Comments
 (0)