/** * Ensures that the entity content is fully consumed and the content stream, if exists, * is closed. The process is done, <i>quietly</i> , without throwing any IOException. * * @param entity the entity to consume. * * * @since 4.2 */ public static void consumeQuietly(final HttpEntity entity) { try { consume(entity); } catch (final IOException ignore) { } }
data = new byte [0]; } else { data = EntityUtils.toByteArray(entity);
@Override public Content handleEntity(final HttpEntity entity) throws IOException { return entity != null ? new Content(EntityUtils.toByteArray(entity), EntityUtils.getContentTypeOrDefault(entity)) : Content.NO_CONTENT; }
/** * Read the contents of an entity and return it as a String. * The content is converted using the character set from the entity (if any), * failing that, "ISO-8859-1" is used. * * @param entity the entity to convert to a string; must not be null * @return String containing the content. * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws IOException if an error occurs reading the input stream * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static String toString(final HttpEntity entity) throws IOException, ParseException { Args.notNull(entity, "Entity"); return toString(entity, getContentType(entity)); }
/** * Extracts {@code Content-Type} value from {@link HttpEntity} or returns the default value * {@link ContentType#DEFAULT_TEXT} if not explicitly specified or incorrect (could not be parsed). * * @param entity HTTP entity * @return content type * * @since 4.4 */ public static ContentType getContentTypeLenientOrDefault(final HttpEntity entity) throws UnsupportedCharsetException { final ContentType contentType = getContentType(entity); return contentType != null ? contentType : ContentType.DEFAULT_TEXT; }
final ContentType contentType = EntityUtils.getContentType(originalEntity); if (contentType != null && contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { try { final List<NameValuePair> formParams = EntityUtils.parse(originalEntity); if (!formParams.isEmpty()) { parameters = formParams;
@Test public void testParseEntity() throws Exception { try (final StringEntity entity = new StringEntity("Name1=Value1")) { entity.setContentType(URLEncodedUtils.CONTENT_TYPE); final List<NameValuePair> result = EntityUtils.parse(entity); Assert.assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name1", "Value1"); entity.setContentType("text/test"); Assert.assertTrue(EntityUtils.parse(entity).isEmpty()); }}
@Test public void testExtracLenienttNullInput() throws Exception { Assert.assertNull(EntityUtils.getContentTypeLenient(null)); }
/** * Read the contents of an entity and return it as a String. * The content is converted using the character set from the entity (if any), * failing that, "ISO-8859-1" is used. * * @param entity the entity to convert to a string; must not be null * @return String containing the content. * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws IOException if an error occurs reading the input stream * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static String toString(final HttpEntity entity) throws IOException, ParseException { Args.notNull(entity, "Entity"); return toString(entity, getContentType(entity)); }
public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
/** * Extracts {@code Content-Type} value from {@link HttpEntity} or returns the default value * {@link ContentType#DEFAULT_TEXT} if not explicitly specified or incorrect (could not be parsed). * * @param entity HTTP entity * @return content type * * @since 4.4 */ public static ContentType getContentTypeLenientOrDefault(final HttpEntity entity) throws UnsupportedCharsetException { final ContentType contentType = getContentType(entity); return contentType != null ? contentType : ContentType.DEFAULT_TEXT; }
@Test public void testParseUTF8Entity() throws Exception { final String ru_hello = constructString(RUSSIAN_HELLO); final String ch_hello = constructString(SWISS_GERMAN_HELLO); final List <NameValuePair> parameters = new ArrayList<>(); parameters.add(new BasicNameValuePair("russian", ru_hello)); parameters.add(new BasicNameValuePair("swiss", ch_hello)); final String s = URLEncodedUtils.format(parameters, StandardCharsets.UTF_8); Assert.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" + "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s); try (final StringEntity entity = new StringEntity(s, ContentType.create(URLEncodedUtils.CONTENT_TYPE, StandardCharsets.UTF_8))) { final List<NameValuePair> result = EntityUtils.parse(entity); Assert.assertEquals(2, result.size()); assertNameValuePair(result.get(0), "russian", ru_hello); assertNameValuePair(result.get(1), "swiss", ch_hello); } }
@Test public void testExtractLenientNullContentType() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType(null); Assert.assertNull(EntityUtils.getContentTypeLenient(httpentity)); }}
/** * Ensures that the entity content is fully consumed and the content stream, if exists, * is closed. The process is done, <i>quietly</i> , without throwing any IOException. * * @param entity the entity to consume. * * * @since 4.2 */ public static void consumeQuietly(final HttpEntity entity) { try { consume(entity); } catch (final IOException ignore) { } }
debugResponse(response); String json = EntityUtils.toString(response.getEntity()); if (response.getCode() == HttpStatus.SC_OK) { archiveWorkspace(workspaceId, json);
ContentType contentType = null; try { contentType = getContentType(entity); } catch (final UnsupportedCharsetException ex) { if (defaultCharset == null) { contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset); return toString(entity, contentType);
@Test public void testCombinedEntityBasics() throws Exception { final HttpEntity httpEntity = mock(HttpEntity.class); when(httpEntity.getContent()).thenReturn( new ByteArrayInputStream(new byte[] { 6, 7, 8, 9, 10 })); final ByteArrayBuffer buf = new ByteArrayBuffer(1024); final byte[] tmp = new byte[] { 1, 2, 3, 4, 5 }; buf.append(tmp, 0, tmp.length); final CombinedEntity entity = new CombinedEntity(httpEntity, buf); Assert.assertEquals(-1, entity.getContentLength()); Assert.assertFalse(entity.isRepeatable()); Assert.assertTrue(entity.isStreaming()); Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, EntityUtils.toByteArray(entity)); verify(httpEntity).getContent(); entity.close(); verify(httpEntity).close(); }