本文整理汇总了Java中org.apache.http.HttpEntity.getContentType方法的典型用法代码示例。如果您正苦于以下问题:Java HttpEntity.getContentType方法的具体用法?Java HttpEntity.getContentType怎么用?Java HttpEntity.getContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpEntity
的用法示例。
在下文中一共展示了HttpEntity.getContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getContentCharSet
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Obtains character set of the entity, if known.
*
* @param entity must not be null
* @return the character set, or null if not found
* @throws ParseException if header elements cannot be parsed
* @throws IllegalArgumentException if entity is null
*
* @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}
*/
@Deprecated
public static String getContentCharSet(final HttpEntity entity) throws ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
String charset = null;
if (entity.getContentType() != null) {
HeaderElement values[] = entity.getContentType().getElements();
if (values.length > 0) {
NameValuePair param = values[0].getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
}
}
return charset;
}
示例2: parseEntity
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
<Resp> Resp parseEntity(
HttpEntity entity, CheckedFunction<XContentParser, Resp, IOException> entityParser) throws IOException {
if (entity == null) {
throw new IllegalStateException("Response body expected but not returned");
}
if (entity.getContentType() == null) {
throw new IllegalStateException("Elasticsearch didn't return the [Content-Type] header, unable to parse response body");
}
XContentType xContentType = XContentType.fromMediaTypeOrFormat(entity.getContentType().getValue());
if (xContentType == null) {
throw new IllegalStateException("Unsupported Content-Type: " + entity.getContentType().getValue());
}
try (XContentParser parser = xContentType.xContent().createParser(registry, entity.getContent())) {
return entityParser.apply(parser);
}
}
示例3: MatrixHttpContentResult
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public MatrixHttpContentResult(CloseableHttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
valid = entity != null && response.getStatusLine().getStatusCode() == 200;
if (entity != null) {
headers = Arrays.asList(response.getAllHeaders());
Header contentTypeHeader = entity.getContentType();
if (contentTypeHeader != null) {
contentType = Optional.of(contentTypeHeader.getValue());
} else {
contentType = Optional.empty();
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
entity.writeTo(outStream);
data = outStream.toByteArray();
} else {
headers = new ArrayList<>();
contentType = Optional.empty();
data = new byte[0];
}
}
示例4: HttpEntityBody
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
HttpEntityBody(HttpEntity entity, String contentTypeHeader) {
this.entity = entity;
if (contentTypeHeader != null) {
mediaType = MediaType.parse(contentTypeHeader);
} else if (entity.getContentType() != null) {
mediaType = MediaType.parse(entity.getContentType().getValue());
} else {
// Apache is forgiving and lets you skip specifying a content type with an entity. OkHttp is
// not forgiving so we fall back to a generic type if it's missing.
mediaType = DEFAULT_MEDIA_TYPE;
}
}
示例5: getContentType
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static String getContentType(HttpEntity entity)
{
if (entity == null || entity.getContentType() == null)
{
return null;
}
return entity.getContentType().getValue();
}
示例6: isEncoded
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Returns true if the entity's Content-Type header is
* <code>application/x-www-form-urlencoded</code>.
*/
public static boolean isEncoded(final HttpEntity entity) {
Header h = entity.getContentType();
if (h != null) {
HeaderElement[] elems = h.getElements();
if (elems.length > 0) {
String contentType = elems[0].getName();
return contentType.equalsIgnoreCase(CONTENT_TYPE);
} else {
return false;
}
} else {
return false;
}
}
示例7: executeContentRequest
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
protected MatrixHttpContentResult executeContentRequest(MatrixHttpRequest matrixRequest) {
log(matrixRequest.getHttpRequest());
try (CloseableHttpResponse response = client.execute(matrixRequest.getHttpRequest())) {
HttpEntity entity = response.getEntity();
int responseStatus = response.getStatusLine().getStatusCode();
MatrixHttpContentResult result = new MatrixHttpContentResult(response);
if (responseStatus == 200) {
log.debug("Request successfully executed.");
if (entity == null) {
log.debug("No data received.");
} else if (entity.getContentType() == null) {
log.debug("No content type was given.");
}
} else if (matrixRequest.getIgnoredErrorCodes().contains(responseStatus)) {
log.debug("Error code ignored: " + responseStatus);
} else {
String body = getBody(entity);
MatrixErrorInfo info = createErrorInfo(body, responseStatus);
result = handleErrorContentRequest(matrixRequest, responseStatus, info);
}
return result;
} catch (IOException e) {
throw new MatrixClientRequestException(e);
}
}
示例8: isPrintable
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static boolean isPrintable(HttpEntity entity) {
if (entity == null) {
return false;
}
return entity.getContentType() != null
&& HttpUtils.isPrintable(entity.getContentType().getValue());
}
示例9: isEncoded
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Returns true if the entity's Content-Type header is
* <code>application/x-www-form-urlencoded</code>.
*/
public static boolean isEncoded (final HttpEntity entity) {
Header h = entity.getContentType();
if (h != null) {
HeaderElement[] elems = h.getElements();
if (elems.length > 0) {
String contentType = elems[0].getName();
return contentType.equalsIgnoreCase(CONTENT_TYPE);
} else {
return false;
}
} else {
return false;
}
}
示例10: get
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Extracts <code>Content-Type</code> value from {@link HttpEntity} exactly as
* specified by the <code>Content-Type</code> header of the entity. Returns <code>null</code>
* if not specified.
*
* @param entity HTTP entity
* @return content type
* @throws ParseException if the given text does not represent a valid
* <code>Content-Type</code> value.
*/
public static ContentType get(
final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
if (entity == null) {
return null;
}
Header header = entity.getContentType();
if (header != null) {
HeaderElement[] elements = header.getElements();
if (elements.length > 0) {
return create(elements[0]);
}
}
return null;
}
示例11: getContentMimeType
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Obtains mime type of the entity, if known.
*
* @param entity must not be null
* @return the character set, or null if not found
* @throws ParseException if header elements cannot be parsed
* @throws IllegalArgumentException if entity is null
*
* @since 4.1
*
* @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}
*/
@Deprecated
public static String getContentMimeType(final HttpEntity entity) throws ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
String mimeType = null;
if (entity.getContentType() != null) {
HeaderElement values[] = entity.getContentType().getElements();
if (values.length > 0) {
mimeType = values[0].getName();
}
}
return mimeType;
}
示例12: proxyRequest
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
Response proxyRequest(String method, ContainerRequestContext ctx) {
if (!Config.getConfigBoolean("es.proxy_enabled", false)) {
return Response.status(Response.Status.FORBIDDEN.getStatusCode(), "This feature is disabled.").build();
}
String appid = ParaObjectUtils.getAppidFromAuthHeader(ctx.getHeaders().getFirst(HttpHeaders.AUTHORIZATION));
String path = getCleanPath(getPath(ctx));
if (StringUtils.isBlank(appid)) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
try {
if ("reindex".equals(path) && POST.equals(method)) {
return handleReindexTask(appid);
}
Header[] headers = getHeaders(ctx.getHeaders());
HttpEntity resp;
RestClient client = getClient(appid);
if (client != null) {
if (ctx.getEntityStream() != null && ctx.getEntityStream().available() > 0) {
HttpEntity body = new InputStreamEntity(ctx.getEntityStream(), ContentType.APPLICATION_JSON);
resp = client.performRequest(method, path, Collections.emptyMap(), body, headers).getEntity();
} else {
resp = client.performRequest(method, path, headers).getEntity();
}
if (resp != null && resp.getContent() != null) {
Header type = resp.getContentType();
Object response = getTransformedResponse(appid, resp.getContent(), ctx);
return Response.ok(response).header(type.getName(), type.getValue()).build();
}
}
} catch (Exception ex) {
logger.warn("Failed to proxy '{} {}' to Elasticsearch: {}", method, path, ex.getMessage());
}
return Response.status(Response.Status.BAD_REQUEST).build();
}
示例13: 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);
}
示例14: 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());
}
}
}
示例15: 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");
}
}
}