Search in sources :

Example 11 with RemoteOperation

use of com.owncloud.android.lib.common.operations.RemoteOperation in project android by nextcloud.

the class UploadFileOperation method grantFolderExistence.

/**
 * Checks the existence of the folder where the current file will be uploaded both
 * in the remote server and in the local database.
 * <p/>
 * If the upload is set to enforce the creation of the folder, the method tries to
 * create it both remote and locally.
 *
 * @param pathToGrant Full remote path whose existence will be granted.
 * @return An {@link OCFile} instance corresponding to the folder where the file
 * will be uploaded.
 */
private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudClient client) {
    RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, false);
    RemoteOperationResult result = operation.execute(client);
    if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
        SyncOperation syncOp = new CreateFolderOperation(pathToGrant, user, getContext(), getStorageManager());
        result = syncOp.execute(client);
    }
    if (result.isSuccess()) {
        OCFile parentDir = getStorageManager().getFileByPath(pathToGrant);
        if (parentDir == null) {
            parentDir = createLocalFolder(pathToGrant);
        }
        if (parentDir != null) {
            result = new RemoteOperationResult(ResultCode.OK);
        } else {
            result = new RemoteOperationResult(ResultCode.CANNOT_CREATE_FILE);
        }
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ChunkedFileUploadRemoteOperation(com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) UnlockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) SyncOperation(com.owncloud.android.operations.common.SyncOperation)

Example 12 with RemoteOperation

use of com.owncloud.android.lib.common.operations.RemoteOperation in project android by nextcloud.

the class OperationsService method onStartCommand.

/**
 * Entry point to add a new operation to the queue of operations.
 * <p/>
 * New operations are added calling to startService(), resulting in a call to this method. This ensures the service
 * will keep on working although the caller activity goes away.
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log_OC.d(TAG, "Starting command with id " + startId);
    // the rest of the operations are requested through the Binder
    if (intent != null && ACTION_SYNC_FOLDER.equals(intent.getAction())) {
        if (!intent.hasExtra(EXTRA_ACCOUNT) || !intent.hasExtra(EXTRA_REMOTE_PATH)) {
            Log_OC.e(TAG, "Not enough information provided in intent");
            return START_NOT_STICKY;
        }
        Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
        String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
        Pair<Account, String> itemSyncKey = new Pair<>(account, remotePath);
        Pair<Target, RemoteOperation> itemToQueue = newOperation(intent);
        if (itemToQueue != null) {
            mSyncFolderHandler.add(account, remotePath, (SynchronizeFolderOperation) itemToQueue.second);
            Message msg = mSyncFolderHandler.obtainMessage();
            msg.arg1 = startId;
            msg.obj = itemSyncKey;
            mSyncFolderHandler.sendMessage(msg);
        }
    } else {
        Message msg = mOperationsHandler.obtainMessage();
        msg.arg1 = startId;
        mOperationsHandler.sendMessage(msg);
    }
    return START_NOT_STICKY;
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) RestoreFileVersionRemoteOperation(com.owncloud.android.lib.resources.files.RestoreFileVersionRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) Message(android.os.Message) Pair(android.util.Pair)

Example 13 with RemoteOperation

use of com.owncloud.android.lib.common.operations.RemoteOperation in project android by nextcloud.

the class UpdateSharePermissionsOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // ShareType.USER | ShareType.GROUP
    OCShare share = getStorageManager().getShareById(shareId);
    if (share == null) {
        // TODO try to get remote share before failing?
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
    }
    path = share.getPath();
    // Update remote share with password
    UpdateShareRemoteOperation updateOp = new UpdateShareRemoteOperation(share.getRemoteId());
    updateOp.setPassword(password);
    updateOp.setPermissions(permissions);
    updateOp.setExpirationDate(expirationDateInMillis);
    RemoteOperationResult result = updateOp.execute(client);
    if (result.isSuccess()) {
        RemoteOperation getShareOp = new GetShareRemoteOperation(share.getRemoteId());
        result = getShareOp.execute(client);
        if (result.isSuccess()) {
            share = (OCShare) result.getData().get(0);
            // TODO check permissions are being saved
            updateData(share);
        }
    }
    return result;
}
Also used : UpdateShareRemoteOperation(com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation) GetShareRemoteOperation(com.owncloud.android.lib.resources.shares.GetShareRemoteOperation) UpdateShareRemoteOperation(com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetShareRemoteOperation(com.owncloud.android.lib.resources.shares.GetShareRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare)

Example 14 with RemoteOperation

use of com.owncloud.android.lib.common.operations.RemoteOperation in project android by nextcloud.

the class UpdateShareInfoOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    OCShare share;
    if (shareId > 0) {
        share = getStorageManager().getShareById(shareId);
    } else {
        share = this.share;
    }
    if (share == null) {
        // TODO try to get remote share before failing?
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
    }
    // Update remote share
    UpdateShareRemoteOperation updateOp = new UpdateShareRemoteOperation(share.getRemoteId());
    updateOp.setExpirationDate(expirationDateInMillis);
    updateOp.setHideFileDownload(hideFileDownload);
    if (!TextUtils.isEmpty(note)) {
        updateOp.setNote(note);
    }
    if (permissions > -1) {
        updateOp.setPermissions(permissions);
    }
    updateOp.setPassword(password);
    updateOp.setLabel(label);
    RemoteOperationResult result = updateOp.execute(client);
    if (result.isSuccess()) {
        RemoteOperation getShareOp = new GetShareRemoteOperation(share.getRemoteId());
        result = getShareOp.execute(client);
        // this will be triggered by editing existing share
        if (result.isSuccess() && shareId > 0) {
            OCShare ocShare = (OCShare) result.getData().get(0);
            ocShare.setPasswordProtected(!TextUtils.isEmpty(password));
            getStorageManager().saveShare(ocShare);
        }
    }
    return result;
}
Also used : UpdateShareRemoteOperation(com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation) GetShareRemoteOperation(com.owncloud.android.lib.resources.shares.GetShareRemoteOperation) UpdateShareRemoteOperation(com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetShareRemoteOperation(com.owncloud.android.lib.resources.shares.GetShareRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare)

Example 15 with RemoteOperation

use of com.owncloud.android.lib.common.operations.RemoteOperation in project android by nextcloud.

the class DetectAuthenticationMethodOperation method run.

/**
 *  Performs the operation.
 *
 *  Triggers a check of existence on the root folder of the server, granting
 *  that the request is not authenticated.
 *
 *  Analyzes the result of check to find out what authentication method, if
 *  any, is requested by the server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
    RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
    client.clearCredentials();
    client.setFollowRedirects(false);
    // try to access the root folder, following redirections but not SAML SSO redirections
    result = operation.execute(client);
    String redirectedLocation = result.getRedirectedLocation();
    while (!TextUtils.isEmpty(redirectedLocation) && !result.isIdPRedirection()) {
        client.setBaseUri(Uri.parse(result.getRedirectedLocation()));
        result = operation.execute(client);
        redirectedLocation = result.getRedirectedLocation();
    }
    // analyze response
    if (result.getHttpCode() == HttpStatus.SC_UNAUTHORIZED || result.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
        ArrayList<String> authHeaders = result.getAuthenticateHeaders();
        for (String header : authHeaders) {
            // currently we only support basic auth
            if (header.toLowerCase(Locale.ROOT).contains("basic")) {
                authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
                break;
            }
        }
    // else - fall back to UNKNOWN
    } else if (result.isSuccess()) {
        authMethod = AuthenticationMethod.NONE;
    } else if (result.isIdPRedirection()) {
        authMethod = AuthenticationMethod.SAML_WEB_SSO;
    }
    // else - fall back to UNKNOWN
    Log_OC.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
    if (authMethod != AuthenticationMethod.UNKNOWN) {
        result = new RemoteOperationResult(true, result.getHttpCode(), result.getHttpPhrase(), new Header[0]);
    }
    ArrayList<Object> data = new ArrayList<>();
    data.add(authMethod);
    result.setData(data);
    // same result instance, so that other errors
    return result;
// can be handled by the caller transparently
}
Also used : ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) Header(org.apache.commons.httpclient.Header) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList)

Aggregations

RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)31 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)24 Account (android.accounts.Account)7 OCFile (com.owncloud.android.datamodel.OCFile)7 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)7 Pair (android.util.Pair)6 OCShare (com.owncloud.android.lib.resources.shares.OCShare)6 ArrayList (java.util.ArrayList)6 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)5 Handler (android.os.Handler)4 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)4 GetUserInfoRemoteOperation (com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation)4 User (com.nextcloud.client.account.User)3 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)3 ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)3 RestoreFileVersionRemoteOperation (com.owncloud.android.lib.resources.files.RestoreFileVersionRemoteOperation)3 SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)3 GetShareRemoteOperation (com.owncloud.android.lib.resources.shares.GetShareRemoteOperation)3 UpdateShareRemoteOperation (com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation)3 AuthenticatorException (android.accounts.AuthenticatorException)2