All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kaizen4j.common.httpclient.interceptor.GZIPHttpResponseInterceptor Maven / Gradle / Ivy

package org.kaizen4j.common.httpclient.interceptor;

import org.apache.http.*;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

import static java.util.Objects.nonNull;

public final class GZIPHttpResponseInterceptor implements HttpResponseInterceptor {

    private static final String GZIP = "gzip";

    @Override
    public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
        HttpEntity responseEntity = response.getEntity();

        if (nonNull(responseEntity)) {
            Header contentHeader = responseEntity.getContentEncoding();

            if (nonNull(contentHeader)) {
                HeaderElement[] headerElements = contentHeader.getElements();

                for (HeaderElement headerElement : headerElements) {
                    if (GZIP.equalsIgnoreCase(headerElement.getName())) {
                        response.setEntity(new GzipDecompressingEntity(responseEntity));
                        return;
                    }
                }
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy