|
| 1 | +package info.unterrainer.commons.restclient; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import okhttp3.Headers; |
| 6 | +import okhttp3.Interceptor; |
| 7 | +import okhttp3.MediaType; |
| 8 | +import okhttp3.Request; |
| 9 | +import okhttp3.Request.Builder; |
| 10 | +import okhttp3.RequestBody; |
| 11 | +import okhttp3.Response; |
| 12 | +import okhttp3.ResponseBody; |
| 13 | +import okio.BufferedSink; |
| 14 | +import okio.GzipSink; |
| 15 | +import okio.GzipSource; |
| 16 | +import okio.Okio; |
| 17 | + |
| 18 | +public class GzipInterceptor implements Interceptor { |
| 19 | + @Override |
| 20 | + public Response intercept(final Interceptor.Chain chain) throws IOException { |
| 21 | + |
| 22 | + Request originalRequest = chain.request(); |
| 23 | + |
| 24 | + if (acceptGzipAlreadySetAndNoContentToZip(originalRequest)) |
| 25 | + return chain.proceed(originalRequest); |
| 26 | + |
| 27 | + Builder newRequestBuilder = originalRequest.newBuilder().addHeader("Accept-Encoding", "gzip"); |
| 28 | + if (contentToZip(originalRequest)) |
| 29 | + newRequestBuilder.method(originalRequest.method(), gzip(originalRequest.body())); |
| 30 | + |
| 31 | + // Make the call. |
| 32 | + Response response = chain.proceed(newRequestBuilder.build()); |
| 33 | + |
| 34 | + if (isGzipped(response)) |
| 35 | + return unzip(response); |
| 36 | + else |
| 37 | + return response; |
| 38 | + } |
| 39 | + |
| 40 | + private boolean acceptGzipAlreadySetAndNoContentToZip(final Request originalRequest) { |
| 41 | + return originalRequest.header("Accept-Encoding") == "gzip" |
| 42 | + && (originalRequest.body() == null || originalRequest.header("Content-Encoding") != "gzip"); |
| 43 | + } |
| 44 | + |
| 45 | + private RequestBody gzip(final RequestBody body) { |
| 46 | + return new RequestBody() { |
| 47 | + @Override |
| 48 | + public MediaType contentType() { |
| 49 | + return body.contentType(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public long contentLength() { |
| 54 | + return -1; // We don't know the compressed length in advance! |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void writeTo(final BufferedSink sink) throws IOException { |
| 59 | + BufferedSink gzipSink = Okio.buffer(new GzipSink(sink)); |
| 60 | + body.writeTo(gzipSink); |
| 61 | + gzipSink.close(); |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + private Response unzip(final Response response) throws IOException { |
| 67 | + |
| 68 | + if (response.body() == null) |
| 69 | + return response; |
| 70 | + |
| 71 | + GzipSource gzipSource = new GzipSource(response.body().source()); |
| 72 | + String bodyString = Okio.buffer(gzipSource).readUtf8(); |
| 73 | + |
| 74 | + ResponseBody responseBody = ResponseBody.create(response.body().contentType(), bodyString); |
| 75 | + |
| 76 | + Headers strippedHeaders = response.headers() |
| 77 | + .newBuilder() |
| 78 | + .removeAll("Content-Encoding") |
| 79 | + .removeAll("Content-Length") |
| 80 | + .build(); |
| 81 | + return response.newBuilder().headers(strippedHeaders).body(responseBody).message(response.message()).build(); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + private Boolean isGzipped(final Response response) { |
| 86 | + return response.header("Content-Encoding") != null && response.header("Content-Encoding").equals("gzip"); |
| 87 | + } |
| 88 | + |
| 89 | + private boolean contentToZip(final Request originalRequest) { |
| 90 | + return originalRequest.body() != null && originalRequest.header("Content-Encoding") == "gzip"; |
| 91 | + } |
| 92 | +} |
0 commit comments