/** * Constructor for non yet saved OC accounts. * * @param baseUri URI to the OC server to get access to. * @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials. */ public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { if (baseUri == null) { throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); } mSavedAccount = null; mSavedAccountName = null; mBaseUri = baseUri; mCredentials = credentials != null ? credentials : OwnCloudCredentialsFactory.getAnonymousCredentials(); String username = mCredentials.getUsername(); if (username != null) { mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); } }
/** * Constructs full url to host and webdav resource basing on host version * * @param context Valid Android {@link Context}, needed to access the {@link AccountManager} * @param account A stored ownCloud {@link Account} * @return Full URL to WebDAV endpoint in the server corresponding to 'account'. * @throws AccountNotFoundException When 'account' is unknown for the AccountManager */ public static String getWebDavUrlForAccount(Context context, Account account) throws AccountNotFoundException { String webDavUrlForAccount = ""; try { OwnCloudCredentials ownCloudCredentials = getCredentialsForAccount(context, account); webDavUrlForAccount = getBaseUrlForAccount(context, account) + OwnCloudClient.WEBDAV_FILES_PATH_4_0 + ownCloudCredentials.getUsername(); } catch (OperationCanceledException e) { e.printStackTrace(); } catch (AuthenticatorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return webDavUrlForAccount; }
public String getDisplayName() { if (mDisplayName != null && mDisplayName.length() > 0) { return mDisplayName; } else if (mCredentials != null) { return mCredentials.getUsername(); } else if (mSavedAccount != null) { return AccountUtils.getUsernameForAccount(mSavedAccount); } else { return null; } }
AccountNotFoundException { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); String username = AccountUtils.getUsernameForAccount(account); if (isOauth2) { String accessToken = am.blockingGetAuthToken( false); OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext); AccountUtils.restoreCookies(account, client, appContext);
throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); String username = AccountUtils.getUsernameForAccount(account); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture<Bundle> future = am.getAuthToken( AccountUtils.restoreCookies(account, client, appContext);
String accountName = account.getName(); String sessionName = account.getCredentials() == null ? "" : AccountUtils.buildAccountName( account.getBaseUri(), account.getCredentials().getAuthToken() AccountUtils.restoreCookies(accountName, client, context);
@Override public void saveAllClients(Context context, String accountType) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log_OC.d(TAG, "Saving sessions... "); } Iterator<String> accountNames = mClientsWithKnownUsername.keySet().iterator(); String accountName = null; Account account = null; while (accountNames.hasNext()) { accountName = accountNames.next(); account = new Account(accountName, accountType); AccountUtils.saveClient( mClientsWithKnownUsername.get(accountName), account, context); } if (Log.isLoggable(TAG, Log.DEBUG)) { Log_OC.d(TAG, "All sessions saved"); } }
/** * Extracts url server from the account * * @deprecated This method will be removed in version 1.0. * Use {@link #getBaseUrlForAccount(Context, Account)} * instead. * * @param context * @param account * @return url server or null on failure * @throws AccountNotFoundException When 'account' is unknown for the AccountManager */ @Deprecated public static String constructBasicURLForAccount(Context context, Account account) throws AccountNotFoundException { return getBaseUrlForAccount(context, account); }
/** * Restore the client cookies from accountName * @param accountName * @param client * @param context */ public static void restoreCookies(String accountName, OwnCloudClient client, Context context) { Log_OC.d(TAG, "Restoring cookies for " + accountName); // Account Manager AccountManager am = AccountManager.get(context.getApplicationContext()); // Get account Account account = null; Account accounts[] = am.getAccounts(); for (Account a : accounts) { if (a.name.equals(accountName)) { account = a; break; } } // Restoring cookies if (account != null) { restoreCookies(account, client, context); } }
/** * Method for deferred load of account attributes from AccountManager * * @param context * @throws AccountNotFoundException * @throws AuthenticatorException * @throws IOException * @throws OperationCanceledException */ public void loadCredentials(Context context) throws AuthenticatorException, IOException, OperationCanceledException { if (context == null) { throw new IllegalArgumentException("Parameter 'context' cannot be null"); } if (mSavedAccount != null) { mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount); } }
@Override public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { OwnCloudVersion ownCloudVersion = null; if (account.getSavedAccount() != null) { ownCloudVersion = AccountUtils.getServerVersionForAccount( account.getSavedAccount(), context ); } if (ownCloudVersion != null && ownCloudVersion.isSessionMonitoringSupported()) { return mSingleSessionManager.getClientFor(account, context); } else { return mSimpleFactoryManager.getClientFor(account, context); } }
public Uri getUserFilesWebDavUri() { return mCredentials instanceof OwnCloudAnonymousCredentials ? Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0) : Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0 + AccountUtils.getUserId( mAccount.getSavedAccount(), getContext() ) ); }
/** * Constructs full url to host and webdav resource basing on host version * * @deprecated To be removed in release 1.0. * * @param context * @param account * @return url or null on failure * @throws AccountNotFoundException When 'account' is unknown for the AccountManager */ @Deprecated public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException { AccountManager ama = AccountManager.get(context); String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL); String version = ama.getUserData(account, Constants.KEY_OC_VERSION); boolean supportsOAuth = (ama.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2) != null); boolean supportsSamlSso = (ama.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null); String webdavpath = getWebdavPath(supportsOAuth, supportsSamlSso); if (baseurl == null || webdavpath == null) throw new AccountNotFoundException(account, "Account not found", null); return baseurl + webdavpath; }
throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); String username = AccountUtils.getUsernameForAccount(account); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture<Bundle> future = am.getAuthToken( OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext); client.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( AccountUtils.restoreCookies(account, client, appContext);
@Override public void saveAllClients(Context context, String accountType) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log_OC.d(TAG, "Saving sessions... "); } Iterator<String> accountNames = mClientsWithKnownUsername.keySet().iterator(); String accountName; Account account; while (accountNames.hasNext()) { accountName = accountNames.next(); account = new Account(accountName, accountType); AccountUtils.saveClient( mClientsWithKnownUsername.get(accountName), account, context); } if (Log.isLoggable(TAG, Log.DEBUG)) { Log_OC.d(TAG, "All sessions saved"); } }
/** * Constructor for already saved OC accounts. * * Do not use for anonymous credentials. */ public OwnCloudAccount(Account savedAccount, Context context) throws AccountNotFoundException { if (savedAccount == null) { throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); } if (context == null) { throw new IllegalArgumentException("Parameter 'context' cannot be null"); } mSavedAccount = savedAccount; mSavedAccountName = savedAccount.name; mCredentials = null; // load of credentials is delayed AccountManager ama = AccountManager.get(context.getApplicationContext()); String baseUrl = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_OC_BASE_URL); if (baseUrl == null ) { throw new AccountNotFoundException(mSavedAccount, "Account not found", null); } mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount)); mDisplayName = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME); }
private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) { AccountManager am = AccountManager.get(context.getApplicationContext()); if (am != null && account.getSavedAccount() != null) { String recentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES); String previousCookies = reusedClient.getCookiesString(); if (recentCookies != null && previousCookies != "" && !recentCookies.equals(previousCookies)) { AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context); } } }
/** * Method for deferred load of account attributes from AccountManager * * @param context * @throws AuthenticatorException * @throws IOException * @throws OperationCanceledException */ public void loadCredentials(Context context) throws AuthenticatorException, IOException, OperationCanceledException { if (context == null) { throw new IllegalArgumentException("Parameter 'context' cannot be null"); } if (mSavedAccount != null) { mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount); } }
public Uri getUploadsWebDavUri() { return mCredentials instanceof OwnCloudAnonymousCredentials ? Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0) : Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0 + AccountUtils.getUserId( mAccount.getSavedAccount(), getContext() ) ); }
/** * Constructor for non yet saved OC accounts. * * @param baseUri URI to the OC server to get access to. * @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials. */ public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { if (baseUri == null) { throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); } mSavedAccount = null; mSavedAccountName = null; mBaseUri = baseUri; mCredentials = credentials != null ? credentials : OwnCloudCredentialsFactory.getAnonymousCredentials(); String username = mCredentials.getUsername(); if (username != null) { mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); } }