model for file representation
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / AbstractOwnCloudSyncAdapter.java
index 8d5c209..e4bacf2 100644 (file)
@@ -1,35 +1,53 @@
-package eu.alefzero.owncloud.syncadapter;
-
-import java.io.IOException;
-import java.net.UnknownHostException;
-import java.util.Date;
-import java.util.LinkedList;
+/* ownCloud Android client application\r
+ *   Copyright (C) 2011  Bartek Przybylski\r
+ *\r
+ *   This program is free software: you can redistribute it and/or modify\r
+ *   it under the terms of the GNU General Public License as published by\r
+ *   the Free Software Foundation, either version 3 of the License, or\r
+ *   (at your option) any later version.\r
+ *\r
+ *   This program is distributed in the hope that it will be useful,\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *   GNU General Public License for more details.\r
+ *\r
+ *   You should have received a copy of the GNU General Public License\r
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
 
-import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.conn.ConnectionKeepAliveStrategy;
-import org.apache.http.impl.auth.BasicScheme;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.HttpContext;
+package eu.alefzero.owncloud.syncadapter;
 
-import android.accounts.Account;
-import android.accounts.AccountManager;
-import android.accounts.AuthenticatorException;
-import android.accounts.OperationCanceledException;
-import android.content.AbstractThreadedSyncAdapter;
-import android.content.ContentProviderClient;
-import android.content.Context;
-import android.net.Uri;
-import android.text.TextUtils;
-import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
-import eu.alefzero.webdav.HttpPropFind;
-import eu.alefzero.webdav.TreeNode;
-import eu.alefzero.webdav.TreeNode.NodeProperty;
-import eu.alefzero.webdav.WebdavUtils;
+import java.io.IOException;\r
+import java.net.UnknownHostException;\r
+import java.util.Date;\r
+import java.util.LinkedList;\r
+\r
+import org.apache.http.HttpHost;\r
+import org.apache.http.HttpRequest;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.conn.ConnectionKeepAliveStrategy;\r
+import org.apache.http.impl.auth.BasicScheme;\r
+import org.apache.http.impl.client.DefaultHttpClient;\r
+import org.apache.http.protocol.BasicHttpContext;\r
+import org.apache.http.protocol.HttpContext;\r
+\r
+import android.accounts.Account;\r
+import android.accounts.AccountManager;\r
+import android.accounts.AuthenticatorException;\r
+import android.accounts.OperationCanceledException;\r
+import android.content.AbstractThreadedSyncAdapter;\r
+import android.content.ContentProviderClient;\r
+import android.content.Context;\r
+import android.net.Uri;\r
+import android.text.TextUtils;\r
+import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
+import eu.alefzero.webdav.HttpPropFind;\r
+import eu.alefzero.webdav.TreeNode;\r
+import eu.alefzero.webdav.WebdavClient;\r
+import eu.alefzero.webdav.WebdavUtils;\r
+import eu.alefzero.webdav.TreeNode.NodeProperty;\r
 
 /**
  * Base SyncAdapter for OwnCloud
@@ -45,8 +63,8 @@ public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAd
        private ContentProviderClient contentProvider;
        private Date lastUpdated;
        
-       private DefaultHttpClient client = null;
-       private HttpHost host;
+       private HttpHost mHost;
+       private WebdavClient mClient = null;
 
        public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
                super(context, autoInitialize);
@@ -96,32 +114,37 @@ public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAd
                };
        }
        
-       protected HttpPropFind getBasicQuery() throws OperationCanceledException, AuthenticatorException, IOException {
+       protected HttpPropFind getPropFindQuery() throws OperationCanceledException, AuthenticatorException, IOException {
                HttpPropFind query = new HttpPropFind(getUri().toString());
                query.setHeader("Content-type", "text/xml");
                query.setHeader("User-Agent", "Android-ownCloud");
                return query;
        }
        
-       protected TreeNode fireQuery(HttpPropFind query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
-               BasicHttpContext httpContext = new BasicHttpContext();
-               BasicScheme basicAuth = new BasicScheme();
-               httpContext.setAttribute("preemptive-auth", basicAuth);
-               
-               HttpResponse response = getClient().execute(this.host, query, httpContext);
+       protected HttpResponse fireRawRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
+           BasicHttpContext httpContext = new BasicHttpContext();
+        BasicScheme basicAuth = new BasicScheme();
+        httpContext.setAttribute("preemptive-auth", basicAuth);
+        
+        HttpResponse response = getClient().execute(mHost, query, httpContext);
+        return response;
+       }
+       
+       protected TreeNode fireRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
+               HttpResponse response = fireRawRequest(query);
                
                TreeNode root = new TreeNode();
                root.setProperty(TreeNode.NodeProperty.NAME, "/");
-               this.parseResponse(response, getUri(), getClient(), this.host, httpContext, root.getChildList());
+               this.parseResponse(response, getUri(), getClient(), mHost, root.getChildList());
                return root;
        }
        
-       private Uri getUri() {
+       protected Uri getUri() {
                return Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL));
        }
        
        private DefaultHttpClient getClient() throws OperationCanceledException, AuthenticatorException, IOException {
-               if(this.client == null) {
+               if(mClient == null) {
                        String username = getAccount().name.split("@")[0];
                        String password = this.getAccountManager().blockingGetAuthToken(getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);
                        if (this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) == null) {
@@ -129,20 +152,16 @@ public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAd
                        }
                        Uri uri = getUri();
        
-                       int port = (uri.getPort() == -1) ? 80 : uri.getPort();
-                       this.client = new DefaultHttpClient();
-                       this.client.getCredentialsProvider().setCredentials(
-                               new AuthScope(uri.getHost(), port),
-                               new UsernamePasswordCredentials(username, password)
-                       );
-                       this.client.setKeepAliveStrategy(this.getKeepAliveStrategy());
-                       this.host = new HttpHost(uri.getHost(), port, (uri.getScheme() == "https") ? "https" : "http");
+                       mClient = new WebdavClient(uri);
+                       mClient.setCredentials(username, password);
+                       mClient.allowUnsignedCertificates();
+                       mHost = mClient.getTargetHost();
                }
                
-               return this.client;
+               return mClient.getHttpClient();
        }
        
-       private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, BasicHttpContext httpContext, LinkedList<TreeNode> insertList) throws IOException {
+       private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, LinkedList<TreeNode> insertList) throws IOException, OperationCanceledException, AuthenticatorException {
                boolean skipFirst = true;
                for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {
                        String path = n.stripPathFromFilename(uri.getPath());
@@ -154,11 +173,11 @@ public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAd
 
                        if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME)) &&
                                        n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
-                               HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
-                               HttpResponse response = client.execute(targetHost, method, httpContext);
-                               parseResponse(response, uri, client, targetHost, httpContext, n.getChildList());
+                           
+                           HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
+                               HttpResponse response = fireRawRequest(method);
+                               parseResponse(response, uri, client, targetHost, n.getChildList());
                        }
                }
        }
-       
 }
\ No newline at end of file