org.apache.http.client.entity.GzipDecompressingEntity Java Examples

The following examples show how to use org.apache.http.client.entity.GzipDecompressingEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientHelper.java    From whirlpool with Apache License 2.0 6 votes vote down vote up
public static CloseableHttpClient buildHttpClient() {
    CloseableHttpClient httpClient = HttpClients.custom()
            .addInterceptorFirst((HttpRequestInterceptor) (request, context) -> {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }).addInterceptorFirst((HttpResponseInterceptor) (response1, context) -> {
                HttpEntity entity = response1.getEntity();
                if (entity != null) {
                    Header ceHeader = entity.getContentEncoding();
                    if (ceHeader != null) {
                        HeaderElement[] codecs = ceHeader.getElements();
                        for (HeaderElement codec : codecs) {
                            if (codec.getName().equalsIgnoreCase("gzip")) {
                                response1.setEntity(
                                        new GzipDecompressingEntity(response1.getEntity()));
                                return;
                            }
                        }
                    }
                }
            }).build();

    return httpClient;
}
 
Example #2
Source File: ApacheHttpRequester.java    From algoliasearch-client-java-2 with MIT License 6 votes vote down vote up
private static HttpEntity handleCompressedEntity(org.apache.http.HttpEntity entity) {

    Header contentEncoding = entity.getContentEncoding();

    if (contentEncoding != null)
      for (HeaderElement e : contentEncoding.getElements()) {
        if (Defaults.CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) {
          return new GzipDecompressingEntity(entity);
        }

        if (Defaults.CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) {
          return new DeflateDecompressingEntity(entity);
        }
      }

    return entity;
  }
 
Example #3
Source File: ProxyServlet.java    From cloud-connectivityproxy with Apache License 2.0 6 votes vote down vote up
private void handleContentEncoding(HttpResponse response) throws ServletException {
	HttpEntity entity = response.getEntity();
	if (entity != null) {
		Header contentEncodingHeader = entity.getContentEncoding();
		if (contentEncodingHeader != null) {
			HeaderElement[] codecs = contentEncodingHeader.getElements();
			LOGGER.debug("Content-Encoding in response:");
			for (HeaderElement codec : codecs) {
				String codecname = codec.getName().toLowerCase();
				LOGGER.debug("    => codec: " + codecname);
				if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
					response.setEntity(new GzipDecompressingEntity(response.getEntity()));
					return;
				} else if ("deflate".equals(codecname)) {
					response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
					return;
				} else if ("identity".equals(codecname)) {
					return;
				} else {
					throw new ServletException("Unsupported Content-Encoding: " + codecname);
				}
			}
		}
	}
}
 
Example #4
Source File: HTTPSession.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 (HeaderElement h : codecs) {
        if (h.getName().equalsIgnoreCase("gzip")) {
          response.setEntity(new GzipDecompressingEntity(response.getEntity()));
          return;
        }
      }
    }
  }
}
 
Example #5
Source File: HttpSendClient.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
private String uncompress(HttpResponse httpResponse) throws ParseException, IOException {
    int ret = httpResponse.getStatusLine().getStatusCode();
    if (!isOK(ret)) {
        return null;
    }

    // Read the contents
    String respBody = null;
    HttpEntity entity = httpResponse.getEntity();
    String charset = EntityUtils.getContentCharSet(entity);
    if (charset == null) {
        charset = "UTF-8";
    }

    // "Content-Encoding"
    Header contentEncodingHeader = entity.getContentEncoding();
    if (contentEncodingHeader != null) {
        String contentEncoding = contentEncodingHeader.getValue();
        if (contentEncoding.contains("gzip")) {
            respBody = EntityUtils.toString(new GzipDecompressingEntity(entity), charset);
        } else if (contentEncoding.contains("deflate")) {
            respBody = EntityUtils.toString(new DeflateDecompressingEntity(entity), charset);
        }
    } else {
        // "Content-Type"
        Header contentTypeHeader = entity.getContentType();
        if (contentTypeHeader != null) {
            String contentType = contentTypeHeader.getValue();
            if (contentType != null) {
                if (contentType.startsWith("application/x-gzip-compressed")) {
                    respBody = EntityUtils.toString(new GzipDecompressingEntity(entity), charset);
                } else if (contentType.startsWith("application/x-deflate")) {
                    respBody = EntityUtils.toString(new DeflateDecompressingEntity(entity), charset);
                }
            }
        }
    }
    return respBody;
}
 
Example #6
Source File: HttpFactory.java    From rainbow with Apache License 2.0 5 votes vote down vote up
public HttpResponse getHttpResponse(String url) throws ClientProtocolException, IOException
{
    HttpResponse response = null;
    HttpGet get = new HttpGet(url);
    get.addHeader("Accept", "text/html");
    get.addHeader("Accept-Charset", "utf-8");
    get.addHeader("Accept-Encoding", "gzip");
    get.addHeader("Accept-Language", "en-US,en");
    int uai = rand.nextInt() % userAgents.size();
    if (uai < 0)
    {
        uai = -uai;
    }
    get.addHeader("User-Agent", userAgents.get(uai));
    response = getHttpClient().execute(get);
    HttpEntity entity = response.getEntity();
    Header header = entity.getContentEncoding();
    if (header != null)
    {
        HeaderElement[] codecs = header.getElements();
        for (int i = 0; i < codecs.length; i++)
        {
            if (codecs[i].getName().equalsIgnoreCase("gzip"))
            {
                response.setEntity(new GzipDecompressingEntity(entity));
            }
        }
    }
    return response;

}
 
Example #7
Source File: HttpUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Processes the HttpResponse to create a DHisHttpResponse object.
 *
 * @throws IOException </pre>
 */
private static DhisHttpResponse processResponse( String requestURL, String username, HttpResponse response )
    throws Exception
{
    DhisHttpResponse dhisHttpResponse;
    String output;
    int statusCode;
    if ( response != null )
    {
        HttpEntity responseEntity = response.getEntity();

        if ( responseEntity != null && responseEntity.getContent() != null )
        {
            Header contentType = response.getEntity().getContentType();

            if ( contentType != null && checkIfGzipContentType( contentType ) )
            {
                GzipDecompressingEntity gzipDecompressingEntity = new GzipDecompressingEntity( response.getEntity() );
                InputStream content = gzipDecompressingEntity.getContent();
                output = IOUtils.toString( content, StandardCharsets.UTF_8 );
            }
            else
            {
                output = EntityUtils.toString( response.getEntity() );
            }
            statusCode = response.getStatusLine().getStatusCode();
        }
        else
        {
            throw new Exception( "No content found in the response received from http POST call to " + requestURL + " with username " + username );
        }

        dhisHttpResponse = new DhisHttpResponse( response, output, statusCode );
    }
    else
    {
        throw new Exception( "NULL response received from http POST call to " + requestURL + " with username " + username );
    }

    return dhisHttpResponse;
}
 
Example #8
Source File: NUHttpClientUtils.java    From neembuu-uploader with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the content of a gzip encoded page
 * @param url url from which to read
 * @return the String content of the page
 * @throws Exception 
 */
public static String getGzipedData(String url) throws Exception {
    NUHttpGet httpGet = new NUHttpGet(url);
    HttpResponse httpResponse = NUHttpClient.getHttpClient().execute(httpGet);
    return EntityUtils.toString(new GzipDecompressingEntity(httpResponse.getEntity()));
}
 
Example #9
Source File: NUHttpClientUtils.java    From neembuu-uploader with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the content of a gzip encoded page
 * @param url url from which to read
 * @param httpContext the httpContext in which to make the request
 * @return the String content of the page
 * @throws Exception 
 */
public static String getGzipedData(String url, HttpContext httpContext) throws Exception {
    NUHttpGet httpGet = new NUHttpGet(url);
    HttpResponse httpResponse = NUHttpClient.getHttpClient().execute(httpGet, httpContext);
    return EntityUtils.toString(new GzipDecompressingEntity(httpResponse.getEntity()));
}
 
Example #10
Source File: HttpConnnectionManager.java    From Crawer with MIT License 2 votes vote down vote up
/**
 * 
 * 从response返回的实体中读取页面代码
 * 
 * @param httpEntity
 *            Http实体
 * 
 * @return 页面代码
 * 
 * @throws ParseException
 * 
 * @throws IOException
 */

private static String readHtmlContentFromEntity(HttpEntity httpEntity)
		throws ParseException, IOException {

	String html = "";

	Header header = httpEntity.getContentEncoding();

	if (httpEntity.getContentLength() < 2147483647L) { // EntityUtils无法处理ContentLength超过2147483647L的Entity

		if (header != null && "gzip".equals(header.getValue())) {

			html = EntityUtils.toString(new GzipDecompressingEntity(
					httpEntity));

		} else {

			html = EntityUtils.toString(httpEntity);

		}

	} else {

		InputStream in = httpEntity.getContent();

		if (header != null && "gzip".equals(header.getValue())) {

			html = unZip(in, ContentType.getOrDefault(httpEntity)
					.getCharset().toString());

		} else {

			html = readInStreamToString(in,
					ContentType.getOrDefault(httpEntity).getCharset()
							.toString());

		}

		if (in != null) {

			in.close();

		}

	}

	return html;

}