Search in sources :

Example 1 with TokenMismatchException

use of com.nextcloud.android.sso.exceptions.TokenMismatchException in project nextcloud-notes by stefan-niedermann.

the class NotesServerSyncTask method pushLocalChanges.

/**
 * Push local changes: for each locally created/edited/deleted Note, use NotesClient in order to push the changed to the server.
 */
private boolean pushLocalChanges() {
    Log.d(TAG, "pushLocalChanges()");
    boolean success = true;
    final var notes = repo.getLocalModifiedNotes(localAccount.getId());
    for (Note note : notes) {
        Log.d(TAG, "   Process Local Note: " + (BuildConfig.DEBUG ? note : note.getTitle()));
        try {
            Note remoteNote;
            switch(note.getStatus()) {
                case LOCAL_EDITED:
                    Log.v(TAG, "   ...create/edit");
                    if (note.getRemoteId() != null) {
                        Log.v(TAG, "   ...Note has remoteId → try to edit");
                        final var editResponse = notesAPI.editNote(note).execute();
                        if (editResponse.isSuccessful()) {
                            remoteNote = editResponse.body();
                            if (remoteNote == null) {
                                Log.e(TAG, "   ...Tried to edit \"" + note.getTitle() + "\" (#" + note.getId() + ") but the server response was null.");
                                throw new Exception("Server returned null after editing \"" + note.getTitle() + "\" (#" + note.getId() + ")");
                            }
                        } else if (editResponse.code() == HTTP_NOT_FOUND) {
                            Log.v(TAG, "   ...Note does no longer exist on server → recreate");
                            final var createResponse = notesAPI.createNote(note).execute();
                            if (createResponse.isSuccessful()) {
                                remoteNote = createResponse.body();
                                if (remoteNote == null) {
                                    Log.e(TAG, "   ...Tried to recreate \"" + note.getTitle() + "\" (#" + note.getId() + ") but the server response was null.");
                                    throw new Exception("Server returned null after recreating \"" + note.getTitle() + "\" (#" + note.getId() + ")");
                                }
                            } else {
                                throw new Exception(createResponse.message());
                            }
                        } else {
                            throw new Exception(editResponse.message());
                        }
                    } else {
                        Log.v(TAG, "   ...Note does not have a remoteId yet → create");
                        final var createResponse = notesAPI.createNote(note).execute();
                        if (createResponse.isSuccessful()) {
                            remoteNote = createResponse.body();
                            if (remoteNote == null) {
                                Log.e(TAG, "   ...Tried to create \"" + note.getTitle() + "\" (#" + note.getId() + ") but the server response was null.");
                                throw new Exception("Server returned null after creating \"" + note.getTitle() + "\" (#" + note.getId() + ")");
                            }
                            repo.updateRemoteId(note.getId(), remoteNote.getRemoteId());
                        } else {
                            throw new Exception(createResponse.message());
                        }
                    }
                    // Please note, that db.updateNote() realized an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
                    repo.updateIfNotModifiedLocallyDuringSync(note.getId(), remoteNote.getModified().getTimeInMillis(), remoteNote.getTitle(), remoteNote.getFavorite(), remoteNote.getETag(), remoteNote.getContent(), generateNoteExcerpt(remoteNote.getContent(), remoteNote.getTitle()), note.getContent(), note.getCategory(), note.getFavorite());
                    break;
                case LOCAL_DELETED:
                    if (note.getRemoteId() == null) {
                        Log.v(TAG, "   ...delete (only local, since it has never been synchronized)");
                    } else {
                        Log.v(TAG, "   ...delete (from server and local)");
                        final var deleteResponse = notesAPI.deleteNote(note.getRemoteId()).execute();
                        if (!deleteResponse.isSuccessful()) {
                            if (deleteResponse.code() == HTTP_NOT_FOUND) {
                                Log.v(TAG, "   ...delete (note has already been deleted remotely)");
                            } else {
                                throw new Exception(deleteResponse.message());
                            }
                        }
                    }
                    // Please note, that db.deleteNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
                    repo.deleteByNoteId(note.getId(), LOCAL_DELETED);
                    break;
                default:
                    throw new IllegalStateException("Unknown State of Note " + note + ": " + note.getStatus());
            }
        } catch (NextcloudHttpRequestFailedException e) {
            if (e.getStatusCode() == HTTP_NOT_MODIFIED) {
                Log.d(TAG, "Server returned HTTP Status Code 304 - Not Modified");
            } else {
                exceptions.add(e);
                success = false;
            }
        } catch (Exception e) {
            if (e instanceof TokenMismatchException) {
                apiProvider.invalidateAPICache(ssoAccount);
            }
            exceptions.add(e);
            success = false;
        }
    }
    return success;
}
Also used : TokenMismatchException(com.nextcloud.android.sso.exceptions.TokenMismatchException) Note(it.niedermann.owncloud.notes.persistence.entity.Note) NextcloudHttpRequestFailedException(com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException) NextcloudApiNotRespondingException(com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException) TokenMismatchException(com.nextcloud.android.sso.exceptions.TokenMismatchException) NextcloudHttpRequestFailedException(com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException)

Example 2 with TokenMismatchException

use of com.nextcloud.android.sso.exceptions.TokenMismatchException in project nextcloud-notes by stefan-niedermann.

the class TipsAdapter method setThrowables.

public void setThrowables(@NonNull List<Throwable> throwables) {
    for (final var throwable : throwables) {
        if (throwable instanceof TokenMismatchException) {
            add(R.string.error_dialog_tip_token_mismatch_retry);
            add(R.string.error_dialog_tip_token_mismatch_clear_storage);
            final var intent = new Intent(ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:" + BuildConfig.APPLICATION_ID)).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_deck_info);
            add(R.string.error_dialog_tip_clear_storage, intent);
        } else if (throwable instanceof NextcloudFilesAppNotSupportedException) {
            add(R.string.error_dialog_tip_files_outdated);
        } else if (throwable instanceof NextcloudApiNotRespondingException) {
            if (VERSION.SDK_INT >= VERSION_CODES.M) {
                add(R.string.error_dialog_tip_disable_battery_optimizations, new Intent().setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_battery_settings));
            } else {
                add(R.string.error_dialog_tip_disable_battery_optimizations);
            }
            add(R.string.error_dialog_tip_files_force_stop);
            add(R.string.error_dialog_tip_files_delete_storage);
            final var intent = new Intent(ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:" + BuildConfig.APPLICATION_ID)).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_deck_info);
            add(R.string.error_dialog_tip_clear_storage, intent);
        } else if (throwable instanceof SocketTimeoutException || throwable instanceof ConnectException) {
            add(R.string.error_dialog_timeout_instance);
            add(R.string.error_dialog_timeout_toggle, new Intent(Settings.ACTION_WIFI_SETTINGS).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_network));
        } else if (throwable instanceof JSONException || throwable instanceof NullPointerException) {
            add(R.string.error_dialog_check_server);
        } else if (throwable instanceof NextcloudHttpRequestFailedException) {
            final int statusCode = ((NextcloudHttpRequestFailedException) throwable).getStatusCode();
            switch(statusCode) {
                case 302:
                    add(R.string.error_dialog_server_app_enabled);
                    add(R.string.error_dialog_redirect);
                    break;
                case 500:
                    add(R.string.error_dialog_check_server_logs);
                    break;
                case 503:
                    add(R.string.error_dialog_check_maintenance);
                    break;
                case 507:
                    add(R.string.error_dialog_insufficient_storage);
                    break;
            }
        } else if (throwable instanceof UnknownErrorException) {
            if ("com.nextcloud.android.sso.QueryParam".equals(throwable.getMessage())) {
                add(R.string.error_dialog_min_version, new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.nextcloud.client")).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_update_files_app));
            }
        }
    }
    notifyDataSetChanged();
}
Also used : TokenMismatchException(com.nextcloud.android.sso.exceptions.TokenMismatchException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownErrorException(com.nextcloud.android.sso.exceptions.UnknownErrorException) NextcloudApiNotRespondingException(com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException) NextcloudHttpRequestFailedException(com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException) JSONException(org.json.JSONException) Intent(android.content.Intent) NextcloudFilesAppNotSupportedException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotSupportedException) ConnectException(java.net.ConnectException)

Aggregations

NextcloudApiNotRespondingException (com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException)2 NextcloudHttpRequestFailedException (com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException)2 TokenMismatchException (com.nextcloud.android.sso.exceptions.TokenMismatchException)2 Intent (android.content.Intent)1 NextcloudFilesAppAccountNotFoundException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)1 NextcloudFilesAppNotSupportedException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotSupportedException)1 UnknownErrorException (com.nextcloud.android.sso.exceptions.UnknownErrorException)1 Note (it.niedermann.owncloud.notes.persistence.entity.Note)1 ConnectException (java.net.ConnectException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 JSONException (org.json.JSONException)1