Search in sources :

Example 21 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project elasticsearch by elastic.

the class RetryHttpInitializerWrapperTests method testRetryWaitTooLong.

public void testRetryWaitTooLong() throws Exception {
    TimeValue maxWaitTime = TimeValue.timeValueMillis(10);
    int maxRetryTimes = 50;
    FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes);
    JsonFactory jsonFactory = new JacksonFactory();
    MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
    MockSleeper oneTimeSleeper = new MockSleeper() {

        @Override
        public void sleep(long millis) throws InterruptedException {
            Thread.sleep(maxWaitTime.getMillis());
            // important number, use this to get count
            super.sleep(0);
        }
    };
    RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime);
    Compute client = new Compute.Builder(fakeTransport, jsonFactory, null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
    HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
    try {
        request1.execute();
        fail("Request should fail if wait too long");
    } catch (HttpResponseException e) {
        assertThat(e.getStatusCode(), equalTo(HttpStatusCodes.STATUS_CODE_SERVER_ERROR));
        // should only retry once.
        assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes));
    }
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) JsonFactory(com.google.api.client.json.JsonFactory) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Compute(com.google.api.services.compute.Compute) TimeValue(org.elasticsearch.common.unit.TimeValue) MockSleeper(com.google.api.client.testing.util.MockSleeper)

Example 22 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project OpenRefine by OpenRefine.

the class FusionTableSerializer method sendBatch.

private boolean sendBatch(int batchSize) {
    try {
        // TODO: we really want to do GZIP compression here 
        // FIXME: text/csv doesn't work even though that's what the content is
        AbstractInputStreamContent content = ByteArrayContent.fromString("application/octet-stream", sbBatch.toString());
        Long count = FusionTableHandler.insertRows(service, tableId, content);
        if (count != null && count.intValue() != batchSize) {
            exceptions.add(new IOException("only imported " + count + " of " + batchSize + " rows"));
        }
    } catch (IOException e) {
        exceptions.add(e);
        if (e instanceof HttpResponseException) {
            int code = ((HttpResponseException) e).getStatusCode();
            if (code >= 400 && code < 500) {
                return false;
            }
        // 500s appear to be retried automatically by li
        }
    } finally {
        sbBatch = null;
    }
    return true;
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException) AbstractInputStreamContent(com.google.api.client.http.AbstractInputStreamContent)

Example 23 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method write.

@Override
public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
    try {
        if (length == 0 && !last) {
            return;
        }
        GenericUrl url = new GenericUrl(uploadId);
        HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
        long limit = destOffset + length;
        StringBuilder range = new StringBuilder("bytes ");
        if (length == 0) {
            range.append('*');
        } else {
            range.append(destOffset).append('-').append(limit - 1);
        }
        range.append('/');
        if (last) {
            range.append(limit);
        } else {
            range.append('*');
        }
        httpRequest.getHeaders().setContentRange(range.toString());
        int code;
        String message;
        IOException exception = null;
        try {
            HttpResponse response = httpRequest.execute();
            code = response.getStatusCode();
            message = response.getStatusMessage();
        } catch (HttpResponseException ex) {
            exception = ex;
            code = ex.getStatusCode();
            message = ex.getStatusMessage();
        }
        if (!last && code != 308 || last && !(code == 200 || code == 201)) {
            if (exception != null) {
                throw exception;
            }
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(code);
            error.setMessage(message);
            throw translate(error);
        }
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 24 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.

the class StorageExceptionTest method testStorageException.

@Test
public void testStorageException() {
    StorageException exception = new StorageException(500, "message");
    assertEquals(500, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertTrue(exception.isRetryable());
    exception = new StorageException(502, "message");
    assertEquals(502, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertTrue(exception.isRetryable());
    exception = new StorageException(503, "message");
    assertEquals(503, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertTrue(exception.isRetryable());
    exception = new StorageException(504, "message");
    assertEquals(504, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertTrue(exception.isRetryable());
    exception = new StorageException(429, "message");
    assertEquals(429, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertTrue(exception.isRetryable());
    exception = new StorageException(408, "message");
    assertEquals(408, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertTrue(exception.isRetryable());
    exception = new StorageException(400, "message");
    assertEquals(400, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertFalse(exception.isRetryable());
    IOException cause = new SocketTimeoutException();
    exception = new StorageException(cause);
    assertNull(exception.getReason());
    assertNull(exception.getMessage());
    assertTrue(exception.isRetryable());
    assertSame(cause, exception.getCause());
    GoogleJsonError error = new GoogleJsonError();
    error.setCode(503);
    error.setMessage("message");
    exception = new StorageException(error);
    assertEquals(503, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertTrue(exception.isRetryable());
    exception = new StorageException(400, "message", cause);
    assertEquals(400, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertFalse(exception.isRetryable());
    assertSame(cause, exception.getCause());
    HttpResponseException httpResponseException = new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build();
    exception = new StorageException(httpResponseException);
    assertEquals(404, exception.getCode());
    assertFalse(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build();
    exception = new StorageException(httpResponseException);
    assertEquals(503, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build();
    exception = new StorageException(httpResponseException);
    assertEquals(502, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build();
    exception = new StorageException(httpResponseException);
    assertEquals(500, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(429, null, new HttpHeaders()).build();
    exception = new StorageException(httpResponseException);
    assertEquals(429, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(408, null, new HttpHeaders()).build();
    exception = new StorageException(httpResponseException);
    assertEquals(408, exception.getCode());
    assertTrue(exception.isRetryable());
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) SocketTimeoutException(java.net.SocketTimeoutException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 25 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryExceptionTest method testBigqueryException.

@Test
public void testBigqueryException() {
    BigQueryException exception = new BigQueryException(500, "message");
    assertEquals(500, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertNull(exception.getError());
    assertTrue(exception.isRetryable());
    exception = new BigQueryException(502, "message");
    assertEquals(502, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertNull(exception.getError());
    assertTrue(exception.isRetryable());
    exception = new BigQueryException(503, "message");
    assertEquals(503, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertNull(exception.getError());
    assertTrue(exception.isRetryable());
    exception = new BigQueryException(504, "message");
    assertEquals(504, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertNull(exception.getError());
    assertTrue(exception.isRetryable());
    exception = new BigQueryException(400, "message");
    assertEquals(400, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertNull(exception.getError());
    assertFalse(exception.isRetryable());
    BigQueryError error = new BigQueryError("reason", null, null);
    exception = new BigQueryException(504, "message", error);
    assertEquals(504, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertEquals("reason", exception.getReason());
    assertEquals(error, exception.getError());
    assertTrue(exception.isRetryable());
    IOException cause = new SocketTimeoutException("socketTimeoutMessage");
    exception = new BigQueryException(cause);
    assertEquals(BigQueryException.UNKNOWN_CODE, exception.getCode());
    assertNull(exception.getReason());
    assertEquals("socketTimeoutMessage", exception.getMessage());
    assertEquals(cause, exception.getCause());
    assertTrue(exception.isRetryable());
    assertSame(cause, exception.getCause());
    exception = new BigQueryException(504, "message", cause);
    assertEquals(504, exception.getCode());
    assertEquals("message", exception.getMessage());
    assertNull(exception.getReason());
    assertNull(exception.getError());
    assertTrue(exception.isRetryable());
    assertSame(cause, exception.getCause());
    HttpResponseException httpResponseException = new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build();
    exception = new BigQueryException(httpResponseException);
    assertEquals(404, exception.getCode());
    assertFalse(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(504, null, new HttpHeaders()).build();
    exception = new BigQueryException(httpResponseException);
    assertEquals(504, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build();
    exception = new BigQueryException(httpResponseException);
    assertEquals(503, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build();
    exception = new BigQueryException(httpResponseException);
    assertEquals(502, exception.getCode());
    assertTrue(exception.isRetryable());
    httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build();
    exception = new BigQueryException(httpResponseException);
    assertEquals(500, exception.getCode());
    assertTrue(exception.isRetryable());
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) SocketTimeoutException(java.net.SocketTimeoutException) HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpResponseException (com.google.api.client.http.HttpResponseException)38 GenericUrl (com.google.api.client.http.GenericUrl)15 IOException (java.io.IOException)15 HttpResponse (com.google.api.client.http.HttpResponse)13 Test (org.junit.Test)13 HttpRequest (com.google.api.client.http.HttpRequest)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)6 HttpHeaders (com.google.api.client.http.HttpHeaders)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)4 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)3 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PServiceCall (net.morimekta.providence.PServiceCall)3 Request (net.morimekta.test.providence.client.Request)3 TestService (net.morimekta.test.providence.client.TestService)3 AccountManager (android.accounts.AccountManager)2 ByteArrayContent (com.google.api.client.http.ByteArrayContent)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2