Search in sources :

Example 16 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError 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 17 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError 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 18 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testUpdateAllIterable.

@Test
public void testUpdateAllIterable() {
    RpcBatch batchMock = EasyMock.createMock(RpcBatch.class);
    Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance();
    Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance();
    batchMock.addPatch(EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    batchMock.addPatch(EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock);
    batchMock.submit();
    EasyMock.replay(storageRpcMock, batchMock);
    initializeService();
    List<Blob> resultBlobs = storage.update(ImmutableList.of(BLOB_INFO1, BLOB_INFO2));
    callback1.getValue().onSuccess(BLOB_INFO1.toPb());
    callback2.getValue().onFailure(new GoogleJsonError());
    assertEquals(2, resultBlobs.size());
    assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0));
    assertNull(resultBlobs.get(1));
    EasyMock.verify(batchMock);
}
Also used : BlobWriteOption(com.google.cloud.storage.Storage.BlobWriteOption) BucketSourceOption(com.google.cloud.storage.Storage.BucketSourceOption) BlobSourceOption(com.google.cloud.storage.Storage.BlobSourceOption) BlobTargetOption(com.google.cloud.storage.Storage.BlobTargetOption) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) RpcBatch(com.google.cloud.storage.spi.v1.RpcBatch) Test(org.junit.Test)

Example 19 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError 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 20 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError 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

GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)20 Test (org.junit.Test)13 StorageObject (com.google.api.services.storage.model.StorageObject)8 IOException (java.io.IOException)8 BlobSourceOption (com.google.cloud.storage.Storage.BlobSourceOption)6 BlobTargetOption (com.google.cloud.storage.Storage.BlobTargetOption)6 BlobWriteOption (com.google.cloud.storage.Storage.BlobWriteOption)6 BucketSourceOption (com.google.cloud.storage.Storage.BucketSourceOption)6 RpcBatch (com.google.cloud.storage.spi.v1.RpcBatch)6 HttpHeaders (com.google.api.client.http.HttpHeaders)5 DnsRpc (com.google.cloud.dns.spi.v1.DnsRpc)4 RpcBatch (com.google.cloud.dns.spi.v1.RpcBatch)4 Map (java.util.Map)4 HttpResponseException (com.google.api.client.http.HttpResponseException)3 Objects (com.google.api.services.storage.model.Objects)3 SocketTimeoutException (java.net.SocketTimeoutException)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpResponse (com.google.api.client.http.HttpResponse)2 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)2