Search in sources :

Example 1 with OwnCloudClient

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

use of com.owncloud.android.lib.common.OwnCloudClient in project android by owncloud.

the class GetShareWithUsersAsyncTask method doInBackground.

@Override
protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
    GetSharesForFileOperation operation = null;
    RemoteOperationResult result = null;
    if (params != null && params.length == 3) {
        OCFile file = (OCFile) params[0];
        Account account = (Account) params[1];
        FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
        try {
            // Get shares request
            operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
            OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
            OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
            result = operation.execute(client, fileDataStorageManager);
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting shares", e);
        }
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return new Pair(operation, result);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) GetSharesForFileOperation(com.owncloud.android.operations.GetSharesForFileOperation) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Pair(android.util.Pair)

Example 3 with OwnCloudClient

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

the class GetShareWithUsersAsyncTask method doInBackground.

@Override
protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
    GetSharesForFileOperation operation = null;
    RemoteOperationResult result = null;
    if (params != null && params.length == 3) {
        OCFile file = (OCFile) params[0];
        Account account = (Account) params[1];
        FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
        try {
            // Get shares request
            operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
            OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
            OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
            result = operation.execute(client, fileDataStorageManager);
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting shares", e);
        }
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return new Pair(operation, result);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) GetSharesForFileOperation(com.owncloud.android.operations.GetSharesForFileOperation) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Pair(android.util.Pair)

Example 4 with OwnCloudClient

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

the class InputStreamBinder method processRequestV2.

private Response processRequestV2(final NextcloudRequest request, final InputStream requestBodyInputStream) throws UnsupportedOperationException, com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
    Account account = accountManager.getAccountByName(request.getAccountName());
    if (account == null) {
        throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
    }
    // Validate token
    if (!isValid(request)) {
        throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
    }
    // Validate URL
    if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
        throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL, new IllegalStateException("URL need to start with a /"));
    }
    OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
    OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
    OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
    HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
    if (request.getParameterV2() != null && !request.getParameterV2().isEmpty()) {
        method.setQueryString(convertListToNVP(request.getParameterV2()));
    } else {
        method.setQueryString(convertMapToNVP(request.getParameter()));
    }
    method.addRequestHeader("OCS-APIREQUEST", "true");
    for (Map.Entry<String, List<String>> header : request.getHeader().entrySet()) {
        // https://stackoverflow.com/a/3097052
        method.addRequestHeader(header.getKey(), TextUtils.join(",", header.getValue()));
        if ("OCS-APIREQUEST".equalsIgnoreCase(header.getKey())) {
            throw new IllegalStateException("The 'OCS-APIREQUEST' header will be automatically added by the Nextcloud SSO Library. " + "Please remove the header before making a request");
        }
    }
    client.setFollowRedirects(request.isFollowRedirects());
    int status = client.executeMethod(method);
    // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
    if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
        return new Response(method);
    } else {
        InputStream inputStream = method.getResponseBodyAsStream();
        String total = "No response body";
        // If response body is available
        if (inputStream != null) {
            total = inputStreamToString(inputStream);
            Log_OC.e(TAG, total);
        }
        method.releaseConnection();
        throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED, new IllegalStateException(String.valueOf(status), new IllegalStateException(total)));
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OwnCloudClientManager(com.owncloud.android.lib.common.OwnCloudClientManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) List(java.util.List) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Map(java.util.Map)

Example 5 with OwnCloudClient

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

the class PushUtils method deleteRegistrationForAccount.

private static void deleteRegistrationForAccount(Account account) {
    Context context = MainApp.getAppContext();
    OwnCloudAccount ocAccount;
    arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver());
    try {
        ocAccount = new OwnCloudAccount(account, context);
        OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context);
        RemoteOperation unregisterAccountDeviceForNotificationsOperation = new UnregisterAccountDeviceForNotificationsOperation();
        RemoteOperationResult remoteOperationResult = unregisterAccountDeviceForNotificationsOperation.execute(mClient);
        if (remoteOperationResult.getHttpCode() == HttpStatus.SC_ACCEPTED) {
            String arbitraryValue;
            if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account.name, KEY_PUSH))) {
                Gson gson = new Gson();
                PushConfigurationState pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
                RemoteOperationResult unregisterResult = new UnregisterAccountDeviceForProxyOperation(context.getResources().getString(R.string.push_server_url), pushArbitraryData.getDeviceIdentifier(), pushArbitraryData.getDeviceIdentifierSignature(), pushArbitraryData.getUserPublicKey()).run();
                if (unregisterResult.isSuccess()) {
                    arbitraryDataProvider.deleteKeyForAccount(account.name, KEY_PUSH);
                }
            }
        }
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        Log_OC.d(TAG, "Failed to find an account");
    } catch (AuthenticatorException e) {
        Log_OC.d(TAG, "Failed via AuthenticatorException");
    } catch (IOException e) {
        Log_OC.d(TAG, "Failed via IOException");
    } catch (OperationCanceledException e) {
        Log_OC.d(TAG, "Failed via OperationCanceledException");
    }
}
Also used : Context(android.content.Context) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UnregisterAccountDeviceForProxyOperation(com.owncloud.android.lib.resources.notifications.UnregisterAccountDeviceForProxyOperation) OperationCanceledException(android.accounts.OperationCanceledException) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) AuthenticatorException(android.accounts.AuthenticatorException) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) IOException(java.io.IOException) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) UnregisterAccountDeviceForNotificationsOperation(com.owncloud.android.lib.resources.notifications.UnregisterAccountDeviceForNotificationsOperation) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient)

Aggregations

OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)21 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)15 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)12 Account (android.accounts.Account)9 IOException (java.io.IOException)6 Context (android.content.Context)5 OwnCloudCredentials (com.owncloud.android.lib.common.OwnCloudCredentials)5 AccountManager (android.accounts.AccountManager)4 AuthenticatorException (android.accounts.AuthenticatorException)4 OperationCanceledException (android.accounts.OperationCanceledException)4 User (com.nextcloud.client.account.User)4 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)4 OCFile (com.owncloud.android.datamodel.OCFile)4 Intent (android.content.Intent)3 Uri (android.net.Uri)3 Gson (com.google.gson.Gson)3 ClientFactory (com.nextcloud.client.network.ClientFactory)3 AccountUtils (com.owncloud.android.authentication.AccountUtils)3 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)3 PushConfigurationState (com.owncloud.android.datamodel.PushConfigurationState)3