Search in sources :

Example 1 with RestClientCallback

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

the class SalesforceReactActivity method login.

protected void login() {
    SalesforceReactLogger.i(TAG, "login called");
    clientManager.getRestClient(this, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            if (client == null) {
                SalesforceReactLogger.i(TAG, "login callback triggered with null client");
                logout(null);
            } else {
                SalesforceReactLogger.i(TAG, "login callback triggered with actual client");
                SalesforceReactActivity.this.restartReactNativeApp();
            }
        }
    });
}
Also used : RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) RestClient(com.salesforce.androidsdk.rest.RestClient)

Example 2 with RestClientCallback

use of com.salesforce.androidsdk.rest.ClientManager.RestClientCallback 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 3 with RestClientCallback

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

the class ClientManagerTest method testGetRestClientWithAccountSetup.

/**
 * Test getRestClient - when there is an account
 * @throws URISyntaxException
 */
@Test
public void testGetRestClientWithAccountSetup() throws URISyntaxException {
    // Make sure we have no accounts initially
    assertNoAccounts();
    // Create account
    createTestAccount();
    // Call getRestClient - expect restClient
    final BlockingQueue<RestClient> q = new ArrayBlockingQueue<RestClient>(1);
    clientManager.getRestClient(null, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            q.add(client);
        }
    });
    // Wait for getRestClient to complete
    try {
        RestClient restClient = q.poll(10L, TimeUnit.SECONDS);
        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 (InterruptedException e) {
        Assert.fail("getRestClient did not return after 5s");
    }
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) URI(java.net.URI) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with RestClientCallback

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

the class SalesforceReactActivity method authenticate.

/**
 * Method called from bridge to authenticate.
 *
 * @param successCallback Success callback.
 * @param errorCallback Error callback.
 */
public void authenticate(final Callback successCallback, final Callback errorCallback) {
    SalesforceReactLogger.i(TAG, "authenticate called");
    clientManager.getRestClient(this, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            SalesforceReactActivity.this.setRestClient(client);
            getAuthCredentials(successCallback, errorCallback);
        }
    });
}
Also used : RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) RestClient(com.salesforce.androidsdk.rest.RestClient)

Example 5 with RestClientCallback

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

the class SalesforceDroidGapActivity method authenticate.

/**
 * Get a RestClient and refresh the auth token
 *
 * @param callbackContext when not null credentials/errors are sent through to callbackContext.success()/error()
 */
public void authenticate(final CallbackContext callbackContext) {
    SalesforceHybridLogger.i(TAG, "authenticate called");
    clientManager.getRestClient(this, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            if (client == null) {
                SalesforceHybridLogger.i(TAG, "authenticate callback triggered with null client");
                logout(null);
            } else {
                SalesforceHybridLogger.i(TAG, "authenticate callback triggered with actual client");
                SalesforceDroidGapActivity.this.client = client;
                /*
                     * Do a cheap REST call to refresh the access token if needed.
                     * If the login took place a while back (e.g. the already logged
                     * in application was restarted), then the returned session ID
                     * (access token) might be stale. This is not an issue if one
                     * uses exclusively RestClient for calling the server because
                     * it takes care of refreshing the access token when needed,
                     * but a stale session ID will cause the WebView to redirect
                     * to the web login.
                     */
                SalesforceDroidGapActivity.this.client.sendAsync(RestRequest.getRequestForUserInfo(), new AsyncRequestCallback() {

                    @Override
                    public void onSuccess(RestRequest request, RestResponse response) {
                        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.
                                     */
                                SalesforceDroidGapActivity.this.client = SalesforceDroidGapActivity.this.clientManager.peekRestClient();
                                getAuthCredentials(callbackContext);
                            }
                        });
                    }

                    @Override
                    public void onError(Exception exception) {
                        if (callbackContext != null) {
                            callbackContext.error(exception.getMessage());
                        }
                    }
                });
            }
        }
    });
}
Also used : AsyncRequestCallback(com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback) RestRequest(com.salesforce.androidsdk.rest.RestRequest) 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) NoNetworkException(com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)

Aggregations

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