ssl support for syncing
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / AbstractOwnCloudSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.syncadapter;
20
21 import java.io.IOException;
22 import java.net.UnknownHostException;
23 import java.util.Date;
24 import java.util.LinkedList;
25
26 import org.apache.http.HttpHost;
27 import org.apache.http.HttpRequest;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.ClientProtocolException;
30 import org.apache.http.conn.ConnectionKeepAliveStrategy;
31 import org.apache.http.impl.auth.BasicScheme;
32 import org.apache.http.impl.client.DefaultHttpClient;
33 import org.apache.http.protocol.BasicHttpContext;
34 import org.apache.http.protocol.HttpContext;
35
36 import android.accounts.Account;
37 import android.accounts.AccountManager;
38 import android.accounts.AuthenticatorException;
39 import android.accounts.OperationCanceledException;
40 import android.content.AbstractThreadedSyncAdapter;
41 import android.content.ContentProviderClient;
42 import android.content.Context;
43 import android.net.Uri;
44 import android.text.TextUtils;
45 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
46 import eu.alefzero.webdav.HttpPropFind;
47 import eu.alefzero.webdav.TreeNode;
48 import eu.alefzero.webdav.WebdavClient;
49 import eu.alefzero.webdav.WebdavUtils;
50 import eu.alefzero.webdav.TreeNode.NodeProperty;
51
52 /**
53 * Base SyncAdapter for OwnCloud
54 * Designed to be subclassed for the concreete SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
55 *
56 * @author sassman
57 *
58 */
59 public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAdapter {
60
61 private AccountManager accountManager;
62 private Account account;
63 private ContentProviderClient contentProvider;
64 private Date lastUpdated;
65
66 private HttpHost mHost;
67 private WebdavClient mClient = null;
68
69 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
70 super(context, autoInitialize);
71 this.setAccountManager(AccountManager.get(context));
72 }
73
74 public AccountManager getAccountManager() {
75 return accountManager;
76 }
77
78 public void setAccountManager(AccountManager accountManager) {
79 this.accountManager = accountManager;
80 }
81
82 public Account getAccount() {
83 return account;
84 }
85
86 public void setAccount(Account account) {
87 this.account = account;
88 }
89
90 public ContentProviderClient getContentProvider() {
91 return contentProvider;
92 }
93
94 public void setContentProvider(ContentProviderClient contentProvider) {
95 this.contentProvider = contentProvider;
96 }
97
98 public Date getLastUpdated() {
99 return lastUpdated;
100 }
101
102 public void setLastUpdated(Date lastUpdated) {
103 this.lastUpdated = lastUpdated;
104 }
105
106 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
107 return new ConnectionKeepAliveStrategy() {
108 public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
109 // TODO: change keep alive straategy basing on response: ie forbidden/not found/etc
110 // should have keep alive 0
111 // default return: 5s
112 return 5 * 1000;
113 }
114 };
115 }
116
117 protected HttpPropFind getPropFindQuery() throws OperationCanceledException, AuthenticatorException, IOException {
118 HttpPropFind query = new HttpPropFind(getUri().toString());
119 query.setHeader("Content-type", "text/xml");
120 query.setHeader("User-Agent", "Android-ownCloud");
121 return query;
122 }
123
124 protected HttpResponse fireRawRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
125 BasicHttpContext httpContext = new BasicHttpContext();
126 BasicScheme basicAuth = new BasicScheme();
127 httpContext.setAttribute("preemptive-auth", basicAuth);
128
129 HttpResponse response = getClient().execute(mHost, query, httpContext);
130 return response;
131 }
132
133 protected TreeNode fireRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
134 HttpResponse response = fireRawRequest(query);
135
136 TreeNode root = new TreeNode();
137 root.setProperty(TreeNode.NodeProperty.NAME, "/");
138 this.parseResponse(response, getUri(), getClient(), mHost, root.getChildList());
139 return root;
140 }
141
142 protected Uri getUri() {
143 return Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL));
144 }
145
146 private DefaultHttpClient getClient() throws OperationCanceledException, AuthenticatorException, IOException {
147 if(mClient == null) {
148 String username = getAccount().name.split("@")[0];
149 String password = this.getAccountManager().blockingGetAuthToken(getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);
150 if (this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) == null) {
151 throw new UnknownHostException();
152 }
153 Uri uri = getUri();
154
155 mClient = new WebdavClient(uri);
156 mClient.setCredentials(username, password);
157 mClient.allowUnsignedCertificates();
158 mHost = mClient.getTargetHost();
159 }
160
161 return mClient.getHttpClient();
162 }
163
164 private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, LinkedList<TreeNode> insertList) throws IOException, OperationCanceledException, AuthenticatorException {
165 boolean skipFirst = true;
166 for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {
167 String path = n.stripPathFromFilename(uri.getPath());
168 if (skipFirst) {
169 skipFirst = false;
170 continue;
171 }
172 insertList.add(n);
173
174 if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME)) &&
175 n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
176
177 HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
178 HttpResponse response = fireRawRequest(method);
179 parseResponse(response, uri, client, targetHost, n.getChildList());
180 }
181 }
182 }
183 }