summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorBrad Leege <bleege@gmail.com>2016-07-15 14:48:23 -0700
committerBrad Leege <bleege@gmail.com>2016-07-18 10:42:47 -0500
commit371a8ef6b2ee92747072c43c50cde9bb2123d1e7 (patch)
tree1cc97e22f6183a2a1d7d4edee00288e0a1729f25 /platform
parenta9e4e98e8ea3566e77bdb86c211ecc5c7afb0ec8 (diff)
downloadqtlocation-mapboxgl-371a8ef6b2ee92747072c43c50cde9bb2123d1e7.tar.gz
[android] #5536 - Building GzipRequestInterceptor
Diffstat (limited to 'platform')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/GzipRequestInterceptor.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/GzipRequestInterceptor.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/GzipRequestInterceptor.java
new file mode 100644
index 0000000000..1b13e9502f
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/GzipRequestInterceptor.java
@@ -0,0 +1,54 @@
+package com.mapbox.mapboxsdk.telemetry;
+
+import android.util.Log;
+import java.io.IOException;
+import okhttp3.Interceptor;
+import okhttp3.MediaType;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okio.BufferedSink;
+import okio.GzipSink;
+import okio.Okio;
+
+/**
+ * OkHttp Interceptor for Gzipping Telemetry Data requests to the server.
+ * Based on: https://github.com/square/okhttp/wiki/Interceptors
+ */
+public final class GzipRequestInterceptor implements Interceptor {
+
+ private static final String TAG = "GzipRequestInterceptor";
+
+ @Override public Response intercept(Interceptor.Chain chain) throws IOException {
+ Request originalRequest = chain.request();
+ if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
+ Log.d(TAG, "Not compressing");
+ return chain.proceed(originalRequest);
+ }
+
+ Log.d(TAG, "Compressing");
+ Request compressedRequest = originalRequest.newBuilder()
+ .header("Content-Encoding", "gzip")
+ .method(originalRequest.method(), gzip(originalRequest.body()))
+ .build();
+ return chain.proceed(compressedRequest);
+ }
+
+ private RequestBody gzip(final RequestBody body) {
+ return new RequestBody() {
+ @Override public MediaType contentType() {
+ return body.contentType();
+ }
+
+ @Override public long contentLength() {
+ return -1; // We don't know the compressed length in advance!
+ }
+
+ @Override public void writeTo(BufferedSink sink) throws IOException {
+ BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
+ body.writeTo(gzipSink);
+ gzipSink.close();
+ }
+ };
+ }
+}