Example usage for org.apache.http.client.entity GzipDecompressingEntity GzipDecompressingEntity

List of usage examples for org.apache.http.client.entity GzipDecompressingEntity GzipDecompressingEntity

Introduction

In this page you can find the example usage for org.apache.http.client.entity GzipDecompressingEntity GzipDecompressingEntity.

Prototype

public GzipDecompressingEntity(final HttpEntity entity) 

Source Link

Document

Creates a new GzipDecompressingEntity which will wrap the specified HttpEntity .

Usage

From source file:com.dlmu.heipacker.crawler.client.ClientGZipContentCompression.java

public final static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from  w  ww.  jav a2 s .  c  o m
        httpclient.addRequestInterceptor(new HttpRequestInterceptor() {

            public void process(final HttpRequest request, final HttpContext context)
                    throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }

        });

        httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

            public void process(final HttpResponse response, final HttpContext context)
                    throws HttpException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    Header ceheader = entity.getContentEncoding();
                    if (ceheader != null) {
                        HeaderElement[] codecs = ceheader.getElements();
                        for (int i = 0; i < codecs.length; i++) {
                            if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                                response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                                return;
                            }
                        }
                    }
                }
            }

        });

        HttpGet httpget = new HttpGet("http://www.apache.org/");

        // Execute HTTP request
        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println(response.getLastHeader("Content-Encoding"));
        System.out.println(response.getLastHeader("Content-Length"));
        System.out.println("----------------------------------------");

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            String content = EntityUtils.toString(entity);
            System.out.println(content);
            System.out.println("----------------------------------------");
            System.out.println("Uncompressed size: " + content.length());
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:au.com.borner.salesforce.client.rest.HttpCompressionResponseInterceptor.java

@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Header contentEncoding = entity.getContentEncoding();
        if (contentEncoding != null) {
            HeaderElement[] codecs = contentEncoding.getElements();
            for (HeaderElement codec : codecs) {
                if (codec.getName().equalsIgnoreCase(GZIP)) {
                    response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                    return;
                }/*from w ww  .  jav a 2  s. com*/
                if (codec.getName().equalsIgnoreCase(DEFLATE)) {
                    response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
                    return;
                }
            }
        }
    }
}

From source file:org.sonatype.nexus.plugins.crowd.client.rest.CompressedHttpResponseInterceptor.java

@Override
public void process(HttpResponse response, HttpContext hc) throws HttpException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Header header = entity.getContentEncoding();

        if (header != null) {
            for (HeaderElement headerElement : header.getElements()) {
                if (headerElement.getName().equalsIgnoreCase("gzip")) {
                    response.setEntity(new GzipDecompressingEntity(entity));
                    return;
                }//from w w w.  j  a v a  2s  .  c o  m

                if (headerElement.getName().equalsIgnoreCase("deflate")) {
                    response.setEntity(new DeflateDecompressingEntity(entity));
                    return;
                }
            }
        }
    }
}

From source file:org.apache.sling.discovery.etcd.gzip.GzipResponseInterceptor.java

public void process(HttpResponse res, HttpContext context) throws HttpException, IOException {
    HttpEntity entity = res.getEntity();
    if (entity != null) {
        Header contentEncoding = entity.getContentEncoding();
        if (contentEncoding != null) {
            for (HeaderElement element : contentEncoding.getElements()) {
                if ("gzip".equalsIgnoreCase(element.getName())) {
                    res.setEntity(new GzipDecompressingEntity(entity));
                    return;
                }//  ww  w. j  a  v a 2 s.  c o  m
            }
        }
    }
}

From source file:org.hawk.service.api.utils.GZipResponseInterceptor.java

@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Header ceheader = entity.getContentEncoding();
        if (ceheader != null) {
            HeaderElement[] codecs = ceheader.getElements();
            for (int i = 0; i < codecs.length; i++) {
                if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                    response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                    return;
                }//from w  w  w.  j  a  v  a2  s  .  c om
            }
        }
    }
}

From source file:com.dss886.nForumSDK.http.GetMethod.java

public JSONObject getJSON() throws JSONException, NForumException, IOException {
    HttpResponse response = httpClient.execute(httpGet);

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != 200)
        throw new NForumException(NForumException.EXCEPTION_NETWORK + ":" + statusCode);
    Header header = response.getEntity().getContentEncoding();
    if (header != null) {
        for (HeaderElement element : header.getElements()) {
            if (element.getName().equalsIgnoreCase("gzip")) {
                response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                break;
            }/*from   w w w. ja  va2s  .  com*/
        }
    }
    String result = ResponseProcessor.getStringFromResponse(response);
    httpGet.abort();

    JSONObject json = new JSONObject(result);
    if (json.has("msg")) {
        throw new NForumException(json.getString("msg"));
    }
    return json;
}

From source file:net.yacy.cora.protocol.http.GzipResponseInterceptor.java

@Override
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }/*from www.  j a v  a 2s. c o m*/
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Header ceheader = entity.getContentEncoding();
        if (ceheader != null) {
            HeaderElement[] codecs = ceheader.getElements();
            for (int i = 0; i < codecs.length; i++) {
                if (codecs[i].getName().equalsIgnoreCase(HeaderFramework.CONTENT_ENCODING_GZIP)) {
                    //                  response.removeHeader(ceheader);
                    response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                    return;
                }
            }
        }
    }
}

From source file:GUI_The_Code_Book.ParserAPIStackEx.java

public ArrayList<URLlist> getStackEx(String word, String web) {
    urlList = new ArrayList<URLlist>();
    word = word.replaceAll(" ", "+");
    String url = "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&accepted=True&title="
            + word + "&site=" + web + "&filter=withbody";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(url);
    HttpResponse httpResponse;//w  w w  .j  av a2s . c  om
    try {
        httpResponse = httpclient.execute(getRequest);
        HttpEntity entity = httpResponse.getEntity();
        System.out.println("----------------------------------------");
        System.out.println(httpResponse.getStatusLine());
        Header[] headers = httpResponse.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            System.out.println(headers[i]);
        }
        System.out.println("----------------------------------------");
        if (entity != null) {
            entity = new GzipDecompressingEntity(entity);
            String jsonStr = EntityUtils.toString(entity);
            //System.out.println(jsonStr);
            parseStackExchange(jsonStr);
        } else {
            System.out.println("NOTHING");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return urlList;
}

From source file:com.github.parisoft.resty.entity.EntityReaderImpl.java

@Override
public String getEntityAsString() throws IOException {
    if (body == null) {
        final HttpEntity entity = httpResponse.getEntity();

        if (entity == null) {
            return body = "";
        }//ww w  . j av  a  2  s .  co  m

        try {
            final HttpEntity entityWrapper = isGzipEncoding() ? new GzipDecompressingEntity(entity) : entity;
            final ByteArrayOutputStream output = new ByteArrayOutputStream();

            entityWrapper.writeTo(output);

            body = output.toString(getContentCharSet().toString());
        } finally {
            EntityUtils.consume(entity);
        }
    }

    return body;
}

From source file:com.nextdoor.bender.ipc.http.HttpTransportTest.java

private HttpClient getMockClientWithResponse(byte[] respPayload, ContentType contentType, int status,
        boolean compressed) throws IOException {
    HttpClient mockClient = mock(HttpClient.class);
    HttpResponse mockResponse = mock(HttpResponse.class);

    StatusLine mockStatusLine = mock(StatusLine.class);
    doReturn("expected failure").when(mockStatusLine).getReasonPhrase();
    doReturn(status).when(mockStatusLine).getStatusCode();
    doReturn(mockStatusLine).when(mockResponse).getStatusLine();
    EntityBuilder eb = EntityBuilder.create().setBinary(respPayload).setContentType(contentType);

    HttpEntity he;/* www  .  j a  v  a2s.c o m*/
    if (compressed) {
        eb.setContentEncoding("gzip");
        he = new GzipDecompressingEntity(eb.build());
    } else {
        he = eb.build();
    }

    doReturn(he).when(mockResponse).getEntity();

    doReturn(mockResponse).when(mockClient).execute(any(HttpPost.class));
    return mockClient;
}