forked from segmentio/analytics-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: bundle the
gzip
support inside the core SDK (#42)
* feat!: bundle the `gzip` support inside the core SDK Now, GZIP will be enabled by default and have also introduced an API to toggle the GZIP default value, called `enableGZIP`. If user continues to use the `client` API to pass the OkHttpClient then `enableGZIP` API value will not be taken into consideration. BREAKING CHANGE: Now user who are using the Java SDK, with self-hosted dataPlane and without their own custom `OkHttpClient` (passed using `client` API)` needs to make sure that their server supports GZIP i.e., their server version is above 1.4, else they could either use `enableGZIP` API or pass a custom OkHttpClient instance without the GZIP interceptor. * refactor: change API name from `enableGZIP` to `setGZIP`
- Loading branch information
1 parent
97ca4ca
commit bce908f
Showing
3 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
analytics/src/main/java/com/rudderstack/sdk/java/analytics/GzipRequestInterceptor.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,51 @@ | ||
package com.rudderstack.sdk.java.analytics; | ||
|
||
import okhttp3.*; | ||
import okio.BufferedSink; | ||
import okio.GzipSink; | ||
import okio.Okio; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This interceptor compresses the HTTP request body. Copied from | ||
* https://github.com/square/okhttp/wiki/Interceptors#rewriting-requests | ||
*/ | ||
final class GzipRequestInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request originalRequest = chain.request(); | ||
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) { | ||
return chain.proceed(originalRequest); | ||
} | ||
|
||
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(); | ||
} | ||
}; | ||
} | ||
} |
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