com.heroiclabs.sdk.android.util.http.GzipRequestInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.heroiclabs.sdk.android.util.http.GzipRequestInterceptor.java

Source

/*
 * Copyright 2016 Heroic Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.heroiclabs.sdk.android.util.http;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import okio.Buffer;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;

/**
 * An OkHttp interceptor that compresses request bodies.
 * See https://github.com/square/okhttp/issues/350
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GzipRequestInterceptor implements Interceptor {

    /** Singleton instance. */
    public static final GzipRequestInterceptor INSTANCE = new GzipRequestInterceptor();

    /** {@inheritDoc} */
    @Override
    public Response intercept(final Chain chain) throws IOException {
        final Request originalRequest = chain.request();

        // Do not compress requests already encoded, or that are too small.
        if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null
                || originalRequest.body().contentLength() < 300) {
            return chain.proceed(originalRequest);
        }

        final Request compressedRequest = originalRequest.newBuilder().header("Content-Encoding", "gzip")
                .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body()))).build();
        return chain.proceed(compressedRequest);
    }

    /**
     * Force the content length for a given RequestBody that may not have calculated it correctly.
     *
     * @param requestBody The RequestBody to force content length for.
     * @return A new RequestBody with the correct contentLength set.
     * @throws IOException if writing to buffer fails.
     */
    private static RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
        final Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);
        return new RequestBody() {
            @Override
            public MediaType contentType() {
                return requestBody.contentType();
            }

            @Override
            public long contentLength() {
                return buffer.size();
            }

            @Override
            public void writeTo(final BufferedSink sink) throws IOException {
                sink.write(buffer.snapshot());
            }
        };
    }

    /**
     * Compress the contents of the given RequestBody into a new one.
     *
     * @param body The RequestBody to compress.
     * @return A new RequestBody with compressed content.
     */
    private static 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(final BufferedSink sink) throws IOException {
                final BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
                body.writeTo(gzipSink);
                gzipSink.close();
            }
        };
    }

}