Search in sources :

Example 1 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project beam by apache.

the class BigQueryServicesImplTest method errorWithReasonAndStatus.

/** A helper that generates the error JSON payload that Google APIs produce. */
private static GoogleJsonErrorContainer errorWithReasonAndStatus(String reason, int status) {
    ErrorInfo info = new ErrorInfo();
    info.setReason(reason);
    info.setDomain("global");
    // GoogleJsonError contains one or more ErrorInfo objects; our utiities read the first one.
    GoogleJsonError error = new GoogleJsonError();
    error.setErrors(ImmutableList.of(info));
    error.setCode(status);
    error.setMessage(reason);
    // The actual JSON response is an error container.
    GoogleJsonErrorContainer container = new GoogleJsonErrorContainer();
    container.setError(error);
    return container;
}
Also used : ErrorInfo(com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo) GoogleJsonErrorContainer(com.google.api.client.googleapis.json.GoogleJsonErrorContainer) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 2 with GoogleJsonError

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

the class HttpStorageRpc method open.

@Override
public String open(StorageObject object, Map<Option, ?> options) {
    try {
        Insert req = storage.objects().insert(object.getBucket(), object);
        GenericUrl url = req.buildHttpRequest().getUrl();
        String scheme = url.getScheme();
        String host = url.getHost();
        String path = "/upload" + url.getRawPath();
        url = new GenericUrl(scheme + "://" + host + path);
        url.set("uploadType", "resumable");
        url.set("name", object.getName());
        for (Option option : options.keySet()) {
            Object content = option.get(options);
            if (content != null) {
                url.set(option.value(), content.toString());
            }
        }
        JsonFactory jsonFactory = storage.getJsonFactory();
        HttpRequestFactory requestFactory = storage.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
        HttpHeaders requestHeaders = httpRequest.getHeaders();
        requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
        String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
        if (key != null) {
            BaseEncoding base64 = BaseEncoding.base64();
            HashFunction hashFunction = Hashing.sha256();
            requestHeaders.set("x-goog-encryption-algorithm", "AES256");
            requestHeaders.set("x-goog-encryption-key", key);
            requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
        }
        HttpResponse response = httpRequest.execute();
        if (response.getStatusCode() != 200) {
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(response.getStatusCode());
            error.setMessage(response.getStatusMessage());
            throw translate(error);
        }
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Insert(com.google.api.services.storage.Storage.Objects.Insert) BaseEncoding(com.google.common.io.BaseEncoding) HashFunction(com.google.common.hash.HashFunction) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 3 with GoogleJsonError

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

the class DnsBatchTest method testGetZoneNotFound.

@Test
public void testGetZoneNotFound() {
    EasyMock.reset(batchMock);
    Capture<RpcBatch.Callback<ManagedZone>> callback = Capture.newInstance();
    Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance();
    batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions));
    EasyMock.replay(batchMock);
    DnsBatchResult<Zone> batchResult = dnsBatch.getZone(ZONE_NAME);
    assertEquals(0, capturedOptions.getValue().size());
    GoogleJsonError error = new GoogleJsonError();
    error.setCode(404);
    RpcBatch.Callback<ManagedZone> capturedCallback = callback.getValue();
    capturedCallback.onFailure(error);
    assertNull(batchResult.get());
}
Also used : DnsRpc(com.google.cloud.dns.spi.v1.DnsRpc) ManagedZone(com.google.api.services.dns.model.ManagedZone) ManagedZone(com.google.api.services.dns.model.ManagedZone) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) RpcBatch(com.google.cloud.dns.spi.v1.RpcBatch) Map(java.util.Map) Test(org.junit.Test)

Example 4 with GoogleJsonError

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

the class DnsBatchTest method testGetChangeRequestNotFound.

@Test
public void testGetChangeRequestNotFound() {
    EasyMock.reset(batchMock);
    Capture<RpcBatch.Callback<Change>> callback = Capture.newInstance();
    Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance();
    batchMock.addGetChangeRequest(EasyMock.eq(ZONE_NAME), EasyMock.eq(CHANGE_REQUEST_COMPLETE.getGeneratedId()), EasyMock.capture(callback), EasyMock.capture(capturedOptions));
    EasyMock.replay(batchMock);
    DnsBatchResult<ChangeRequest> batchResult = dnsBatch.getChangeRequest(ZONE_NAME, CHANGE_REQUEST_COMPLETE.getGeneratedId());
    assertEquals(0, capturedOptions.getValue().size());
    RpcBatch.Callback<Change> capturedCallback = callback.getValue();
    GoogleJsonError error = new GoogleJsonError();
    GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo();
    errorInfo.setReason("reason");
    errorInfo.setLocation("entity.parameters.changeId");
    error.setCode(404);
    error.setErrors(ImmutableList.of(errorInfo));
    capturedCallback.onFailure(error);
    assertNull(batchResult.get());
}
Also used : Change(com.google.api.services.dns.model.Change) RpcBatch(com.google.cloud.dns.spi.v1.RpcBatch) DnsRpc(com.google.cloud.dns.spi.v1.DnsRpc) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) Map(java.util.Map) Test(org.junit.Test)

Example 5 with GoogleJsonError

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

the class StorageImplTest method testDeleteAllArray.

@Test
public void testDeleteAllArray() {
    BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1);
    BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2);
    RpcBatch batchMock = EasyMock.createMock(RpcBatch.class);
    Capture<RpcBatch.Callback<Void>> callback1 = Capture.newInstance();
    Capture<RpcBatch.Callback<Void>> callback2 = Capture.newInstance();
    batchMock.addDelete(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    batchMock.addDelete(EasyMock.eq(blobId2.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<Boolean> result = storage.delete(blobId1, blobId2);
    callback1.getValue().onSuccess(null);
    callback2.getValue().onFailure(new GoogleJsonError());
    assertEquals(2, result.size());
    assertTrue(result.get(0));
    assertFalse(result.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)

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