Heavy refactoring: Moved UI things to UI packages
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / WebdavClient.java
index 9ff1ed9..8459803 100644 (file)
@@ -1,11 +1,20 @@
 package eu.alefzero.owncloud;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 
 import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.params.ConnManagerPNames;
@@ -62,7 +71,27 @@ public class WebdavClient {
     mSchemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
   }
   
-  boolean downloadFile(String filepath) {
+  boolean downloadFile(String filepath, File targetPath) {
+    HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20"));
+    get.setHeader("Host", mUri.getHost());
+    get.setHeader("User-Agent", "Android-ownCloud");
+    
+    try {
+      HttpResponse response = mHttpClient.execute(mTargetHost, get, mHttpContext);
+      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
+        return false;
+      }
+      BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
+      FileOutputStream fos = new FileOutputStream(targetPath);
+      
+      byte[] bytes = new byte[512];
+      int readResult;
+      while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult);
+      
+    } catch (IOException e) {
+      e.printStackTrace();
+      return false;
+    }
     return true;
   }
   
@@ -100,7 +129,7 @@ public class WebdavClient {
 */
       Log.i(TAG, "Uploading, done");
     } catch (final Exception e) {
-      Log.i(TAG, e.getLocalizedMessage());
+      Log.i(TAG, ""+e.getMessage());
       result = false;
     }