Search in sources :

Example 61 with RemoteOperationResult

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

the class DocumentsStorageProvider method copyDocument.

@Override
public String copyDocument(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
    Log.d(TAG, "copyDocument(), id=" + sourceDocumentId);
    Document document = toDocument(sourceDocumentId);
    FileDataStorageManager storageManager = document.getStorageManager();
    Document targetFolder = toDocument(targetParentDocumentId);
    RemoteOperationResult result = new CopyFileOperation(document.getRemotePath(), targetFolder.getRemotePath(), document.getStorageManager()).execute(document.getClient());
    if (!result.isSuccess()) {
        Log_OC.e(TAG, result.toString());
        throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId + " to " + targetParentDocumentId);
    }
    Context context = getNonNullContext();
    User user = document.getUser();
    RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), false, false, true, storageManager, user, context).execute(targetFolder.getClient());
    if (!updateParent.isSuccess()) {
        Log_OC.e(TAG, updateParent.toString());
        throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId + " to " + targetParentDocumentId);
    }
    String newPath = targetFolder.getRemotePath() + document.getFile().getFileName();
    if (document.getFile().isFolder()) {
        newPath = newPath + PATH_SEPARATOR;
    }
    Document newFile = new Document(storageManager, newPath);
    context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
    return newFile.getDocumentId();
}
Also used : Context(android.content.Context) User(com.nextcloud.client.account.User) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) FileNotFoundException(java.io.FileNotFoundException) CopyFileOperation(com.owncloud.android.operations.CopyFileOperation)

Example 62 with RemoteOperationResult

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

the class CreateShareWithShareeOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    CreateShareRemoteOperation operation = new CreateShareRemoteOperation(path, shareType, shareeName, false, sharePassword, permissions);
    operation.setGetShareDetails(true);
    RemoteOperationResult result = operation.execute(client);
    if (result.isSuccess() && result.getData().size() > 0) {
        OCShare share = (OCShare) result.getData().get(0);
        // once creating share link update other information
        UpdateShareInfoOperation updateShareInfoOperation = new UpdateShareInfoOperation(share, getStorageManager());
        updateShareInfoOperation.setExpirationDateInMillis(expirationDateInMillis);
        updateShareInfoOperation.setHideFileDownload(hideFileDownload);
        updateShareInfoOperation.setNote(noteMessage);
        updateShareInfoOperation.setLabel(label);
        // execute and save the result in database
        RemoteOperationResult updateShareInfoResult = updateShareInfoOperation.execute(client);
        if (updateShareInfoResult.isSuccess() && updateShareInfoResult.getData().size() > 0) {
            OCShare shareUpdated = (OCShare) updateShareInfoResult.getData().get(0);
            updateData(shareUpdated);
        }
    }
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare) CreateShareRemoteOperation(com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation)

Example 63 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult 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 64 with RemoteOperationResult

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

the class CreateShareViaLinkOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    CreateShareRemoteOperation createOp = new CreateShareRemoteOperation(path, ShareType.PUBLIC_LINK, "", false, password, OCShare.NO_PERMISSION);
    createOp.setGetShareDetails(true);
    RemoteOperationResult result = createOp.execute(client);
    if (result.isSuccess()) {
        if (result.getData().size() > 0) {
            Object item = result.getData().get(0);
            if (item instanceof OCShare) {
                updateData((OCShare) item);
            } else {
                ArrayList<Object> data = result.getData();
                result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
                result.setData(data);
            }
        } else {
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
        }
    }
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare) CreateShareRemoteOperation(com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation)

Example 65 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult 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

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)172 OCFile (com.owncloud.android.datamodel.OCFile)48 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)24 ArrayList (java.util.ArrayList)24 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)22 IOException (java.io.IOException)22 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)18 OCShare (com.owncloud.android.lib.resources.shares.OCShare)18 File (java.io.File)18 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)17 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)16 Context (android.content.Context)15 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)12 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)12 FileNotFoundException (java.io.FileNotFoundException)12 Intent (android.content.Intent)11 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)11 Uri (android.net.Uri)10