private void dispose() { if (this.consumed) { return; } try { final HttpEntity entity = this.response.getEntity(); final InputStream content = entity.getContent(); if (content != null) { content.close(); } } catch (final Exception ignore) { } finally { this.consumed = true; } }
@Override public String getContentType() { return entity.getContentType(); }
@Override public void close() throws IOException { try { combinedStream.close(); } finally { entity.close(); } }
@Test public void testCreateEntityInputChunked() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.CHUNKED); Assert.assertNotNull(entity); Assert.assertTrue(entity.isChunked()); Assert.assertEquals(-1, entity.getContentLength()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof ChunkedInputStream)); }
@Test public void testCreateEntityLengthDelimited() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); message.addHeader("Content-Length", "10"); message.addHeader("Content-Type", "stuff"); message.addHeader("Content-Encoding", "chunked"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, 10); Assert.assertNotNull(entity); Assert.assertFalse(entity.isChunked()); Assert.assertEquals(10, entity.getContentLength()); Assert.assertEquals("stuff", entity.getContentType()); Assert.assertEquals("chunked", entity.getContentEncoding()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof ContentLengthInputStream)); }
@Override public void sendResponseEntity(final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final long len = this.outgoingContentStrategy.determineLength(response); try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), entity.getTrailers())) { entity.writeTo(outStream); } } }
/** * Creates a new buffered entity wrapper. * * @param entity the entity to wrap, not null * @throws IllegalArgumentException if wrapped is null */ public BufferedHttpEntity(final HttpEntity entity) throws IOException { super(entity); if (!entity.isRepeatable() || entity.getContentLength() < 0) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); out.flush(); this.buffer = out.toByteArray(); } else { this.buffer = null; } }
@Test public void testReadResponseEntityWithContentLength() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\nContent-Length: 3\r\n\r\n123"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); final HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(3, entity.getContentLength()); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ContentLengthInputStream); }
@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(); }
@Override public long getContentLength() { return wrappedEntity.getContentLength(); }
@Override public String getContentEncoding() { return entity.getContentEncoding(); }
@Override public boolean isRepeatable() { return wrappedEntity.isRepeatable(); }
if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) { final String contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) { final ParserCursor cursor = new ParserCursor(0, contentEncoding.length());
public void saveContent(final File file) throws IOException { assertNotConsumed(); final int status = response.getCode(); if (status >= HttpStatus.SC_REDIRECTION) { throw new HttpResponseException(status, response.getReasonPhrase()); } try (FileOutputStream out = new FileOutputStream(file)) { final HttpEntity entity = this.response.getEntity(); if (entity != null) { entity.writeTo(out); } } finally { this.consumed = true; } }
@Override public Set<String> getTrailerNames() { return entity.getTrailerNames(); }
@Override public Supplier<List<? extends Header>> getTrailers() { return entity.getTrailers(); }
if (entity != null && !entity.isRepeatable()) { try { if (entity != null) { entity.writeTo(entityDigester);
@Override public boolean isChunked() { return wrappedEntity.isChunked(); }
@Test public void testCreateEntityInputUndefined() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.UNDEFINED); Assert.assertNotNull(entity); Assert.assertFalse(entity.isChunked()); Assert.assertEquals(-1, entity.getContentLength()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof IdentityInputStream)); }
@Override public void sendResponseEntity(final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final long len = this.outgoingContentStrategy.determineLength(response); try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), entity.getTrailers())) { entity.writeTo(outStream); } } }