Search in sources :

Example 71 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project ThinkAndroid by white-cat.

the class AsyncHttpResponseHandler method sendResponseMessage.

protected void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
            responseBody = EntityUtils.toString(entity, "UTF-8");
        }
    } catch (IOException e) {
        sendFailureMessage(e, (String) null);
    }
    if (status.getStatusCode() >= 300) {
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
    } else {
        sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 72 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 73 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 74 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 75 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project android-sms-relay by nyaruka.

the class RelayService method checkOutbox.

/**
 * Sends a message to our server.
 * @param msg
 */
public void checkOutbox() throws IOException {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String updateInterval = prefs.getString("update_interval", "30000");
    long interval = Long.parseLong(updateInterval);
    String outboxURL = prefs.getString("outbox_url", null);
    TextMessageHelper helper = getHelper();
    // no delivery url means we don't do anything
    if (outboxURL == null || outboxURL.length() == 0) {
        return;
    }
    // if our update interval is set to 0, then that means we shouldn't be checking, so skip
    if (interval == 0) {
        return;
    }
    Log.d(TAG, "Outbox URL: " + outboxURL);
    try {
        String content = fetchURL(outboxURL);
        if (content.trim().length() > 0) {
            JSONObject json = new JSONObject(content);
            JSONArray responses = json.getJSONArray("outbox");
            for (int i = 0; i < responses.length(); i++) {
                JSONObject response = responses.getJSONObject(i);
                if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
                    String number = "+" + response.getString("contact");
                    String message = response.getString("text");
                    long serverId = response.getLong("id");
                    // if this message doesn't already exist
                    TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
                    if (existing == null) {
                        Log.d(TAG, "New outgoing msg: " + serverId + ": " + message);
                        TextMessage toSend = new TextMessage(number, message, serverId);
                        helper.createMessage(toSend);
                        sendMessage(toSend);
                    } else {
                        if (existing.status == TextMessage.DONE) {
                            existing.status = TextMessage.SENT;
                            helper.updateMessage(existing);
                        }
                        Log.d(TAG, "Ignoring message: " + serverId + " already queued.");
                    }
                }
            }
        }
        Log.d(TAG, "Outbox fetched from server");
    } catch (HttpResponseException e) {
        Log.d(TAG, "Got Error: " + e.getMessage(), e);
    } catch (IOException e) {
        throw e;
    } catch (Throwable t) {
        Log.d(TAG, "Got Error: " + t.getMessage(), t);
    }
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)82 IOException (java.io.IOException)32 StatusLine (org.apache.http.StatusLine)30 HttpEntity (org.apache.http.HttpEntity)25 HttpGet (org.apache.http.client.methods.HttpGet)14 HttpResponse (org.apache.http.HttpResponse)13 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)11 Header (org.apache.http.Header)9 ClientProtocolException (org.apache.http.client.ClientProtocolException)9 URISyntaxException (java.net.URISyntaxException)8 ArrayList (java.util.ArrayList)8 URI (java.net.URI)7 Document (org.jsoup.nodes.Document)7 Test (org.junit.Test)7 InputStream (java.io.InputStream)6 Expectations (mockit.Expectations)5 HttpClient (org.apache.http.client.HttpClient)5 Element (org.jsoup.nodes.Element)5 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)4 HttpPost (org.apache.http.client.methods.HttpPost)4