Search in sources :

Example 6 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project SmartAndroidSource by jaychou2012.

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 7 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project ignition by mttkay.

the class IgnitedHttpRequestBase method handleResponse.

@Override
public IgnitedHttpResponse handleResponse(HttpResponse response) throws IOException {
    int status = response.getStatusLine().getStatusCode();
    if (expectedStatusCodes != null && !expectedStatusCodes.isEmpty() && !expectedStatusCodes.contains(status)) {
        throw new HttpResponseException(status, "Unexpected status code: " + status);
    }
    IgnitedHttpResponse bhttpr = new IgnitedHttpResponseImpl(response);
    HttpResponseCache responseCache = ignitedHttp.getResponseCache();
    if (responseCache != null && bhttpr.getResponseBody() != null) {
        ResponseData responseData = new ResponseData(status, bhttpr.getResponseBodyAsBytes());
        responseCache.put(getRequestUrl(), responseData);
    }
    return bhttpr;
}
Also used : ResponseData(com.github.ignition.support.http.cache.CachedHttpResponse.ResponseData) HttpResponseCache(com.github.ignition.support.http.cache.HttpResponseCache) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 8 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.

the class AbstractWSGetClientTest method validatePackagingOfResponseCode404.

@Test
public void validatePackagingOfResponseCode404() throws ApplicationException, IOException {
    final int errorCode = 404;
    final String errorMessage = "Not found";
    HttpResponseException hpe = new HttpResponseException(errorCode, errorMessage);
    TestWSGetClient testWSClient = getTestWSClient(hpe);
    WSResponse response = testWSClient.testCall();
    Assert.assertTrue(response.wasCallAllowed());
    Assert.assertFalse(response.wasCallSuccessful());
    Assert.assertFalse(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 9 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.

the class MusicBrainzServiceTest method handlesArtistFailureDuringUpdate.

@Test
public void handlesArtistFailureDuringUpdate() throws ApplicationException {
    final String revName = reverse(artistName);
    submitFile(additionDao, getFile(revName, albumName, trackName));
    Mockito.when(service.artistQueryClient.get(revName)).thenThrow(new ApplicationException("Fail", new HttpResponseException(503, "Overloaded")));
    service.updateDiscography();
    List<MBAlbum> albums = service.getMissingAlbums(artistName, asList(TYPE_EP), null, -1, 0);
    Assert.assertEquals(2, albums.size());
    assertEquals("Switchblade / Cult of Luna", albums.get(0).getTitle());
    assertEquals("Bodies / Recluse", albums.get(1).getTitle());
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpResponseException(org.apache.http.client.HttpResponseException) MBAlbum(com.github.hakko.musiccabinet.domain.model.music.MBAlbum) Test(org.junit.Test)

Example 10 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.

the class AbstractMusicBrainzClient method executeWSRequest.

protected String executeWSRequest(WebserviceInvocation invocation, String path, List<NameValuePair> params) throws ApplicationException {
    String response = null;
    HttpGet httpGet = new HttpGet(getURI(path, params));
    httpGet.setHeader(USER_AGENT, CLIENT_INFO);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        long elapsedMs = -currentTimeMillis();
        response = httpClient.execute(httpGet, responseHandler);
        elapsedMs += currentTimeMillis();
        sleep(Math.max(INTERVAL_MS - elapsedMs, 0));
    } catch (HttpResponseException e) {
        LOG.warn(format("MusicBrainz internal error: %d, %s", e.getStatusCode(), e.getMessage()));
        throw new ApplicationException("MusicBrainz internal error!", e);
    } catch (IOException e) {
        throw new ApplicationException("MusicBrainz communication failed!", e);
    } catch (InterruptedException e) {
        LOG.warn("MusicBrainz sleep interrupted!", e);
    }
    webserviceHistoryService.logWebserviceInvocation(invocation);
    return response;
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

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