use of org.apache.http.client.HttpResponseException in project Libraries-for-Android-Developers by eoecn.
the class AsyncHttpResponseHandler method sendResponseMessage.
@Override
public void sendResponseMessage(HttpResponse response) throws IOException {
// do not process if request has been cancelled
if (!Thread.currentThread().isInterrupted()) {
StatusLine status = response.getStatusLine();
byte[] responseBody;
responseBody = getResponseData(response.getEntity());
// additional cancellation check as getResponseData() can take non-zero time to process
if (!Thread.currentThread().isInterrupted()) {
if (status.getStatusCode() >= 300) {
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), responseBody, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
} else {
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
}
}
}
}
use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.
the class AbstractWSGetClientTest method validatePackagingOfResponseCode503.
@Test
public void validatePackagingOfResponseCode503() throws ApplicationException, IOException {
final int errorCode = 503;
final String errorMessage = "Service temporary unavailable";
HttpResponseException hpe = new HttpResponseException(errorCode, errorMessage);
TestWSGetClient testWSClient = getTestWSClient(hpe);
WSResponse response = testWSClient.testCall();
Assert.assertTrue(response.wasCallAllowed());
Assert.assertFalse(response.wasCallSuccessful());
Assert.assertTrue(response.isErrorRecoverable());
Assert.assertEquals(errorCode, response.getErrorCode());
Assert.assertEquals(errorMessage, response.getErrorMessage());
}
use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.
the class AbstractWSGetClient method invokeSingleCall.
/*
* Make a single call to a Last.fm web service, and return a packaged result.
*/
private WSResponse invokeSingleCall(List<NameValuePair> params) throws ApplicationException {
if (throttleService != null) {
throttleService.awaitAllowance();
}
WSResponse wsResponse;
HttpClient httpClient = getHttpClient();
try {
HttpGet httpGet = new HttpGet(getURI(params));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpGet, responseHandler);
wsResponse = new WSResponse(responseBody);
} catch (HttpResponseException e) {
wsResponse = new WSResponse(isHttpRecoverable(e.getStatusCode()), e.getStatusCode(), e.getMessage());
} catch (IOException e) {
LOG.warn("Could not fetch data from Last.fm!", e);
wsResponse = new WSResponse(true, -1, "Call failed due to " + e.getMessage());
}
return wsResponse;
}
use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.
the class AbstractMusicBrainzClientTest method failsWithApplicationExceptionOnInternalErrors.
@Test(expected = ApplicationException.class)
@SuppressWarnings("unchecked")
public void failsWithApplicationExceptionOnInternalErrors() throws Exception {
TestMusicBrainzClient client = getClient();
when(client.getHttpClient().execute(any(HttpUriRequest.class), any(ResponseHandler.class))).thenThrow(new HttpResponseException(503, "NA"));
client.get();
}
use of org.apache.http.client.HttpResponseException in project SmartAndroidSource by jaychou2012.
the class BinaryHttpResponseHandler method sendResponseMessage.
@Override
public final void sendResponseMessage(HttpResponse response) throws IOException {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders(AsyncHttpClient.HEADER_CONTENT_TYPE);
if (contentTypeHeaders.length != 1) {
//malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"));
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : getAllowedContentTypes()) {
try {
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
} catch (PatternSyntaxException e) {
Log.e("BinaryHttpResponseHandler", "Given pattern is not valid: " + anAllowedContentType, e);
}
}
if (!foundAllowedContentType) {
//Content-Type not in allowed list, ABORT!
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"));
return;
}
super.sendResponseMessage(response);
}
Aggregations