Quick patch to fix operations not ready for HTTPS
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / AbstractOwnCloudSyncAdapter.java
index 8d5c209..fa982e7 100644 (file)
-package eu.alefzero.owncloud.syncadapter;
-
-import java.io.IOException;
-import java.net.UnknownHostException;
-import java.util.Date;
-import java.util.LinkedList;
-
-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;
-
-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;
-
-/**
- * Base SyncAdapter for OwnCloud
- * Designed to be subclassed for the concreete SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
- * 
- * @author sassman
- *
- */
-public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAdapter {
-
-       private AccountManager accountManager;
-       private Account account;
-       private ContentProviderClient contentProvider;
-       private Date lastUpdated;
-       
-       private DefaultHttpClient client = null;
-       private HttpHost host;
-
-       public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
-               super(context, autoInitialize);
-               this.setAccountManager(AccountManager.get(context));
-       }
-
-       public AccountManager getAccountManager() {
-               return accountManager;
-       }
-
-       public void setAccountManager(AccountManager accountManager) {
-               this.accountManager = accountManager;
-       }
-
-       public Account getAccount() {
-               return account;
-       }
-
-       public void setAccount(Account account) {
-               this.account = account;
-       }
-
-       public ContentProviderClient getContentProvider() {
-               return contentProvider;
-       }
-
-       public void setContentProvider(ContentProviderClient contentProvider) {
-               this.contentProvider = contentProvider;
-       }
-
-       public Date getLastUpdated() {
-               return lastUpdated;
-       }
-
-       public void setLastUpdated(Date lastUpdated) {
-               this.lastUpdated = lastUpdated;
-       }
-       
-       protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
-               return new ConnectionKeepAliveStrategy() {
-                       public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
-                               // TODO: change keep alive straategy basing on response: ie forbidden/not found/etc
-                               // should have keep alive 0
-                               // default return: 5s
-                               return 5 * 1000;
-                       }
-               };
-       }
-       
-       protected HttpPropFind getBasicQuery() 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);
-               
-               TreeNode root = new TreeNode();
-               root.setProperty(TreeNode.NodeProperty.NAME, "/");
-               this.parseResponse(response, getUri(), getClient(), this.host, httpContext, root.getChildList());
-               return root;
-       }
-       
-       private Uri getUri() {
-               return Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL));
-       }
-       
-       private DefaultHttpClient getClient() throws OperationCanceledException, AuthenticatorException, IOException {
-               if(this.client == 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) {
-                               throw new UnknownHostException();
-                       }
-                       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");
-               }
-               
-               return this.client;
-       }
-       
-       private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, BasicHttpContext httpContext, LinkedList<TreeNode> insertList) throws IOException {
-               boolean skipFirst = true;
-               for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {
-                       String path = n.stripPathFromFilename(uri.getPath());
-                       if (skipFirst) {
-                               skipFirst = false;
-                               continue;
-                       }
-                       insertList.add(n);
-
-                       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());
-                       }
-               }
-       }
-       
+/* 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
+\r
+package eu.alefzero.owncloud.syncadapter;\r
+\r
+import java.io.IOException;\r
+import java.net.UnknownHostException;\r
+import java.util.Date;\r
+\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.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 eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
+import eu.alefzero.owncloud.datamodel.DataStorageManager;\r
+import eu.alefzero.webdav.WebdavClient;\r
+\r
+/**\r
+ * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete\r
+ * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..\r
+ * \r
+ * @author sassman\r
+ * \r
+ */\r
+public abstract class AbstractOwnCloudSyncAdapter extends\r
+        AbstractThreadedSyncAdapter {\r
+\r
+    private AccountManager accountManager;\r
+    private Account account;\r
+    private ContentProviderClient contentProvider;\r
+    private Date lastUpdated;\r
+    private DataStorageManager mStoreManager;\r
+\r
+    private WebdavClient mClient = null;\r
+\r
+    public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {\r
+        super(context, autoInitialize);\r
+        this.setAccountManager(AccountManager.get(context));\r
+    }\r
+\r
+    public AccountManager getAccountManager() {\r
+        return accountManager;\r
+    }\r
+\r
+    public void setAccountManager(AccountManager accountManager) {\r
+        this.accountManager = accountManager;\r
+    }\r
+\r
+    public Account getAccount() {\r
+        return account;\r
+    }\r
+\r
+    public void setAccount(Account account) {\r
+        this.account = account;\r
+    }\r
+\r
+    public ContentProviderClient getContentProvider() {\r
+        return contentProvider;\r
+    }\r
+\r
+    public void setContentProvider(ContentProviderClient contentProvider) {\r
+        this.contentProvider = contentProvider;\r
+    }\r
+\r
+    public Date getLastUpdated() {\r
+        return lastUpdated;\r
+    }\r
+\r
+    public void setLastUpdated(Date lastUpdated) {\r
+        this.lastUpdated = lastUpdated;\r
+    }\r
+\r
+    public void setStorageManager(DataStorageManager storage_manager) {\r
+        mStoreManager = storage_manager;\r
+    }\r
+\r
+    public DataStorageManager getStorageManager() {\r
+        return mStoreManager;\r
+    }\r
+\r
+    protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {\r
+        return new ConnectionKeepAliveStrategy() {\r
+            public long getKeepAliveDuration(HttpResponse response,\r
+                    HttpContext context) {\r
+                // Change keep alive straategy basing on response: ie\r
+                // forbidden/not found/etc\r
+                // should have keep alive 0\r
+                // default return: 5s\r
+                int statusCode = response.getStatusLine().getStatusCode();\r
+\r
+                // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed\r
+                // out\r
+                if ((statusCode >= 400 && statusCode <= 418)\r
+                        || (statusCode >= 421 && statusCode <= 426)\r
+                        || (statusCode >= 500 && statusCode <= 510)\r
+                        || statusCode == 118) {\r
+                    return 0;\r
+                }\r
+\r
+                return 5 * 1000;\r
+            }\r
+        };\r
+    }\r
+\r
+    protected HttpResponse fireRawRequest(HttpRequest query)\r
+            throws ClientProtocolException, OperationCanceledException,\r
+            AuthenticatorException, IOException {\r
+        /*\r
+         * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme\r
+         * basicAuth = new BasicScheme();\r
+         * httpContext.setAttribute("preemptive-auth", basicAuth);\r
+         * \r
+         * HttpResponse response = getClient().execute(mHost, query,\r
+         * httpContext);\r
+         */\r
+        return null;\r
+    }\r
+\r
+    protected Uri getUri() {\r
+        return Uri.parse(this.getAccountManager().getUserData(getAccount(),\r
+                AccountAuthenticator.KEY_OC_URL));\r
+    }\r
+\r
+    protected WebdavClient getClient() throws OperationCanceledException,\r
+            AuthenticatorException, IOException {\r
+        if (mClient == null) {\r
+            if (this.getAccountManager().getUserData(getAccount(),\r
+                    AccountAuthenticator.KEY_OC_URL) == null) {\r
+                throw new UnknownHostException();\r
+            }\r
+            mClient = new WebdavClient(account, getContext());\r
+            mClient.allowSelfsignedCertificates();\r
+            // mHost = mClient.getTargetHost();\r
+        }\r
+\r
+        return mClient;\r
+    }\r
 }
\ No newline at end of file