Search in sources :

Example 21 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project robovm by robovm.

the class EntityDeserializer method doDeserialize.

protected BasicHttpEntity doDeserialize(final SessionInputBuffer inbuffer, final HttpMessage message) throws HttpException, IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = this.lenStrategy.determineLength(message);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, 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;
}
Also used : Header(org.apache.http.Header) ContentLengthInputStream(org.apache.http.impl.io.ContentLengthInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) IdentityInputStream(org.apache.http.impl.io.IdentityInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 22 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project robolectric by robolectric.

the class ShadowDefaultRequestDirector method interceptResponseContent.

private void interceptResponseContent(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    if (entity instanceof HttpEntityWrapper) {
        HttpEntityWrapper entityWrapper = (HttpEntityWrapper) entity;
        try {
            Field wrappedEntity = HttpEntityWrapper.class.getDeclaredField("wrappedEntity");
            wrappedEntity.setAccessible(true);
            entity = (HttpEntity) wrappedEntity.get(entityWrapper);
        } catch (Exception e) {
        // fail to record
        }
    }
    if (entity instanceof BasicHttpEntity) {
        BasicHttpEntity basicEntity = (BasicHttpEntity) entity;
        try {
            Field contentField = BasicHttpEntity.class.getDeclaredField("content");
            contentField.setAccessible(true);
            InputStream content = (InputStream) contentField.get(basicEntity);
            byte[] buffer = Util.readBytes(content);
            FakeHttp.getFakeHttpLayer().addHttpResponseContent(buffer);
            contentField.set(basicEntity, new ByteArrayInputStream(buffer));
        } catch (Exception e) {
        // fail to record
        }
    }
}
Also used : Field(java.lang.reflect.Field) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpEntityWrapper(org.apache.http.entity.HttpEntityWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException) HttpException(org.apache.http.HttpException)

Example 23 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project cloudstack by apache.

the class ClusterServiceServletHttpHandler method writeResponse.

private void writeResponse(HttpResponse response, int statusCode, String content) {
    if (content == null) {
        content = "";
    }
    response.setStatusCode(statusCode);
    final BasicHttpEntity body = new BasicHttpEntity();
    body.setContentType("text/html; charset=UTF-8");
    final byte[] bodyData = content.getBytes();
    body.setContent(new ByteArrayInputStream(bodyData));
    body.setContentLength(bodyData.length);
    response.setEntity(body);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 24 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project platform_external_apache-http by android.

the class EntityDeserializer method doDeserialize.

protected BasicHttpEntity doDeserialize(final SessionInputBuffer inbuffer, final HttpMessage message) throws HttpException, IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = this.lenStrategy.determineLength(message);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, 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;
}
Also used : Header(org.apache.http.Header) ContentLengthInputStream(org.apache.http.impl.io.ContentLengthInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) IdentityInputStream(org.apache.http.impl.io.IdentityInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 25 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project CodeUtils by boredream.

the class HttpUtils method entityFromConnection.

/**
	 * Initializes an {@link HttpEntity} from the given
	 * {@link HttpURLConnection}.
	 * 
	 * @param connection
	 * @return an HttpEntity populated with data from <code>connection</code>.
	 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
Also used : BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)27 InputStream (java.io.InputStream)12 IOException (java.io.IOException)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6 IdentityInputStream (org.apache.http.impl.io.IdentityInputStream)6 Test (org.junit.Test)4 FetchProfile (com.fsck.k9.mail.FetchProfile)3 Header (org.apache.http.Header)3 HttpResponse (org.apache.http.HttpResponse)3 StatusLine (org.apache.http.StatusLine)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 HttpContext (org.apache.http.protocol.HttpContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 URISyntaxException (java.net.URISyntaxException)2 HttpEntity (org.apache.http.HttpEntity)2 HttpException (org.apache.http.HttpException)2 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)2