Java Code Examples for com.google.api.client.http.HttpStatusCodes#STATUS_CODE_UNAUTHORIZED

The following examples show how to use com.google.api.client.http.HttpStatusCodes#STATUS_CODE_UNAUTHORIZED . 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: EmailAccount.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Triggers the authentication process for the associated {@code username}.
 */
public void getUserAuthenticated() throws IOException {
    // assume user is authenticated before
    service = new GmailServiceMaker(username).makeGmailService();

    while (true) {
        try {
            // touch one API endpoint to check authentication
            getListOfUnreadEmailOfUser();
            break;
        } catch (HttpResponseException e) {
            if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN
                    || e.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED
                    || e.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST) {
                System.out.println(e.getMessage());
                // existing credential missing or not working, should do authentication for the account again
                service = new GmailServiceMaker(username, true).makeGmailService();
            } else {
                throw new IOException(e);
            }
        }
    }
}
 
Example 2
Source File: IDicomWebClient.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 5 votes vote down vote up
private int httpStatusToDicomStatus(int httpStatus, int defaultStatus) {
  switch (httpStatus) {
    case HttpStatusCodes.STATUS_CODE_SERVICE_UNAVAILABLE:
      return Status.OutOfResources;
    case HttpStatusCodes.STATUS_CODE_UNAUTHORIZED:
      return Status.NotAuthorized;
    default:
      return defaultStatus;
  }
}
 
Example 3
Source File: OAuthHmacCredential.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
public boolean handleResponse(
    HttpRequest request, HttpResponse response, boolean retrySupported) {
  if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
    // If the token was revoked, we must mark our credential as invalid
    token = null;
  }

  // We didn't do anything to fix the problem
  return false;
}
 
Example 4
Source File: OAuthHmacCredential.java    From android-oauth-client with Apache License 2.0 5 votes vote down vote up
@Override
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) {
    if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
        // If the token was revoked, we must mark our credential as invalid
        setAccessToken(null);
    }

    // We didn't do anything to fix the problem
    return false;
}
 
Example 5
Source File: Credential.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Default implementation checks if {@code WWW-Authenticate} exists and contains a "Bearer" value
 * (see <a href="http://tools.ietf.org/html/rfc6750#section-3.1">rfc6750 section 3.1</a> for more
 * details). If so, it calls {@link #refreshToken} in case the error code contains
 * {@code invalid_token}. If there is no "Bearer" in {@code WWW-Authenticate} and the status code
 * is {@link HttpStatusCodes#STATUS_CODE_UNAUTHORIZED} it calls {@link #refreshToken}. If
 * {@link #executeRefreshToken()} throws an I/O exception, this implementation will log the
 * exception and return {@code false}. Subclasses may override.
 * </p>
 */
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) {
  boolean refreshToken = false;
  boolean bearer = false;

  List<String> authenticateList = response.getHeaders().getAuthenticateAsList();

  // TODO(peleyal): this logic should be implemented as a pluggable interface, in the same way we
  // implement different AccessMethods

  // if authenticate list is not null we will check if one of the entries contains "Bearer"
  if (authenticateList != null) {
    for (String authenticate : authenticateList) {
      if (authenticate.startsWith(BearerToken.AuthorizationHeaderAccessMethod.HEADER_PREFIX)) {
        // mark that we found a "Bearer" value, and check if there is a invalid_token error
        bearer = true;
        refreshToken = BearerToken.INVALID_TOKEN_ERROR.matcher(authenticate).find();
        break;
      }
    }
  }

  // if "Bearer" wasn't found, we will refresh the token, if we got 401
  if (!bearer) {
    refreshToken = response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED;
  }

  if (refreshToken) {
    try {
      lock.lock();
      try {
        // need to check if another thread has already refreshed the token
        return !Objects.equal(accessToken, method.getAccessTokenFromRequest(request))
            || refreshToken();
      } finally {
        lock.unlock();
      }
    } catch (IOException exception) {
      LOGGER.log(Level.SEVERE, "unable to refresh token", exception);
    }
  }
  return false;
}