Example usage for com.amazonaws.services.cognitoidp.model TooManyRequestsException getMessage

List of usage examples for com.amazonaws.services.cognitoidp.model TooManyRequestsException getMessage

Introduction

In this page you can find the example usage for com.amazonaws.services.cognitoidp.model TooManyRequestsException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:com.kdgregory.example.cognito.servlets.ValidatedAction.java

License:Apache License

/**
 *  Attempts to create a new access token based on the provided refresh token.
 *//*from  w w w .  j ava2 s  .c o  m*/
private void attemptRefresh(String refreshToken, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        Map<String, String> authParams = new HashMap<String, String>();
        authParams.put("REFRESH_TOKEN", refreshToken);

        AdminInitiateAuthRequest refreshRequest = new AdminInitiateAuthRequest()
                .withAuthFlow(AuthFlowType.REFRESH_TOKEN).withAuthParameters(authParams)
                .withClientId(cognitoClientId()).withUserPoolId(cognitoPoolId());

        AdminInitiateAuthResult refreshResponse = cognitoClient.adminInitiateAuth(refreshRequest);
        if (StringUtil.isBlank(refreshResponse.getChallengeName())) {
            logger.debug("successfully refreshed token");
            updateCredentialCookies(response, refreshResponse.getAuthenticationResult());
            reportResult(response, Constants.ResponseMessages.LOGGED_IN);
        } else {
            logger.warn("unexpected challenge when refreshing token: {}", refreshResponse.getChallengeName());
            reportResult(response, Constants.ResponseMessages.NOT_LOGGED_IN);
        }
    } catch (TooManyRequestsException ex) {
        logger.warn("caught TooManyRequestsException, delaying then retrying");
        ThreadUtil.sleepQuietly(250);
        attemptRefresh(refreshToken, response);
    } catch (AWSCognitoIdentityProviderException ex) {
        logger.debug("exception during token refresh: {}", ex.getMessage());
        reportResult(response, Constants.ResponseMessages.NOT_LOGGED_IN);
    }
}