Refactoring of WebdavClient creation and setup
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 16 Aug 2012 14:06:12 +0000 (16:06 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 16 Aug 2012 14:06:12 +0000 (16:06 +0200)
19 files changed:
src/com/owncloud/android/Uploader.java
src/com/owncloud/android/authenticator/AuthenticationRunnable.java
src/com/owncloud/android/authenticator/ConnectionCheckerRunnable.java
src/com/owncloud/android/authenticator/EasySSLSocketFactory.java
src/com/owncloud/android/files/interfaces/OnDatatransferProgressListener.java [deleted file]
src/com/owncloud/android/files/services/FileDownloader.java
src/com/owncloud/android/files/services/FileOperation.java
src/com/owncloud/android/files/services/FileUploader.java
src/com/owncloud/android/files/services/InstantUploadService.java
src/com/owncloud/android/syncadapter/AbstractOwnCloudSyncAdapter.java
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/ui/activity/AuthenticatorActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java
src/com/owncloud/android/utils/OwnCloudClientUtils.java [new file with mode: 0644]
src/eu/alefzero/webdav/FileRequestEntity.java
src/eu/alefzero/webdav/OnDatatransferProgressListener.java [new file with mode: 0644]
src/eu/alefzero/webdav/OnUploadProgressListener.java [deleted file]
src/eu/alefzero/webdav/WebdavClient.java

index d6e7a4a..c3414b3 100644 (file)
@@ -30,6 +30,7 @@ import com.owncloud.android.datamodel.DataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileUploader;\r
+import com.owncloud.android.utils.OwnCloudClientUtils;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -373,8 +374,7 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
 \r
     public void uploadFiles() {\r
         try {\r
-            WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext());\r
-            wdc.allowSelfsignedCertificates();\r
+            WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());\r
 \r
             // create last directory in path if necessary\r
             if (mCreateDir) {\r
index dba3281..f40701f 100644 (file)
@@ -22,6 +22,8 @@ import java.net.URL;
 
 import org.apache.commons.httpclient.HttpStatus;
 
+import com.owncloud.android.utils.OwnCloudClientUtils;
+
 import eu.alefzero.webdav.WebdavClient;
 
 import android.net.Uri;
@@ -52,7 +54,8 @@ public class AuthenticationRunnable implements Runnable {
     public void run() {
         Uri uri;
         uri = Uri.parse(mUrl.toString());
-        int login_result = WebdavClient.tryToLogin(uri, mUsername, mPassword);
+        WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(uri, mUsername, mPassword);
+        int login_result = wdc.tryToLogin();
         switch (login_result) {
         case HttpStatus.SC_OK:
             postResult(true, uri.toString());
index 52e4eb0..2d32f18 100644 (file)
 package com.owncloud.android.authenticator;
 
 import java.io.IOException;
-import java.net.ConnectException;
 import java.net.MalformedURLException;
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
-import java.net.URI;
-import java.net.URL;
 import java.net.UnknownHostException;
 
 import javax.net.ssl.SSLException;
-import javax.net.ssl.SSLHandshakeException;
 import javax.net.ssl.SSLPeerUnverifiedException;
 
+import org.apache.commons.httpclient.ConnectTimeoutException;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.GetMethod;
@@ -39,6 +36,7 @@ import org.json.JSONObject;
 
 import com.owncloud.android.AccountUtils;
 import com.owncloud.android.authenticator.OnConnectCheckListener.ResultType;
+import com.owncloud.android.utils.OwnCloudClientUtils;
 import com.owncloud.android.utils.OwnCloudVersion;
 
 import eu.alefzero.webdav.WebdavClient;
@@ -107,15 +105,14 @@ public class ConnectionCheckerRunnable implements Runnable {
 
     private boolean tryConnection(String urlSt) {
         boolean retval = false;
+        GetMethod get = null;
         try {
-            WebdavClient wc = new WebdavClient();
-            wc.allowSelfsignedCertificates();
-            URL url = new URL(urlSt);   // better than android.net.Uri in this case; provides URL validation
-            GetMethod get = new GetMethod(url.toString());
-            int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT);
+            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(urlSt));
+            get = new GetMethod(urlSt);
+            int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
+            String response = get.getResponseBodyAsString();
             switch (status) {
             case HttpStatus.SC_OK: {
-                String response = get.getResponseBodyAsString();
                 JSONObject json = new JSONObject(response);
                 if (!json.getBoolean("installed")) {
                     mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
@@ -168,17 +165,25 @@ public class ConnectionCheckerRunnable implements Runnable {
             mLatestResult = ResultType.SSL_INIT_ERROR;
             Log.e(TAG, "SSL exception while trying connection", e);
             
-        } catch (HttpException e) { // specific exceptions from org.apache.commons.httpclient
+        } catch (ConnectTimeoutException e) {   // timeout specific exception from org.apache.commons.httpclient
+            mLatestResult = ResultType.TIMEOUT;
+            Log.e(TAG, "Socket timeout exception while trying connection", e);
+            
+        } catch (HttpException e) { // other specific exceptions from org.apache.commons.httpclient
             mLatestResult = ResultType.UNKNOWN_ERROR;
             Log.e(TAG, "HTTP exception while trying connection", e);
             
-        } catch (IOException e) {   // UnkownsServiceException, and any other weird I/O Exception that could occur
+        } catch (IOException e) {   // UnkownsServiceException, and any other transport exceptions that could occur
             mLatestResult = ResultType.UNKNOWN_ERROR;
             Log.e(TAG, "I/O exception while trying connection", e);
             
         } catch (Exception e) {
             mLatestResult = ResultType.UNKNOWN_ERROR;
             Log.e(TAG, "Unexpected exception while trying connection", e);
+            
+        } finally {
+            if (get != null)
+                get.releaseConnection();
         }
 
         return retval;
index 7503546..5546419 100644 (file)
@@ -186,7 +186,7 @@ public class EasySSLSocketFactory implements ProtocolSocketFactory {
             socket.setSoTimeout(params.getSoTimeout());
             return socket;
         } else {
-            Log.d(TAG, " ... with connection timeout " + timeout + " and socket timeout" + params.getSoTimeout());
+            Log.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
             Socket socket = socketfactory.createSocket();
             SocketAddress localaddr = new InetSocketAddress(localAddress,
                     localPort);
diff --git a/src/com/owncloud/android/files/interfaces/OnDatatransferProgressListener.java b/src/com/owncloud/android/files/interfaces/OnDatatransferProgressListener.java
deleted file mode 100644 (file)
index f3ae82b..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-package com.owncloud.android.files.interfaces;
-
-public interface OnDatatransferProgressListener {
-    void transferProgress(long progressRate);
-
-}
index 4aafcce..d88dbb0 100644 (file)
@@ -7,7 +7,8 @@ import java.util.Map;
 \r
 import com.owncloud.android.authenticator.AccountAuthenticator;\r
 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;\r
-import com.owncloud.android.files.interfaces.OnDatatransferProgressListener;\r
+import eu.alefzero.webdav.OnDatatransferProgressListener;\r
+import com.owncloud.android.utils.OwnCloudClientUtils;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -141,22 +142,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         boolean downloadResult = false;\r
 \r
         /// prepare client object to send the request to the ownCloud server\r
-        AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
-        WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext());\r
-        String username = mAccount.name.split("@")[0];\r
-        String password = null;\r
-        try {\r
-            password = am.blockingGetAuthToken(mAccount,\r
-                    AccountAuthenticator.AUTH_TOKEN_TYPE, true);\r
-        } catch (Exception e) {\r
-            Log.e(TAG, "Access to account credentials failed", e);\r
-            sendFinalBroadcast(downloadResult, null);\r
-            return;\r
-        }\r
-        wdc.setCredentials(username, password);\r
-        wdc.allowSelfsignedCertificates();\r
+        WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());\r
         wdc.setDataTransferProgressListener(this);\r
-\r
         \r
         /// download will be in a temporal file\r
         File tmpFile = new File(getTemporalPath(mAccount.name) + mFilePath);\r
index bdb76cd..ce61dc7 100644 (file)
@@ -21,6 +21,7 @@ import java.io.File;
 
 import com.owncloud.android.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.utils.OwnCloudClientUtils;
 
 import android.accounts.Account;
 import android.content.Context;
@@ -42,7 +43,7 @@ public class FileOperation {
     public boolean delete(OCFile file){
         
         Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
-        WebdavClient client = new WebdavClient(account, mContext);
+        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(account, mContext);
         if(client.deleteFile(file.getRemotePath())){
             File localFile = new File(file.getStoragePath());
             return localFile.delete();
index 34d795b..488447a 100644 (file)
@@ -7,7 +7,8 @@ import java.util.Map;
 
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.interfaces.OnDatatransferProgressListener;
+import eu.alefzero.webdav.OnDatatransferProgressListener;
+import com.owncloud.android.utils.OwnCloudClientUtils;
 
 import android.accounts.Account;
 import android.app.Notification;
@@ -150,8 +151,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         mTotalDataToSend = mSendData = mPreviousPercent = 0;
         
         /// prepare client object to send the request to the ownCloud server
-        WebdavClient wc = new WebdavClient(mAccount, getApplicationContext());
-        wc.allowSelfsignedCertificates();
+        WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
         wc.setDataTransferProgressListener(this);
 
         /// create status notification to show the upload progress
index 0324f59..a0b5b77 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
 
 import com.owncloud.android.AccountUtils;
 import com.owncloud.android.authenticator.AccountAuthenticator;
+import com.owncloud.android.utils.OwnCloudClientUtils;
 import com.owncloud.android.utils.OwnCloudVersion;
 
 import eu.alefzero.webdav.WebdavClient;
@@ -126,35 +127,19 @@ public class InstantUploadService extends Service {
         
         public void run() {
             HashMap<String, Object> working_map;
-            AccountManager am = AccountManager.get(getApplicationContext());
             
             while ((working_map = getFirstObject()) != null) {
                 Account account = (Account) working_map.get(KEY_ACCOUNT);
-                String username = account.name.substring(0, account.name.lastIndexOf('@'));
-                String password = am.getPassword(account);
                 String filename = (String) working_map.get(KEY_DISPLAY_NAME);
                 String filepath = (String) working_map.get(KEY_FILE_PATH);
                 String mimetype = (String) working_map.get(KEY_MIME_TYPE);
                 
-                String oc_base_url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
-                String oc_version = am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
-                OwnCloudVersion ocv = new OwnCloudVersion(oc_version);
-                String webdav_path = AccountUtils.getWebdavPath(ocv);
-                WebdavClient wdc = new WebdavClient(account, getApplicationContext());
-                wdc.allowSelfsignedCertificates();
-                wdc.setCredentials(username, password);
+                WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(account, getApplicationContext());
                 
-                MkColMethod mkcol = new MkColMethod(oc_base_url+webdav_path+INSTANT_UPLOAD_DIR);
                 int status = 0;
-                try {
-                    status = wdc.executeMethod(mkcol);
-                    Log.e(TAG, "mkcol returned " + status);
-                    wdc.putFile(filepath, INSTANT_UPLOAD_DIR + "/" + filename, mimetype);
-                } catch (HttpException e) {
-                    e.printStackTrace();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
+                wdc.createDirectory(INSTANT_UPLOAD_DIR);
+                Log.e(TAG, "mkcol returned " + status);
+                wdc.putFile(filepath, INSTANT_UPLOAD_DIR + "/" + filename, mimetype);
             }
         }
     }
index e8b1fcb..ecb320e 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.http.protocol.HttpContext;
 \r
 import com.owncloud.android.authenticator.AccountAuthenticator;\r
 import com.owncloud.android.datamodel.DataStorageManager;\r
+import com.owncloud.android.utils.OwnCloudClientUtils;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -154,9 +155,7 @@ public abstract class AbstractOwnCloudSyncAdapter extends
                     AccountAuthenticator.KEY_OC_URL) == null) {\r
                 throw new UnknownHostException();\r
             }\r
-            mClient = new WebdavClient(account, getContext());\r
-            mClient.allowSelfsignedCertificates();\r
-            // mHost = mClient.getTargetHost();\r
+            mClient = OwnCloudClientUtils.createOwnCloudClient(account, getContext());\r
         }\r
 \r
         return mClient;\r
index f7bfa43..1a3afe6 100644 (file)
@@ -91,7 +91,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
 \r
         sendStickyBroadcast(true, null);  // message to signal the start to the UI\r
 \r
-        PropFindMethod query;\r
+        PropFindMethod query = null;\r
         try {\r
             mCurrentSyncTime = System.currentTimeMillis();\r
             query = new PropFindMethod(getUri().toString() + "/");\r
@@ -123,6 +123,10 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             // TODO update syncResult\r
             Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);\r
             t.printStackTrace();\r
+            \r
+        } finally {\r
+            if (query != null)\r
+                query.releaseConnection();  // let the connection available for other methods\r
         }\r
         \r
         /*  Commented code for ugly performance tests\r
@@ -150,11 +154,12 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     }\r
 \r
     private void fetchData(String uri, SyncResult syncResult, long parentId) {\r
+        PropFindMethod query = null;\r
         try {\r
             Log.d(TAG, "fetching " + uri);\r
             \r
             // remote request \r
-            PropFindMethod query = new PropFindMethod(uri);\r
+            query = new PropFindMethod(uri);\r
             /*  Commented code for ugly performance tests\r
             long responseDelay = System.currentTimeMillis();\r
             */\r
@@ -236,8 +241,6 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             if (mDelaysIndex >= MAX_DELAYS)\r
                 mDelaysIndex = 0;\r
              */\r
-            \r
-\r
 \r
         } catch (OperationCanceledException e) {\r
             e.printStackTrace();\r
@@ -254,6 +257,10 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             // TODO update syncResult\r
             Log.e(TAG, "problem while synchronizing owncloud account " + mAccount.name, t);\r
             t.printStackTrace();\r
+            \r
+        } finally {\r
+            if (query != null)\r
+                query.releaseConnection();  // let the connection available for other methods\r
         }\r
     }\r
 \r
index 6344dad..a71f65b 100644 (file)
@@ -266,7 +266,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             String url_str = prefix + url + webdav_path;\r
             uri = new URL(url_str);\r
         } catch (MalformedURLException e) {\r
-            // should not happend\r
+            // should not happen\r
             e.printStackTrace();\r
         }\r
 \r
index da1668a..5956004 100644 (file)
@@ -70,6 +70,7 @@ import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.syncadapter.FileSyncService;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
 import com.owncloud.android.ui.fragment.OCFileListFragment;\r
+import com.owncloud.android.utils.OwnCloudClientUtils;\r
 \r
 import com.owncloud.android.R;\r
 import eu.alefzero.webdav.WebdavClient;\r
@@ -643,14 +644,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     \r
         @Override\r
         public void run() {\r
-            WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext());\r
-            \r
-            String username = mAccount.name.substring(0,\r
-                    mAccount.name.lastIndexOf('@'));\r
-            String password = mAm.getPassword(mAccount);\r
-    \r
-            wdc.setCredentials(username, password);\r
-            wdc.allowSelfsignedCertificates();\r
+            WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());\r
             boolean created = wdc.createDirectory(mTargetPath);\r
             if (created) {\r
                 mHandler.post(new Runnable() {\r
index ae38a0a..a77d9ad 100644 (file)
@@ -82,6 +82,7 @@ import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.ui.activity.FileDetailActivity;\r
 import com.owncloud.android.ui.activity.FileDisplayActivity;\r
+import com.owncloud.android.utils.OwnCloudClientUtils;\r
 import com.owncloud.android.utils.OwnCloudVersion;\r
 \r
 import com.owncloud.android.R;\r
@@ -661,7 +662,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 final String WEBDAV_SCRIPT = "webdav.php";\r
                 final String WEBDAV_FILES_LOCATION = "/files/";\r
                 \r
-                WebdavClient wc = new WebdavClient();\r
+                WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(account, getActivity().getApplicationContext());\r
                 HttpConnectionManagerParams params = new HttpConnectionManagerParams();\r
                 params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5);\r
 \r
@@ -684,7 +685,6 @@ public class FileDetailFragment extends SherlockFragment implements
                     PropFindMethod find = new PropFindMethod(url+"/");\r
                     find.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));\r
                     Log.d("sharer", ""+ url+"/");\r
-                    wc.setCredentials(account.name.substring(0, account.name.lastIndexOf('@')), am.getPassword(account));\r
                     \r
                     for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) {\r
                         Log.d("sharer-h", a.getName() + ":"+a.getValue());\r
@@ -809,8 +809,7 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
         \r
         public void run() {\r
-            WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
-            wc.allowSelfsignedCertificates();\r
+            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
             AccountManager am = AccountManager.get(getSherlockActivity());\r
             String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
             OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
@@ -825,6 +824,7 @@ public class FileDetailFragment extends SherlockFragment implements
             try {\r
                 int status = wc.executeMethod(move);\r
                 success = move.succeeded();\r
+                move.getResponseBodyAsString(); // exhaust response, although not interesting\r
                 Log.d(TAG, "Move returned status: " + status);\r
                 \r
             } catch (HttpException e) {\r
@@ -835,7 +835,10 @@ public class FileDetailFragment extends SherlockFragment implements
                 \r
             } catch (Exception e) {\r
                 Log.e(TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
-            }\r
+                \r
+            } finally {\r
+               move.releaseConnection();\r
+            } \r
             \r
             if (success) {\r
                 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
@@ -973,8 +976,7 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
         \r
         public void run() {\r
-            WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
-            wc.allowSelfsignedCertificates();\r
+            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
             AccountManager am = AccountManager.get(getSherlockActivity());\r
             String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
             OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
@@ -988,6 +990,7 @@ public class FileDetailFragment extends SherlockFragment implements
             try {\r
                 status = wc.executeMethod(delete);\r
                 success = (delete.succeeded());\r
+                delete.getResponseBodyAsString();   // exhaust the response, although not interesting\r
                 Log.d(TAG, "Delete: returned status " + status);\r
                 \r
             } catch (HttpException e) {\r
@@ -998,6 +1001,9 @@ public class FileDetailFragment extends SherlockFragment implements
                 \r
             } catch (Exception e) {\r
                 Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);\r
+                \r
+            } finally {\r
+                delete.releaseConnection();\r
             }\r
             \r
             if (success) {\r
diff --git a/src/com/owncloud/android/utils/OwnCloudClientUtils.java b/src/com/owncloud/android/utils/OwnCloudClientUtils.java
new file mode 100644 (file)
index 0000000..f8b5a68
--- /dev/null
@@ -0,0 +1,147 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2011  Bartek Przybylski
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package com.owncloud.android.utils;
+
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+import org.apache.commons.httpclient.protocol.Protocol;
+
+import com.owncloud.android.AccountUtils;
+import com.owncloud.android.authenticator.AccountAuthenticator;
+import com.owncloud.android.authenticator.EasySSLSocketFactory;
+import com.owncloud.android.utils.OwnCloudVersion;
+
+import eu.alefzero.webdav.WebdavClient;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.content.Context;
+import android.net.Uri;
+import android.util.Log;
+
+public class OwnCloudClientUtils {
+    
+    final private static String TAG = "OwnCloudClientFactory";
+    
+    /** Default timeout for waiting data from the server */
+    public static final int DEFAULT_DATA_TIMEOUT = 10000;
+    
+    /** Default timeout for establishing a connection */
+    public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
+
+    /** Connection manager for all the WebdavClients */
+    static private MultiThreadedHttpConnectionManager mConnManager = null;
+    
+    
+    /**
+     * Creates a WebdavClient setup for an ownCloud account
+     * 
+     * @param account   The ownCloud account
+     * @param context   The application context
+     * @return          A WebdavClient object ready to be used
+     */
+    public static WebdavClient createOwnCloudClient (Account account, Context context) {
+        Log.d(TAG, "Creating WebdavClient associated to " + account.name);
+       
+        String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
+        OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
+        String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);
+        
+        WebdavClient client = createOwnCloudClient(Uri.parse(baseUrl + webDavPath));
+        
+        String username = account.name.substring(0, account.name.lastIndexOf('@'));
+        String password = AccountManager.get(context).getPassword(account);
+        //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
+        
+        client.setCredentials(username, password);
+        
+        return client;
+    }
+    
+    
+    /**
+     * Creates a WebdavClient to try a new account before saving it
+     * 
+     * @param uri       URL to the ownCloud server
+     * @param username  User name
+     * @param password  User password
+     * @return          A WebdavClient object ready to be used
+     */
+    public static WebdavClient createOwnCloudClient(Uri uri, String username, String password) {
+        Log.d(TAG, "Creating WebdavClient for " + username + "@" + uri);
+        
+        WebdavClient client = createOwnCloudClient(uri);
+        
+        client.setCredentials(username, password);
+        
+        return client;
+    }
+    
+    
+    /**
+     * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
+     * 
+     * @param uri       URL to the ownCloud server
+     * @return          A WebdavClient object ready to be used
+     */
+    public static WebdavClient createOwnCloudClient(Uri uri) {
+        Log.d(TAG, "Creating WebdavClient for " + uri);
+        
+        allowSelfsignedCertificates(true);
+        
+        WebdavClient client = new WebdavClient(getMultiThreadedConnManager());
+        
+        allowSelfsignedCertificates(true);
+        client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
+        client.setBaseUri(uri);
+        
+        return client;
+    }
+    
+    
+    /**
+     * Allows or disallows self-signed certificates in ownCloud servers to reach
+     * 
+     * @param allow     'True' to allow, 'false' to disallow
+     */
+    public static void allowSelfsignedCertificates(boolean allow) {
+        Protocol pr = null;
+        try {
+            pr = Protocol.getProtocol("https");
+        } catch (IllegalStateException e) {
+            // nothing to do here; really
+        }
+        boolean isAllowed = (pr != null && pr.getSocketFactory() instanceof EasySSLSocketFactory);
+        if (allow && !isAllowed) {
+            Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
+        } else if (!allow && isAllowed) {
+            // TODO - a more strict SocketFactory object should be provided here
+        }
+    }
+
+    
+    
+    static private MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {
+        if (mConnManager == null) {
+            mConnManager = new MultiThreadedHttpConnectionManager();
+            mConnManager.getParams().setDefaultMaxConnectionsPerHost(5);
+            mConnManager.getParams().setMaxTotalConnections(5);
+        }
+        return mConnManager;
+    }
+
+}
index 61ba456..2a3ba02 100644 (file)
@@ -8,7 +8,7 @@ import java.io.OutputStream;
 
 import org.apache.commons.httpclient.methods.RequestEntity;
 
-import com.owncloud.android.files.interfaces.OnDatatransferProgressListener;
+import eu.alefzero.webdav.OnDatatransferProgressListener;
 
 import android.util.Log;
 
diff --git a/src/eu/alefzero/webdav/OnDatatransferProgressListener.java b/src/eu/alefzero/webdav/OnDatatransferProgressListener.java
new file mode 100644 (file)
index 0000000..7d6dda1
--- /dev/null
@@ -0,0 +1,5 @@
+package eu.alefzero.webdav;
+
+public interface OnDatatransferProgressListener {
+    public void transferProgress(long progressRate);
+}
diff --git a/src/eu/alefzero/webdav/OnUploadProgressListener.java b/src/eu/alefzero/webdav/OnUploadProgressListener.java
deleted file mode 100644 (file)
index 9a92c51..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-package eu.alefzero.webdav;
-
-public interface OnUploadProgressListener {
-    public void OnUploadProgress(long currentProgress);
-}
index 493c4b8..abd7048 100644 (file)
@@ -21,35 +21,26 @@ import java.io.BufferedInputStream;
 import java.io.File;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
+import java.io.InputStream;\r
 \r
 import org.apache.commons.httpclient.Credentials;\r
 import org.apache.commons.httpclient.HttpClient;\r
+import org.apache.commons.httpclient.HttpConnectionManager;\r
 import org.apache.commons.httpclient.HttpException;\r
 import org.apache.commons.httpclient.HttpMethodBase;\r
 import org.apache.commons.httpclient.HttpVersion;\r
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;\r
 import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
 import org.apache.commons.httpclient.auth.AuthScope;\r
 import org.apache.commons.httpclient.methods.GetMethod;\r
 import org.apache.commons.httpclient.methods.HeadMethod;\r
 import org.apache.commons.httpclient.methods.PutMethod;\r
 import org.apache.commons.httpclient.params.HttpMethodParams;\r
-import org.apache.commons.httpclient.protocol.Protocol;\r
 import org.apache.http.HttpStatus;\r
 import org.apache.http.params.CoreProtocolPNames;\r
 import org.apache.jackrabbit.webdav.client.methods.DavMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;\r
 \r
-import com.owncloud.android.AccountUtils;\r
-import com.owncloud.android.authenticator.AccountAuthenticator;\r
-import com.owncloud.android.authenticator.EasySSLSocketFactory;\r
-import com.owncloud.android.files.interfaces.OnDatatransferProgressListener;\r
-import com.owncloud.android.utils.OwnCloudVersion;\r
-\r
-import android.accounts.Account;\r
-import android.accounts.AccountManager;\r
-import android.content.Context;\r
 import android.net.Uri;\r
 import android.util.Log;\r
 \r
@@ -59,56 +50,17 @@ public class WebdavClient extends HttpClient {
     final private static String TAG = "WebdavClient";\r
     private static final String USER_AGENT = "Android-ownCloud";\r
     \r
-    /** Default timeout for waiting data from the server: 10 seconds */\r
-    public static final int DEFAULT_DATA_TIMEOUT = 10000;\r
-    \r
-    /** Default timeout for establishing a connection: infinite */\r
-    public static final int DEFAULT_CONNECTION_TIMEOUT = 0;\r
-    \r
     private OnDatatransferProgressListener mDataTransferListener;\r
-    static private MultiThreadedHttpConnectionManager mConnManager = null;\r
-    \r
-    static public MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {\r
-        if (mConnManager == null) {\r
-            mConnManager = new MultiThreadedHttpConnectionManager();\r
-            mConnManager.setMaxConnectionsPerHost(5);\r
-            mConnManager.setMaxTotalConnections(5);\r
-        }\r
-        return mConnManager;\r
-    }\r
+    static private byte[] sExhaustBuffer = new byte[1024];\r
     \r
     /**\r
-     * Creates a WebdavClient setup for the current account\r
-     * @param account The client accout\r
-     * @param context The application context\r
-     * @return\r
+     * Constructor\r
      */\r
-    public WebdavClient (Account account, Context context) {\r
-        Log.d(TAG, "Creating WebdavClient associated to " + account.name);\r
-        \r
-        setDefaultTimeouts();\r
-        \r
-        OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account,\r
-                AccountAuthenticator.KEY_OC_VERSION));\r
-        String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
-        String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);        \r
-        String username = account.name.substring(0, account.name.lastIndexOf('@'));\r
-        String password = AccountManager.get(context).getPassword(account);\r
-        \r
-        mUri = Uri.parse(baseUrl + webDavPath);\r
-        Log.e("ASD", ""+username);\r
-        setCredentials(username, password);\r
-    }\r
-    \r
-    public WebdavClient() {\r
-        super(getMultiThreadedConnManager());\r
+    public WebdavClient(HttpConnectionManager connectionMgr) {\r
+        super(connectionMgr);\r
         Log.d(TAG, "Creating WebdavClient");\r
-        \r
-        setDefaultTimeouts();\r
-        \r
         getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);\r
         getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);\r
-        allowSelfsignedCertificates();\r
     }\r
 \r
     public void setCredentials(String username, String password) {\r
@@ -124,33 +76,18 @@ public class WebdavClient extends HttpClient {
     }\r
     \r
     /**\r
-     * Sets the connection and wait-for-data timeouts to be applied by default.\r
-     */\r
-    private void setDefaultTimeouts() {\r
-        getParams().setSoTimeout(DEFAULT_DATA_TIMEOUT);\r
-        getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);\r
-    }\r
-\r
-    public void allowSelfsignedCertificates() {\r
-        // https\r
-        Protocol.registerProtocol("https", new Protocol("https",\r
-                new EasySSLSocketFactory(), 443));\r
-    }\r
-\r
-    /**\r
      * Downloads a file in remoteFilepath to the local targetPath.\r
      * \r
      * @param remoteFilepath    Path to the file in the remote server, URL DECODED. \r
      * @param targetFile        Local path to save the downloaded file.\r
      * @return                  'True' when the file is successfully downloaded.\r
      */\r
-    public boolean downloadFile(String remoteFilepath, File targetFile) {\r
+    public boolean downloadFile(String remoteFilePath, File targetFile) {\r
         boolean ret = false;\r
-        GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));\r
+        GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilePath));\r
 \r
-        int status = -1;\r
         try {\r
-            status = executeMethod(get);\r
+            int status = executeMethod(get);\r
             if (status == HttpStatus.SC_OK) {\r
                 targetFile.createNewFile();\r
                 BufferedInputStream bis = new BufferedInputStream(\r
@@ -165,26 +102,19 @@ public class WebdavClient extends HttpClient {
                     fos.write(bytes, 0, readResult);\r
                 }\r
                 ret = true;\r
+            } else {\r
+                exhaustResponse(get.getResponseBodyAsStream());\r
             }\r
+            Log.e(TAG, "Download of " + remoteFilePath + " to " + targetFile + " finished with HTTP status " + status + (!ret?"(FAIL)":""));\r
             \r
-        } catch (HttpException e) {\r
-            Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e);\r
-\r
-        } catch (IOException e) {\r
-            Log.e(TAG, "I/O exception downloading " + remoteFilepath, e);\r
-\r
         } catch (Exception e) {\r
-            Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e);\r
+            logException(e, "dowloading " + remoteFilePath);\r
             \r
         } finally {\r
-            if (!ret) {\r
-                if (status >= 0) {\r
-                    Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status);\r
-                }\r
-                if (targetFile.exists()) {\r
-                    targetFile.delete();\r
-                }\r
+            if (!ret && targetFile.exists()) {\r
+                targetFile.delete();\r
             }\r
+            get.releaseConnection();    // let the connection available for other methods\r
         }\r
         return ret;\r
     }\r
@@ -194,17 +124,26 @@ public class WebdavClient extends HttpClient {
      * @param remoteFilePath       Remote file path of the file to delete, in URL DECODED format.\r
      * @return\r
      */\r
-    public boolean deleteFile(String remoteFilePath){\r
+    public boolean deleteFile(String remoteFilePath) {\r
+        boolean ret = false;\r
         DavMethod delete = new DeleteMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilePath));\r
         try {\r
-            executeMethod(delete);\r
-        }  catch (Throwable e) {\r
-            Log.e(TAG, "Deleting failed with error: " + e.getMessage(), e);\r
-            return false;\r
+            int status = executeMethod(delete);\r
+            ret = (status == HttpStatus.SC_OK || status == HttpStatus.SC_ACCEPTED || status == HttpStatus.SC_NO_CONTENT);\r
+            exhaustResponse(delete.getResponseBodyAsStream());\r
+            \r
+            Log.e(TAG, "DELETE of " + remoteFilePath + " finished with HTTP status " + status +  (!ret?"(FAIL)":""));\r
+            \r
+        } catch (Exception e) {\r
+            logException(e, "deleting " + remoteFilePath);\r
+            \r
+        } finally {\r
+            delete.releaseConnection();    // let the connection available for other methods\r
         }\r
-        return true;\r
+        return ret;\r
     }\r
 \r
+    \r
     public void setDataTransferProgressListener(OnDatatransferProgressListener listener) {\r
         mDataTransferListener = listener;\r
     }\r
@@ -221,56 +160,51 @@ public class WebdavClient extends HttpClient {
     public boolean putFile(String localFile, String remoteTarget, String contentType) {\r
         boolean result = false;\r
         int status = -1;\r
-\r
+        PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));\r
+        \r
         try {\r
             File f = new File(localFile);\r
             FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
             entity.setOnDatatransferProgressListener(mDataTransferListener);\r
-            PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));\r
             put.setRequestEntity(entity);\r
             status = executeMethod(put);\r
             \r
             result = (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT);\r
             \r
-            Log.d(TAG, "PUT response for " + remoteTarget + " finished with HTTP status " + status);\r
-            \r
-        } catch (HttpException e) {\r
-            Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e);\r
-\r
-        } catch (IOException e) {\r
-            Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e);\r
+            Log.d(TAG, "PUT to " + remoteTarget + " finished with HTTP status " + status + (!result?"(FAIL)":""));\r
 \r
+            exhaustResponse(put.getResponseBodyAsStream());\r
+            \r
         } catch (Exception e) {\r
-            Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);\r
+            logException(e, "uploading " + localFile + " to " + remoteTarget);\r
+            \r
+        } finally {\r
+            put.releaseConnection();    // let the connection available for other methods\r
         }\r
-        \r
-        if (!result && status >= 0) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);\r
-        \r
         return result;\r
     }\r
 \r
     /**\r
-     * Tries to log in to the given WedDavURI, with the given credentials\r
-     * @param uri To test\r
-     * @param username Username to check\r
-     * @param password Password to verify\r
+     * Tries to log in to the current URI, with the current credentials\r
+     * \r
      * @return A {@link HttpStatus}-Code of the result. SC_OK is good.\r
      */\r
-    public static int tryToLogin(Uri uri, String username, String password) {\r
-        int returnCode = 0;\r
+    public int tryToLogin() {\r
+        int status = 0;\r
+        HeadMethod head = new HeadMethod(mUri.toString());\r
         try {\r
-            WebdavClient client = new WebdavClient();\r
-            client.setCredentials(username, password);\r
-            HeadMethod head = new HeadMethod(uri.toString());\r
-            returnCode = client.executeMethod(head);\r
-        } catch (HttpException e) {\r
-            Log.e(TAG, "HTTP exception trying to login at " + uri.getEncodedPath(), e);\r
-        } catch (IOException e) {\r
-            Log.e(TAG, "I/O exception trying to login at " + uri.getEncodedPath(), e);\r
+            status = executeMethod(head);\r
+            boolean result = status == HttpStatus.SC_OK;\r
+            Log.d(TAG, "HEAD for " + mUri + " finished with HTTP status " + status + (!result?"(FAIL)":""));\r
+            exhaustResponse(head.getResponseBodyAsStream());\r
+            \r
         } catch (Exception e) {\r
-            Log.e(TAG, "Unexpected exception trying to login at " + uri.getEncodedPath(), e);\r
+            logException(e, "trying to login at " + mUri.toString());\r
+            \r
+        } finally {\r
+            head.releaseConnection();\r
         }\r
-        return returnCode;\r
+        return status;\r
     }\r
 \r
     /**\r
@@ -282,25 +216,21 @@ public class WebdavClient extends HttpClient {
     public boolean createDirectory(String path) {\r
         boolean result = false;\r
         int status = -1;\r
+        MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
         try {\r
-            MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
             Log.d(TAG, "Creating directory " + path);\r
             status = executeMethod(mkcol);\r
             Log.d(TAG, "Status returned: " + status);\r
             result = mkcol.succeeded();\r
             \r
-        } catch (HttpException e) {\r
-            Log.e(TAG, "HTTP exception creating directory " + path, e);\r
-\r
-        } catch (IOException e) {\r
-            Log.e(TAG, "I/O exception creating directory " + path, e);\r
-\r
+            Log.d(TAG, "MKCOL to " + path + " finished with HTTP status " + status + (!result?"(FAIL)":""));\r
+            exhaustResponse(mkcol.getResponseBodyAsStream());\r
+            \r
         } catch (Exception e) {\r
-            Log.e(TAG, "Unexpected exception creating directory " + path, e);\r
+            logException(e, "creating directory " + path);\r
             \r
-        }\r
-        if (!result && status >= 0) {\r
-            Log.e(TAG, "Creation of directory " + path + " failed with HTTP status " + status);\r
+        } finally {\r
+            mkcol.releaseConnection();    // let the connection available for other methods\r
         }\r
         return result;\r
     }\r
@@ -312,13 +242,19 @@ public class WebdavClient extends HttpClient {
      * @return      'Boolean.TRUE' if the file exists; 'Boolean.FALSE' it doesn't exist; NULL if couldn't be checked\r
      */\r
     public Boolean existsFile(String path) {\r
+        HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
         try {\r
-            HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
             int status = executeMethod(head);\r
+            Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":""));\r
+            exhaustResponse(head.getResponseBodyAsStream());\r
             return (status == HttpStatus.SC_OK);\r
+            \r
         } catch (Exception e) {\r
-            e.printStackTrace();\r
+            logException(e, "checking existence of " + path);\r
             return null;\r
+            \r
+        } finally {\r
+            head.releaseConnection();    // let the connection available for other methods\r
         }\r
     }\r
 \r
@@ -328,24 +264,83 @@ public class WebdavClient extends HttpClient {
      * \r
      * Executes the method through the inherited HttpClient.executedMethod(method).\r
      * \r
-     * Sets the socket timeout for the HttpMethodBase method received.\r
+     * Sets the socket and connection timeouts only for the method received.\r
+     * \r
+     * The timeouts are both in milliseconds; 0 means 'infinite'; < 0 means 'do not change the default'\r
      * \r
-     * @param method    HTTP method request.\r
-     * @param timeout   Timeout to set, in milliseconds; <= 0 means infinite.\r
+     * @param method            HTTP method request.\r
+     * @param readTimeout       Timeout to set for data reception\r
+     * @param conntionTimout    Timeout to set for connection establishment\r
      */\r
-    public int executeMethod(HttpMethodBase method, int readTimeout) throws HttpException, IOException {\r
+    public int executeMethod(HttpMethodBase method, int readTimeout, int connectionTimeout) throws HttpException, IOException {\r
         int oldSoTimeout = getParams().getSoTimeout();\r
+        int oldConnectionTimeout = getHttpConnectionManager().getParams().getConnectionTimeout();\r
         try {\r
-            if (readTimeout < 0) { \r
-                readTimeout = 0;\r
+            if (readTimeout >= 0) { \r
+                method.getParams().setSoTimeout(readTimeout);   // this should be enough...\r
+                getParams().setSoTimeout(readTimeout);          // ... but this looks like necessary for HTTPS\r
+            }\r
+            if (connectionTimeout >= 0) {\r
+                getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);\r
             }\r
-            HttpMethodParams params = method.getParams();\r
-            params.setSoTimeout(readTimeout);       \r
-            method.setParams(params);               // this should be enough...\r
-            getParams().setSoTimeout(readTimeout);  // ... but this is necessary for HTTPS\r
             return executeMethod(method);\r
         } finally {\r
             getParams().setSoTimeout(oldSoTimeout);\r
+            getHttpConnectionManager().getParams().setConnectionTimeout(oldConnectionTimeout);\r
         }\r
     }\r
+\r
+    /**\r
+     * Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.\r
+     * \r
+     * @param responseBodyAsStream      InputStream with the HTTP response to exhaust.\r
+     */\r
+    private static void exhaustResponse(InputStream responseBodyAsStream) {\r
+        if (responseBodyAsStream != null) {\r
+            try {\r
+                while (responseBodyAsStream.read(sExhaustBuffer) >= 0);\r
+                responseBodyAsStream.close();\r
+            \r
+            } catch (IOException io) {\r
+                Log.e(TAG, "Unexpected exception while exhausting not interesting HTTP response; will be IGNORED", io);\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * Logs an exception triggered in a HTTP request. \r
+     * \r
+     * @param e         Caught exception.\r
+     * @param doing     Suffix to add at the end of the logged message.\r
+     */\r
+    private static void logException(Exception e, String doing) {\r
+        if (e instanceof HttpException) {\r
+            Log.e(TAG, "HTTP violation while " + doing, e);\r
+\r
+        } else if (e instanceof IOException) {\r
+            Log.e(TAG, "Unrecovered transport exception while " + doing, e);\r
+\r
+        } else {\r
+            Log.e(TAG, "Unexpected exception while " + doing, e);\r
+        }\r
+    }\r
+\r
+    \r
+    /**\r
+     * Sets the connection and wait-for-data timeouts to be applied by default to the methods performed by this client.\r
+     */\r
+    public void setDefaultTimeouts(int defaultDataTimeout, int defaultConnectionTimeout) {\r
+            getParams().setSoTimeout(defaultDataTimeout);\r
+            getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout);\r
+    }\r
+\r
+    /**\r
+     * Sets the base URI for the helper methods that receive paths as parameters, instead of full URLs\r
+     * @param uri\r
+     */\r
+    public void setBaseUri(Uri uri) {\r
+        mUri = uri;\r
+    }\r
+    \r
 }\r