Search in sources :

Example 21 with HttpHeaders

use of com.google.api.client.http.HttpHeaders 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 22 with HttpHeaders

use of com.google.api.client.http.HttpHeaders 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)

Example 23 with HttpHeaders

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

the class HttpTransportOptions method getHttpRequestInitializer.

/**
   * Returns a request initializer responsible for initializing requests according to service
   * options.
   */
public HttpRequestInitializer getHttpRequestInitializer(final ServiceOptions<?, ?> serviceOptions) {
    Credentials scopedCredentials = serviceOptions.getScopedCredentials();
    final HttpRequestInitializer delegate = scopedCredentials != null && scopedCredentials != NoCredentials.getInstance() ? new HttpCredentialsAdapter(scopedCredentials) : null;
    return new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            if (delegate != null) {
                delegate.initialize(httpRequest);
            }
            if (connectTimeout >= 0) {
                httpRequest.setConnectTimeout(connectTimeout);
            }
            if (readTimeout >= 0) {
                httpRequest.setReadTimeout(readTimeout);
            }
            HttpHeaders headers = httpRequest.getHeaders();
            headers.set("x-goog-api-client", getXGoogApiClientHeader(serviceOptions));
        }
    };
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) NoCredentials(com.google.cloud.NoCredentials) Credentials(com.google.auth.Credentials)

Example 24 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project beam by apache.

the class GcsUtil method enqueueCopy.

private void enqueueCopy(final GcsPath from, final GcsPath to, BatchRequest batch) throws IOException {
    Storage.Objects.Copy copyRequest = storageClient.objects().copy(from.getBucket(), from.getObject(), to.getBucket(), to.getObject(), null);
    copyRequest.queue(batch, new JsonBatchCallback<StorageObject>() {

        @Override
        public void onSuccess(StorageObject obj, HttpHeaders responseHeaders) {
            LOG.debug("Successfully copied {} to {}", from, to);
        }

        @Override
        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
            throw new IOException(String.format("Error trying to copy %s to %s: %s", from, to, e));
        }
    });
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) StorageObject(com.google.api.services.storage.model.StorageObject) Objects(com.google.api.services.storage.model.Objects) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException)

Example 25 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project beam by apache.

the class GcsUtil method enqueueDelete.

private void enqueueDelete(final GcsPath file, BatchRequest batch) throws IOException {
    Storage.Objects.Delete deleteRequest = storageClient.objects().delete(file.getBucket(), file.getObject());
    deleteRequest.queue(batch, new JsonBatchCallback<Void>() {

        @Override
        public void onSuccess(Void obj, HttpHeaders responseHeaders) {
            LOG.debug("Successfully deleted {}", file);
        }

        @Override
        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
            throw new IOException(String.format("Error trying to delete %s: %s", file, e));
        }
    });
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) Objects(com.google.api.services.storage.model.Objects) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException)

Aggregations

HttpHeaders (com.google.api.client.http.HttpHeaders)46 HttpRequest (com.google.api.client.http.HttpRequest)22 IOException (java.io.IOException)21 GenericUrl (com.google.api.client.http.GenericUrl)13 HttpResponse (com.google.api.client.http.HttpResponse)13 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)10 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)7 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)6 Test (org.junit.Test)6 HttpContent (com.google.api.client.http.HttpContent)5 Objects (com.google.api.services.storage.model.Objects)5 HttpResponseException (com.google.api.client.http.HttpResponseException)4 HttpTransport (com.google.api.client.http.HttpTransport)4 JsonObjectParser (com.google.api.client.json.JsonObjectParser)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 SocketTimeoutException (java.net.SocketTimeoutException)4 Map (java.util.Map)4 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3