OwnCloudClientUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  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.
  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. package com.owncloud.android.utils;
  19. import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
  20. import org.apache.commons.httpclient.protocol.Protocol;
  21. import com.owncloud.android.AccountUtils;
  22. import com.owncloud.android.authenticator.AccountAuthenticator;
  23. import com.owncloud.android.authenticator.EasySSLSocketFactory;
  24. import com.owncloud.android.utils.OwnCloudVersion;
  25. import eu.alefzero.webdav.WebdavClient;
  26. import android.accounts.Account;
  27. import android.accounts.AccountManager;
  28. import android.content.Context;
  29. import android.net.Uri;
  30. import android.util.Log;
  31. public class OwnCloudClientUtils {
  32. final private static String TAG = "OwnCloudClientFactory";
  33. /** Default timeout for waiting data from the server */
  34. public static final int DEFAULT_DATA_TIMEOUT = 60000;
  35. /** Default timeout for establishing a connection */
  36. public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
  37. /** Connection manager for all the WebdavClients */
  38. static private MultiThreadedHttpConnectionManager mConnManager = null;
  39. /**
  40. * Creates a WebdavClient setup for an ownCloud account
  41. *
  42. * @param account The ownCloud account
  43. * @param context The application context
  44. * @return A WebdavClient object ready to be used
  45. */
  46. public static WebdavClient createOwnCloudClient (Account account, Context context) {
  47. Log.d(TAG, "Creating WebdavClient associated to " + account.name);
  48. String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
  49. OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
  50. String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);
  51. WebdavClient client = createOwnCloudClient(Uri.parse(baseUrl + webDavPath));
  52. String username = account.name.substring(0, account.name.lastIndexOf('@'));
  53. String password = AccountManager.get(context).getPassword(account);
  54. //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
  55. client.setCredentials(username, password);
  56. return client;
  57. }
  58. /**
  59. * Creates a WebdavClient to try a new account before saving it
  60. *
  61. * @param uri URL to the ownCloud server
  62. * @param username User name
  63. * @param password User password
  64. * @return A WebdavClient object ready to be used
  65. */
  66. public static WebdavClient createOwnCloudClient(Uri uri, String username, String password) {
  67. Log.d(TAG, "Creating WebdavClient for " + username + "@" + uri);
  68. WebdavClient client = createOwnCloudClient(uri);
  69. client.setCredentials(username, password);
  70. return client;
  71. }
  72. /**
  73. * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
  74. *
  75. * @param uri URL to the ownCloud server
  76. * @return A WebdavClient object ready to be used
  77. */
  78. public static WebdavClient createOwnCloudClient(Uri uri) {
  79. Log.d(TAG, "Creating WebdavClient for " + uri);
  80. allowSelfsignedCertificates(true);
  81. WebdavClient client = new WebdavClient(getMultiThreadedConnManager());
  82. allowSelfsignedCertificates(true);
  83. client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
  84. client.setBaseUri(uri);
  85. return client;
  86. }
  87. /**
  88. * Allows or disallows self-signed certificates in ownCloud servers to reach
  89. *
  90. * @param allow 'True' to allow, 'false' to disallow
  91. */
  92. public static void allowSelfsignedCertificates(boolean allow) {
  93. Protocol pr = null;
  94. try {
  95. pr = Protocol.getProtocol("https");
  96. } catch (IllegalStateException e) {
  97. // nothing to do here; really
  98. }
  99. boolean isAllowed = (pr != null && pr.getSocketFactory() instanceof EasySSLSocketFactory);
  100. if (allow && !isAllowed) {
  101. Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
  102. } else if (!allow && isAllowed) {
  103. // TODO - a more strict SocketFactory object should be provided here
  104. }
  105. }
  106. static private MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {
  107. if (mConnManager == null) {
  108. mConnManager = new MultiThreadedHttpConnectionManager();
  109. mConnManager.getParams().setDefaultMaxConnectionsPerHost(5);
  110. mConnManager.getParams().setMaxTotalConnections(5);
  111. }
  112. return mConnManager;
  113. }
  114. }