60520ed473a5b7cb2bd6031240a5d0c94e659555
[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 android.util.Log;
46 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
47 import eu.alefzero.owncloud.datamodel.OCFile;
48 import eu.alefzero.webdav.HttpPropFind;
49 import eu.alefzero.webdav.TreeNode;
50 import eu.alefzero.webdav.TreeNode.NodeProperty;
51 import eu.alefzero.webdav.WebdavClient;
52 import eu.alefzero.webdav.WebdavUtils;
53
54 /**
55 * Base SyncAdapter for OwnCloud
56 * Designed to be subclassed for the concrete SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
57 *
58 * @author sassman
59 *
60 */
61 public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAdapter {
62
63 private AccountManager accountManager;
64 private Account account;
65 private ContentProviderClient contentProvider;
66 private Date lastUpdated;
67
68 private HttpHost mHost;
69 private WebdavClient mClient = null;
70 private static String TAG = "AbstractOwnCloudSyncAdapter";
71
72 public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
73 super(context, autoInitialize);
74 this.setAccountManager(AccountManager.get(context));
75 }
76
77 public AccountManager getAccountManager() {
78 return accountManager;
79 }
80
81 public void setAccountManager(AccountManager accountManager) {
82 this.accountManager = accountManager;
83 }
84
85 public Account getAccount() {
86 return account;
87 }
88
89 public void setAccount(Account account) {
90 this.account = account;
91 }
92
93 public ContentProviderClient getContentProvider() {
94 return contentProvider;
95 }
96
97 public void setContentProvider(ContentProviderClient contentProvider) {
98 this.contentProvider = contentProvider;
99 }
100
101 public Date getLastUpdated() {
102 return lastUpdated;
103 }
104
105 public void setLastUpdated(Date lastUpdated) {
106 this.lastUpdated = lastUpdated;
107 }
108
109 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
110 return new ConnectionKeepAliveStrategy() {
111 public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
112 // Change keep alive straategy basing on response: ie forbidden/not found/etc
113 // should have keep alive 0
114 // default return: 5s
115 int statusCode = response.getStatusLine().getStatusCode();
116
117 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed out
118 if((statusCode >= 400 && statusCode <= 418) ||
119 (statusCode >= 421 && statusCode <= 426) ||
120 (statusCode >= 500 && statusCode <= 510 ) ||
121 statusCode == 118) {
122 return 0;
123 }
124
125 return 5 * 1000;
126 }
127 };
128 }
129
130 protected HttpPropFind getPropFindQuery() throws OperationCanceledException, AuthenticatorException, IOException {
131 HttpPropFind query = new HttpPropFind(getUri().toString());
132 query.setHeader("Content-type", "text/xml");
133 query.setHeader("User-Agent", "Android-ownCloud");
134 return query;
135 }
136
137 protected HttpResponse fireRawRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
138 BasicHttpContext httpContext = new BasicHttpContext();
139 BasicScheme basicAuth = new BasicScheme();
140 httpContext.setAttribute("preemptive-auth", basicAuth);
141
142 HttpResponse response = getClient().execute(mHost, query, httpContext);
143 return response;
144 }
145
146 protected TreeNode fireRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
147 HttpResponse response = fireRawRequest(query);
148
149 TreeNode root = new TreeNode();
150 root.setProperty(TreeNode.NodeProperty.NAME, "");
151 this.parseResponse(response, getUri(), getClient(), mHost, root.getChildList(), false, 0);
152 return root;
153 }
154
155 protected Uri getUri() {
156 return Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL));
157 }
158
159 private DefaultHttpClient getClient() throws OperationCanceledException, AuthenticatorException, IOException {
160 if(mClient == null) {
161 String username = getAccount().name.split("@")[0];
162 String password = this.getAccountManager().blockingGetAuthToken(getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);
163 if (this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) == null) {
164 throw new UnknownHostException();
165 }
166 Uri uri = getUri();
167
168 mClient = new WebdavClient(uri);
169 mClient.setCredentials(username, password);
170 mClient.allowUnsignedCertificates();
171 mHost = mClient.getTargetHost();
172 }
173
174 return mClient.getHttpClient();
175 }
176
177 private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, LinkedList<TreeNode> insertList, boolean sf, long parent_id) throws IOException, OperationCanceledException, AuthenticatorException {
178 boolean skipFirst = sf, override_parent = !sf;
179 for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {
180 if (skipFirst) {
181 skipFirst = false;
182 continue;
183 }
184 String path = n.stripPathFromFilename(uri.getPath());
185
186 long mod = n.getProperty(NodeProperty.LAST_MODIFIED_DATE) == null ?
187 0 :
188 Long.parseLong(n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
189 OCFile file = new OCFile(getContentProvider(), getAccount(), n.getProperty(NodeProperty.PATH));
190 if (file.fileExtist() && file.getModificationTimestamp() >= mod) {
191 Log.d(TAG, "No update for file/dir " + file.getFileName() + " is needed");
192 } else {
193 Log.d(TAG, "File " + n.getProperty(NodeProperty.PATH) + " will be " + (file.fileExtist() ? "updated" : "created"));
194 long len = n.getProperty(NodeProperty.CONTENT_LENGTH) == null ?
195 0 :
196 Long.parseLong(n.getProperty(NodeProperty.CONTENT_LENGTH));
197 long create = n.getProperty(NodeProperty.CREATE_DATE) == null ?
198 0 :
199 Long.parseLong(n.getProperty(NodeProperty.CREATE_DATE));
200 file = OCFile.createNewFile(getContentProvider(),
201 getAccount(),
202 n.getProperty(NodeProperty.PATH),
203 len,
204 create,
205 mod,
206 n.getProperty(NodeProperty.RESOURCE_TYPE),
207 parent_id);
208 file.save();
209 if (override_parent) {
210 parent_id = file.getFileId();
211 override_parent = false;
212 }
213 }
214
215 if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME)) &&
216 n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
217
218 HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
219 HttpResponse response = fireRawRequest(method);
220 parseResponse(response, uri, client, targetHost, n.getChildList(), true, file.getFileId());
221 }
222 }
223 }
224 }