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);
}
}
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());
}
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);
}
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));
}
});
}
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));
}
});
}
Aggregations