Forráskód Böngészése

initial move to jackrabbit (syncing)

Bartek Przybylski 13 éve
szülő
commit
87814cec42

BIN
libs/commons-codec-1.4.jar


BIN
libs/commons-httpclient-3.0.1.jar


BIN
libs/commons-io-2.0.1.jar


BIN
libs/commons-logging-1.1.1.jar


BIN
libs/httpclient-4.1.2.jar


BIN
libs/httpclient-cache-4.1.2.jar


BIN
libs/httpcore-4.1.2.jar


BIN
libs/httpmime-4.1.2.jar


BIN
libs/not-yet-commons-ssl-0.3.11.jar


+ 1 - 1
src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java

@@ -124,7 +124,7 @@ public class FileDataStorageManager implements DataStorageManager {
         file.setFileId(new_id);
       }
     }
-    
+
     if (file.isDirectory() && file.needsUpdatingWhileSaving())
       for (OCFile f : getDirectoryContent(file))
         saveFile(f);

+ 7 - 7
src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java

@@ -153,12 +153,12 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 	protected HttpResponse fireRawRequest(HttpRequest query)
 			throws ClientProtocolException, OperationCanceledException,
 			AuthenticatorException, IOException {
-		BasicHttpContext httpContext = new BasicHttpContext();
+		/*BasicHttpContext httpContext = new BasicHttpContext();
 		BasicScheme basicAuth = new BasicScheme();
 		httpContext.setAttribute("preemptive-auth", basicAuth);
 
-		HttpResponse response = getClient().execute(mHost, query, httpContext);
-		return response;
+		HttpResponse response = getClient().execute(mHost, query, httpContext);*/
+		return null;
 	}
 
 	protected TreeNode fireRequest(HttpRequest query)
@@ -168,8 +168,8 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 
 		TreeNode root = new TreeNode();
 		root.setProperty(TreeNode.NodeProperty.NAME, "");
-		this.parseResponse(response, getUri(), getClient(), mHost,
-				root.getChildList(), false, 0);
+		//this.parseResponse(response, getUri(), getClient(), mHost,
+		//		root.getChildList(), false, 0);
 		return root;
 	}
 
@@ -178,7 +178,7 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 				AccountAuthenticator.KEY_OC_URL));
 	}
 
-	private DefaultHttpClient getClient() throws OperationCanceledException,
+	protected WebdavClient getClient() throws OperationCanceledException,
 			AuthenticatorException, IOException {
 		if (mClient == null) {
 			String username = getAccount().name.split("@")[0];
@@ -196,7 +196,7 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 			mHost = mClient.getTargetHost();
 		}
 
-		return mClient.getHttpClient();
+		return mClient;
 	}
 
 	private void parseResponse(HttpResponse resp, Uri uri,

+ 40 - 73
src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java

@@ -21,6 +21,11 @@ package eu.alefzero.owncloud.syncadapter;
 import java.io.IOException;
 
 import org.apache.http.entity.StringEntity;
+import org.apache.jackrabbit.webdav.DavException;
+import org.apache.jackrabbit.webdav.MultiStatus;
+import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
+import org.apache.jackrabbit.webdav.property.DavProperty;
+import org.apache.jackrabbit.webdav.property.DavPropertyName;
 
 import android.accounts.Account;
 import android.accounts.AuthenticatorException;
@@ -35,10 +40,12 @@ import android.os.Bundle;
 import android.os.RemoteException;
 import android.util.Log;
 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
+import eu.alefzero.owncloud.datamodel.OCFile;
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
 import eu.alefzero.webdav.HttpPropFind;
 import eu.alefzero.webdav.TreeNode;
 import eu.alefzero.webdav.TreeNode.NodeProperty;
+import eu.alefzero.webdav.WebdavEntry;
 import eu.alefzero.webdav.WebdavUtils;
 
 /**
@@ -53,7 +60,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
 	public FileSyncAdapter(Context context, boolean autoInitialize) {
 		super(context, autoInitialize);
 	}
-
+	
 	@Override
 	public synchronized void onPerformSync(
 			Account account, 
@@ -62,83 +69,43 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
 			ContentProviderClient provider, 
 			SyncResult syncResult) {
 
-		try {
 			this.setAccount(account);
 			this.setContentProvider(provider);
 			this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
 
-			HttpPropFind query = this.getPropFindQuery();
-			query.setEntity(new StringEntity(WebdavUtils.prepareXmlForPropFind()));
-			TreeNode root = this.fireRequest(query);
-
-		//	commitToDatabase(root, null);
-		} catch (OperationCanceledException e) {
-			e.printStackTrace();
-		} catch (AuthenticatorException e) {
-			syncResult.stats.numAuthExceptions++;
-			e.printStackTrace();
-		} catch (IOException e) {
-			syncResult.stats.numIoExceptions++;
-			e.printStackTrace();
-		}// catch (RemoteException e) {
-		//	e.printStackTrace();
-		// q}
+			fetchData(getUri().toString(), syncResult, 0);
 	}
 
-	private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {
-		for (TreeNode n : root.getChildList()) {
-			Log.d(TAG, n.toString());
-			ContentValues cv = new ContentValues();
-			cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));
-			cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
-			cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));
-			cv.put(ProviderTableMeta.FILE_PARENT, parentId);
+  private void fetchData(String uri, SyncResult syncResult, long parentId) {
+    try {
+      PropFindMethod query = new PropFindMethod(uri);
+      getClient().executeMethod(query);
+      MultiStatus resp = null;
+      resp = query.getResponseBodyAsMultiStatus();
+      for (int i = (parentId==0?0:1); i < resp.getResponses().length; ++i) {
+        WebdavEntry we = new WebdavEntry(resp.getResponses()[i]);
+        OCFile file = new OCFile(we.path());
+        file.setCreationTimestamp(we.createTimestamp());
+        file.setFileLength(we.contentLength());
+        file.setMimetype(we.contentType());
+        file.setModificationTimestamp(we.modifiedTimesamp());
+        file.setParentId(parentId);
+        if (we.contentType().equals("DIR"))
+          fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());
+      }
 
-			String name = n.getProperty(NodeProperty.NAME),
-					path = n.getProperty(NodeProperty.PATH);
-			Cursor c = this.getContentProvider().query(ProviderTableMeta.CONTENT_URI_FILE,
-					null,
-					ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
-					new String[]{name, path, this.getAccount().name},
-					null);
-			if (c.moveToFirst()) {
-				this.getContentProvider().update(ProviderTableMeta.CONTENT_URI,
-						cv,
-						ProviderTableMeta._ID+"=?",
-								new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});
-				Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
-			} else {
-				cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));
-				cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));
-				cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, this.getAccount().name);
-				Uri entry = this.getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
-				Log.d(TAG, "Inserting new entry " + path);
-				c = this.getContentProvider().query(entry, null, null, null, null);
-				c.moveToFirst();
-			}
-			if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
-				commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
-			}
-		}
-		// clean removed files
-		String[] selection = new String[root.getChildList().size()+2];
-		selection[0] = this.getAccount().name;
-		selection[1] = parentId;
-		String qm = "";
-		for (int i = 2; i < selection.length-1; ++i) {
-			qm += "?,";
-			selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);
-		}
-		if (selection.length >= 3) {
-			selection[selection.length-1] = root.getChildrenNames()[selection.length-3];
-			qm += "?";
-		}
-		for (int i = 0; i < selection.length; ++i) {
-			Log.d(TAG,selection[i]+"");
-		}
-		Log.d(TAG,"Removing files "+ parentId);
-		this.getContentProvider().delete(ProviderTableMeta.CONTENT_URI,
-				ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",
-				selection);
-	}
+    } catch (OperationCanceledException e) {
+      e.printStackTrace();
+    } catch (AuthenticatorException e) {
+      syncResult.stats.numAuthExceptions++;
+      e.printStackTrace();
+    } catch (IOException e) {
+      syncResult.stats.numIoExceptions++;
+      e.printStackTrace();
+    } catch (DavException e) {
+      syncResult.stats.numIoExceptions++;
+      e.printStackTrace();
+    }
+  }
+	
 }

+ 1 - 0
src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java

@@ -104,6 +104,7 @@ public class FileListFragment extends FragmentListView {
     String s = "/";
     for (String a : mDirNames)
       s+= a + "/";
+    Log.e("ASD", s);
 
     mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
     OCFile file = mStorageManager.getFileByPath(s);

+ 16 - 12
src/eu/alefzero/webdav/WebdavClient.java

@@ -22,12 +22,14 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.conn.ClientConnectionManager;
@@ -38,7 +40,6 @@ import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.conn.ssl.SSLSocketFactory;
 import org.apache.http.entity.FileEntity;
-import org.apache.http.entity.mime.content.FileBody;
 import org.apache.http.impl.auth.BasicScheme;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
@@ -53,12 +54,13 @@ import eu.alefzero.webdav.HttpMkCol;
 import android.net.Uri;
 import android.util.Log;
 
-public class WebdavClient {
+public class WebdavClient extends HttpClient {
   private DefaultHttpClient mHttpClient;
   private BasicHttpContext mHttpContext;
   private HttpHost mTargetHost;
   private SchemeRegistry mSchemeRegistry;
-  private Uri mUri;
+  private Uri mUri;
+  private Credentials mCredentials;
   final private static String TAG = "WebdavClient";
   
   public DefaultHttpClient getHttpClient() {
@@ -78,15 +80,17 @@ public class WebdavClient {
     // determine default port for http or https
     int targetPort = mTargetHost.getPort() == -1 ? 
                         ( mUri.getScheme().equals("https") ? 443 : 80)
-                        : mUri.getPort();
-
-    mHttpClient.getCredentialsProvider().setCredentials(
-        new AuthScope(mUri.getHost(), targetPort), 
-        new UsernamePasswordCredentials(username, password));
-    BasicScheme basicAuth = new BasicScheme();
-    mHttpContext.setAttribute("preemptive-auth", basicAuth);
+                        : mUri.getPort();
+
+    getParams().setAuthenticationPreemptive(true);
+    getState().setCredentials(AuthScope.ANY, getCredentials(username, password));
   }
   
+  private Credentials getCredentials(String username, String password) {
+    if (mCredentials == null)
+      mCredentials = new UsernamePasswordCredentials(username, password); 
+    return mCredentials;
+  }
   public void allowUnsignedCertificates() {
     // https
     mSchemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

+ 101 - 0
src/eu/alefzero/webdav/WebdavEntry.java

@@ -0,0 +1,101 @@
+package eu.alefzero.webdav;
+
+import java.util.Date;
+
+import org.apache.jackrabbit.webdav.MultiStatusResponse;
+import org.apache.jackrabbit.webdav.property.DavProperty;
+import org.apache.jackrabbit.webdav.property.DavPropertyName;
+import org.apache.jackrabbit.webdav.property.DavPropertySet;
+
+import android.util.Log;
+
+public class WebdavEntry {
+  private String mName, mPath, mUri, mContentType;
+  private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
+
+  public WebdavEntry(MultiStatusResponse ms) {
+    resetData();
+    if (ms.getStatus().length != 0) {
+      mUri = ms.getHref();
+      
+      mPath = mUri.split("webdav.php", 2)[1];
+
+      int status = ms.getStatus()[0].getStatusCode();
+      DavPropertySet propSet = ms.getProperties(status);
+      @SuppressWarnings("rawtypes")
+      DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
+      if (prop != null) mName = (String) prop.getName().toString();
+      else {
+        String[] tmp = mPath.split("/");
+        if (tmp.length > 0)
+          mName = tmp[tmp.length-1];
+      }
+
+      prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
+      if (prop != null) {
+        mContentType = (String) prop.getValue();
+      } else {
+        mContentType = "DIR";
+        /*prop = propSet.get(DavPropertyName.ISCOLLECTION);
+        if (prop != null && Boolean.parseBoolean((String) prop.getValue()))
+            mContentType = "DIR";*/
+      }
+
+      prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
+      if (prop != null)
+        mContentLength = Long.parseLong((String) prop.getValue());
+      
+      prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
+      if (prop != null) {
+        Date d = WebdavUtils.parseResponseDate((String)prop.getValue());
+        mModifiedTimestamp = (d != null) ? d.getTime() : 0;
+      }
+
+      prop = propSet.get(DavPropertyName.CREATIONDATE);
+      if (prop != null) {
+        Date d = WebdavUtils.parseResponseDate((String)prop.getValue());
+        mCreateTimestamp = (d != null) ? d.getTime() : 0;
+      }  
+
+    } else {
+      Log.e("WebdavEntry", "General fuckup, no status for webdav response");
+    }
+  }
+
+  public String path() {
+    return mPath;
+  }
+  
+  public String name() {
+    return mName;
+  }
+  
+  public boolean isDirectory() {
+    return mContentType.equals("DIR");
+  }
+
+  public String contentType() {
+    return mContentType;
+  }
+
+  public String uri() {
+    return mUri;
+  }
+  
+  public long contentLength() {
+    return mContentLength;
+  }
+  
+  public long createTimestamp() {
+    return mCreateTimestamp;
+  }
+  
+  public long modifiedTimesamp() {
+    return mModifiedTimestamp;
+  }
+  
+  private void resetData() {
+    mName = mUri = mContentType = null;
+    mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
+  }
+}

+ 1 - 1
src/eu/alefzero/webdav/WebdavUtils.java

@@ -78,7 +78,7 @@ public class WebdavUtils {
     return "<?xml version=\"1.0\" ?><D:propertyupdate xmlns:D=\"DAV:\"></D:propertyupdate>";
   }
   
-  private static Date parseResponseDate(String date) {
+  public static Date parseResponseDate(String date) {
     Date returnDate = null;
     for (int i = 0; i < DATETIME_FORMATS.length; ++i) {
       try {