sync option in filelist menu
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
index 722614e..1f19629 100644 (file)
 package eu.alefzero.webdav;
 
 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.commons.httpclient.Credentials;\r
+import org.apache.commons.httpclient.HttpClient;\r
+import org.apache.commons.httpclient.HttpMethod;\r
+import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
+import org.apache.commons.httpclient.auth.AuthScope;\r
+import org.apache.commons.httpclient.methods.HeadMethod;\r
 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.HttpHead;\r
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.params.ConnManagerPNames;
@@ -41,7 +43,6 @@ import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.conn.ssl.SSLSocketFactory;
 import org.apache.http.entity.FileEntity;
-import org.apache.http.entity.mime.content.FileBody;
 import org.apache.http.impl.auth.BasicScheme;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
@@ -56,12 +57,13 @@ import eu.alefzero.webdav.HttpMkCol;
 import android.net.Uri;
 import android.util.Log;
 
-public class WebdavClient {
+public class WebdavClient extends HttpClient {
   private DefaultHttpClient mHttpClient;
   private BasicHttpContext mHttpContext;
   private HttpHost mTargetHost;
   private SchemeRegistry mSchemeRegistry;
-  private Uri mUri;
+  private Uri mUri;\r
+  private Credentials mCredentials;
   final private static String TAG = "WebdavClient";
   
   public DefaultHttpClient getHttpClient() {
@@ -81,15 +83,17 @@ public class WebdavClient {
     // determine default port for http or https
     int targetPort = mTargetHost.getPort() == -1 ? 
                         ( mUri.getScheme().equals("https") ? 443 : 80)
-                        : mUri.getPort();
-
-    mHttpClient.getCredentialsProvider().setCredentials(
-        new AuthScope(mUri.getHost(), targetPort), 
-        new UsernamePasswordCredentials(username, password));
-    BasicScheme basicAuth = new BasicScheme();
-    mHttpContext.setAttribute("preemptive-auth", basicAuth);
+                        : mUri.getPort();\r
+\r
+    getParams().setAuthenticationPreemptive(true);
+    getState().setCredentials(AuthScope.ANY, getCredentials(username, password));
   }
   
+  private Credentials getCredentials(String username, String password) {\r
+    if (mCredentials == null)\r
+      mCredentials = new UsernamePasswordCredentials(username, password); \r
+    return mCredentials;\r
+  }\r
   public void allowUnsignedCertificates() {
     // https
     mSchemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
@@ -129,7 +133,6 @@ public class WebdavClient {
     method.setHeader("User-Agent", "Android-ownCloud");
 
     try {
-      FileBody fb = new FileBody(new File(localFile, contentType));
       final FileEntity fileEntity = new FileEntity(new File(localFile), contentType);
 
       method.setEntity(fileEntity);
@@ -155,6 +158,17 @@ public class WebdavClient {
     
     return result;
   }
+  \r
+  public int tryToLogin() {\r
+    int r = 0; \r
+    HeadMethod head = new HeadMethod(mUri.toString());\r
+    try {\r
+      r = executeMethod(head);\r
+    } catch (Exception e) {\r
+      Log.e(TAG, "Error: " + e.getMessage());\r
+    }\r
+    return r;\r
+  }\r
   
   public boolean createDirectory(String path) {
     HttpMkCol method = new HttpMkCol(mUri.toString() + path + "/");