Better failure handling in files renaming and removal
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
index 1af39b4..1135daf 100644 (file)
@@ -142,12 +142,8 @@ public class WebdavClient extends HttpClient {
      */\r
     public boolean downloadFile(String remoteFilepath, File targetFile) {\r
         boolean ret = false;\r
-        boolean caughtException = false;\r
         GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));\r
 \r
-        // get.setHeader("Host", mUri.getHost());\r
-        // get.setHeader("User-Agent", "Android-ownCloud");\r
-\r
         int status = -1;\r
         try {\r
             status = executeMethod(get);\r
@@ -169,19 +165,16 @@ public class WebdavClient extends HttpClient {
             \r
         } catch (HttpException e) {\r
             Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e);\r
-            caughtException = true;\r
 \r
         } catch (IOException e) {\r
             Log.e(TAG, "I/O exception downloading " + remoteFilepath, e);\r
-            caughtException = true;\r
 \r
         } catch (Exception e) {\r
             Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e);\r
-            caughtException = true;\r
             \r
         } finally {\r
             if (!ret) {\r
-                if (!caughtException) {\r
+                if (status >= 0) {\r
                     Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status);\r
                 }\r
                 if (targetFile.exists()) {\r
@@ -221,32 +214,34 @@ public class WebdavClient extends HttpClient {
      * @param contentType       MIME type of the file.\r
      * @return                  'True' then the upload was successfully completed\r
      */\r
-    public boolean putFile(String localFile, String remoteTarget,\r
-            String contentType) {\r
+    public boolean putFile(String localFile, String remoteTarget, String contentType) {\r
         boolean result = false;\r
+        int status = -1;\r
 \r
         try {\r
-            Log.e("ASD", contentType + "");\r
             File f = new File(localFile);\r
             FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
             entity.setOnDatatransferProgressListener(mDataTransferListener);\r
-            Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
             PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));\r
             put.setRequestEntity(entity);\r
-            Log.d(TAG, "" + put.getURI().toString());\r
-            int status = executeMethod(put, 0);\r
-            Log.d(TAG, "PUT method return with status " + status);\r
-\r
-            if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT) {\r
-                result = true;\r
-                Log.i(TAG, "Uploading, done");\r
-            }\r
+            status = executeMethod(put);\r
             \r
-        } catch (final Exception e) {\r
-            Log.i(TAG, "" + e.getMessage());\r
-            result = false;\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
+\r
+        } catch (Exception e) {\r
+            Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);\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
@@ -264,8 +259,12 @@ public class WebdavClient extends HttpClient {
         HeadMethod head = new HeadMethod(uri.toString());\r
         try {\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
         } catch (Exception e) {\r
-            Log.e(TAG, "Error: " + e.getMessage());\r
+            Log.e(TAG, "Unexpected exception trying to login at " + uri.getEncodedPath(), e);\r
         }\r
         return returnCode;\r
     }\r
@@ -277,17 +276,29 @@ public class WebdavClient extends HttpClient {
      * @return          'True' when the directory is successfully created\r
      */\r
     public boolean createDirectory(String path) {\r
+        boolean result = false;\r
+        int status = -1;\r
         try {\r
             MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
-            int status = executeMethod(mkcol);\r
-            Log.d(TAG, "Status returned " + status);\r
-            Log.d(TAG, "uri: " + mkcol.getURI().toString());\r
-            Log.i(TAG, "Creating dir completed");\r
-        } catch (final Exception e) {\r
-            e.printStackTrace();\r
-            return false;\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
+        } catch (Exception e) {\r
+            Log.e(TAG, "Unexpected exception creating directory " + path, e);\r
+            \r
         }\r
-        return true;\r
+        if (!result && status >= 0) {\r
+            Log.e(TAG, "Creation of directory " + path + " failed with HTTP status " + status);\r
+        }\r
+        return result;\r
     }\r
     \r
     \r