1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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/>.
18 package com
.owncloud
.android
.utils
;
20 import org
.apache
.commons
.httpclient
.MultiThreadedHttpConnectionManager
;
21 import org
.apache
.commons
.httpclient
.protocol
.Protocol
;
23 import com
.owncloud
.android
.AccountUtils
;
24 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
25 import com
.owncloud
.android
.authenticator
.EasySSLSocketFactory
;
26 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
28 import eu
.alefzero
.webdav
.WebdavClient
;
30 import android
.accounts
.Account
;
31 import android
.accounts
.AccountManager
;
32 import android
.content
.Context
;
33 import android
.net
.Uri
;
34 import android
.util
.Log
;
36 public class OwnCloudClientUtils
{
38 final private static String TAG
= "OwnCloudClientFactory";
40 /** Default timeout for waiting data from the server */
41 public static final int DEFAULT_DATA_TIMEOUT
= 20000;
43 /** Default timeout for establishing a connection */
44 public static final int DEFAULT_CONNECTION_TIMEOUT
= 60000;
46 /** Connection manager for all the WebdavClients */
47 static private MultiThreadedHttpConnectionManager mConnManager
= null
;
51 * Creates a WebdavClient setup for an ownCloud account
53 * @param account The ownCloud account
54 * @param context The application context
55 * @return A WebdavClient object ready to be used
57 public static WebdavClient
createOwnCloudClient (Account account
, Context context
) {
58 Log
.d(TAG
, "Creating WebdavClient associated to " + account
.name
);
60 String baseUrl
= AccountManager
.get(context
).getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
61 OwnCloudVersion ownCloudVersion
= new OwnCloudVersion(AccountManager
.get(context
).getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
62 String webDavPath
= AccountUtils
.getWebdavPath(ownCloudVersion
);
64 WebdavClient client
= createOwnCloudClient(Uri
.parse(baseUrl
+ webDavPath
));
66 String username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
67 String password
= AccountManager
.get(context
).getPassword(account
);
68 //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
70 client
.setCredentials(username
, password
);
77 * Creates a WebdavClient to try a new account before saving it
79 * @param uri URL to the ownCloud server
80 * @param username User name
81 * @param password User password
82 * @return A WebdavClient object ready to be used
84 public static WebdavClient
createOwnCloudClient(Uri uri
, String username
, String password
) {
85 Log
.d(TAG
, "Creating WebdavClient for " + username
+ "@" + uri
);
87 WebdavClient client
= createOwnCloudClient(uri
);
89 client
.setCredentials(username
, password
);
96 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
98 * @param uri URL to the ownCloud server
99 * @return A WebdavClient object ready to be used
101 public static WebdavClient
createOwnCloudClient(Uri uri
) {
102 Log
.d(TAG
, "Creating WebdavClient for " + uri
);
104 allowSelfsignedCertificates(true
);
106 WebdavClient client
= new WebdavClient(getMultiThreadedConnManager());
108 allowSelfsignedCertificates(true
);
109 client
.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT
, DEFAULT_CONNECTION_TIMEOUT
);
110 client
.setBaseUri(uri
);
117 * Allows or disallows self-signed certificates in ownCloud servers to reach
119 * @param allow 'True' to allow, 'false' to disallow
121 public static void allowSelfsignedCertificates(boolean allow
) {
124 pr
= Protocol
.getProtocol("https");
125 } catch (IllegalStateException e
) {
126 // nothing to do here; really
128 boolean isAllowed
= (pr
!= null
&& pr
.getSocketFactory() instanceof EasySSLSocketFactory
);
129 if (allow
&& !isAllowed
) {
130 Protocol
.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
131 } else if (!allow
&& isAllowed
) {
132 // TODO - a more strict SocketFactory object should be provided here
138 static private MultiThreadedHttpConnectionManager
getMultiThreadedConnManager() {
139 if (mConnManager
== null
) {
140 mConnManager
= new MultiThreadedHttpConnectionManager();
141 mConnManager
.getParams().setDefaultMaxConnectionsPerHost(5);
142 mConnManager
.getParams().setMaxTotalConnections(5);