Merge branch 'develop' into oauth_login
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
index 9830f66..0b3cf81 100644 (file)
@@ -15,6 +15,7 @@
  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
+
 package eu.alefzero.webdav;
 
 import java.io.BufferedInputStream;
@@ -22,14 +23,20 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HostConfiguration;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpConnectionManager;
 import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.HttpState;
 import org.apache.commons.httpclient.HttpVersion;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthPolicy;
 import org.apache.commons.httpclient.auth.AuthScope;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.HeadMethod;
@@ -39,12 +46,13 @@ import org.apache.http.HttpStatus;
 import org.apache.http.params.CoreProtocolPNames;
 import org.apache.jackrabbit.webdav.client.methods.DavMethod;
 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
-import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
 
 import com.owncloud.android.Log_OC;
 
+import com.owncloud.android.network.BearerAuthScheme;
+import com.owncloud.android.network.BearerCredentials;
+
 import android.net.Uri;
-import android.util.Log;
 
 public class WebdavClient extends HttpClient {
     private Uri mUri;
@@ -65,16 +73,25 @@ public class WebdavClient extends HttpClient {
         getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     }
 
-    public void setCredentials(String username, String password) {
-        getParams().setAuthenticationPreemptive(true);
-        getState().setCredentials(AuthScope.ANY,
-                getCredentials(username, password));
+    public void setBearerCredentials(String accessToken) {
+        AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
+        
+        List<String> authPrefs = new ArrayList<String>(1);
+        authPrefs.add(BearerAuthScheme.AUTH_POLICY);
+        getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);        
+        
+        mCredentials = new BearerCredentials(accessToken);
+        getState().setCredentials(AuthScope.ANY, mCredentials);
     }
 
-    private Credentials getCredentials(String username, String password) {
-        if (mCredentials == null)
-            mCredentials = new UsernamePasswordCredentials(username, password);
-        return mCredentials;
+    public void setBasicCredentials(String username, String password) {
+        List<String> authPrefs = new ArrayList<String>(1);
+        authPrefs.add(AuthPolicy.BASIC);
+        getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);        
+        
+        getParams().setAuthenticationPreemptive(true);
+        mCredentials = new UsernamePasswordCredentials(username, password);
+        getState().setCredentials(AuthScope.ANY, mCredentials);
     }
     
     /**
@@ -122,6 +139,7 @@ public class WebdavClient extends HttpClient {
         return ret;
     }
     
+    
     /**
      * Deletes a remote file via webdav
      * @param remoteFilePath       Remote file path of the file to delete, in URL DECODED format.
@@ -204,35 +222,6 @@ public class WebdavClient extends HttpClient {
     }
 
     /**
-     * Creates a remote directory with the received path.
-     * 
-     * @param path      Path of the directory to create, URL DECODED
-     * @return          'True' when the directory is successfully created
-     */
-    public boolean createDirectory(String path) {
-        boolean result = false;
-        int status = -1;
-        MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));
-        try {
-            Log_OC.d(TAG, "Creating directory " + path);
-            status = executeMethod(mkcol);
-            Log_OC.d(TAG, "Status returned: " + status);
-            result = mkcol.succeeded();
-            
-            Log_OC.d(TAG, "MKCOL to " + path + " finished with HTTP status " + status + (!result?"(FAIL)":""));
-            exhaustResponse(mkcol.getResponseBodyAsStream());
-            
-        } catch (Exception e) {
-            logException(e, "creating directory " + path);
-            
-        } finally {
-            mkcol.releaseConnection();    // let the connection available for other methods
-        }
-        return result;
-    }
-    
-    
-    /**
      * Check if a file exists in the OC server
      * 
      * @return              'true' if the file exists; 'false' it doesn't exist
@@ -250,7 +239,7 @@ public class WebdavClient extends HttpClient {
             head.releaseConnection();    // let the connection available for other methods
         }
     }
-
+    
     /**
      * Requests the received method with the received timeout (milliseconds).
      * 
@@ -338,4 +327,18 @@ public class WebdavClient extends HttpClient {
         return mUri;
     }
 
+    @Override\r
+    public int executeMethod(HostConfiguration hostconfig, final HttpMethod method, final HttpState state) throws IOException, HttpException  {\r
+        if (mCredentials instanceof BearerCredentials) {\r
+            method.getHostAuthState().setAuthScheme(AuthPolicy.getAuthScheme(BearerAuthScheme.AUTH_POLICY));\r
+            method.getHostAuthState().setAuthAttempted(true);\r
+        }\r
+        return super.executeMethod(hostconfig, method, state);\r
+    }\r
+\r
+    \r
+    public final Credentials getCredentials() {\r
+        return mCredentials;\r
+    }\r
+\r
 }