org.apache.http.HttpEntity#getContentEncoding ( )源码实例Demo

下面列出了org.apache.http.HttpEntity#getContentEncoding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public void process(final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {
    final HttpEntity entity = response.getEntity();
    final Header encoding = entity.getContentEncoding();
    if (encoding != null) {
        for (final HeaderElement element : encoding.getElements()) {
            if (element.getName().equalsIgnoreCase(
                    GzipResponseInterceptor.ENCODING_GZIP)) {
                response.setEntity(new GzipDecompressingEntity(response
                        .getEntity()));
                break;
            }
        }
    }
}
 
源代码2 项目: cloud-connectivityproxy   文件: ProxyServlet.java
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);
				}
			}
		}
	}
}
 
源代码3 项目: lucene-solr   文件: HttpClientUtil.java
@Override
public void process(final HttpResponse response, final HttpContext context)
    throws HttpException, IOException {
  
  HttpEntity entity = response.getEntity();
  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;
      }
      if (codecs[i].getName().equalsIgnoreCase("deflate")) {
        response.setEntity(new DeflateDecompressingEntity(response
            .getEntity()));
        return;
      }
    }
  }
}
 
源代码4 项目: p4ic4idea   文件: BasicResponse.java
private static Charset getEncoding(HttpEntity entity) {
    Header header = entity.getContentEncoding();
    if (header == null) {
        return Charset.forName("UTF-8");
    }
    for (HeaderElement headerElement : header.getElements()) {
        for (NameValuePair pair : headerElement.getParameters()) {
            if (pair != null && pair.getValue() != null) {
                if (Charset.isSupported(pair.getValue())) {
                    return Charset.forName(pair.getValue());
                }
            }
        }
    }
    return Charset.forName("UTF-8");
}
 
源代码5 项目: netcdf-java   文件: HTTPSession.java
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;
        }
      }
    }
  }
}
 
源代码6 项目: PYX-Reloaded   文件: GoogleApacheHttpResponse.java
@Override
public String getContentEncoding() {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Header contentEncodingHeader = entity.getContentEncoding();
        if (contentEncodingHeader != null) {
            return contentEncodingHeader.getValue();
        }
    }

    return null;
}
 
private static String entityAsString(HttpEntity entity) {
    try {
        InputStream content = entity.getContent();
        Charset encoding = entity.getContentEncoding() == null ? defaultCharset() : Charset.forName(entity.getContentEncoding().getValue());
        String contentString = inputStreamAsString(content, encoding);

        return contentString;
    } catch (Exception ignored) {
        return "<empty body>";
    }
}
 
源代码8 项目: beam   文件: InfluxDBPublisher.java
private static String getErrorMessage(final HttpEntity entity) throws IOException {
  final Header encodingHeader = entity.getContentEncoding();
  final Charset encoding =
      encodingHeader == null
          ? StandardCharsets.UTF_8
          : Charsets.toCharset(encodingHeader.getValue());
  final JsonElement errorElement =
      new Gson().fromJson(EntityUtils.toString(entity, encoding), JsonObject.class).get("error");
  return isNull(errorElement) ? "[Unable to get error message]" : errorElement.toString();
}
 
源代码9 项目: JMCCC   文件: HttpAsyncDownloader.java
@Override
protected void onResponseReceived(HttpResponse response) throws HttpException, IOException {
	StatusLine statusLine = response.getStatusLine();
	if (statusLine != null) {
		int statusCode = statusLine.getStatusCode();
		if (statusCode < 200 || statusCode > 299)
			// non-2xx response code
			throw new IllegalHttpResponseCodeException(statusLine.toString(), statusCode);
	}

	if (session == null) {
		boolean gzipOn = false;
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity != null) {
			long contextLength = httpEntity.getContentLength();
			if (contextLength >= 0) {
				this.contextLength = contextLength;
			}

			Header contentEncodingHeader = httpEntity.getContentEncoding();
			if (contentEncodingHeader != null && "gzip".equals(contentEncodingHeader.getValue())) {
				gzipOn = true;
			}
		}

		session = contextLength > 0
				? task.createSession(contextLength)
				: task.createSession();

		if (gzipOn) {
			session = new GzipDownloadSession<>(session);
		}
	}
}
 
@Override
public String getContentEncoding() {
  HttpEntity entity = response.getEntity();
  if (entity != null) {
    Header contentEncodingHeader = entity.getContentEncoding();
    if (contentEncodingHeader != null) {
      return contentEncodingHeader.getValue();
    }
  }
  return null;
}
 
@Override
public String getContentEncoding() {
  HttpEntity entity = response.getEntity();
  if (entity != null) {
    Header contentEncodingHeader = entity.getContentEncoding();
    if (contentEncodingHeader != null) {
      return contentEncodingHeader.getValue();
    }
  }
  return null;
}
 
源代码12 项目: volley   文件: HttpClientStack.java
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
    // add gzip support, not all request need gzip support
    if (request.isShouldGzip()) {
        httpRequest.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
    }
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    onPrepareRequest(httpRequest);
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // TODO: Reevaluate this connection timeout based on more wide-scale
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIME_OUT_MS);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    HttpResponse response = mClient.execute(httpRequest);
    if (response != null) {
        final HttpEntity entity = response.getEntity();
        if (entity == null) {
            return response;
        }
        final Header encoding = entity.getContentEncoding();
        if (encoding != null) {
            for (HeaderElement element : encoding.getElements()) {
                if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                    response.setEntity(new InflatingEntity(response.getEntity()));
                    break;
                }
            }
        }
    }
    return response;
}
 
源代码13 项目: jbosh   文件: ApacheHTTPResponse.java
/**
 * Await the response, storing the result in the instance variables of
 * this class when they arrive.
 *
 * @throws InterruptedException if interrupted while awaiting the response
 * @throws BOSHException on communication failure
 */
private synchronized void awaitResponse() throws BOSHException {
    HttpEntity entity = null;
    try {
        HttpResponse httpResp = client.execute(post, context);
        entity = httpResp.getEntity();
        byte[] data = EntityUtils.toByteArray(entity);
        String encoding = entity.getContentEncoding() != null ?
                entity.getContentEncoding().getValue() :
                null;
        if (ZLIBCodec.getID().equalsIgnoreCase(encoding)) {
            data = ZLIBCodec.decode(data);
        } else if (GZIPCodec.getID().equalsIgnoreCase(encoding)) {
            data = GZIPCodec.decode(data);
        }
        body = StaticBody.fromString(new String(data, CHARSET));
        statusCode = httpResp.getStatusLine().getStatusCode();
        sent = true;
    } catch (IOException iox) {
        abort();
        toThrow = new BOSHException("Could not obtain response", iox);
        throw(toThrow);
    } catch (RuntimeException ex) {
        abort();
        throw(ex);
    }
}
 
源代码14 项目: esigate   文件: ResponseSender.java
void sendHeaders(HttpResponse httpResponse, IncomingRequest httpRequest, HttpServletResponse response) {
    response.setStatus(httpResponse.getStatusLine().getStatusCode());
    for (Header header : httpResponse.getAllHeaders()) {
        String name = header.getName();
        String value = header.getValue();
        response.addHeader(name, value);
    }

    // Copy new cookies
    Cookie[] newCookies = httpRequest.getNewCookies();

    for (Cookie newCooky : newCookies) {

        // newCooky may be null. In that case just ignore.
        // See https://github.com/esigate/esigate/issues/181
        if (newCooky != null) {
            response.addHeader("Set-Cookie", CookieUtil.encodeCookie(newCooky));
        }
    }
    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        long contentLength = httpEntity.getContentLength();
        if (contentLength > -1 && contentLength < Integer.MAX_VALUE) {
            response.setContentLength((int) contentLength);
        }
        Header contentType = httpEntity.getContentType();
        if (contentType != null) {
            response.setContentType(contentType.getValue());
        }
        Header contentEncoding = httpEntity.getContentEncoding();
        if (contentEncoding != null) {
            response.setHeader(contentEncoding.getName(), contentEncoding.getValue());
        }
    }
}
 
源代码15 项目: YiBo   文件: GzipResponseInterceptor.java
@Override
public void process(final HttpResponse response, final HttpContext context) throws HttpException,
		IOException {
	HttpEntity entity = response.getEntity();
	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;
			}
		}
	}
}
 
源代码16 项目: cyberduck   文件: HttpComponentsConnector.java
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
    final HttpUriRequest request = this.toUriHttpRequest(clientRequest);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);

    try {
        final CloseableHttpResponse response;
        response = client.execute(new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()), request, new BasicHttpContext(context));
        HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());

        final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null
            ? Statuses.from(response.getStatusLine().getStatusCode())
            : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());

        final ClientResponse responseContext = new ClientResponse(status, clientRequest);
        final List<URI> redirectLocations = context.getRedirectLocations();
        if(redirectLocations != null && !redirectLocations.isEmpty()) {
            responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
        }

        final Header[] respHeaders = response.getAllHeaders();
        final MultivaluedMap<String, String> headers = responseContext.getHeaders();
        for(final Header header : respHeaders) {
            final String headerName = header.getName();
            List<String> list = headers.get(headerName);
            if(list == null) {
                list = new ArrayList<>();
            }
            list.add(header.getValue());
            headers.put(headerName, list);
        }

        final HttpEntity entity = response.getEntity();

        if(entity != null) {
            if(headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
                headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
            }

            final Header contentEncoding = entity.getContentEncoding();
            if(headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
                headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
            }
        }
        responseContext.setEntityStream(this.toInputStream(response));
        return responseContext;
    }
    catch(final Exception e) {
        throw new ProcessingException(e);
    }
}
 
源代码17 项目: cyberduck   文件: HttpComponentsConnector.java
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
    final HttpUriRequest request = this.toUriHttpRequest(clientRequest);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);

    try {
        final CloseableHttpResponse response;
        response = client.execute(new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()), request, new BasicHttpContext(context));
        HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());

        final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null
                ? Statuses.from(response.getStatusLine().getStatusCode())
                : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());

        final ClientResponse responseContext = new ClientResponse(status, clientRequest);
        final List<URI> redirectLocations = context.getRedirectLocations();
        if(redirectLocations != null && !redirectLocations.isEmpty()) {
            responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
        }

        final Header[] respHeaders = response.getAllHeaders();
        final MultivaluedMap<String, String> headers = responseContext.getHeaders();
        for(final Header header : respHeaders) {
            final String headerName = header.getName();
            List<String> list = headers.get(headerName);
            if(list == null) {
                list = new ArrayList<>();
            }
            list.add(header.getValue());
            headers.put(headerName, list);
        }

        final HttpEntity entity = response.getEntity();

        if(entity != null) {
            if(headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
                headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
            }

            final Header contentEncoding = entity.getContentEncoding();
            if(headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
                headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
            }
        }
        responseContext.setEntityStream(this.toInputStream(response));
        return responseContext;
    }
    catch(final Exception e) {
        throw new ProcessingException(e);
    }
}
 
源代码18 项目: uavstack   文件: ApacheHttpClientAdapter.java
@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {

    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
        SlowOperContext slowOperContext = new SlowOperContext();
        if (Throwable.class.isAssignableFrom(args[0].getClass())) {

            Throwable e = (Throwable) args[0];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, e.toString());

        }
        else {
            HttpResponse response = (HttpResponse) args[0];

            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // 由于存在读取失败和无法缓存的大entity会使套壳失败,故此处添加如下判断
                if (BufferedHttpEntity.class.isAssignableFrom(entity.getClass())) {
                    Header header = entity.getContentEncoding();
                    String encoding = header == null ? "utf-8" : header.getValue();
                    slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY,
                            getHttpEntityContent(entity, encoding));
                }
                else {
                    slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY,
                            "HttpEntityWrapper failed! Maybe HTTP entity too large to be buffered in memory");
                }
            }
            else {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "");
            }
        }

        Object params[] = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap",
                span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
    }

}
 
源代码19 项目: gatk   文件: HtsgetReader.java
@Override
public Object doWork() {
    // construct request from command line args and convert to URI
    final HtsgetRequestBuilder req = new HtsgetRequestBuilder(endpoint, id)
        .withFormat(format)
        .withDataClass(dataClass)
        .withInterval(interval)
        .withFields(fields)
        .withTags(tags)
        .withNotags(notags);
    final URI reqURI = req.toURI();

    final HttpGet getReq = new HttpGet(reqURI);
    try (final CloseableHttpResponse resp = this.client.execute(getReq)) {
        // get content of response
        final HttpEntity entity = resp.getEntity();
        final Header encodingHeader = entity.getContentEncoding();
        final Charset encoding = encodingHeader == null 
            ? StandardCharsets.UTF_8
            : Charsets.toCharset(encodingHeader.getValue());
        final String jsonBody = EntityUtils.toString(entity, encoding);

        final ObjectMapper mapper = this.getObjectMapper();

        if (resp.getStatusLine() == null) {
            throw new UserException("htsget server response did not contain status line");
        }
        final int statusCode = resp.getStatusLine().getStatusCode();
        if (400 <= statusCode && statusCode < 500) {
            final HtsgetErrorResponse err = mapper.readValue(jsonBody, HtsgetErrorResponse.class);
            throw new UserException("Invalid request, received error code: " + statusCode + ", error type: "
                    + err.getError() + ", message: " + err.getMessage());
        } else if (statusCode == 200) {
            final HtsgetResponse response = mapper.readValue(jsonBody, HtsgetResponse.class);

            if (this.readerThreads > 1) {
                this.getDataParallel(response);
            } else {
                this.getData(response);
            }

            logger.info("Successfully wrote to: " + outputFile);

            if (checkMd5) {
                this.checkMd5(response);
            }
        } else {
            throw new UserException("Unrecognized status code: " + statusCode);
        }
    } catch (final IOException e) {
        throw new UserException("IOException during htsget download", e);
    }
    return null;
}
 
源代码20 项目: Crawer   文件: HttpConnnectionManager.java
/**
 * 
 * 从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;

}