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

The following examples show how to use com.google.api.client.googleapis.json.GoogleJsonResponseException. 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 File: ProductstatusesWorkflow.java    From googleads-shopping-samples with 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 #2
Source File: GoogleCalendarResource.java    From camel-quarkus with 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 File: AsyncRequest.java    From connector-sdk with 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 #4
Source File: ProductstatusGetSample.java    From googleads-shopping-samples with 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 #5
Source File: RepositoryDocTest.java    From connector-sdk with 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 #6
Source File: GCEComputeResourceController.java    From pubsub with 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 File: RepositoryDoc.java    From connector-sdk with 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 #8
Source File: BigQueryTasksTest.java    From flo with 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 #9
Source File: TestUtils.java    From connector-sdk with 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 #10
Source File: GoogleCalendarResource.java    From camel-quarkus with 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 #11
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with 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 File: BigQueryTasksTest.java    From flo with 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 File: BqDdlOperatorFactory.java    From digdag with 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 File: ShippingsettingsWorkflow.java    From googleads-shopping-samples with 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 File: GCSFilesSource.java    From policyscanner with 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 File: AccountWorkflow.java    From googleads-shopping-samples with 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 File: GoogleDriveApiImpl.java    From science-journal with 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 File: Authenticator.java    From styx with 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 File: ShippingsettingsUpdateSample.java    From googleads-shopping-samples with 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 File: UpdateSnapshotViewAction.java    From nomulus with 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 File: KubernetesGCPServiceAccountSecretManagerTest.java    From styx with 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 File: PubsubHelper.java    From beam with 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 File: BqClient.java    From digdag with 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 File: BaseWorkflowSample.java    From googleads-shopping-samples with 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 File: PubSubWrapper.java    From eip with 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 File: ProductsWorkflow.java    From googleads-shopping-samples with 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 File: GoogleDriveResource.java    From camel-quarkus with 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 #28
Source File: GcsUtilTest.java    From beam with 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()));
}
 
Example #29
Source File: CloudSqlInstance.java    From cloud-sql-jdbc-socket-factory with Apache License 2.0 5 votes vote down vote up
/**
 * Checks for common errors that can occur when interacting with the Cloud SQL Admin API, and adds
 * additional context to help the user troubleshoot them.
 *
 * @param ex exception thrown by the Admin API request
 * @param fallbackDesc generic description used as a fallback if no additional information can be
 *     provided to the user
 */
private RuntimeException addExceptionContext(IOException ex, String fallbackDesc) {
  // Verify we are able to extract a reason from an exception, or fallback to a generic desc
  GoogleJsonResponseException gjrEx =
      ex instanceof GoogleJsonResponseException ? (GoogleJsonResponseException) ex : null;
  if (gjrEx == null
      || gjrEx.getDetails() == null
      || gjrEx.getDetails().getErrors() == null
      || gjrEx.getDetails().getErrors().isEmpty()) {
    return new RuntimeException(fallbackDesc, ex);
  }
  // Check for commonly occurring user errors and add additional context
  String reason = gjrEx.getDetails().getErrors().get(0).getReason();
  if ("accessNotConfigured".equals(reason)) {
    // This error occurs when the project doesn't have the "Cloud SQL Admin API" enabled
    String apiLink =
        "https://console.cloud.google.com/apis/api/sqladmin/overview?project=" + projectId;
    return new RuntimeException(
        String.format(
            "[%s] The Google Cloud SQL Admin API is not enabled for the project \"%s\". Please "
                + "use the Google Developers Console to enable it: %s",
            connectionName, projectId, apiLink),
        ex);
  } else if ("notAuthorized".equals(reason)) {
    // This error occurs if the instance doesn't exist or the account isn't authorized
    // TODO(kvg): Add credential account name to error string.
    return new RuntimeException(
        String.format(
            "[%s] The Cloud SQL Instance does not exist or your account is not authorized to "
                + "access it. Please verify the instance connection name and check the IAM "
                + "permissions for project \"%s\" ",
            connectionName, projectId),
        ex);
  }
  // Fallback to the generic description
  return new RuntimeException(fallbackDesc, ex);
}
 
Example #30
Source File: WaxTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void updateSessionItem_invalidItemIdChange() throws IOException {
  try {
    String sessionId = createSession();
    wax.items().update(sessionId, ITEM_B.getId(), ITEM_C).execute();
    fail("expected exception");
  } catch (GoogleJsonResponseException e) {
    assertThatResponseCode(e).isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
  }
}