Search in sources :

Example 21 with HttpResponseException

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);
            }
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 22 with HttpResponseException

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());
}
Also used : HttpResponseException(org.apache.http.client.HttpResponseException) Test(org.junit.Test)

Example 23 with HttpResponseException

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;
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 24 with HttpResponseException

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();
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResponseHandler(org.apache.http.client.ResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) Test(org.junit.Test)

Example 25 with HttpResponseException

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);
}
Also used : StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) HttpResponseException(org.apache.http.client.HttpResponseException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)40 StatusLine (org.apache.http.StatusLine)23 HttpEntity (org.apache.http.HttpEntity)17 IOException (java.io.IOException)16 Header (org.apache.http.Header)5 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)5 HttpClient (org.apache.http.client.HttpClient)4 HttpGet (org.apache.http.client.methods.HttpGet)4 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)4 Test (org.junit.Test)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 SharedPreferences (android.content.SharedPreferences)2 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)2 TextMessage (com.nyaruka.androidrelay.data.TextMessage)2 TextMessageHelper (com.nyaruka.androidrelay.data.TextMessageHelper)2 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)2 Job (com.offbytwo.jenkins.model.Job)2 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)2