Search in sources :

Example 1 with AccountInfoNotFoundException

use of com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException in project SalesforceMobileSDK-Android by forcedotcom.

the class SalesforceDroidGapActivity method refresh.

/**
 * If an action causes a redirect to the login page, this method will be called.
 * It causes the session to be refreshed and reloads url through the front door.
 *
 * @param url the page to load once the session has been refreshed.
 */
public void refresh(final String url) {
    SalesforceHybridLogger.i(TAG, "refresh called");
    /*
         * If client is null at this point, authentication hasn't been performed yet.
         * We need to trigger authentication, and recreate the webview in the
         * callback, to load the page correctly. This handles some corner cases
         * involving hitting the back button when authentication is in progress.
         */
    if (client == null) {
        clientManager.getRestClient(this, new RestClientCallback() {

            @Override
            public void authenticatedRestClient(RestClient client) {
                recreate();
            }
        });
        return;
    }
    client.sendAsync(RestRequest.getRequestForUserInfo(), new AsyncRequestCallback() {

        @Override
        public void onSuccess(RestRequest request, RestResponse response) {
            SalesforceHybridLogger.i(TAG, "refresh callback - refresh succeeded");
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    /*
                         * The client instance being used here needs to be refreshed, to ensure we
                         * use the new access token. However, if the refresh token was revoked
                         * when the app was in the background, we need to catch that exception
                         * and trigger a proper logout to reset the state of this class.
                         */
                    try {
                        SalesforceDroidGapActivity.this.client = SalesforceDroidGapActivity.this.clientManager.peekRestClient();
                        final String frontDoorUrl = getFrontDoorUrl(url, BootConfig.isAbsoluteUrl(url));
                        loadUrl(frontDoorUrl);
                    } catch (AccountInfoNotFoundException e) {
                        SalesforceHybridLogger.i(TAG, "User has been logged out.");
                        logout(null);
                    }
                }
            });
        }

        @Override
        public void onError(Exception exception) {
            SalesforceHybridLogger.w(TAG, "refresh callback - refresh failed", exception);
            // Only logout if we are NOT offline
            if (!(exception instanceof NoNetworkException)) {
                logout(null);
            }
        }
    });
}
Also used : AsyncRequestCallback(com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback) RestRequest(com.salesforce.androidsdk.rest.RestRequest) NoNetworkException(com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException) RestResponse(com.salesforce.androidsdk.rest.RestResponse) RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) RestClient(com.salesforce.androidsdk.rest.RestClient) AccountInfoNotFoundException(com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException) AccountInfoNotFoundException(com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException) NoNetworkException(com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)

Example 2 with AccountInfoNotFoundException

use of com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException in project SalesforceMobileSDK-Android by forcedotcom.

the class ClientManagerTest method testPeekRestClientWithAccountSetup.

/**
 * Test peekRestClient - when there is an account
 * @throws URISyntaxException
 */
@Test
public void testPeekRestClientWithAccountSetup() throws URISyntaxException {
    // Make sure we have no accounts initially
    assertNoAccounts();
    // Create account
    createTestAccount();
    // Call peekRestClient - expect restClient
    try {
        RestClient restClient = clientManager.peekRestClient();
        Assert.assertNotNull("RestClient expected", restClient);
        Assert.assertEquals("Wrong authToken", TEST_AUTH_TOKEN, restClient.getAuthToken());
        Assert.assertEquals("Wrong instance Url", new URI(TEST_INSTANCE_URL), restClient.getClientInfo().instanceUrl);
    } catch (AccountInfoNotFoundException e) {
        Assert.fail("Did not expect AccountInfoNotFoundException");
    }
}
Also used : AccountInfoNotFoundException(com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException) URI(java.net.URI) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

AccountInfoNotFoundException (com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException)2 MediumTest (androidx.test.filters.MediumTest)1 NoNetworkException (com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)1 RestClientCallback (com.salesforce.androidsdk.rest.ClientManager.RestClientCallback)1 RestClient (com.salesforce.androidsdk.rest.RestClient)1 AsyncRequestCallback (com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback)1 RestRequest (com.salesforce.androidsdk.rest.RestRequest)1 RestResponse (com.salesforce.androidsdk.rest.RestResponse)1 URI (java.net.URI)1 Test (org.junit.Test)1