/** * Loads the content of this page from a fetched HttpEntity. * * @param entity HttpEntity * @param maxBytes The maximum number of bytes to read * @throws IOException when load fails */ public void load(HttpEntity entity, int maxBytes) throws IOException { 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; try { charset = ContentType.getOrDefault(entity).getCharset(); } catch (Exception e) { logger.warn("parse charset failed: {}", e.getMessage()); charset = Charset.forName("UTF-8"); } if (charset != null) { contentCharset = charset.displayName(); } contentData = toByteArray(entity, maxBytes); }
rec.setCharset(getDefaultCharset()); try { Charset charset = ContentType.getOrDefault(response.getEntity()).getCharset(); if (charset != null) { rec.setCharset(charset);
ContentType contentType = ContentType.getOrDefault( httpEntity ); charSet = contentType.getCharset(); processRows( httpEntity.getContent(), rowData, rowMeta, cachedWsdl.getWsdlTypes()
public ContentType getContentType() { ContentType contentType = ContentType.getOrDefault(this.response .getEntity()); return contentType; }
private void debug(final HttpEntity entity) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Default charset: {}", Charset.defaultCharset()); LOGGER.debug("Entity Content-Type: {}", org.apache.http.entity.ContentType.getOrDefault(entity)); } }
public static String readPayload(HttpEntity entity) throws IOException { return entity == null ? null : EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset()); }
static String getCharset(HttpResponse response) { ContentType contentType = ContentType.getOrDefault(response.getEntity()); Charset charset = contentType.getCharset(); return charset == null ? "UTF-8" : charset.name(); }
public String getContentEncoding() { return ContentType.getOrDefault( response.getEntity() ).getCharset().name(); }
public static void main(String[] args) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try (CloseableHttpResponse response = httpclient.execute(new HttpGet("http://httpbin.org/anything"))) { HttpEntity entity = response.getEntity(); String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); System.out.println(contentMimeType); System.out.println(contentMimeType.equals(ContentType.APPLICATION_JSON.getMimeType())); // and ensure it is fully consumed EntityUtils.consume(entity); } }
public static String readResponseBody(HttpResponse response, int limit) throws IOException { InputStream in = response.getEntity().getContent(); if (in == null) return null; Charset charset = ContentType.getOrDefault(response.getEntity()).getCharset(); if (charset == null) charset = Consts.ISO_8859_1; InputStreamReader reader = new InputStreamReader(in, charset); StringWriter writer = new StringWriter(); StreamUtils.transfer(reader, writer, limit); return writer.toString(); }
protected Charset parseCharset(final HttpEntity entity) { try { final ContentType contentType = ContentType.getOrDefault(entity); if (contentType != null && contentType.getCharset() != null) { return contentType.getCharset(); } } catch (final ParseException e) { logger.warn("Failed to parse content type", e); } return Charset.defaultCharset(); }
private String getContent(HttpEntity entity) throws IOException { ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); charset = (charset != null) ? charset : StandardCharsets.UTF_8; byte[] content = FileCopyUtils.copyToByteArray(entity.getContent()); return new String(content, charset); }
protected Charset parseCharset(final HttpEntity entity) { try { final ContentType contentType = ContentType.getOrDefault(entity); if (contentType != null && contentType.getCharset() != null) { return contentType.getCharset(); } } catch (final ParseException e) { logger.warn("Failed to parse content type", e); } return Charset.defaultCharset(); }
private JsonObject getBody(HttpEntity entity) throws IOException { Charset charset = ContentType.getOrDefault(entity).getCharset(); String raw = IOUtils.toString(entity.getContent(), charset); if (raw.isEmpty()) { return new JsonObject(); } try { return GsonUtil.parseObj(raw); } catch (InvalidJsonException e) { return GsonUtil.parse(raw).getAsJsonArray().get(1).getAsJsonObject(); } }
public String getContentString() { ContentType contentType = ContentType.getOrDefault(this.response .getEntity()); return new String(this.getContent(), contentType.getCharset()); }
/** * Use {@link #onResponseReceived(HttpResponse)} instead. */ @Override public final void responseReceived( final HttpResponse response) throws IOException, HttpException { onResponseReceived(response); final HttpEntity entity = response.getEntity(); if (entity != null) { final ContentType contentType = ContentType.getOrDefault(entity); onEntityEnclosed(entity, contentType); } }
/** * Use {@link #onResponseReceived(HttpResponse)} instead. */ @Override public final void responseReceived( final HttpResponse response) throws IOException, HttpException { onResponseReceived(response); final HttpEntity entity = response.getEntity(); if (entity != null) { final ContentType contentType = ContentType.getOrDefault(entity); onEntityEnclosed(entity, contentType); } }
@Override public Content handleEntity(final HttpEntity entity) throws IOException { return entity != null ? new Content(EntityUtils.toByteArray(entity), ContentType.getOrDefault(entity)) : Content.NO_CONTENT; }
private Content handleEntity(final HttpEntity entity) throws IOException { Content content = null; if (entity != null) { content = new Content(EntityUtils.toByteArray(entity), ContentType.getOrDefault(entity)); } else { content = Content.NO_CONTENT; } return content; } }
private ProjectGenerationResponse createResponse(CloseableHttpResponse httpResponse, HttpEntity httpEntity) throws IOException { ProjectGenerationResponse response = new ProjectGenerationResponse( ContentType.getOrDefault(httpEntity)); response.setContent(FileCopyUtils.copyToByteArray(httpEntity.getContent())); String fileName = extractFileName( httpResponse.getFirstHeader("Content-Disposition")); if (fileName != null) { response.setFileName(fileName); } return response; }