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; } }
@Override public String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public final String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public final String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public final String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public final String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public String getContentType() { return contentType != null ? contentType.toString() : null; }
@Override public final String getContentType() { return contentType != null ? contentType.toString() : null; }
addHeaders(httpPut, EntityUtils.toString(stringEntity), ContentType.APPLICATION_JSON.toString());
public ByteBufferEntity(final ByteBuffer buffer, final ContentType contentType) { super(); Args.notNull(buffer, "Source byte buffer"); this.buffer = buffer; if (contentType != null) { setContentType(contentType.toString()); } }
public ByteBufferEntity(final ByteBuffer buffer, final ContentType contentType) { super(); Args.notNull(buffer, "Source byte buffer"); this.buffer = buffer; if (contentType != null) { setContentType(contentType.toString()); } }
/** * @since 4.2 */ public ByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = this.b.length; if (contentType != null) { setContentType(contentType.toString()); } }
/** * @since 4.2 */ public ByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = this.b.length; if (contentType != null) { setContentType(contentType.toString()); } }
@Test public void testWithCharset() throws Exception { ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); contentType = contentType.withCharset(Charset.forName("UTF-8")); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals("UTF-8", contentType.getCharset().name()); Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString()); }
@Test public void testWithCharsetString() throws Exception { ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); contentType = contentType.withCharset(StandardCharsets.UTF_8); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString()); }
@Test public void testBasis() throws Exception { final ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); }
@Test public void testParse() throws Exception { final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\""); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=ascii", contentType.toString()); }
@Test public void testTextContent() throws Exception { final AsyncEntityProducer producer = new StringAsyncEntityProducer( "abc", ContentType.TEXT_PLAIN); Assert.assertEquals(-1, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII)); }
@Test public void testTextContent() throws Exception { final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN); Assert.assertEquals(6, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII)); }