Java Code Examples for com.google.api.client.http.HttpResponseException#getContent()

The following examples show how to use com.google.api.client.http.HttpResponseException#getContent() . 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: GooglePhotosInterface.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private HttpResponse handleHttpResponseException(
    SupplierWithIO<HttpRequest> httpRequest, HttpResponseException e)
    throws IOException, InvalidTokenException, PermissionDeniedException {
  // if the response is "unauthorized", refresh the token and try the request again
  final int statusCode = e.getStatusCode();

  if (statusCode == 401) {
    monitor.info(() -> "Attempting to refresh authorization token");
    // if the credential refresh failed, let the error bubble up via the IOException that gets
    // thrown
    credential = credentialFactory.refreshCredential(credential);
    monitor.info(() -> "Refreshed authorization token successfuly");

    // if the second attempt throws an error, then something else is wrong, and we bubble up the
    // response errors
    return httpRequest.getWithIO().execute();
  }
  // "The caller does not have permission" is potential error for albums.
  // "Google Photos is disabled for the user" is potential error for photos.
  if (statusCode == 403 &&
          (e.getContent().contains("The caller does not have permission") ||
           e.getContent().contains("Google Photos is disabled for the user"))) {
    throw new PermissionDeniedException("User permission to google photos was denied", e);
  } else {
    // something else is wrong, bubble up the error
    throw new IOException(
        "Bad status code: "
            + e.getStatusCode()
            + " Error: '"
            + e.getStatusMessage()
            + "' Content: "
            + e.getContent());
  }
}
 
Example 2
Source File: FirebaseMessagingClientImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private FirebaseMessagingException createExceptionFromResponse(HttpResponseException e) {
  MessagingServiceErrorResponse response = new MessagingServiceErrorResponse();
  if (e.getContent() != null) {
    try {
      JsonParser parser = jsonFactory.createJsonParser(e.getContent());
      parser.parseAndClose(response);
    } catch (IOException ignored) {
      // ignored
    }
  }

  return newException(response, e);
}
 
Example 3
Source File: InstanceIdClientImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private FirebaseMessagingException createExceptionFromResponse(HttpResponseException e) {
  InstanceIdServiceErrorResponse response = new InstanceIdServiceErrorResponse();
  if (e.getContent() != null) {
    try {
      JsonParser parser = jsonFactory.createJsonParser(e.getContent());
      parser.parseAndClose(response);
    } catch (IOException ignored) {
      // ignored
    }
  }
  return newException(response, e);
}
 
Example 4
Source File: FirebaseErrorParsingUtils.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if a firebase token is invalid by checking if it either expired or not forbidden
 * @param throwable Throwable of firebase execution error
 * @return TokenStatus denoting status of the token
 */
public static TokenStatus getTokenStatus(Throwable throwable) {
    Throwable current = throwable;
    while (!(current instanceof FirebaseMessagingException) && current != null) {
        current = current.getCause();
    }

    if (current == null)
        throw new InvalidThrowableException(throwable);

    // We have a FirebaseMessagingException

    FirebaseMessagingException firebaseMessagingException = (FirebaseMessagingException) current;

    while (!(current instanceof HttpResponseException) && current != null) {
        current = current.getCause();
    }

    if (current == null)
        throw new InvalidThrowableException(throwable);

    // We have a HttpResponseException

    HttpResponseException httpResponseException = (HttpResponseException) current;
    int statusCode = httpResponseException.getStatusCode();

    Reason reason = new Reason(statusCode, current.getMessage(), httpResponseException.getContent());

    boolean isTokenExpired = statusCode == 404 || statusCode == 400;
    boolean isTokenForbidden = statusCode == 403;

    if (isTokenExpired || isTokenForbidden) {
        return new TokenStatus(false, reason);
    } else {
        return new TokenStatus(true, reason);
    }
}
 
Example 5
Source File: GitHubApiTransportImpl.java    From copybara with Apache License 2.0 5 votes vote down vote up
private static ClientError parseErrorOrIgnore(HttpResponseException e) {
  if (e.getContent() == null) {
    return null;
  }
  try {
    return JSON_FACTORY.createJsonParser(e.getContent()).parse(ClientError.class);
  } catch (IOException ignore) {
    logger.atWarning().withCause(ignore).log("Invalid error response");
    return new ClientError();
  }
}
 
Example 6
Source File: GerritApiTransportImpl.java    From copybara with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T execute(Type responseType, HttpRequest httpRequest)
    throws IOException, GerritApiException {
  HttpResponse response;
  try {
    response = httpRequest.execute();
  } catch (HttpResponseException e) {
    throw new GerritApiException(e.getStatusCode(), e.getContent(), e.getContent());
  }
  return (T) response.parseAs(responseType);
}