Java Code Examples for com.google.api.client.googleapis.json.GoogleJsonResponseException

The following examples show how to use com.google.api.client.googleapis.json.GoogleJsonResponseException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: camel-quarkus   Author: apache   File: GoogleDriveResource.java    License: Apache License 2.0 6 votes vote down vote up
@Path("/read")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response readFile(@QueryParam("fileId") String fileId) {
    try {
        File response = producerTemplate.requestBody("google-drive://drive-files/get?inBody=fileId", fileId, File.class);
        if (response != null) {
            return Response.ok(response.getTitle()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (CamelExecutionException e) {
        Exception exchangeException = e.getExchange().getException();
        if (exchangeException != null && exchangeException.getCause() instanceof GoogleJsonResponseException) {
            GoogleJsonResponseException originalException = (GoogleJsonResponseException) exchangeException.getCause();
            return Response.status(originalException.getStatusCode()).build();
        }
        throw e;
    }
}
 
Example #2
Source Project: camel-quarkus   Author: apache   File: GoogleCalendarResource.java    License: Apache License 2.0 6 votes vote down vote up
@Path("/read")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response readCalendar(@QueryParam("calendarId") String calendarId) {
    try {
        Calendar response = producerTemplate.requestBody("google-calendar://calendars/get?inBody=calendarId", calendarId,
                Calendar.class);
        if (response != null) {
            return Response.ok(response.getSummary()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (CamelExecutionException e) {
        Exception exchangeException = e.getExchange().getException();
        if (exchangeException != null && exchangeException.getCause() instanceof GoogleJsonResponseException) {
            GoogleJsonResponseException originalException = (GoogleJsonResponseException) exchangeException.getCause();
            return Response.status(originalException.getStatusCode()).build();
        }
        throw e;
    }
}
 
Example #3
Source Project: camel-quarkus   Author: apache   File: GoogleCalendarResource.java    License: Apache License 2.0 6 votes vote down vote up
@Path("/read/event")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response readCalendarEvent(@QueryParam("calendarId") String calendarId, @QueryParam("eventId") String eventId) {
    Map<String, Object> headers = new HashMap<>();
    headers.put("CamelGoogleCalendar.calendarId", calendarId);
    headers.put("CamelGoogleCalendar.eventId", eventId);
    try {
        Event response = producerTemplate.requestBodyAndHeaders("google-calendar://events/get", null, headers, Event.class);
        if (response != null) {
            return Response.ok(response.getSummary()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (CamelExecutionException e) {
        Exception exchangeException = e.getExchange().getException();
        if (exchangeException != null && exchangeException.getCause() instanceof GoogleJsonResponseException) {
            GoogleJsonResponseException originalException = (GoogleJsonResponseException) exchangeException.getCause();
            return Response.status(originalException.getStatusCode()).build();
        }
        throw e;
    }
}
 
Example #4
Source Project: connector-sdk   Author: google-cloudsearch   File: RepositoryDoc.java    License: Apache License 2.0 6 votes vote down vote up
private static Optional<RepositoryError> getRepositoryErrorForResponseException(
    IOException exception) {
  if (!(exception instanceof GoogleJsonResponseException)) {
    return Optional.empty();
  }
  GoogleJsonResponseException responseException = (GoogleJsonResponseException) exception;

  if (responseException.getStatusCode() == HTTP_NOT_FOUND
      || responseException.getStatusCode() == HTTP_BAD_REQUEST) {
    return Optional.empty();
  }
  return Optional.of(
      new RepositoryError()
          .setErrorMessage(responseException.getMessage())
          .setType("SERVER_ERROR")
          .setHttpStatusCode(responseException.getStatusCode()));
}
 
Example #5
Source Project: flo   Author: spotify   File: BigQueryTasksTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void lookupLatestDailyShouldBeRunnable() throws Exception {
  final Future<TableId> future = FloRunner.runTask(BigQueryTasks.lookupLatestDaily(
      "foo", "bar", "baz", Date.parse("2018-01-01"), 7)).future();

  try {
    future.get();
    fail("Did not expect to find a non-existent table");
  } catch (ExecutionException e) {
    // Verify that we are getting some well known error here so we know with some
    // certainty that we didn't get a serialization error. Yes, this is quite awful.
    final Throwable rootCause = Throwables.getRootCause(e);
    if (rootCause instanceof NotReady) {
      // Seems we had working credentials and the lookup worked. We're done here.
    } else if (rootCause instanceof GoogleJsonResponseException) {
      // Seems we managed to make a request, so the lookup executed. We're done here.
    } else if (rootCause instanceof IllegalArgumentException &&
        rootCause.getMessage().startsWith("A project ID is required")) {
      // Seems we managed to get as far as trying to instantiate the BigQuery client (in the task process).
      // We're done here.
    } else {
      // Not sure what went wrong here, might be serialization error, so be conservative and fail here.
      throw e;
    }
  }
}
 
Example #6
Source Project: pubsub   Author: GoogleCloudPlatform   File: GCEComputeResourceController.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void stopAction() throws Exception {
  log.info("Cleaning up compute resource_controllers.");
  compute.instanceGroupManagers().delete(project, params.getZone(), instanceName()).execute();
  boolean managerExists = true;
  while (managerExists) {
    try {
      compute.instanceGroupManagers().get(project, params.getZone(), instanceName()).execute();
    } catch (GoogleJsonResponseException e) {
      if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        managerExists = false;
      } else {
        throw e;
      }
    }
    Thread.sleep(1000);
  }
  compute.instanceTemplates().delete(project, instanceName());
  log.info("Cleaned up compute resource_controllers.");
}
 
Example #7
Source Project: connector-sdk   Author: google-cloudsearch   File: RepositoryDocTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void execute_indexItemNotFound_notPushedToQueue_throwsIOException() throws Exception {
  Item item = new Item().setName("id1").setAcl(getCustomerAcl());
  RepositoryDoc doc = new RepositoryDoc.Builder().setItem(item).build();
  doAnswer(
          invocation -> {
            SettableFuture<Operation> updateFuture = SettableFuture.create();
            updateFuture.setException(
                new GoogleJsonResponseException(
                    new HttpResponseException.Builder(
                        HTTP_NOT_FOUND, "not found", new HttpHeaders()),
                    new GoogleJsonError()));
            return updateFuture;
          })
      .when(mockIndexingService)
      .indexItem(item, RequestMode.UNSPECIFIED);
  try {
    doc.execute(mockIndexingService);
  } finally {
    InOrder inOrder = inOrder(mockIndexingService);
    inOrder.verify(mockIndexingService).indexItem(item, RequestMode.UNSPECIFIED);
    inOrder.verifyNoMoreInteractions();
    assertEquals("id1", doc.getItem().getName());
  }
}
 
Example #8
Source Project: googleads-shopping-samples   Author: googleads   File: ProductstatusGetSample.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void execute() throws IOException {
  try {
    ProductStatus productStatus =
        content
            .productstatuses()
            .get(this.config.getMerchantId(), ExampleProductFactory.sampleProductId())
            .execute();
    ProductstatusUtils.printProductStatus(productStatus);
  } catch (GoogleJsonResponseException e) {
    if (e.getDetails().getCode() == 404) {
      System.out.println(
          "The item was not found. Try running "
              + "shopping.content.v2.samples.products.ProductInsertSample first.");
    } else {
      checkGoogleJsonResponseException(e);
    }
  }
}
 
Example #9
Source Project: connector-sdk   Author: google-cloudsearch   File: AsyncRequest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Wrapper on {@link JsonBatchCallback#onFailure} to record failure while executing batched
 * request.
 */
@Override
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) {
  if (event != null) {
    event.failure();
    event = null;
  } else {
    operationStats.event(request.requestToExecute.getClass().getName()).failure();
  }
  logger.log(Level.WARNING, "Request failed with error {0}", error);
  if (request.retryPolicy.isRetryableStatusCode(error.getCode())) {
    if (request.getRetries() < request.retryPolicy.getMaxRetryLimit()) {
      request.setStatus(Status.RETRYING);
      request.incrementRetries();
      return;
    }
  }
  GoogleJsonResponseException exception =
      new GoogleJsonResponseException(
          new Builder(error.getCode(), error.getMessage(), responseHeaders), error);
  fail(exception);
}
 
Example #10
Source Project: connector-sdk   Author: google-cloudsearch   File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Waits for the item with the given ID to be deleted.
 *
 * @throws org.awaitility.core.ConditionTimeoutException if the item is not deleted before the
 *     timeout
 */
// TODO(jlacey): Resolve this one-off to support the verifier repository (as of commit 20b0d95)
// into a consistent design.
public void waitUntilDeleted(String itemId, Duration timeout, Duration pollInterval) {
  Awaitility.await()
      .atMost(timeout)
      .pollInterval(pollInterval)
      .until(() -> {
        try {
          service.getItem(itemId);
          return false;
        } catch (GoogleJsonResponseException e) {
          if (e.getStatusCode() == HTTP_NOT_FOUND) {
            return true;
          }
          throw e;
        }
      });
}
 
Example #11
Source Project: java-monitoring-client-library   Author: google   File: StackdriverWriterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void registerMetric_rethrowsException() throws Exception {
  ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));
  HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream);
  HttpResponseException.Builder httpResponseExceptionBuilder =
      new HttpResponseException.Builder(response);
  httpResponseExceptionBuilder.setStatusCode(404);
  GoogleJsonResponseException exception =
      new GoogleJsonResponseException(httpResponseExceptionBuilder, null);
  when(metricDescriptorCreate.execute()).thenThrow(exception);
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);

  assertThrows(GoogleJsonResponseException.class, () -> writer.registerMetric(metric));
  assertThat(exception.getStatusCode()).isEqualTo(404);
}
 
Example #12
Source Project: flo   Author: spotify   File: BigQueryTasksTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void lookupShouldBeRunnable() throws Exception {
  final Future<TableId> future = FloRunner.runTask(BigQueryTasks.lookup(
      "non-existent-project", "non-existent-dataset", "non-existent-table")).future();

  try {
    future.get();
    fail("Did not expect to find a non-existent table");
  } catch (ExecutionException e) {
    // Verify that we are getting some well known error here so we know with some
    // certainty that we didn't get a serialization error. Yes, this is quite awful.
    final Throwable rootCause = Throwables.getRootCause(e);
    if (rootCause instanceof NotReady) {
      // Seems we had working credentials and the lookup worked. We're done here.
    } else if (rootCause instanceof GoogleJsonResponseException) {
      // Seems we managed to make a request, so the lookup executed. We're done here.
    } else if (rootCause instanceof IllegalArgumentException &&
        rootCause.getMessage().startsWith("A project ID is required")) {
      // Seems we managed to get as far as trying to instantiate the BigQuery client (in the task process).
      // We're done here.
    } else {
      // Not sure what went wrong here, might be serialization error, so be conservative and fail here.
      throw e;
    }
  }
}
 
Example #13
Source Project: digdag   Author: treasure-data   File: BqDdlOperatorFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected TaskResult run(BqClient bq, String projectId)

{
    List<BqOperation> operations = Stream.of(
            params.getListOrEmpty("delete_datasets", JsonNode.class).stream().map(this::deleteDataset),
            params.getListOrEmpty("empty_datasets", JsonNode.class).stream().map(this::emptyDataset),
            params.getListOrEmpty("create_datasets", JsonNode.class).stream().map(this::createDataset),
            params.getListOrEmpty("delete_tables", JsonNode.class).stream().map(this::deleteTable),
            params.getListOrEmpty("empty_tables", JsonNode.class).stream().map(this::emptyTable),
            params.getListOrEmpty("create_tables", JsonNode.class).stream().map(this::createTable))
            .flatMap(s -> s)
            .collect(Collectors.toList());

    int operation = state.params().get("operation", int.class, 0);
    for (int i = operation; i < operations.size(); i++) {
        state.params().set("operation", i);
        BqOperation o = operations.get(i);
        pollingRetryExecutor(state, "request")
                .retryUnless(GoogleJsonResponseException.class, Gcp::isDeterministicException)
                .withErrorMessage("BiqQuery DDL operation failed")
                .runAction(s -> o.perform(bq, projectId));
    }

    return TaskResult.empty(request);
}
 
Example #14
Source Project: googleads-shopping-samples   Author: googleads   File: ShippingsettingsWorkflow.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
  CommandLine parsedArgs = BaseOption.parseOptions(args);
  File configPath = null;
  if (!NO_CONFIG.isSet(parsedArgs)) {
    configPath = BaseOption.checkedConfigPath(parsedArgs);
  }
  ContentConfig config = ContentConfig.load(configPath);

  ShoppingContent.Builder builder = createStandardBuilder(parsedArgs, config);
  ShoppingContent content = createService(builder);
  ShoppingContent sandbox = createSandboxContentService(builder);
  retrieveConfiguration(content, config);

  try {
    new ShippingsettingsWorkflow(content, sandbox, config).execute();
  } catch (GoogleJsonResponseException e) {
    checkGoogleJsonResponseException(e);
  }
}
 
Example #15
Source Project: policyscanner   Author: GoogleCloudPlatform   File: GCSFilesSource.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructor for GCSFileSource.
 * @param bucket The bucket where the configuration files reside.
 * @param repository The root directory where the files reside.
 * @throws GeneralSecurityException Thrown if there's a permissions error using the GCS API.
 * @throws IOException Thrown if there's a IO error using the GCS API.
 */
public GCSFilesSource(String bucket, String repository)
    throws GeneralSecurityException, IOException {
  this.bucket = bucket;
  this.repository = repository;

  try {
    // test that the bucket actually exists.
    getStorageApiStub().buckets().get(bucket).execute();
  } catch (GoogleJsonResponseException gjre) {
    String msgFormat = new StringBuilder()
        .append("Can't access bucket \"gs://%s\".\n\n")
        .append("1. Check that your appengine-web.xml has the correct environment variables.\n")
        .append("2. Check your project IAM settings: the Compute Engine service account should\n")
        .append("   have Editor access (or at least Storage Object Creator) if you're running\n")
        .append("   on production. If you are running this locally, your user account or group\n")
        .append("   should be granted Storage Object Creator.\n")
        .append("3. Try re-authing your application-default credentials with this command:\n\n")
        .append("   $ gcloud auth application-default login\n\n")
        .append("More details:\n%s").toString();
    String message = String.format(msgFormat, bucket, gjre.getContent());
    throw new BucketAccessException(message);
  }
}
 
Example #16
Source Project: googleads-shopping-samples   Author: googleads   File: AccountWorkflow.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
  CommandLine parsedArgs = BaseOption.parseOptions(args);
  File configPath = null;
  if (!NO_CONFIG.isSet(parsedArgs)) {
    configPath = BaseOption.checkedConfigPath(parsedArgs);
  }
  ContentConfig config = ContentConfig.load(configPath);

  ShoppingContent.Builder builder = createStandardBuilder(parsedArgs, config);
  ShoppingContent content = createService(builder);
  ShoppingContent sandbox = createSandboxContentService(builder);
  retrieveConfiguration(content, config);

  try {
    new AccountWorkflow(content, sandbox, config).execute();
  } catch (GoogleJsonResponseException e) {
    checkGoogleJsonResponseException(e);
  }
}
 
Example #17
Source Project: science-journal   Author: google   File: GoogleDriveApiImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean getFileExists(String fileId) throws IOException {
  try {
    File file =
        driveApi
            .files()
            .get(fileId)
            .execute();
    if (file == null || file.getLabels() == null) {
      return false;
    }
    return !file.getLabels().getTrashed();
  } catch (GoogleJsonResponseException e) {
    // Drive will return a GoogleJsonResponseException if the file is not found.
    return false;
  }
}
 
Example #18
Source Project: styx   Author: spotify   File: Authenticator.java    License: Apache License 2.0 6 votes vote down vote up
private boolean resolveProjectAccess(String projectId) throws IOException {
  final GetAncestryResponse ancestry;
  var request = cloudResourceManager.projects().getAncestry(projectId, new GetAncestryRequest());
  try {
    ancestry = executeWithRetries(request, retryWaitStrategy, retryStopStrategy);
  } catch (GoogleJsonResponseException e) {
    if (e.getStatusCode() == 404) {
      logger.debug("Project {} doesn't exist", projectId, e);
      return false;
    }

    logger.info("Cannot get project with id {}", projectId, e);
    return false;
  }
  if (ancestry.getAncestor() == null) {
    return false;
  }
  return resolveAccess(ancestry.getAncestor());
}
 
Example #19
Source Project: googleads-shopping-samples   Author: googleads   File: ShippingsettingsUpdateSample.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void execute() throws IOException {
  try {
    ShippingSettings newSettings = ExampleShippingSettingsFactory.create();

    content
        .shippingsettings()
        .update(config.getMerchantId(), config.getMerchantId(), newSettings)
        .execute();
    ShippingSettings response =
        content.shippingsettings().get(config.getMerchantId(), config.getMerchantId()).execute();
    System.out.println("Set the following shipping information:");
    ShippingsettingsUtils.printShippingSettings(response);
  } catch (GoogleJsonResponseException e) {
    checkGoogleJsonResponseException(e);
  }
}
 
Example #20
Source Project: nomulus   Author: google   File: UpdateSnapshotViewAction.java    License: Apache License 2.0 6 votes vote down vote up
private static void updateTable(Bigquery bigquery, Table table) throws IOException {
  TableReference ref = table.getTableReference();
  try {
    bigquery
        .tables()
        .update(ref.getProjectId(), ref.getDatasetId(), ref.getTableId(), table)
        .execute();
  } catch (GoogleJsonResponseException e) {
    if (e.getDetails() != null && e.getDetails().getCode() == 404) {
      bigquery.tables().insert(ref.getProjectId(), ref.getDatasetId(), table).execute();
    } else {
      logger.atWarning().withCause(e).log(
          "UpdateSnapshotViewAction failed, caught exception %s", e.getDetails());
    }
  }
}
 
Example #21
Source Project: styx   Author: spotify   File: KubernetesGCPServiceAccountSecretManagerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleTooManyKeysCreated() throws IOException {
  when(serviceAccountKeyManager.serviceAccountExists(anyString())).thenReturn(true);

  final GoogleJsonResponseException resourceExhausted = new GoogleJsonResponseException(
      new HttpResponseException.Builder(429, "RESOURCE_EXHAUSTED", new HttpHeaders()),
      new GoogleJsonError().set("status", "RESOURCE_EXHAUSTED"));

  doThrow(resourceExhausted).when(serviceAccountKeyManager).createJsonKey(any());
  doThrow(resourceExhausted).when(serviceAccountKeyManager).createP12Key(any());

  exception.expect(InvalidExecutionException.class);
  exception.expectMessage(String.format(
      "Maximum number of keys on service account reached: %s. Styx requires 4 keys to operate.",
      SERVICE_ACCOUNT));

  sut.ensureServiceAccountKeySecret(WORKFLOW_ID.toString(), SERVICE_ACCOUNT);
}
 
Example #22
Source Project: beam   Author: apache   File: PubsubHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a topic from short name. Delete it if it already exists. Ensure the topic will be
 * deleted on cleanup. Return full topic name.
 */
public TopicPath createTopic(String shortTopic) throws IOException {
  TopicPath topic = PubsubClient.topicPathFromName(project, shortTopic);
  while (true) {
    try {
      NexmarkUtils.console("create topic %s", topic);
      pubsubClient.createTopic(topic);
      createdTopics.add(topic);
      return topic;
    } catch (GoogleJsonResponseException ex) {
      NexmarkUtils.console("attempting to cleanup topic %s", topic);
      pubsubClient.deleteTopic(topic);
      try {
        if (!BackOffUtils.next(sleeper, backOff)) {
          NexmarkUtils.console("too many retries for creating topic %s", topic);
          throw ex;
        }
      } catch (InterruptedException in) {
        throw new IOException(in);
      }
    }
  }
}
 
Example #23
Source Project: digdag   Author: treasure-data   File: BqClient.java    License: Apache License 2.0 6 votes vote down vote up
void createTable(String projectId, Table table)
        throws IOException
{
    String datasetId = table.getTableReference().getDatasetId();
    try {
        client.tables().insert(projectId, datasetId, table)
                .execute();
    }
    catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_CONFLICT) {
            logger.debug("Table already exists: {}:{}.{}", projectId, datasetId, table.getTableReference().getTableId());
        }
        else {
            throw e;
        }
    }
}
 
Example #24
Source Project: googleads-shopping-samples   Author: googleads   File: BaseWorkflowSample.java    License: Apache License 2.0 6 votes vote down vote up
protected <T extends GenericJson> T retryFailures(
    AbstractGoogleClientRequest<T> request, BackOff backOff) throws IOException {
  while (true) {
    try {
      return request.execute();
    } catch (GoogleJsonResponseException e) {
      try {
        long nextPause = backOff.nextBackOffMillis();
        if (nextPause == BackOff.STOP) {
          throw e;
        }
        System.out.printf("Operation failed, retrying in %f seconds.%n", nextPause / 1000.0);
        BackOffUtils.next(Sleeper.DEFAULT, backOff);
      } catch (InterruptedException ie) {
        // Just go straight into retry if interrupted.
      }
    }
  }
}
 
Example #25
Source Project: eip   Author: spac3lord   File: PubSubWrapper.java    License: MIT License 6 votes vote down vote up
/**
 * Sets up a subscription to projects/<code>appName</code>/subscriptions/<code>subName</code>.
 * Ignores error if the subscription already exists.
 * <p/>
 * See <a href="https://cloud.google.com/pubsub/subscriber">cloud.google.com/pubsub/subscriber</a>
 */
Subscription subscribeTopic(String subscriptionName, String topicName) throws IOException {
    String sub = getSubscription(subscriptionName);
    Subscription subscription = new Subscription()
            .setName(sub)
            .setAckDeadlineSeconds(15)
            .setTopic(getTopic(topicName));
    try {
        return pubsub.projects().subscriptions().create(sub, subscription).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == HttpURLConnection.HTTP_CONFLICT) {
            return subscription;
        } else {
            throw e;
        }
    }
}
 
Example #26
Source Project: googleads-shopping-samples   Author: googleads   File: ProductsWorkflow.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
  CommandLine parsedArgs = BaseOption.parseOptions(args);
  File configPath = null;
  if (!NO_CONFIG.isSet(parsedArgs)) {
    configPath = BaseOption.checkedConfigPath(parsedArgs);
  }
  ContentConfig config = ContentConfig.load(configPath);

  ShoppingContent.Builder builder = createStandardBuilder(parsedArgs, config);
  ShoppingContent content = createService(builder);
  ShoppingContent sandbox = createSandboxContentService(builder);
  retrieveConfiguration(content, config);

  try {
    new ProductsWorkflow(content, sandbox, config).execute();
  } catch (GoogleJsonResponseException e) {
    checkGoogleJsonResponseException(e);
  }
}
 
Example #27
Source Project: googleads-shopping-samples   Author: googleads   File: ProductstatusesWorkflow.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
  CommandLine parsedArgs = BaseOption.parseOptions(args);
  File configPath = null;
  if (!NO_CONFIG.isSet(parsedArgs)) {
    configPath = BaseOption.checkedConfigPath(parsedArgs);
  }
  ContentConfig config = ContentConfig.load(configPath);

  ShoppingContent.Builder builder = createStandardBuilder(parsedArgs, config);
  ShoppingContent content = createService(builder);
  ShoppingContent sandbox = createSandboxContentService(builder);
  retrieveConfiguration(content, config);

  try {
    new ProductstatusesWorkflow(content, sandbox, config).execute();
  } catch (GoogleJsonResponseException e) {
    checkGoogleJsonResponseException(e);
  }
}
 
Example #28
Source Project: hadoop-connectors   Author: GoogleCloudDataproc   File: GoogleCloudStorageExceptions.java    License: Apache License 2.0 5 votes vote down vote up
public static GoogleJsonResponseException createJsonResponseException(
    GoogleJsonError e, HttpHeaders responseHeaders) {
  if (e != null) {
    return new GoogleJsonResponseException(
        new HttpResponseException.Builder(e.getCode(), e.getMessage(), responseHeaders), e);
  }
  return null;
}
 
Example #29
Source Project: digdag   Author: treasure-data   File: BqClient.java    License: Apache License 2.0 5 votes vote down vote up
void deleteTable(String projectId, String datasetId, String tableId)
        throws IOException
{
    try {
        client.tables().delete(projectId, datasetId, tableId).execute();
    }
    catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
            // Already deleted
            return;
        }
        throw e;
    }
}
 
Example #30
Source Project: beam   Author: apache   File: GcsUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBucketDoesNotExistBecauseOfAccessError() throws IOException {
  GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
  GcsUtil gcsUtil = pipelineOptions.getGcsUtil();

  Storage mockStorage = Mockito.mock(Storage.class);
  gcsUtil.setStorageClient(mockStorage);

  Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class);
  Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class);

  BackOff mockBackOff = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff());
  GoogleJsonResponseException expectedException =
      googleJsonResponseException(
          HttpStatusCodes.STATUS_CODE_FORBIDDEN,
          "Waves hand mysteriously",
          "These aren't the buckets you're looking for");

  when(mockStorage.buckets()).thenReturn(mockStorageObjects);
  when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet);
  when(mockStorageGet.execute()).thenThrow(expectedException);

  assertFalse(
      gcsUtil.bucketAccessible(
          GcsPath.fromComponents("testbucket", "testobject"),
          mockBackOff,
          new FastNanoClockAndSleeper()));
}