Skip to content

Commit a71e0e8

Browse files
authored
Add Gzip example (segmentio#132)
Rather than support gzip natively, I've added an example to show how customers can do this themselves. I'll add this to our public facing docs.
1 parent ae3f0a6 commit a71e0e8

2 files changed

Lines changed: 74 additions & 4 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sample;
2+
3+
import java.io.IOException;
4+
import okhttp3.*;
5+
import okio.BufferedSink;
6+
import okio.GzipSink;
7+
import okio.Okio;
8+
9+
/**
10+
* This interceptor compresses the HTTP request body. Copied from
11+
* https://github.com/square/okhttp/wiki/Interceptors#rewriting-requests
12+
*/
13+
final class GzipRequestInterceptor implements Interceptor {
14+
@Override
15+
public Response intercept(Interceptor.Chain chain) throws IOException {
16+
Request originalRequest = chain.request();
17+
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
18+
return chain.proceed(originalRequest);
19+
}
20+
21+
Request compressedRequest =
22+
originalRequest
23+
.newBuilder()
24+
.header("Content-Encoding", "gzip")
25+
.method(originalRequest.method(), gzip(originalRequest.body()))
26+
.build();
27+
return chain.proceed(compressedRequest);
28+
}
29+
30+
private RequestBody gzip(final RequestBody body) {
31+
return new RequestBody() {
32+
@Override
33+
public MediaType contentType() {
34+
return body.contentType();
35+
}
36+
37+
@Override
38+
public long contentLength() {
39+
return -1; // We don't know the compressed length in advance!
40+
}
41+
42+
@Override
43+
public void writeTo(BufferedSink sink) throws IOException {
44+
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
45+
body.writeTo(gzipSink);
46+
gzipSink.close();
47+
}
48+
};
49+
}
50+
}

analytics-sample/src/main/java/sample/Main.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
package sample;
22

3+
import com.jakewharton.retrofit.Ok3Client;
34
import com.segment.analytics.Analytics;
45
import com.segment.analytics.messages.TrackMessage;
56
import java.util.LinkedHashMap;
67
import java.util.Map;
78
import java.util.UUID;
9+
import java.util.concurrent.TimeUnit;
810
import java.util.concurrent.atomic.AtomicInteger;
11+
import okhttp3.OkHttpClient;
12+
import retrofit.client.Client;
913

1014
public class Main {
1115
public static void main(String... args) throws Exception {
1216
final BlockingFlush blockingFlush = BlockingFlush.create();
1317

1418
// https://segment.com/segment-engineering/sources/test-java/debugger
1519
final Analytics analytics =
16-
Analytics.builder("xemyw6oe3n") //
20+
Analytics.builder("xemyw6oe3n")
1721
.plugin(blockingFlush.plugin())
1822
.plugin(new LoggingPlugin())
23+
.client(createClient())
1924
.build();
2025

2126
final String userId = System.getProperty("user.name");
@@ -27,9 +32,9 @@ public static void main(String... args) throws Exception {
2732
Map<String, Object> properties = new LinkedHashMap<>();
2833
properties.put("count", count.incrementAndGet());
2934
analytics.enqueue(
30-
TrackMessage.builder("Java Test") //
31-
.properties(properties) //
32-
.anonymousId(anonymousId) //
35+
TrackMessage.builder("Java Test")
36+
.properties(properties)
37+
.anonymousId(anonymousId)
3338
.userId(userId));
3439
}
3540
}
@@ -38,4 +43,19 @@ public static void main(String... args) throws Exception {
3843
blockingFlush.block();
3944
analytics.shutdown();
4045
}
46+
47+
/**
48+
* By default, the analytics client uses an HTTP client with sane defaults. However you can
49+
* customize the client to your needs. For instance, this client is configured to automatically
50+
* gzip outgoing requests.
51+
*/
52+
private static Client createClient() {
53+
return new Ok3Client(
54+
new OkHttpClient.Builder()
55+
.connectTimeout(15, TimeUnit.SECONDS)
56+
.readTimeout(15, TimeUnit.SECONDS)
57+
.writeTimeout(15, TimeUnit.SECONDS)
58+
.addInterceptor(new GzipRequestInterceptor())
59+
.build());
60+
}
4161
}

0 commit comments

Comments
 (0)