Examples of BasicHttpEntity


Examples of org.apache.http.entity.BasicHttpEntity

    public void suspendOutput() {
        this.session.clearEvent(EventMask.WRITE);
    }

    protected HttpEntity prepareDecoder(final HttpMessage message) throws HttpException {
        BasicHttpEntity entity = new BasicHttpEntity();
        long len = this.incomingContentStrategy.determineLength(message);
        if (len == ContentLengthStrategy.CHUNKED) {
            this.contentDecoder = new ChunkDecoder(
                    this.session.channel(),
                    this.inbuf,
                    this.inTransportMetrics);
            entity.setChunked(true);
            entity.setContentLength(-1);
        } else if (len == ContentLengthStrategy.IDENTITY) {
            this.contentDecoder = new IdentityDecoder(
                    this.session.channel(),
                    this.inbuf,
                    this.inTransportMetrics);
            entity.setChunked(false);
            entity.setContentLength(-1);
        } else {
            this.contentDecoder = new LengthDelimitedDecoder(
                    this.session.channel(),
                    this.inbuf,
                    this.inTransportMetrics,
                    len);
            entity.setChunked(false);
            entity.setContentLength(len);
        }
       
        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
        if (contentTypeHeader != null) {
            entity.setContentType(contentTypeHeader);   
        }
        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
        if (contentEncodingHeader != null) {
            entity.setContentEncoding(contentEncodingHeader);   
        }
        return entity;
    }
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        }
        if (message == null) {
            throw new IllegalArgumentException("HTTP message may not be null");
        }

        BasicHttpEntity entity = new BasicHttpEntity();
       
        HttpParams params = message.getParams();
        boolean strict = params.isParameterTrue(HttpProtocolParams.STRICT_TRANSFER_ENCODING);
       
        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
        Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
        Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
        // We use Transfer-Encoding if present and ignore Content-Length.
        // RFC2616, 4.4 item number 3
        if (transferEncodingHeader != null) {
            HeaderElement[] encodings = transferEncodingHeader.getElements();
            if (strict) {
                // Currently only chunk and identity are supported
                for (int i = 0; i < encodings.length; i++) {
                    String encoding = encodings[i].getName();
                    if (encoding != null && !encoding.equals("")
                        && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
                        && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
                        throw new ProtocolException("Unsupported transfer encoding: " + encoding);
                    }
                }
            }
            // The chunked encoding must be the last one applied RFC2616, 14.41
            int len = encodings.length;
            if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
                entity.setChunked(false);
                entity.setContentLength(-1);
                entity.setContent(new HttpDataInputStream(datareceiver));                           
            } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
                    encodings[len - 1].getName()))) {
                entity.setChunked(true);
                entity.setContentLength(-1);
                entity.setContent(new ChunkedInputStream(datareceiver));
            } else {
                if (strict) {
                    throw new ProtocolException("Chunk-encoding must be the last one applied");
                }
                entity.setChunked(false);
                entity.setContentLength(-1);
                entity.setContent(new HttpDataInputStream(datareceiver));                           
            }
        } else if (contentLengthHeader != null) {
            long contentlen = -1;
            Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
            if (strict && headers.length > 1) {
                throw new ProtocolException("Multiple content length headers");
            }
            for (int i = headers.length - 1; i >= 0; i--) {
                Header header = headers[i];
                try {
                    contentlen = Long.parseLong(header.getValue());
                    break;
                } catch (NumberFormatException e) {
                    if (strict) {
                        throw new ProtocolException("Invalid content length: " + header.getValue());
                    }
                }
                // See if we can have better luck with another header, if present
            }
            entity.setChunked(false);
            entity.setContentLength(contentlen);
            if (contentlen >= 0) {
                entity.setContent(new ContentLengthInputStream(datareceiver, contentlen));
            } else {
                entity.setContent(new HttpDataInputStream(datareceiver));
            }
        } else {
            entity.setChunked(false);
            entity.setContentLength(-1);
            entity.setContent(new HttpDataInputStream(datareceiver));                           
        }
        if (contentTypeHeader != null) {
            entity.setContentType(contentTypeHeader);   
        }
        if (contentEncodingHeader != null) {
            entity.setContentEncoding(contentEncodingHeader);   
        }
        return entity;
    }
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

    }
   
    public void testContentWithContentTypeToString() throws Exception {
        String content = constructString(RUSSIAN_HELLO);
        byte[] bytes = content.getBytes("UTF-8");
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContent(new ByteArrayInputStream(bytes));
        httpentity.setContentType(new Header("Content-Type", "text/plain; charset=UTF-8"));
        String s = EntityUtils.toString(httpentity, "ISO-8859-1");
        assertEquals(content, s);
    }
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        assertEquals(0, bytes.length);
    }
   
    public void testMaxIntContentToByteArray() throws Exception {
        byte[] content = "Message content".getBytes("ISO-8859-1");
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContent(new ByteArrayInputStream(content));
        httpentity.setContentLength(Integer.MAX_VALUE + 100L);
        try {
            EntityUtils.toByteArray(httpentity);
            fail("IllegalArgumentException should have been thrown");
        } catch (IllegalArgumentException ex) {
            // expected
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        }
    }
   
    public void testUnknownLengthContentToByteArray() throws Exception {
        byte[] bytes = "Message content".getBytes("ISO-8859-1");
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContent(new ByteArrayInputStream(bytes));
        httpentity.setContentLength(-1L);
        byte[] bytes2 = EntityUtils.toByteArray(httpentity);
        assertNotNull(bytes2);
        assertEquals(bytes.length, bytes2.length);
        for (int i = 0; i < bytes.length; i++) {
            assertEquals(bytes[i], bytes2[i]);
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        }
    }
   
    public void testKnownLengthContentToByteArray() throws Exception {
        byte[] bytes = "Message content".getBytes("ISO-8859-1");
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContent(new ByteArrayInputStream(bytes));
        httpentity.setContentLength(bytes.length);
        byte[] bytes2 = EntityUtils.toByteArray(httpentity);
        assertNotNull(bytes2);
        assertEquals(bytes.length, bytes2.length);
        for (int i = 0; i < bytes.length; i++) {
            assertEquals(bytes[i], bytes2[i]);
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

            // expected
        }
    }
   
    public void testNullContentTypeGetContentCharset() throws Exception {
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContentType((Header)null);
        assertNull(EntityUtils.getContentCharSet(httpentity));
    }
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        httpentity.setContentType((Header)null);
        assertNull(EntityUtils.getContentCharSet(httpentity));
    }
   
    public void testNoCharsetGetContentCharset() throws Exception {
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContentType(new Header("Content-Type", "text/plain; param=yadayada"));
        assertNull(EntityUtils.getContentCharSet(httpentity));
    }
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        httpentity.setContentType(new Header("Content-Type", "text/plain; param=yadayada"));
        assertNull(EntityUtils.getContentCharSet(httpentity));
    }
   
    public void testGetContentCharset() throws Exception {
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContentType(new Header("Content-Type", "text/plain; charset = UTF-8"));
        assertEquals("UTF-8", EntityUtils.getContentCharSet(httpentity));
    }
View Full Code Here

Examples of org.apache.http.entity.BasicHttpEntity

        assertEquals("", s);
    }
       
    public void testMaxIntContentToString() throws Exception {
        byte[] content = "Message content".getBytes("ISO-8859-1");
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContent(new ByteArrayInputStream(content));
        httpentity.setContentLength(Integer.MAX_VALUE + 100L);
        try {
            EntityUtils.toString(httpentity);
            fail("IllegalArgumentException should have been thrown");
        } catch (IllegalArgumentException ex) {
            // expected
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.