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());
}
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));
}
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} --> returned as-is.</li>
* <li>{@code byte[]}, {@link Byte}[] --> {@link ByteArrayEntity}</li>
* <li>java.io.{@link File} --> {@link FileEntity}</li>
* <li>java.io.{@link InputStream} --> {@link BufferedHttpEntity}</li>
* <li>{@link CharSequence} --> {@link StringEntity}</li>
* <li>java.io.{@link Serializable} --> {@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);
}
}
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;
}
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;
}
Aggregations