5395f917f4f5552ea0d9a0b552c25b0dcd15866a
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / accounts / AccountUtils.java
1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
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.oc_framework.accounts;
27
28 import com.owncloud.android.oc_framework.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 *
47 * @param version version of owncloud
48 * @return webdav path for given OC version, null if OC version unknown
49 */
50 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
51 if (version != null) {
52 if (supportsOAuth) {
53 return ODAV_PATH;
54 }
55 if (supportsSamlSso) {
56 return SAML_SSO_PATH;
57 }
58 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
59 return WEBDAV_PATH_4_0;
60 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
61 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
62 return WEBDAV_PATH_2_0;
63 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
64 return WEBDAV_PATH_1_2;
65 }
66 return null;
67 }
68
69 // /**
70 // * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
71 // * according to its version and the authorization method used.
72 // *
73 // * @param version Version of ownCloud server.
74 // * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
75 // * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
76 // */
77 // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
78 // if (version != null) {
79 // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
80 // return ODAV_PATH;
81 // }
82 // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
83 // return SAML_SSO_PATH;
84 // }
85 // if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
86 // return WEBDAV_PATH_4_0;
87 // if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
88 // || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
89 // return WEBDAV_PATH_2_0;
90 // if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
91 // return WEBDAV_PATH_1_2;
92 // }
93 // return null;
94 // }
95
96 /**
97 * Constructs full url to host and webdav resource basing on host version
98 * @param context
99 * @param account
100 * @return url or null on failure
101 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
102 */
103 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
104 AccountManager ama = AccountManager.get(context);
105 String baseurl = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_BASE_URL);
106 String strver = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_VERSION);
107 boolean supportsOAuth = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null);
108 boolean supportsSamlSso = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
109 OwnCloudVersion ver = new OwnCloudVersion(strver);
110 String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
111
112 if (baseurl == null || webdavpath == null)
113 throw new AccountNotFoundException(account, "Account not found", null);
114
115 return baseurl + webdavpath;
116 }
117
118
119 public static class AccountNotFoundException extends AccountsException {
120
121 /** Generated - should be refreshed every time the class changes!! */
122 private static final long serialVersionUID = -9013287181793186830L;
123
124 private Account mFailedAccount;
125
126 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
127 super(message, cause);
128 mFailedAccount = failedAccount;
129 }
130
131 public Account getFailedAccount() {
132 return mFailedAccount;
133 }
134 }
135
136 }