48
48
import java .util .Map ;
49
49
import java .util .Properties ;
50
50
import java .util .TimeZone ;
51
+ import java .util .zip .GZIPInputStream ;
51
52
52
53
import com .infradna .tool .bridge_method_injector .WithBridgeMethods ;
53
54
import org .apache .commons .io .IOUtils ;
@@ -199,20 +200,32 @@ private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean
199
200
uc .setRequestProperty ("Content-Length" ,"0" );
200
201
uc .getOutputStream ().close ();
201
202
}
203
+ uc .setRequestProperty ("Accept-Encoding" , "gzip" );
202
204
205
+ InputStreamReader r = null ;
203
206
try {
204
- InputStreamReader r = new InputStreamReader (uc .getInputStream (), "UTF-8" );
207
+ r = new InputStreamReader (wrapStream ( uc , uc .getInputStream () ), "UTF-8" );
205
208
if (type ==null ) {
206
209
String data = IOUtils .toString (r );
207
210
return null ;
208
211
}
209
212
return MAPPER .readValue (r ,type );
210
213
} catch (IOException e ) {
211
214
handleApiError (e ,uc );
215
+ } finally {
216
+ IOUtils .closeQuietly (r );
212
217
}
213
218
}
214
219
}
215
220
221
+ private InputStream wrapStream (HttpURLConnection uc , InputStream in ) throws IOException {
222
+ String encoding = uc .getContentEncoding ();
223
+ if (encoding ==null ) return in ;
224
+ if (encoding .equals ("gzip" )) return new GZIPInputStream (in );
225
+
226
+ throw new UnsupportedOperationException ("Unexpected Content-Encoding: " +encoding );
227
+ }
228
+
216
229
/**
217
230
* If the error is because of the API limit, wait 10 sec and return normally.
218
231
* Otherwise throw an exception reporting an error.
@@ -231,11 +244,15 @@ private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean
231
244
if (e instanceof FileNotFoundException )
232
245
throw e ; // pass through 404 Not Found to allow the caller to handle it intelligently
233
246
234
- InputStream es = uc .getErrorStream ();
235
- if (es !=null )
236
- throw (IOException )new IOException (IOUtils .toString (es ,"UTF-8" )).initCause (e );
237
- else
238
- throw e ;
247
+ InputStream es = wrapStream (uc , uc .getErrorStream ());
248
+ try {
249
+ if (es !=null )
250
+ throw (IOException )new IOException (IOUtils .toString (es ,"UTF-8" )).initCause (e );
251
+ else
252
+ throw e ;
253
+ } finally {
254
+ IOUtils .closeQuietly (es );
255
+ }
239
256
}
240
257
241
258
/**
0 commit comments