1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.syncadapter
;
21 import java
.io
.IOException
;
22 import java
.net
.UnknownHostException
;
23 import java
.util
.Date
;
24 import java
.util
.LinkedList
;
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
;
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
;
55 * Base SyncAdapter for OwnCloud
56 * Designed to be subclassed for the concrete SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
61 public abstract class AbstractOwnCloudSyncAdapter
extends AbstractThreadedSyncAdapter
{
63 private AccountManager accountManager
;
64 private Account account
;
65 private ContentProviderClient contentProvider
;
66 private Date lastUpdated
;
68 private HttpHost mHost
;
69 private WebdavClient mClient
= null
;
70 private static String TAG
= "AbstractOwnCloudSyncAdapter";
72 public AbstractOwnCloudSyncAdapter(Context context
, boolean autoInitialize
) {
73 super(context
, autoInitialize
);
74 this.setAccountManager(AccountManager
.get(context
));
77 public AccountManager
getAccountManager() {
78 return accountManager
;
81 public void setAccountManager(AccountManager accountManager
) {
82 this.accountManager
= accountManager
;
85 public Account
getAccount() {
89 public void setAccount(Account account
) {
90 this.account
= account
;
93 public ContentProviderClient
getContentProvider() {
94 return contentProvider
;
97 public void setContentProvider(ContentProviderClient contentProvider
) {
98 this.contentProvider
= contentProvider
;
101 public Date
getLastUpdated() {
105 public void setLastUpdated(Date lastUpdated
) {
106 this.lastUpdated
= lastUpdated
;
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();
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 ) ||
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");
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
);
142 HttpResponse response
= getClient().execute(mHost
, query
, httpContext
);
146 protected TreeNode
fireRequest(HttpRequest query
) throws ClientProtocolException
, OperationCanceledException
, AuthenticatorException
, IOException
{
147 HttpResponse response
= fireRawRequest(query
);
149 TreeNode root
= new TreeNode();
150 root
.setProperty(TreeNode
.NodeProperty
.NAME
, "");
151 this.parseResponse(response
, getUri(), getClient(), mHost
, root
.getChildList(), false
, 0);
155 protected Uri
getUri() {
156 return Uri
.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator
.KEY_OC_URL
));
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();
168 mClient
= new WebdavClient(uri
);
169 mClient
.setCredentials(username
, password
);
170 mClient
.allowUnsignedCertificates();
171 mHost
= mClient
.getTargetHost();
174 return mClient
.getHttpClient();
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())) {
184 String path
= n
.stripPathFromFilename(uri
.getPath());
186 long mod
= n
.getProperty(NodeProperty
.LAST_MODIFIED_DATE
) == null ?
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");
193 Log
.d(TAG
, "File " + n
.getProperty(NodeProperty
.PATH
) + " will be " + (file
.fileExtist() ?
"updated" : "created"));
194 long len
= n
.getProperty(NodeProperty
.CONTENT_LENGTH
) == null ?
196 Long
.parseLong(n
.getProperty(NodeProperty
.CONTENT_LENGTH
));
197 long create
= n
.getProperty(NodeProperty
.CREATE_DATE
) == null ?
199 Long
.parseLong(n
.getProperty(NodeProperty
.CREATE_DATE
));
200 file
= OCFile
.createNewFile(getContentProvider(),
202 n
.getProperty(NodeProperty
.PATH
),
206 n
.getProperty(NodeProperty
.RESOURCE_TYPE
),
209 if (override_parent
) {
210 parent_id
= file
.getFileId();
211 override_parent
= false
;
215 if (!TextUtils
.isEmpty(n
.getProperty(NodeProperty
.NAME
)) &&
216 n
.getProperty(NodeProperty
.RESOURCE_TYPE
).equals("DIR")) {
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());