Created
November 27, 2018 09:05
-
-
Save fcaldarelli/b46fc150b0be5fa3072a7dc9690485f0 to your computer and use it in GitHub Desktop.
GZip logging interceptor for OkHttp and Retrofit
This file contains hidden or 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
package myapp; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import okhttp3.Headers; | |
import okhttp3.Interceptor; | |
import okhttp3.MediaType; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.ResponseBody; | |
import okhttp3.internal.platform.Platform; | |
import okio.Buffer; | |
import okio.BufferedSource; | |
import okio.GzipSource; | |
import static okhttp3.internal.platform.Platform.INFO; | |
public final class GZipLoggingInterceptor implements Interceptor { | |
private static final Charset UTF8 = Charset.forName("UTF-8"); | |
public interface Logger { | |
void log(String message); | |
Logger DEFAULT = new Logger() { | |
@Override public void log(String message) { | |
Platform.get().log(INFO, message, null); | |
} | |
}; | |
} | |
public GZipLoggingInterceptor() { | |
this(Logger.DEFAULT); | |
} | |
public GZipLoggingInterceptor(Logger logger) { | |
this.logger = logger; | |
} | |
private final Logger logger; | |
@Override public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
Response response; | |
try { | |
response = chain.proceed(request); | |
} catch (Exception e) { | |
logger.log("<-- HTTP FAILED: " + e); | |
throw e; | |
} | |
ResponseBody responseBody = response.body(); | |
long contentLength = responseBody.contentLength(); | |
Headers headers = response.headers(); | |
BufferedSource source = responseBody.source(); | |
source.request(Long.MAX_VALUE); // Buffer the entire body. | |
Buffer buffer = source.buffer(); | |
if ("gzip".equalsIgnoreCase(headers.get("Content-Encoding"))) { | |
GzipSource gzippedResponseBody = null; | |
try { | |
gzippedResponseBody = new GzipSource(buffer.clone()); | |
buffer = new Buffer(); | |
buffer.writeAll(gzippedResponseBody); | |
} finally { | |
if (gzippedResponseBody != null) { | |
gzippedResponseBody.close(); | |
} | |
} | |
Charset charset = UTF8; | |
MediaType contentType = responseBody.contentType(); | |
if (contentType != null) { | |
charset = contentType.charset(UTF8); | |
} | |
if ((contentLength != 0)&&(buffer!=null)) { | |
logger.log(""); | |
String bufferString = buffer.clone().readString(charset); | |
logger.log("<-- BEGIN GZIP CONTENT (" + bufferString.length() + "-byte)"); | |
logger.log(bufferString); | |
logger.log("<-- END GZIP CONTENT (" + bufferString.length() + "-byte)"); | |
} | |
} | |
return response; | |
} | |
} |
This file contains hidden or 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
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); | |
clientBuilder.addNetworkInterceptor(new GZipLoggingInterceptor()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment