Search in sources :

Example 6 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project k-9 by k9mail.

the class WebDavFolderTest method folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies.

@Test
public void folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies() throws MessagingException, IOException, URISyntaxException {
    setupStoreForMessageFetching();
    when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))).thenReturn(mockDataSet);
    List<WebDavMessage> messages = setup25MessagesToFetch();
    when(mockHttpClient.executeOverride(any(HttpUriRequest.class), any(HttpContext.class))).thenAnswer(new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
            HttpResponse httpResponse = mock(HttpResponse.class);
            StatusLine statusLine = mock(StatusLine.class);
            when(httpResponse.getStatusLine()).thenReturn(statusLine);
            when(statusLine.getStatusCode()).thenReturn(200);
            BasicHttpEntity httpEntity = new BasicHttpEntity();
            String body = "";
            httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
            when(httpResponse.getEntity()).thenReturn(httpEntity);
            return httpResponse;
        }
    });
    FetchProfile profile = new FetchProfile();
    profile.add(FetchProfile.Item.FLAGS);
    profile.add(FetchProfile.Item.BODY);
    folder.fetch(messages, profile, listener);
    verify(listener, times(25)).messageStarted(any(String.class), anyInt(), anyInt());
    verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), anyInt());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) FetchProfile(com.fsck.k9.mail.FetchProfile) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Matchers.anyString(org.mockito.Matchers.anyString) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 7 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project k-9 by k9mail.

the class WebDavFolderTest method folder_ignores_exception_thrown_when_closing.

@Test
public void folder_ignores_exception_thrown_when_closing() throws MessagingException, IOException {
    setupStoreForMessageFetching();
    List<WebDavMessage> messages = setup25MessagesToFetch();
    when(mockHttpClient.executeOverride(any(HttpUriRequest.class), any(HttpContext.class))).thenAnswer(new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
            HttpResponse httpResponse = mock(HttpResponse.class);
            StatusLine statusLine = mock(StatusLine.class);
            when(httpResponse.getStatusLine()).thenReturn(statusLine);
            when(statusLine.getStatusCode()).thenReturn(200);
            BasicHttpEntity httpEntity = new BasicHttpEntity();
            InputStream mockInputStream = mock(InputStream.class);
            when(mockInputStream.read(any(byte[].class), anyInt(), anyInt())).thenReturn(1).thenReturn(-1);
            doThrow(new IOException("Test")).when(mockInputStream).close();
            httpEntity.setContent(mockInputStream);
            when(httpResponse.getEntity()).thenReturn(httpEntity);
            return httpResponse;
        }
    });
    FetchProfile profile = new FetchProfile();
    profile.add(FetchProfile.Item.BODY_SANE);
    folder.fetch(messages, profile, listener);
    verify(listener, times(25)).messageStarted(any(String.class), anyInt(), eq(25));
    verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) FetchProfile(com.fsck.k9.mail.FetchProfile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) StatusLine(org.apache.http.StatusLine) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 8 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project RoboZombie by sahan.

the class Entities method resolve.

/**
	  * <p>Discovers which implementation of {@link HttpEntity} is suitable for wrapping the given object. 
	  * This discovery proceeds in the following order by checking the runtime-type of the object:</p> 
	  *
	  * <ol>
	  * 	<li>org.apache.http.{@link HttpEntity} --&gt; returned as-is.</li> 
	  * 	<li>{@code byte[]}, {@link Byte}[] --&gt; {@link ByteArrayEntity}</li> 
	  *  	<li>java.io.{@link File} --&gt; {@link FileEntity}</li>
	  * 	<li>java.io.{@link InputStream} --&gt; {@link BufferedHttpEntity}</li>
	  * 	<li>{@link CharSequence} --&gt; {@link StringEntity}</li>
	  * 	<li>java.io.{@link Serializable} --&gt; {@link SerializableEntity} (with an internal buffer)</li>
	  * </ol>
	  *
	  * @param genericEntity
	  * 			a generic reference to an object whose concrete {@link HttpEntity} is to be resolved 
	  * <br><br>
	  * @return the resolved concrete {@link HttpEntity} implementation
	  * <br><br>
	  * @throws NullPointerException
	  * 			if the supplied generic type was {@code null}
	  * <br><br>
	  * @throws EntityResolutionFailedException
	  * 			if the given generic instance failed to be translated to an {@link HttpEntity} 
	  * <br><br>
	  * @since 1.3.0
	  */
public static final HttpEntity resolve(Object genericEntity) {
    assertNotNull(genericEntity);
    try {
        if (genericEntity instanceof HttpEntity) {
            return (HttpEntity) genericEntity;
        } else if (byte[].class.isAssignableFrom(genericEntity.getClass())) {
            return new ByteArrayEntity((byte[]) genericEntity);
        } else if (Byte[].class.isAssignableFrom(genericEntity.getClass())) {
            Byte[] wrapperBytes = (Byte[]) genericEntity;
            byte[] primitiveBytes = new byte[wrapperBytes.length];
            for (int i = 0; i < wrapperBytes.length; i++) {
                primitiveBytes[i] = wrapperBytes[i].byteValue();
            }
            return new ByteArrayEntity(primitiveBytes);
        } else if (genericEntity instanceof File) {
            return new FileEntity((File) genericEntity, null);
        } else if (genericEntity instanceof InputStream) {
            BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
            basicHttpEntity.setContent((InputStream) genericEntity);
            return new BufferedHttpEntity(basicHttpEntity);
        } else if (genericEntity instanceof CharSequence) {
            return new StringEntity(((CharSequence) genericEntity).toString());
        } else if (genericEntity instanceof Serializable) {
            return new SerializableEntity((Serializable) genericEntity, true);
        } else {
            throw new EntityResolutionFailedException(genericEntity);
        }
    } catch (Exception e) {
        throw (e instanceof EntityResolutionFailedException) ? (EntityResolutionFailedException) e : new EntityResolutionFailedException(genericEntity, e);
    }
}
Also used : FileEntity(org.apache.http.entity.FileEntity) Serializable(java.io.Serializable) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) InputStream(java.io.InputStream) SerializableEntity(org.apache.http.entity.SerializableEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) File(java.io.File)

Example 9 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project iosched by google.

the class HurlStack 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 : InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException)

Example 10 with BasicHttpEntity

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

the class AndroidHttpClientConnection method receiveResponseEntity.

/**
     * Return the next response entity.
     * @param headers contains values for parsing entity
     * @see HttpClientConnection#receiveResponseEntity(HttpResponse response)
     */
public HttpEntity receiveResponseEntity(final Headers headers) {
    assertOpen();
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = determineLength(headers);
    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));
    }
    String contentTypeHeader = headers.getContentType();
    if (contentTypeHeader != null) {
        entity.setContentType(contentTypeHeader);
    }
    String contentEncodingHeader = headers.getContentEncoding();
    if (contentEncodingHeader != null) {
        entity.setContentEncoding(contentEncodingHeader);
    }
    return entity;
}
Also used : 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)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)26 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 HttpEntity (org.apache.http.HttpEntity)2 HttpException (org.apache.http.HttpException)2 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)2 AccountLimitException (com.cloud.exception.AccountLimitException)1