use of org.apache.http.entity.BasicHttpEntity in project cloudstack by apache.
the class ApiServer method writeResponse.
// FIXME: rather than isError, we might was to pass in the status code to give more flexibility
private void writeResponse(final HttpResponse resp, final String responseText, final int statusCode, final String responseType, final String reasonPhrase) {
try {
resp.setStatusCode(statusCode);
resp.setReasonPhrase(reasonPhrase);
final BasicHttpEntity body = new BasicHttpEntity();
if (HttpUtils.RESPONSE_TYPE_JSON.equalsIgnoreCase(responseType)) {
// JSON response
body.setContentType(JSONcontentType.value());
if (responseText == null) {
body.setContent(new ByteArrayInputStream("{ \"error\" : { \"description\" : \"Internal Server Error\" } }".getBytes(HttpUtils.UTF_8)));
}
} else {
body.setContentType("text/xml");
if (responseText == null) {
body.setContent(new ByteArrayInputStream("<error>Internal Server Error</error>".getBytes(HttpUtils.UTF_8)));
}
}
if (responseText != null) {
body.setContent(new ByteArrayInputStream(responseText.getBytes(HttpUtils.UTF_8)));
}
resp.setEntity(body);
} catch (final Exception ex) {
s_logger.error("error!", ex);
}
}
use of org.apache.http.entity.BasicHttpEntity in project TaEmCasa by Dionen.
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 CodeUtils by boredream.
the class LeanCloudHttpUtils 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 FastDev4Android by jiangqqlmj.
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 k-9 by k9mail.
the class WebDavFolderTest method folder_can_fetch_sensible_body_data_and_notifies_listener.
@Test
public void folder_can_fetch_sensible_body_data_and_notifies_listener() throws MessagingException, IOException, URISyntaxException {
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();
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.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));
}
Aggregations