810f3eba060df40fdad5116a6ec00b0cacb24dbc
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / accounts / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.oc_framework.accounts;
20
21 import com.owncloud.android.oc_framework.utils.OwnCloudVersion;
22
23 import android.accounts.Account;
24 import android.accounts.AccountManager;
25 import android.accounts.AccountsException;
26 import android.content.Context;
27
28 public class AccountUtils {
29 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
30 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
31 public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
32 private static final String ODAV_PATH = "/remote.php/odav";
33 private static final String SAML_SSO_PATH = "/remote.php/webdav";
34 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
35 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
36 public static final String STATUS_PATH = "/status.php";
37
38 /**
39 *
40 * @param version version of owncloud
41 * @return webdav path for given OC version, null if OC version unknown
42 */
43 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
44 if (version != null) {
45 if (supportsOAuth) {
46 return ODAV_PATH;
47 }
48 if (supportsSamlSso) {
49 return SAML_SSO_PATH;
50 }
51 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
52 return WEBDAV_PATH_4_0;
53 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
54 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
55 return WEBDAV_PATH_2_0;
56 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
57 return WEBDAV_PATH_1_2;
58 }
59 return null;
60 }
61
62 // /**
63 // * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
64 // * according to its version and the authorization method used.
65 // *
66 // * @param version Version of ownCloud server.
67 // * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
68 // * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
69 // */
70 // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
71 // if (version != null) {
72 // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
73 // return ODAV_PATH;
74 // }
75 // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
76 // return SAML_SSO_PATH;
77 // }
78 // if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
79 // return WEBDAV_PATH_4_0;
80 // if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
81 // || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
82 // return WEBDAV_PATH_2_0;
83 // if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
84 // return WEBDAV_PATH_1_2;
85 // }
86 // return null;
87 // }
88
89 /**
90 * Constructs full url to host and webdav resource basing on host version
91 * @param context
92 * @param account
93 * @return url or null on failure
94 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
95 */
96 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
97 AccountManager ama = AccountManager.get(context);
98 String baseurl = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_BASE_URL);
99 String strver = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_VERSION);
100 boolean supportsOAuth = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null);
101 boolean supportsSamlSso = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
102 OwnCloudVersion ver = new OwnCloudVersion(strver);
103 String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
104
105 if (baseurl == null || webdavpath == null)
106 throw new AccountNotFoundException(account, "Account not found", null);
107
108 return baseurl + webdavpath;
109 }
110
111
112 public static class AccountNotFoundException extends AccountsException {
113
114 /** Generated - should be refreshed every time the class changes!! */
115 private static final long serialVersionUID = -9013287181793186830L;
116
117 private Account mFailedAccount;
118
119 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
120 super(message, cause);
121 mFailedAccount = failedAccount;
122 }
123
124 public Account getFailedAccount() {
125 return mFailedAccount;
126 }
127 }
128
129 }