79983814d3fbe42b1dd05191ba708d7cdc95e7ae
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / lib / accounts / AccountUtils.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 * Copyright (C) 2012 Bartek Przybylski
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26 package com.owncloud.android.lib.accounts;
27
28 import com.owncloud.android.lib.utils.OwnCloudVersion;
29
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.accounts.AccountsException;
33 import android.content.Context;
34
35 public class AccountUtils {
36 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
37 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
38 public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
39 private static final String ODAV_PATH = "/remote.php/odav";
40 private static final String SAML_SSO_PATH = "/remote.php/webdav";
41 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
42 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
43 public static final String STATUS_PATH = "/status.php";
44
45 /**
46 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
47 * according to its version and the authorization method used.
48 *
49 * @param version Version of ownCloud server.
50 * @param supportsOAuth If true, access with OAuth 2 authorization is considered.
51 * @param supportsSamlSso If true, and supportsOAuth is false, access with SAML-based single-sign-on is considered.
52 * @return WebDAV path for given OC version, null if OC version unknown
53 */
54 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
55 if (version != null) {
56 if (supportsOAuth) {
57 return ODAV_PATH;
58 }
59 if (supportsSamlSso) {
60 return SAML_SSO_PATH;
61 }
62 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
63 return WEBDAV_PATH_4_0;
64 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
65 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
66 return WEBDAV_PATH_2_0;
67 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
68 return WEBDAV_PATH_1_2;
69 }
70 return null;
71 }
72
73 /**
74 * Constructs full url to host and webdav resource basing on host version
75 * @param context
76 * @param account
77 * @return url or null on failure
78 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
79 */
80 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
81 AccountManager ama = AccountManager.get(context);
82 String baseurl = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_BASE_URL);
83 String strver = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_VERSION);
84 boolean supportsOAuth = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null);
85 boolean supportsSamlSso = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
86 OwnCloudVersion ver = new OwnCloudVersion(strver);
87 String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
88
89 if (baseurl == null || webdavpath == null)
90 throw new AccountNotFoundException(account, "Account not found", null);
91
92 return baseurl + webdavpath;
93 }
94
95
96 public static class AccountNotFoundException extends AccountsException {
97
98 /** Generated - should be refreshed every time the class changes!! */
99 private static final long serialVersionUID = -1684392454798508693L;
100
101 private Account mFailedAccount;
102
103 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
104 super(message, cause);
105 mFailedAccount = failedAccount;
106 }
107
108 public Account getFailedAccount() {
109 return mFailedAccount;
110 }
111 }
112 }