Search in sources :

Example 1 with AsyncRequestCallback

use of com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback in project SalesforceMobileSDK-Android by forcedotcom.

the class ExplorerActivity method sendFromUIThread.

/**
 * Sends a REST request using RestClient's sendAsync method.
 * Note: Synchronous calls are not allowed from code running on the UI thread.
 *
 * @param restRequest REST request.
 */
private void sendFromUIThread(RestRequest restRequest) {
    client.sendAsync(restRequest, new AsyncRequestCallback() {

        private long start = System.nanoTime();

        @Override
        public void onSuccess(RestRequest request, final RestResponse result) {
            // consume before going back to main thread
            result.consumeQuietly();
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        long duration = System.nanoTime() - start;
                        println(result);
                        int size = result.asString().length();
                        int statusCode = result.getStatusCode();
                        printRequestInfo(duration, size, statusCode);
                        extractIdsFromResponse(result.asString());
                    } catch (Exception e) {
                        printException(e);
                    }
                    EventsObservable.get().notifyEvent(EventType.RenditionComplete);
                }
            });
        }

        @Override
        public void onError(final Exception exception) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    printException(exception);
                    EventsObservable.get().notifyEvent(EventType.RenditionComplete);
                }
            });
        }
    });
}
Also used : AsyncRequestCallback(com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback) RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with AsyncRequestCallback

use of com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback 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 AsyncRequestCallback

use of com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback 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

AsyncRequestCallback (com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback)3 RestRequest (com.salesforce.androidsdk.rest.RestRequest)3 RestResponse (com.salesforce.androidsdk.rest.RestResponse)3 NoNetworkException (com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)2 AccountInfoNotFoundException (com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException)2 RestClientCallback (com.salesforce.androidsdk.rest.ClientManager.RestClientCallback)2 RestClient (com.salesforce.androidsdk.rest.RestClient)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1