1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.oc_framework
.accounts
;
21 import com
.owncloud
.android
.oc_framework
.utils
.OwnCloudVersion
;
23 import android
.accounts
.Account
;
24 import android
.accounts
.AccountManager
;
25 import android
.accounts
.AccountsException
;
26 import android
.content
.Context
;
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";
40 * @param version version of owncloud
41 * @return webdav path for given OC version, null if OC version unknown
43 public static String
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
, boolean supportsSamlSso
) {
44 if (version
!= null
) {
48 if (supportsSamlSso
) {
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
;
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.
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.
70 // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
71 // if (version != null) {
72 // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
75 // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
76 // return SAML_SSO_PATH;
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;
90 * Constructs full url to host and webdav resource basing on host version
93 * @return url or null on failure
94 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
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
);
105 if (baseurl
== null
|| webdavpath
== null
)
106 throw new AccountNotFoundException(account
, "Account not found", null
);
108 return baseurl
+ webdavpath
;
112 public static class AccountNotFoundException
extends AccountsException
{
114 /** Generated - should be refreshed every time the class changes!! */
115 private static final long serialVersionUID
= -9013287181793186830L;
117 private Account mFailedAccount
;
119 public AccountNotFoundException(Account failedAccount
, String message
, Throwable cause
) {
120 super(message
, cause
);
121 mFailedAccount
= failedAccount
;
124 public Account
getFailedAccount() {
125 return mFailedAccount
;