File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed
main/java/com/google/api/client/http
test/java/com/google/api/client/http Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -224,6 +224,16 @@ static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
224
224
/** OpenCensus tracing component. */
225
225
private final Tracer tracer = OpenCensusUtils .getTracer ();
226
226
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
+
227
237
/**
228
238
* @param transport HTTP transport
229
239
* @param requestMethod HTTP request method or {@code null} for none
@@ -835,6 +845,31 @@ public HttpRequest setSuppressUserAgentSuffix(boolean suppressUserAgentSuffix) {
835
845
return this ;
836
846
}
837
847
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
+
838
873
/**
839
874
* Execute the HTTP request and returns the HTTP response.
840
875
*
Original file line number Diff line number Diff line change @@ -82,6 +82,9 @@ public final class HttpResponse {
82
82
/** HTTP request. */
83
83
private final HttpRequest request ;
84
84
85
+ /** Whether {@link #getContent()} should return raw input stream. */
86
+ private final boolean returnRawInputStream ;
87
+
85
88
/**
86
89
* Determines the limit to the content size that will be logged during {@link #getContent()}.
87
90
*
@@ -118,6 +121,7 @@ public final class HttpResponse {
118
121
119
122
HttpResponse (HttpRequest request , LowLevelHttpResponse response ) throws IOException {
120
123
this .request = request ;
124
+ this .returnRawInputStream = request .getResponseReturnRawInputStream ();
121
125
contentLoggingLimit = request .getContentLoggingLimit ();
122
126
loggingEnabled = request .isLoggingEnabled ();
123
127
this .response = response ;
@@ -359,7 +363,8 @@ public InputStream getContent() throws IOException {
359
363
try {
360
364
// gzip encoding (wrap content with GZipInputStream)
361
365
String contentEncoding = this .contentEncoding ;
362
- if (contentEncoding != null && contentEncoding .contains ("gzip" )) {
366
+ if (!returnRawInputStream && contentEncoding != null && contentEncoding
367
+ .contains ("gzip" )) {
363
368
lowLevelResponseContent = new GZIPInputStream (lowLevelResponseContent );
364
369
}
365
370
// logging (wrap content with LoggingInputStream)
Original file line number Diff line number Diff line change 29
29
import java .text .NumberFormat ;
30
30
import java .util .Arrays ;
31
31
import java .util .logging .Level ;
32
+ import java .util .zip .GZIPInputStream ;
32
33
import junit .framework .TestCase ;
33
34
34
35
/**
@@ -400,4 +401,26 @@ public LowLevelHttpResponse execute() throws IOException {
400
401
transport .createRequestFactory ().buildHeadRequest (HttpTesting .SIMPLE_GENERIC_URL );
401
402
request .execute ().getContent ();
402
403
}
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
+ }
403
426
}
You can’t perform that action at this time.
0 commit comments