This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/main/java/ar/rulosoft/navegadores/UnzippingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ar.rulosoft.navegadores; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Headers; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
import okhttp3.internal.http.RealResponseBody; | ||
import okio.GzipSource; | ||
import okio.Okio; | ||
|
||
|
||
public class UnzippingInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Response response = chain.proceed(chain.request()); | ||
return unzip(response); | ||
} | ||
|
||
private Response unzip(final Response response) { | ||
if (response.body() == null) { | ||
return response; | ||
} | ||
|
||
String contentEncoding = response.headers().get("Content-Encoding"); | ||
if (contentEncoding != null && contentEncoding.equals("gzip")) { | ||
Long contentLength = response.body().contentLength(); | ||
GzipSource responseBody = new GzipSource(response.body().source()); | ||
Headers strippedHeaders = response.headers().newBuilder().build(); | ||
return response.newBuilder().headers(strippedHeaders) | ||
.body(new RealResponseBody(response.body().contentType().toString(), contentLength, Okio.buffer(responseBody))) | ||
.build(); | ||
} else { | ||
return response; | ||
} | ||
} | ||
} | ||
|
||
|