本文整理汇总了Java中org.apache.http.HttpEntity.getContentEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java HttpEntity.getContentEncoding方法的具体用法?Java HttpEntity.getContentEncoding怎么用?Java HttpEntity.getContentEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpEntity
的用法示例。
在下文中一共展示了HttpEntity.getContentEncoding方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: transformRequest
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
private static Request transformRequest(HttpRequest request) {
Request.Builder builder = new Request.Builder();
RequestLine requestLine = request.getRequestLine();
String method = requestLine.getMethod();
builder.url(requestLine.getUri());
String contentType = null;
for (Header header : request.getAllHeaders()) {
String name = header.getName();
if ("Content-Type".equalsIgnoreCase(name)) {
contentType = header.getValue();
} else {
builder.header(name, header.getValue());
}
}
RequestBody body = null;
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
// Wrap the entity in a custom Body which takes care of the content, length, and type.
body = new HttpEntityBody(entity, contentType);
Header encoding = entity.getContentEncoding();
if (encoding != null) {
builder.header(encoding.getName(), encoding.getValue());
}
} else {
body = Util.EMPTY_REQUEST;
}
}
builder.method(method, body);
return builder.build();
}
示例2: getContentIsGzip
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static boolean getContentIsGzip(final HttpEntity entity) throws ParseException {
boolean gzip = false;
if (entity.getContentEncoding() != null) {
HeaderElement values[] = entity.getContentEncoding().getElements();
for (HeaderElement e : values) {
gzip = e.getName().equals("gzip");
if(gzip) break;
}
}
return gzip;
}
示例3: process
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Handles the following {@code Content-Encoding}s by
* using the appropriate decompressor to wrap the response Entity:
* <ul>
* <li>gzip - see {@link GzipDecompressingEntity}</li>
* <li>deflate - see {@link DeflateDecompressingEntity}</li>
* <li>identity - no action needed</li>
* </ul>
*
* @param response the response which contains the entity
* @param context not currently used
*
* @throws HttpException if the {@code Content-Encoding} is none of the above
*/
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
// It wasn't a 304 Not Modified response, 204 No Content or similar
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (HeaderElement codec : codecs) {
String codecname = codec.getName().toLowerCase(Locale.US);
if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
if (context != null) context.setAttribute(UNCOMPRESSED, true);
return;
} else if ("deflate".equals(codecname)) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
if (context != null) context.setAttribute(UNCOMPRESSED, true);
return;
} else if ("identity".equals(codecname)) {
/* Don't need to transform the content - no-op */
return;
} else {
throw new HttpException("Unsupported Content-Coding: " + codec.getName());
}
}
}
}
}
示例4: load
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* load the content of this page from a fetched HttpEntity.
* @param entity HttpEntity
* @param maxBytes The maximum number of bytes to read
* @throws Exception when load fails
*/
public void load(HttpEntity entity, int maxBytes) throws Exception {
contentType = null;
Header type = entity.getContentType();
if (type != null) {
contentType = type.getValue();
}
contentEncoding = null;
Header encoding = entity.getContentEncoding();
if (encoding != null) {
contentEncoding = encoding.getValue();
}
Charset charset = ContentType.getOrDefault(entity).getCharset();
if (charset != null) {
contentCharset = charset.displayName();
}else {
contentCharset = Charset.defaultCharset().displayName();
}
contentData = toByteArray(entity, maxBytes);
}
示例5: getEncoding
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
private static Charset getEncoding(HttpEntity entity) {
if (entity.getContentEncoding() != null) {
String value = entity.getContentEncoding().getValue();
if (value != null) {
try {
return Charset.forName(value);
} catch (RuntimeException e) {
// use the default charset!
LOGGER.debug("Unsupported charset: {}", value, e);
}
}
}
return StandardCharsets.UTF_8;
}
示例6: process
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public void process(HttpResponse response, HttpContext context) {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header encoding = entity.getContentEncoding();
if (encoding != null) {
for (HeaderElement element : encoding.getElements()) {
if (element.getName().equalsIgnoreCase(AsyncHttpClient.ENCODING_GZIP)) {
response.setEntity(new AsyncHttpClient$InflatingEntity(entity));
return;
}
}
}
}
}
示例7: a
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static InputStream a(HttpEntity httpEntity) {
InputStream content = httpEntity.getContent();
if (content == null) {
return content;
}
Header contentEncoding = httpEntity.getContentEncoding();
if (contentEncoding == null) {
return content;
}
String value = contentEncoding.getValue();
if (value == null) {
return content;
}
return value.contains(AsyncHttpClient.ENCODING_GZIP) ? new GZIPInputStream(content) : content;
}
示例8: afterDoCap
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@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();
// 由于存在读取失败和无法缓存的大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");
}
}
Object params[] = { span, slowOperContext };
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap",
span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
}
}
示例9: handleCompressedEntity
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static HttpEntity handleCompressedEntity(HttpEntity entity) {
org.apache.http.Header contentEncoding = entity.getContentEncoding();
if (contentEncoding != null)
for (HeaderElement e : contentEncoding.getElements()) {
if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) {
return new GzipDecompressingEntity(entity);
}
if (CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) {
return new DeflateDecompressingEntity(entity);
}
}
return entity;
}
示例10: process
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (request instanceof HttpEntityEnclosingRequest) {
if (this.overwrite) {
request.removeHeaders(HTTP.TRANSFER_ENCODING);
request.removeHeaders(HTTP.CONTENT_LEN);
} else {
if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (request.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
}
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
if (entity == null) {
request.addHeader(HTTP.CONTENT_LEN, "0");
return;
}
// Must specify a transfer encoding or a content length
if (entity.isChunked() || entity.getContentLength() < 0) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
throw new ProtocolException(
"Chunked transfer encoding not allowed for " + ver);
}
request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else {
request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !request.containsHeader(
HTTP.CONTENT_TYPE )) {
request.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !request.containsHeader(
HTTP.CONTENT_ENCODING)) {
request.addHeader(entity.getContentEncoding());
}
}
}
示例11: process
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
* @param response The HttpResponse to modify.
* @param context Unused.
* @throws ProtocolException If either the Content-Length or Transfer-Encoding headers are found.
* @throws IllegalArgumentException If the response is null.
*/
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
if (this.overwrite) {
response.removeHeaders(HTTP.TRANSFER_ENCODING);
response.removeHeaders(HTTP.CONTENT_LEN);
} else {
if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (response.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
}
ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else if (len >= 0) {
response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !response.containsHeader(
HTTP.CONTENT_TYPE )) {
response.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !response.containsHeader(
HTTP.CONTENT_ENCODING)) {
response.addHeader(entity.getContentEncoding());
}
} else {
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT
&& status != HttpStatus.SC_NOT_MODIFIED
&& status != HttpStatus.SC_RESET_CONTENT) {
response.addHeader(HTTP.CONTENT_LEN, "0");
}
}
}
示例12: apply
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@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);
}
}
示例13: run
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@Override
public void run() {
try (final CloseableHttpClient httpClient = createMinimal()) {
CloseableHttpResponse response = httpClient.execute(new HttpGet(APP_VERSION_URL));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != 200) {
return;
}
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
Header encoding = entity.getContentEncoding();
String enc = encoding == null ? "UTF-8" : encoding.getValue();
final String targetVersion = IOUtils.toString(content, enc)
.trim()
.replace("v.", "");
if (Config.APP_VERSION.compareTo(new Version(targetVersion)) >= 0) {
return;
}
response = httpClient.execute(new HttpGet(DOWNLOAD_APP_PATH_URL));
statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != 200) {
return;
}
entity = response.getEntity();
content = entity.getContent();
encoding = entity.getContentEncoding();
enc = encoding == null ? "UTF-8" : encoding.getValue();
final String targetLink = IOUtils.toString(content, enc).trim();
Platform.runLater(() -> {
final JFXApplication jfxApplication = JFXApplication.getInstance();
final HostServices hostServices = jfxApplication.getHostServices();
final Hyperlink hyperlink = new Hyperlink(Messages.CHECK_NEW_VERSION_DIALOG_HYPERLINK + targetLink);
hyperlink.setOnAction(event -> hostServices.showDocument(targetLink));
final Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(Messages.CHECK_NEW_VERSION_DIALOG_TITLE);
alert.setHeaderText(Messages.CHECK_NEW_VERSION_DIALOG_HEADER_TEXT + targetVersion);
final DialogPane dialogPane = alert.getDialogPane();
dialogPane.setContent(hyperlink);
alert.show();
});
} catch (final IOException e) {
LOGGER.warning(e);
}
}