Merge branch 'master' of gitorious.org:owncloud/android-devel
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
index 66f27e4..0a231c0 100644 (file)
@@ -21,8 +21,6 @@ import java.io.BufferedInputStream;
 import java.io.File;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.File;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
-import java.net.URLDecoder;\r
-import java.net.URLEncoder;\r
 \r
 import org.apache.commons.httpclient.Credentials;\r
 import org.apache.commons.httpclient.HttpClient;\r
 \r
 import org.apache.commons.httpclient.Credentials;\r
 import org.apache.commons.httpclient.HttpClient;\r
@@ -31,13 +29,13 @@ import org.apache.commons.httpclient.auth.AuthScope;
 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.methods.GetMethod;\r
 import org.apache.commons.httpclient.methods.HeadMethod;\r
 import org.apache.commons.httpclient.methods.PutMethod;\r
-import org.apache.commons.httpclient.methods.RequestEntity;\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.jackrabbit.webdav.client.methods.MkColMethod;\r
 \r
 import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;\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.jackrabbit.webdav.client.methods.MkColMethod;\r
 \r
 import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;\r
+import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;\r
 \r
 import android.net.Uri;\r
 import android.util.Log;\r
 \r
 import android.net.Uri;\r
 import android.util.Log;\r
@@ -47,7 +45,7 @@ public class WebdavClient extends HttpClient {
     private Credentials mCredentials;\r
     final private static String TAG = "WebdavClient";\r
     private static final String USER_AGENT = "Android-ownCloud";\r
     private Credentials mCredentials;\r
     final private static String TAG = "WebdavClient";\r
     private static final String USER_AGENT = "Android-ownCloud";\r
-    private OnUploadProgressListener mUploadProgressListener;\r
+    private OnDatatransferProgressListener mDataTransferListener;\r
 \r
     public WebdavClient(Uri uri) {\r
         mUri = uri;\r
 \r
     public WebdavClient(Uri uri) {\r
         mUri = uri;\r
@@ -66,19 +64,28 @@ public class WebdavClient extends HttpClient {
         return mCredentials;\r
     }\r
 \r
         return mCredentials;\r
     }\r
 \r
-    public void allowUnsignedCertificates() {\r
+    public void allowSelfsignedCertificates() {\r
         // https\r
         Protocol.registerProtocol("https", new Protocol("https",\r
                 new EasySSLSocketFactory(), 443));\r
     }\r
 \r
         // https\r
         Protocol.registerProtocol("https", new Protocol("https",\r
                 new EasySSLSocketFactory(), 443));\r
     }\r
 \r
-    public boolean downloadFile(String filepath, File targetPath) {\r
+    public boolean downloadFile(String remoteFilepath, File targetPath) {\r
         // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",\r
         // "%20"));\r
         // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",\r
         // "%20"));\r
+        /* dvelasco - this is not necessary anymore; OCFile.mRemotePath (the origin of remoteFielPath) keeps valid URL strings\r
+        String[] splitted_filepath = remoteFilepath.split("/");\r
+        remoteFilepath = "";\r
+        for (String s : splitted_filepath) {\r
+            if (s.equals("")) continue;\r
+            remoteFilepath += "/" + URLEncoder.encode(s);\r
+        }\r
 \r
 \r
-        Log.e("ASD", mUri.toString() + filepath.replace(" ", "%20") + "");\r
+        Log.e("ASD", mUri.toString() + remoteFilepath.replace(" ", "%20") + "");\r
         GetMethod get = new GetMethod(mUri.toString()\r
         GetMethod get = new GetMethod(mUri.toString()\r
-                + filepath.replace(" ", "%20"));\r
+                + remoteFilepath.replace(" ", "%20"));\r
+        */\r
+        GetMethod get = new GetMethod(mUri.toString() + remoteFilepath);\r
 \r
         // get.setHeader("Host", mUri.getHost());\r
         // get.setHeader("User-Agent", "Android-ownCloud");\r
 \r
         // get.setHeader("Host", mUri.getHost());\r
         // get.setHeader("User-Agent", "Android-ownCloud");\r
@@ -93,10 +100,13 @@ public class WebdavClient extends HttpClient {
                     get.getResponseBodyAsStream());\r
             FileOutputStream fos = new FileOutputStream(targetPath);\r
 \r
                     get.getResponseBodyAsStream());\r
             FileOutputStream fos = new FileOutputStream(targetPath);\r
 \r
-            byte[] bytes = new byte[512];\r
+            byte[] bytes = new byte[4096];\r
             int readResult;\r
             int readResult;\r
-            while ((readResult = bis.read(bytes)) != -1)\r
+            while ((readResult = bis.read(bytes)) != -1) {\r
+                if (mDataTransferListener != null)\r
+                    mDataTransferListener.transferProgress(readResult);\r
                 fos.write(bytes, 0, readResult);\r
                 fos.write(bytes, 0, readResult);\r
+            }\r
 \r
         } catch (IOException e) {\r
             e.printStackTrace();\r
 \r
         } catch (IOException e) {\r
             e.printStackTrace();\r
@@ -105,8 +115,8 @@ public class WebdavClient extends HttpClient {
         return true;\r
     }\r
 \r
         return true;\r
     }\r
 \r
-    public void setUploadListener(OnUploadProgressListener listener) {\r
-        mUploadProgressListener = listener;\r
+    public void setDataTransferProgressListener(OnDatatransferProgressListener listener) {\r
+        mDataTransferListener = listener;\r
     }\r
     \r
     public boolean putFile(String localFile, String remoteTarget,\r
     }\r
     \r
     public boolean putFile(String localFile, String remoteTarget,\r
@@ -117,7 +127,7 @@ public class WebdavClient extends HttpClient {
             Log.e("ASD", contentType + "");\r
             File f = new File(localFile);\r
             FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
             Log.e("ASD", contentType + "");\r
             File f = new File(localFile);\r
             FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
-            entity.setOnUploadProgressListener(mUploadProgressListener);\r
+            entity.setOnDatatransferProgressListener(mDataTransferListener);\r
             Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
             PutMethod put = new PutMethod(mUri.toString() + remoteTarget);\r
             put.setRequestEntity(entity);\r
             Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
             PutMethod put = new PutMethod(mUri.toString() + remoteTarget);\r
             put.setRequestEntity(entity);\r
@@ -147,8 +157,7 @@ public class WebdavClient extends HttpClient {
 \r
     public boolean createDirectory(String path) {\r
         try {\r
 \r
     public boolean createDirectory(String path) {\r
         try {\r
-            MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path\r
-                    + "/");\r
+            MkColMethod mkcol = new MkColMethod(mUri.toString() + path);\r
             int status = executeMethod(mkcol);\r
             Log.d(TAG, "Status returned " + status);\r
             Log.d(TAG, "uri: " + mkcol.getURI().toString());\r
             int status = executeMethod(mkcol);\r
             Log.d(TAG, "Status returned " + status);\r
             Log.d(TAG, "uri: " + mkcol.getURI().toString());\r