Search in sources :

Example 1 with ExistenceCheckRemoteOperation

use of com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation in project android by owncloud.

the class AuthenticatorAsyncTask method doInBackground.

@Override
protected RemoteOperationResult doInBackground(Object... params) {
    RemoteOperationResult result;
    if (params != null && params.length == 2) {
        String url = (String) params[0];
        OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
        // Client
        Uri uri = Uri.parse(url);
        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
        client.setCredentials(credentials);
        // Operation - try credentials
        ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH, mContext, SUCCESS_IF_ABSENT);
        result = operation.execute(client);
        String targetUrlAfterPermanentRedirection = null;
        if (operation.wasRedirected()) {
            RedirectionPath redirectionPath = operation.getRedirectionPath();
            targetUrlAfterPermanentRedirection = redirectionPath.getLastPermanentLocation();
        }
        // Operation - get display name
        if (result.isSuccess()) {
            GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
            if (targetUrlAfterPermanentRedirection != null) {
                // we can't assume that any subpath of the domain is correctly redirected; ugly stuff
                client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(AccountUtils.trimWebdavSuffix(targetUrlAfterPermanentRedirection)), mContext, true);
                client.setCredentials(credentials);
            }
            result = remoteUserNameOperation.execute(client);
        }
        // let the caller knows what is real URL that should be accessed for the account
        // being authenticated if the initial URL is being redirected permanently (HTTP code 301)
        result.setLastPermanentLocation(targetUrlAfterPermanentRedirection);
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return result;
}
Also used : RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 2 with ExistenceCheckRemoteOperation

use of com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation in project android by nextcloud.

the class FileDisplayActivityIT method showShares.

@Test
public // @ScreenshotTest // todo run without real server
void showShares() {
    assertTrue(new ExistenceCheckRemoteOperation("/shareToAdmin/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/shareToAdmin/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/shareToGroup/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/shareViaLink/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/noShare/", true).execute(client).isSuccess());
    // assertTrue(new CreateFolderRemoteOperation("/shareToCircle/", true).execute(client).isSuccess());
    // share folder to user "admin"
    assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", ShareType.USER, "admin", false, "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(client).isSuccess());
    // share folder via public link
    assertTrue(new CreateShareRemoteOperation("/shareViaLink/", ShareType.PUBLIC_LINK, "", true, "", OCShare.READ_PERMISSION_FLAG).execute(client).isSuccess());
    // share folder to group
    Assert.assertTrue(new CreateShareRemoteOperation("/shareToGroup/", ShareType.GROUP, "users", false, "", OCShare.NO_PERMISSION).execute(client).isSuccess());
    // share folder to circle
    // get share
    // RemoteOperationResult searchResult = new GetShareesRemoteOperation("publicCircle", 1, 50).execute(client);
    // assertTrue(searchResult.getLogMessage(), searchResult.isSuccess());
    // 
    // JSONObject resultJson = (JSONObject) searchResult.getData().get(0);
    // String circleId = resultJson.getJSONObject("value").getString("shareWith");
    // 
    // assertTrue(new CreateShareRemoteOperation("/shareToCircle/",
    // ShareType.CIRCLE,
    // circleId,
    // false,
    // "",
    // OCShare.DEFAULT_PERMISSION)
    // .execute(client).isSuccess());
    Activity sut = activityRule.launchActivity(null);
    getInstrumentation().waitForIdleSync();
    EventBus.getDefault().post(new SearchEvent("", SearchRemoteOperation.SearchType.SHARED_FILTER));
    shortSleep();
    shortSleep();
    screenshot(sut);
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) FileDisplayActivity(com.owncloud.android.ui.activity.FileDisplayActivity) Activity(android.app.Activity) SearchEvent(com.owncloud.android.ui.events.SearchEvent) CreateShareRemoteOperation(com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation) Test(org.junit.Test)

Example 3 with ExistenceCheckRemoteOperation

use of com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation in project android by nextcloud.

the class AuthenticatorAsyncTask method doInBackground.

@Override
protected RemoteOperationResult<UserInfo> doInBackground(Object... params) {
    RemoteOperationResult<UserInfo> result;
    if (params != null && params.length == 2 && mWeakContext.get() != null) {
        String url = (String) params[0];
        Context context = mWeakContext.get();
        OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
        // Client
        Uri uri = Uri.parse(url);
        NextcloudClient nextcloudClient = OwnCloudClientFactory.createNextcloudClient(uri, credentials.getUsername(), credentials.toOkHttpCredentials(), context, true);
        // Operation - get display name
        RemoteOperationResult<UserInfo> userInfoResult = new GetUserInfoRemoteOperation().execute(nextcloudClient);
        // Operation - try credentials
        if (userInfoResult.isSuccess()) {
            OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true);
            client.setUserId(userInfoResult.getResultData().getId());
            client.setCredentials(credentials);
            ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(ROOT_PATH, SUCCESS_IF_ABSENT);
            result = operation.execute(client);
            if (operation.wasRedirected()) {
                RedirectionPath redirectionPath = operation.getRedirectionPath();
                String permanentLocation = redirectionPath.getLastPermanentLocation();
                result.setLastPermanentLocation(permanentLocation);
            }
            result.setResultData(userInfoResult.getResultData());
        } else {
            result = userInfoResult;
        }
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return result;
}
Also used : Context(android.content.Context) RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) NextcloudClient(com.nextcloud.common.NextcloudClient) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UserInfo(com.owncloud.android.lib.common.UserInfo) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 4 with ExistenceCheckRemoteOperation

use of com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation 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 5 with ExistenceCheckRemoteOperation

use of com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation 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

ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)11 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)10 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)4 ArrayList (java.util.ArrayList)3 Uri (android.net.Uri)2 OCFile (com.owncloud.android.datamodel.OCFile)2 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)2 OwnCloudCredentials (com.owncloud.android.lib.common.OwnCloudCredentials)2 RedirectionPath (com.owncloud.android.lib.common.network.RedirectionPath)2 Activity (android.app.Activity)1 Context (android.content.Context)1 NextcloudClient (com.nextcloud.common.NextcloudClient)1 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 UserInfo (com.owncloud.android.lib.common.UserInfo)1 UnlockFileRemoteOperation (com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation)1 ChunkedFileUploadRemoteOperation (com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation)1 CreateFolderRemoteOperation (com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation)1 ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)1 UploadFileRemoteOperation (com.owncloud.android.lib.resources.files.UploadFileRemoteOperation)1 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)1