Search in sources :

Example 16 with GzipSink

use of okio.GzipSink in project id4i-api_client-java by BlueRainSoftware.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink)

Example 17 with GzipSink

use of okio.GzipSink in project mapbox-events-android by mapbox.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody)

Example 18 with GzipSink

use of okio.GzipSink in project apm-agent-java by elastic.

the class ApmServerHttpPayloadSender method sendPayload.

@Override
public void sendPayload(final Payload payload) {
    logger.debug("Sending payload with {} elements to APM server {}", payload.getPayloadObjects().size(), reporterConfiguration.getServerUrl());
    final String path;
    if (payload instanceof ErrorPayload) {
        path = "/v1/errors";
    } else {
        path = "/v1/transactions";
    }
    final Request.Builder builder = new Request.Builder().url(reporterConfiguration.getServerUrl() + path).header("User-Agent", getUserAgent(payload));
    if (reporterConfiguration.getSecretToken() != null) {
        builder.header("Authorization", "Bearer " + reporterConfiguration.getSecretToken());
    }
    if (useGzip(payload)) {
        builder.header("Content-Encoding", "gzip");
    }
    Request request = builder.post(new RequestBody() {

        @Override
        public MediaType contentType() {
            return MEDIA_TYPE_JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            if (useGzip(payload)) {
                GzipSink gzipSink = new GzipSink(sink);
                gzipSink.deflater().setLevel(GZIP_COMPRESSION_LEVEL);
                sink = Okio.buffer(gzipSink);
            }
            payloadSerializer.serializePayload(sink, payload);
            sink.close();
            payload.recycle();
        }
    }).build();
    try {
        Response response = httpClient.newCall(request).execute();
        int statusCode = response.code();
        logger.debug("APM server responded with status code {}", statusCode);
        if (statusCode >= 400) {
            droppedTransactions += payload.getPayloadObjects().size();
            if (response.body() != null) {
                logger.debug(response.body().string());
            }
        }
        response.close();
    } catch (IOException e) {
        logger.debug("Sending payload to APM server failed", e);
        droppedTransactions += payload.getPayloadObjects().size();
    }
}
Also used : Response(okhttp3.Response) ErrorPayload(co.elastic.apm.impl.error.ErrorPayload) GzipSink(okio.GzipSink) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 19 with GzipSink

use of okio.GzipSink in project KalturaGeneratedAPIClientsJava by kaltura.

the class GzipInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink)

Example 20 with GzipSink

use of okio.GzipSink in project xDrip by NightscoutFoundation.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

GzipSink (okio.GzipSink)26 BufferedSink (okio.BufferedSink)20 Buffer (okio.Buffer)13 RequestBody (okhttp3.RequestBody)5 ByteString (okio.ByteString)3 RequestBody (com.squareup.okhttp.RequestBody)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 Test (org.junit.Test)2 ErrorPayload (co.elastic.apm.impl.error.ErrorPayload)1 File (java.io.File)1 IOException (java.io.IOException)1 TreeSet (java.util.TreeSet)1 OkHttpClient (okhttp3.OkHttpClient)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 BufferedSource (okio.BufferedSource)1 Sink (okio.Sink)1