From 7b5d6cdef791cd27a573fcaa9c8214970753843c Mon Sep 17 00:00:00 2001 From: masensio Date: Mon, 27 Jan 2014 13:44:24 +0100 Subject: [PATCH 1/9] Add Share operation to the library --- .../android/lib/accounts/OwnCloudAccount.java | 4 + .../lib/operations/common/RemoteFile.java | 10 +- .../operations/common/ShareRemoteFile.java | 273 ++++++++++++++ .../lib/operations/common/ShareType.java | 78 ++++ .../remote/GetRemoteSharedFilesOperation.java | 120 ++++++ .../remote/OwnCloudServerCheckOperation.java | 172 +++++++++ .../android/lib/utils/OwnCloudVersion.java | 35 +- .../android/lib/utils/ShareXMLParser.java | 348 ++++++++++++++++++ 8 files changed, 1038 insertions(+), 2 deletions(-) create mode 100644 src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java create mode 100644 src/com/owncloud/android/lib/operations/common/ShareType.java create mode 100644 src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java create mode 100644 src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java create mode 100644 src/com/owncloud/android/lib/utils/ShareXMLParser.java diff --git a/src/com/owncloud/android/lib/accounts/OwnCloudAccount.java b/src/com/owncloud/android/lib/accounts/OwnCloudAccount.java index 9d0de82c..740bd17f 100644 --- a/src/com/owncloud/android/lib/accounts/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/accounts/OwnCloudAccount.java @@ -65,6 +65,10 @@ public class OwnCloudAccount extends Account { * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on. */ public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso"; + /** + * Flag signaling if the ownCloud server supports Share API" + */ + public static final String KEY_SUPPORTS_SHARE_API = "oc_supports_share_api"; } private String mAuthTokenType; diff --git a/src/com/owncloud/android/lib/operations/common/RemoteFile.java b/src/com/owncloud/android/lib/operations/common/RemoteFile.java index 3304b1c1..9d81a0b4 100644 --- a/src/com/owncloud/android/lib/operations/common/RemoteFile.java +++ b/src/com/owncloud/android/lib/operations/common/RemoteFile.java @@ -101,6 +101,10 @@ public class RemoteFile implements Parcelable, Serializable { public void setEtag(String etag) { this.mEtag = etag; } + + public RemoteFile() { + resetData(); + } /** * Create new {@link RemoteFile} with given path. @@ -159,7 +163,11 @@ public class RemoteFile implements Parcelable, Serializable { * * @param source The source parcel */ - private RemoteFile(Parcel source) { + protected RemoteFile(Parcel source) { + readFromParcel(source); + } + + public void readFromParcel (Parcel source) { mRemotePath = source.readString(); mMimeType = source.readString(); mLength = source.readLong(); diff --git a/src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java b/src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java new file mode 100644 index 00000000..b47c465a --- /dev/null +++ b/src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java @@ -0,0 +1,273 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.operations.common; + +import com.owncloud.android.lib.network.webdav.WebdavEntry; +import com.owncloud.android.lib.utils.FileUtils; + +import android.os.Parcel; +import android.os.Parcelable; +import android.util.Log; + + +/** + * Contains the data of a Share Remote File from the Share API + * + * @author masensio + * + */ +public class ShareRemoteFile extends RemoteFile { + + /** + * Generated - should be refreshed every time the class changes!! + */ + private static final long serialVersionUID = -5916376011588784325L; + + private static final String TAG = ShareRemoteFile.class.getSimpleName(); + + private long mFileSource; + private long mItemSource; + private ShareType mShareType; + private String mShareWith; + private String mPath; + private int mPermissions; + private long mSharedDate; + private long mExpirationDate; + private String mToken; + private String mSharedWithDisplayName; + private boolean mIsDirectory; + private long mUserId; + private long mIdRemoteShared; + + public ShareRemoteFile() { + super(); + resetData(); + } + + public ShareRemoteFile(String path) { + super(path); + resetData(); + if (path == null || path.length() <= 0 || !path.startsWith(FileUtils.PATH_SEPARATOR)) { + Log.e(TAG, "Trying to create a OCShare with a non valid path"); + throw new IllegalArgumentException("Trying to create a OCShare with a non valid path: " + path); + } + mPath = path; + } + + public ShareRemoteFile(WebdavEntry we) { + super(we); + // TODO Auto-generated constructor stub + } + + /** + * Used internally. Reset all file properties + */ + private void resetData() { + mFileSource = 0; + mItemSource = 0; + mShareType = ShareType.NO_SHARED; + mShareWith = null; + mPath = null; + mPermissions = -1; + mSharedDate = 0; + mExpirationDate = 0; + mToken = null; + mSharedWithDisplayName = null; + mIsDirectory = false; + mUserId = -1; + mIdRemoteShared = -1; + } + + /// Getters and Setters + public long getFileSource() { + return mFileSource; + } + + public void setFileSource(long fileSource) { + this.mFileSource = fileSource; + } + + public long getItemSource() { + return mItemSource; + } + + public void setItemSource(long itemSource) { + this.mItemSource = itemSource; + } + + public ShareType getShareType() { + return mShareType; + } + + public void setShareType(ShareType shareType) { + this.mShareType = shareType; + } + + public String getShareWith() { + return mShareWith; + } + + public void setShareWith(String shareWith) { + this.mShareWith = shareWith; + } + + public String getPath() { + return mPath; + } + + public void setPath(String path) { + this.mPath = path; + } + + public int getPermissions() { + return mPermissions; + } + + public void setPermissions(int permissions) { + this.mPermissions = permissions; + } + + public long getSharedDate() { + return mSharedDate; + } + + public void setSharedDate(long sharedDate) { + this.mSharedDate = sharedDate; + } + + public long getExpirationDate() { + return mExpirationDate; + } + + public void setExpirationDate(long expirationDate) { + this.mExpirationDate = expirationDate; + } + + public String getToken() { + return mToken; + } + + public void setToken(String token) { + this.mToken = token; + } + + public String getSharedWithDisplayName() { + return mSharedWithDisplayName; + } + + public void setSharedWithDisplayName(String sharedWithDisplayName) { + this.mSharedWithDisplayName = sharedWithDisplayName; + } + + public boolean isDirectory() { + return mIsDirectory; + } + + public void setIsDirectory(boolean isDirectory) { + this.mIsDirectory = isDirectory; + } + + public long getUserId() { + return mUserId; + } + + public void setUserId(long userId) { + this.mUserId = userId; + } + + public long getIdRemoteShared() { + return mIdRemoteShared; + } + + public void setIdRemoteShared(long idRemoteShared) { + this.mIdRemoteShared = idRemoteShared; + } + + /** + * Parcelable Methods + */ + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + @Override + public ShareRemoteFile createFromParcel(Parcel source) { + return new ShareRemoteFile(source); + } + + @Override + public ShareRemoteFile[] newArray(int size) { + return new ShareRemoteFile[size]; + } + }; + + /** + * Reconstruct from parcel + * + * @param source The source parcel + */ + protected ShareRemoteFile(Parcel source) { + super(source); + } + + public void readFromParcel(Parcel source) { + super.readFromParcel(source); + + mFileSource = source.readLong(); + mItemSource = source.readLong(); + try { + mShareType = ShareType.valueOf(source.readString()); + } catch (IllegalArgumentException x) { + mShareType = ShareType.NO_SHARED; + } + mShareWith = source.readString(); + mPath = source.readString(); + mPermissions = source.readInt(); + mSharedDate = source.readLong(); + mExpirationDate = source.readLong(); + mToken = source.readString(); + mSharedWithDisplayName = source.readString(); + mIsDirectory = source.readInt() == 0; + mUserId = source.readLong(); + mIdRemoteShared = source.readLong(); + } + + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + + dest.writeLong(mFileSource); + dest.writeLong(mItemSource); + dest.writeString((mShareType == null) ? "" : mShareType.name()); + dest.writeString(mShareWith); + dest.writeString(mPath); + dest.writeInt(mPermissions); + dest.writeLong(mSharedDate); + dest.writeLong(mExpirationDate); + dest.writeString(mToken); + dest.writeString(mSharedWithDisplayName); + dest.writeInt(mIsDirectory ? 1 : 0); + dest.writeLong(mUserId); + dest.writeLong(mIdRemoteShared); + } +} diff --git a/src/com/owncloud/android/lib/operations/common/ShareType.java b/src/com/owncloud/android/lib/operations/common/ShareType.java new file mode 100644 index 00000000..920dc3a3 --- /dev/null +++ b/src/com/owncloud/android/lib/operations/common/ShareType.java @@ -0,0 +1,78 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.operations.common; + +/** + * Enum for Share Type, with values: + * -1 - No shared + * 0 - Shared by user + * 1 - Shared by group + * 3 - Shared by public link + * 4 - Shared by e-mail + * 5 - Shared by contact + * + * @author masensio + * + */ + +public enum ShareType { + NO_SHARED (-1), + USER (0), + GROUP (1), + PUBLIC_LINK (3), + EMAIL (4), + CONTACT (5); + + private int value; + + private ShareType(int value) + { + this.value = value; + } + + public int getValue() { + return value; + } + + public static ShareType fromValue(int value) + { + switch (value) + { + case -1: + return NO_SHARED; + case 0: + return USER; + case 1: + return GROUP; + case 3: + return PUBLIC_LINK; + case 4: + return EMAIL; + case 5: + return CONTACT; + } + return null; + } +}; \ No newline at end of file diff --git a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java new file mode 100644 index 00000000..fc9f64f1 --- /dev/null +++ b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java @@ -0,0 +1,120 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.operations.remote; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.http.HttpStatus; +import org.xmlpull.v1.XmlPullParserException; + +import com.owncloud.android.lib.network.OwnCloudClient; +import com.owncloud.android.lib.operations.common.RemoteOperation; +import com.owncloud.android.lib.operations.common.RemoteOperationResult; +import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.operations.common.ShareRemoteFile; +import com.owncloud.android.lib.utils.ShareXMLParser; + +import android.util.Log; + + +/** + * Get the data from the server to know shared files/folders + * + * @author masensio + * + */ + +public class GetRemoteSharedFilesOperation extends RemoteOperation { + + private static final String TAG = GetRemoteSharedFilesOperation.class.getSimpleName(); + + // OCS Route + private static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files_sharing/api/v1/shares"; + + private ArrayList mSharedFiles; // List of files for result + + private String mUrlServer; + + public ArrayList getSharedFiles() { + return mSharedFiles; + } + + public GetRemoteSharedFilesOperation(String urlServer) { + mUrlServer = urlServer; + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + int status = -1; + + // Get Method + GetMethod get = new GetMethod(mUrlServer + SHAREAPI_ROUTE); + Log.d(TAG, "URL ------> " + mUrlServer + SHAREAPI_ROUTE); + + // Get the response + try{ + status = client.executeMethod(get); + if(isSuccess(status)) { + Log.d(TAG, "Obtain RESPONSE"); + String response = get.getResponseBodyAsString(); + Log.d(TAG, response); + + // Parse xml response --> obtain the response in ShareFiles ArrayList + // convert String into InputStream + InputStream is = new ByteArrayInputStream(response.getBytes()); + ShareXMLParser xmlParser = new ShareXMLParser(); + mSharedFiles = xmlParser.parseXMLResponse(is); + if (mSharedFiles != null) { + Log.d(TAG, "Shared Files: " + mSharedFiles.size()); + result = new RemoteOperationResult(ResultCode.OK); + } + } + } catch (HttpException e) { + result = new RemoteOperationResult(e); + e.printStackTrace(); + } catch (IOException e) { + result = new RemoteOperationResult(e); + e.printStackTrace(); + } catch (XmlPullParserException e) { + result = new RemoteOperationResult(e); + e.printStackTrace(); + } finally { + get.releaseConnection(); + } + return result; + } + + private boolean isSuccess(int status) { + return (status == HttpStatus.SC_OK); + } + + +} diff --git a/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java b/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java new file mode 100644 index 00000000..16708194 --- /dev/null +++ b/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java @@ -0,0 +1,172 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.operations.remote; + +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; +import org.json.JSONException; +import org.json.JSONObject; + +import com.owncloud.android.lib.accounts.AccountUtils; +import com.owncloud.android.lib.network.OwnCloudClient; +import com.owncloud.android.lib.operations.common.RemoteOperation; +import com.owncloud.android.lib.operations.common.RemoteOperationResult; +import com.owncloud.android.lib.utils.OwnCloudVersion; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Uri; +import android.util.Log; + +/** + * Checks if the server is valid and if the server supports the Share API + * + * @author David A. Velasco + * @author masensio + * + */ + +public class OwnCloudServerCheckOperation extends RemoteOperation { + + /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */ + public static final int TRY_CONNECTION_TIMEOUT = 5000; + + private static final String TAG = OwnCloudServerCheckOperation.class.getSimpleName(); + + private static final String OCVERSION_SHARED_SUPPORTED = "5.0.13"; + + private static final String NODE_INSTALLED = "installed"; + private static final String NODE_VERSION = "version"; + private static final String NODE_VERSIONSTRING = "versionstring"; + + private String mUrl; + private RemoteOperationResult mLatestResult; + private Context mContext; + private OwnCloudVersion mOCVersion; + private OwnCloudVersion mOCVersionString; + + public OwnCloudServerCheckOperation(String url, Context context) { + mUrl = url; + mContext = context; + mOCVersion = null; + mOCVersionString = null; + } + + public OwnCloudVersion getDiscoveredVersion() { + return mOCVersion; + } + public boolean isSharedSupported() { + OwnCloudVersion shareServer = new OwnCloudVersion(OCVERSION_SHARED_SUPPORTED); + if (mOCVersionString != null) { + return mOCVersionString.compareTo(shareServer) >= 0; + } + + return false; + + } + + private boolean tryConnection(OwnCloudClient wc, String urlSt) { + boolean retval = false; + GetMethod get = null; + try { + get = new GetMethod(urlSt); + int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); + String response = get.getResponseBodyAsString(); + if (status == HttpStatus.SC_OK) { + JSONObject json = new JSONObject(response); + if (!json.getBoolean(NODE_INSTALLED)) { + mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); + } else { + mOCVersion = new OwnCloudVersion(json.getString(NODE_VERSION)); + mOCVersionString = new OwnCloudVersion(json.getString(NODE_VERSIONSTRING), true); + if (!mOCVersion.isVersionValid()) { + mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); + + } else { + mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ? + RemoteOperationResult.ResultCode.OK_SSL : + RemoteOperationResult.ResultCode.OK_NO_SSL + ); + + retval = true; + } + } + + } else { + mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); + } + + } catch (JSONException e) { + mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); + + } catch (Exception e) { + mLatestResult = new RemoteOperationResult(e); + + } finally { + if (get != null) + get.releaseConnection(); + } + + if (mLatestResult.isSuccess()) { + Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); + + } else if (mLatestResult.getException() != null) { + Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException()); + + } else { + Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); + } + + return retval; + } + + private boolean isOnline() { + ConnectivityManager cm = (ConnectivityManager) mContext + .getSystemService(Context.CONNECTIVITY_SERVICE); + return cm != null && cm.getActiveNetworkInfo() != null + && cm.getActiveNetworkInfo().isConnectedOrConnecting(); + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + if (!isOnline()) { + return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION); + } + if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) { + tryConnection(client, mUrl + AccountUtils.STATUS_PATH); + + } else { + client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH)); + boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH); + if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) { + Log.d(TAG, "establishing secure connection failed, trying non secure connection"); + client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH)); + tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH); + } + } + return mLatestResult; + } + +} diff --git a/src/com/owncloud/android/lib/utils/OwnCloudVersion.java b/src/com/owncloud/android/lib/utils/OwnCloudVersion.java index 542540c8..dd01186d 100644 --- a/src/com/owncloud/android/lib/utils/OwnCloudVersion.java +++ b/src/com/owncloud/android/lib/utils/OwnCloudVersion.java @@ -54,6 +54,17 @@ public class OwnCloudVersion implements Comparable { mIsValid = false; parseVersionString(version); } + + public OwnCloudVersion(String versionstring, boolean isVersionString) { + mVersion = 0; + mIsValid = false; + if (isVersionString) { + parseVersionString(versionstring); + } else { + parseVersion(versionstring); + } + + } public String toString() { return ((mVersion >> 16) % 256) + "." + ((mVersion >> 8) % 256) + "." @@ -70,7 +81,7 @@ public class OwnCloudVersion implements Comparable { : another.mVersion < mVersion ? 1 : -1; } - private void parseVersionString(String version) { + private void parseVersion(String version) { try { String[] nums = version.split("\\."); if (nums.length > 0) { @@ -89,4 +100,26 @@ public class OwnCloudVersion implements Comparable { mIsValid = false; } } + + private void parseVersionString(String versionstring) { + try { + versionstring = versionstring.replaceAll("[^\\d.]", ""); + + String[] nums = versionstring.split("\\."); + if (nums.length > 0) { + mVersion += Integer.parseInt(nums[0]); + } + mVersion = mVersion << 8; + if (nums.length > 1) { + mVersion += Integer.parseInt(nums[1]); + } + mVersion = mVersion << 8; + if (nums.length > 2) { + mVersion += Integer.parseInt(nums[2]); + } + mIsValid = true; + } catch (Exception e) { + mIsValid = false; + } + } } diff --git a/src/com/owncloud/android/lib/utils/ShareXMLParser.java b/src/com/owncloud/android/lib/utils/ShareXMLParser.java new file mode 100644 index 00000000..17b5f703 --- /dev/null +++ b/src/com/owncloud/android/lib/utils/ShareXMLParser.java @@ -0,0 +1,348 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; + +import android.util.Log; +import android.util.Xml; + +import com.owncloud.android.lib.operations.common.ShareRemoteFile; +import com.owncloud.android.lib.operations.common.ShareType; + +/** + * Parser for Share API Response + * @author masensio + * + */ + +public class ShareXMLParser { + + private static final String TAG = ShareXMLParser.class.getSimpleName(); + + // No namespaces + private static final String ns = null; + + // NODES for XML Parser + private static final String NODE_OCS = "ocs"; + + private static final String NODE_META = "meta"; + private static final String NODE_STATUS = "status"; + private static final String NODE_STATUS_CODE = "statuscode"; + //private static final String NODE_MESSAGE = "message"; + + private static final String NODE_DATA = "data"; + private static final String NODE_ELEMENT = "element"; + private static final String NODE_ID = "id"; + private static final String NODE_ITEM_TYPE = "item_type"; + private static final String NODE_ITEM_SOURCE = "item_source"; + private static final String NODE_PARENT = "parent"; + private static final String NODE_SHARE_TYPE = "share_type"; + private static final String NODE_SHARE_WITH = "share_with"; + private static final String NODE_FILE_SOURCE = "file_source"; + private static final String NODE_PATH = "path"; + private static final String NODE_PERMISSIONS = "permissions"; + private static final String NODE_STIME = "stime"; + private static final String NODE_EXPIRATION = "expiration"; + private static final String NODE_TOKEN = "token"; + private static final String NODE_STORAGE = "storage"; + private static final String NODE_MAIL_SEND = "mail_send"; + private static final String NODE_SHARE_WITH_DISPLAY_NAME = "share_with_display_name"; + + private static final String TYPE_FOLDER = "folder"; + + private String mStatus; + private int mStatusCode; + + // Getters and Setters + public String getStatus() { + return mStatus; + } + + public void setStatus(String status) { + this.mStatus = status; + } + + public int getStatusCode() { + return mStatusCode; + } + + public void setStatusCode(int statusCode) { + this.mStatusCode = statusCode; + } + + // Constructor + public ShareXMLParser() { + + } + + /** + * Parse is as response of Share API + * @param is + * @return List of ShareRemoteFiles + * @throws XmlPullParserException + * @throws IOException + */ + public ArrayList parseXMLResponse(InputStream is) throws XmlPullParserException, IOException { + + try { + // XMLPullParser + XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); + factory.setNamespaceAware(true); + + XmlPullParser parser = Xml.newPullParser(); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); + parser.setInput(is, null); + parser.nextTag(); + return readOCS(parser); + + } finally { + is.close(); + } + } + + /** + * Parse OCS node + * @param parser + * @return List of ShareRemoteFiles + * @throws XmlPullParserException + * @throws IOException + */ + private ArrayList readOCS (XmlPullParser parser) throws XmlPullParserException, IOException { + ArrayList sharedFiles = new ArrayList(); + parser.require(XmlPullParser.START_TAG, ns , NODE_OCS); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + String name = parser.getName(); + // read NODE_META and NODE_DATA + if (name.equalsIgnoreCase(NODE_META)) { + readMeta(parser); + } else if (name.equalsIgnoreCase(NODE_DATA)) { + sharedFiles = readData(parser); + } else { + skip(parser); + } + + } + return sharedFiles; + + + } + + /** + * Parse Meta node + * @param parser + * @throws XmlPullParserException + * @throws IOException + */ + private void readMeta(XmlPullParser parser) throws XmlPullParserException, IOException { + parser.require(XmlPullParser.START_TAG, ns, NODE_META); + Log.d(TAG, "---- NODE META ---"); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + String name = parser.getName(); + + if (name.equalsIgnoreCase(NODE_STATUS)) { + setStatus(readNode(parser, NODE_STATUS)); + + } else if (name.equalsIgnoreCase(NODE_STATUS_CODE)) { + setStatusCode(Integer.parseInt(readNode(parser, NODE_STATUS_CODE))); + + } else { + skip(parser); + } + + } + } + + /** + * Parse Data node + * @param parser + * @return + * @throws XmlPullParserException + * @throws IOException + */ + private ArrayList readData(XmlPullParser parser) throws XmlPullParserException, IOException { + ArrayList sharedFiles = new ArrayList(); + + parser.require(XmlPullParser.START_TAG, ns, NODE_DATA); + Log.d(TAG, "---- NODE DATA ---"); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + String name = parser.getName(); + if (name.equalsIgnoreCase(NODE_ELEMENT)) { + sharedFiles.add(readElement(parser)); + } else { + skip(parser); + } + } + + return sharedFiles; + + } + + /** + * Parse Element node + * @param parser + * @return + * @throws XmlPullParserException + * @throws IOException + */ + private ShareRemoteFile readElement(XmlPullParser parser) throws XmlPullParserException, IOException { + parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT); + + ShareRemoteFile sharedFile = new ShareRemoteFile(); + + Log.d(TAG, "---- NODE ELEMENT ---"); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + String name = parser.getName(); + + if (name.equalsIgnoreCase(NODE_ID)) { + sharedFile.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); + + } else if (name.equalsIgnoreCase(NODE_ITEM_TYPE)) { + sharedFile.setIsDirectory(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); + + } else if (name.equalsIgnoreCase(NODE_ITEM_SOURCE)) { + sharedFile.setItemSource(Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))); + + } else if (name.equalsIgnoreCase(NODE_PARENT)) { + readNode(parser, NODE_PARENT); + + } else if (name.equalsIgnoreCase(NODE_SHARE_TYPE)) { + int value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE)); + sharedFile.setShareType(ShareType.fromValue(value)); + + } else if (name.equalsIgnoreCase(NODE_SHARE_WITH)) { + sharedFile.setShareWith(readNode(parser, NODE_SHARE_WITH)); + + } else if (name.equalsIgnoreCase(NODE_FILE_SOURCE)) { + sharedFile.setFileSource(Long.parseLong(readNode(parser, NODE_FILE_SOURCE))); + + } else if (name.equalsIgnoreCase(NODE_PATH)) { + sharedFile.setPath(readNode(parser, NODE_PATH)); + + } else if (name.equalsIgnoreCase(NODE_PERMISSIONS)) { + sharedFile.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); + + } else if (name.equalsIgnoreCase(NODE_STIME)) { + sharedFile.setSharedDate(Long.parseLong(readNode(parser, NODE_STIME))); + + } else if (name.equalsIgnoreCase(NODE_EXPIRATION)) { + String value = readNode(parser, NODE_EXPIRATION); + if (!value.isEmpty()) { + sharedFile.setExpirationDate(Long.parseLong(readNode(parser, NODE_EXPIRATION))); // check if expiration is in long format or date format + } + + } else if (name.equalsIgnoreCase(NODE_TOKEN)) { + sharedFile.setToken(readNode(parser, NODE_TOKEN)); + + } else if (name.equalsIgnoreCase(NODE_STORAGE)) { + readNode(parser, NODE_STORAGE); + } else if (name.equalsIgnoreCase(NODE_MAIL_SEND)) { + readNode(parser, NODE_MAIL_SEND); + + } else if (name.equalsIgnoreCase(NODE_SHARE_WITH_DISPLAY_NAME)) { + sharedFile.setSharedWithDisplayName(readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME)); + + } else { + skip(parser); + } + } + + return sharedFile; + } + + /** + * Parse a node, to obtain its text. Needs readText method + * @param parser + * @param node + * @return Text of the node + * @throws XmlPullParserException + * @throws IOException + */ + private String readNode (XmlPullParser parser, String node) throws XmlPullParserException, IOException{ + parser.require(XmlPullParser.START_TAG, ns, node); + String value = readText(parser); + Log.d(TAG, "node= " + node + ", value= " + value); + parser.require(XmlPullParser.END_TAG, ns, node); + return value; + } + + /** + * Read the text from a node + * @param parser + * @return Text of the node + * @throws IOException + * @throws XmlPullParserException + */ + private String readText(XmlPullParser parser) throws IOException, XmlPullParserException { + String result = ""; + if (parser.next() == XmlPullParser.TEXT) { + result = parser.getText(); + parser.nextTag(); + } + return result; + } + + /** + * Skip tags in parser procedure + * @param parser + * @throws XmlPullParserException + * @throws IOException + */ + private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { + if (parser.getEventType() != XmlPullParser.START_TAG) { + throw new IllegalStateException(); + } + int depth = 1; + while (depth != 0) { + switch (parser.next()) { + case XmlPullParser.END_TAG: + depth--; + break; + case XmlPullParser.START_TAG: + depth++; + break; + } + } + } +} From f53197fc172c856972a7e381018246c464230479 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 10:54:01 +0100 Subject: [PATCH 2/9] OC-2746: rename GetRemoteSharedFilesOperation to GetRemoteSharesOperation --- ...redFilesOperation.java => GetRemoteSharesOperation.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename src/com/owncloud/android/lib/operations/remote/{GetRemoteSharedFilesOperation.java => GetRemoteSharesOperation.java} (94%) diff --git a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java similarity index 94% rename from src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java rename to src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java index fc9f64f1..1953e8e8 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharedFilesOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java @@ -51,9 +51,9 @@ import android.util.Log; * */ -public class GetRemoteSharedFilesOperation extends RemoteOperation { +public class GetRemoteSharesOperation extends RemoteOperation { - private static final String TAG = GetRemoteSharedFilesOperation.class.getSimpleName(); + private static final String TAG = GetRemoteSharesOperation.class.getSimpleName(); // OCS Route private static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files_sharing/api/v1/shares"; @@ -66,7 +66,7 @@ public class GetRemoteSharedFilesOperation extends RemoteOperation { return mSharedFiles; } - public GetRemoteSharedFilesOperation(String urlServer) { + public GetRemoteSharesOperation(String urlServer) { mUrlServer = urlServer; } From 8caf47696835812b1b3417960f2ad93ffd9c7c41 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 11:14:11 +0100 Subject: [PATCH 3/9] OC-2746: Change sharedfile by share --- .../remote/GetRemoteSharesOperation.java | 14 +++---- .../android/lib/utils/ShareXMLParser.java | 40 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java index 1953e8e8..b09bc1ba 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java @@ -45,7 +45,7 @@ import android.util.Log; /** - * Get the data from the server to know shared files/folders + * Get the data from the server to know shares * * @author masensio * @@ -58,12 +58,12 @@ public class GetRemoteSharesOperation extends RemoteOperation { // OCS Route private static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files_sharing/api/v1/shares"; - private ArrayList mSharedFiles; // List of files for result + private ArrayList mShares; // List of shares for result private String mUrlServer; - public ArrayList getSharedFiles() { - return mSharedFiles; + public ArrayList getShares() { + return mShares; } public GetRemoteSharesOperation(String urlServer) { @@ -91,9 +91,9 @@ public class GetRemoteSharesOperation extends RemoteOperation { // convert String into InputStream InputStream is = new ByteArrayInputStream(response.getBytes()); ShareXMLParser xmlParser = new ShareXMLParser(); - mSharedFiles = xmlParser.parseXMLResponse(is); - if (mSharedFiles != null) { - Log.d(TAG, "Shared Files: " + mSharedFiles.size()); + mShares = xmlParser.parseXMLResponse(is); + if (mShares != null) { + Log.d(TAG, "Shared Files: " + mShares.size()); result = new RemoteOperationResult(ResultCode.OK); } } diff --git a/src/com/owncloud/android/lib/utils/ShareXMLParser.java b/src/com/owncloud/android/lib/utils/ShareXMLParser.java index 17b5f703..d24b881a 100644 --- a/src/com/owncloud/android/lib/utils/ShareXMLParser.java +++ b/src/com/owncloud/android/lib/utils/ShareXMLParser.java @@ -137,7 +137,7 @@ public class ShareXMLParser { * @throws IOException */ private ArrayList readOCS (XmlPullParser parser) throws XmlPullParserException, IOException { - ArrayList sharedFiles = new ArrayList(); + ArrayList shares = new ArrayList(); parser.require(XmlPullParser.START_TAG, ns , NODE_OCS); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { @@ -148,13 +148,13 @@ public class ShareXMLParser { if (name.equalsIgnoreCase(NODE_META)) { readMeta(parser); } else if (name.equalsIgnoreCase(NODE_DATA)) { - sharedFiles = readData(parser); + shares = readData(parser); } else { skip(parser); } } - return sharedFiles; + return shares; } @@ -195,7 +195,7 @@ public class ShareXMLParser { * @throws IOException */ private ArrayList readData(XmlPullParser parser) throws XmlPullParserException, IOException { - ArrayList sharedFiles = new ArrayList(); + ArrayList shares = new ArrayList(); parser.require(XmlPullParser.START_TAG, ns, NODE_DATA); Log.d(TAG, "---- NODE DATA ---"); @@ -205,13 +205,13 @@ public class ShareXMLParser { } String name = parser.getName(); if (name.equalsIgnoreCase(NODE_ELEMENT)) { - sharedFiles.add(readElement(parser)); + shares.add(readElement(parser)); } else { skip(parser); } } - return sharedFiles; + return shares; } @@ -225,7 +225,7 @@ public class ShareXMLParser { private ShareRemoteFile readElement(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT); - ShareRemoteFile sharedFile = new ShareRemoteFile(); + ShareRemoteFile share = new ShareRemoteFile(); Log.d(TAG, "---- NODE ELEMENT ---"); while (parser.next() != XmlPullParser.END_TAG) { @@ -236,44 +236,44 @@ public class ShareXMLParser { String name = parser.getName(); if (name.equalsIgnoreCase(NODE_ID)) { - sharedFile.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); + share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); } else if (name.equalsIgnoreCase(NODE_ITEM_TYPE)) { - sharedFile.setIsDirectory(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); + share.setIsDirectory(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); } else if (name.equalsIgnoreCase(NODE_ITEM_SOURCE)) { - sharedFile.setItemSource(Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))); + share.setItemSource(Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))); } else if (name.equalsIgnoreCase(NODE_PARENT)) { readNode(parser, NODE_PARENT); } else if (name.equalsIgnoreCase(NODE_SHARE_TYPE)) { int value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE)); - sharedFile.setShareType(ShareType.fromValue(value)); + share.setShareType(ShareType.fromValue(value)); } else if (name.equalsIgnoreCase(NODE_SHARE_WITH)) { - sharedFile.setShareWith(readNode(parser, NODE_SHARE_WITH)); + share.setShareWith(readNode(parser, NODE_SHARE_WITH)); } else if (name.equalsIgnoreCase(NODE_FILE_SOURCE)) { - sharedFile.setFileSource(Long.parseLong(readNode(parser, NODE_FILE_SOURCE))); + share.setFileSource(Long.parseLong(readNode(parser, NODE_FILE_SOURCE))); } else if (name.equalsIgnoreCase(NODE_PATH)) { - sharedFile.setPath(readNode(parser, NODE_PATH)); + share.setPath(readNode(parser, NODE_PATH)); } else if (name.equalsIgnoreCase(NODE_PERMISSIONS)) { - sharedFile.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); + share.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); } else if (name.equalsIgnoreCase(NODE_STIME)) { - sharedFile.setSharedDate(Long.parseLong(readNode(parser, NODE_STIME))); + share.setSharedDate(Long.parseLong(readNode(parser, NODE_STIME))); } else if (name.equalsIgnoreCase(NODE_EXPIRATION)) { String value = readNode(parser, NODE_EXPIRATION); if (!value.isEmpty()) { - sharedFile.setExpirationDate(Long.parseLong(readNode(parser, NODE_EXPIRATION))); // check if expiration is in long format or date format + share.setExpirationDate(Long.parseLong(readNode(parser, NODE_EXPIRATION))); // check if expiration is in long format or date format } } else if (name.equalsIgnoreCase(NODE_TOKEN)) { - sharedFile.setToken(readNode(parser, NODE_TOKEN)); + share.setToken(readNode(parser, NODE_TOKEN)); } else if (name.equalsIgnoreCase(NODE_STORAGE)) { readNode(parser, NODE_STORAGE); @@ -281,14 +281,14 @@ public class ShareXMLParser { readNode(parser, NODE_MAIL_SEND); } else if (name.equalsIgnoreCase(NODE_SHARE_WITH_DISPLAY_NAME)) { - sharedFile.setSharedWithDisplayName(readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME)); + share.setSharedWithDisplayName(readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME)); } else { skip(parser); } } - return sharedFile; + return share; } /** From 222286e869d17db7378122f155f8aa25c76064b4 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 11:41:55 +0100 Subject: [PATCH 4/9] OC-2746: OCShare in library --- .../operations/common/ShareRemoteFile.java | 273 ------------------ .../remote/GetRemoteSharesOperation.java | 8 +- .../android/lib/utils/ShareXMLParser.java | 16 +- 3 files changed, 12 insertions(+), 285 deletions(-) delete mode 100644 src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java diff --git a/src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java b/src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java deleted file mode 100644 index b47c465a..00000000 --- a/src/com/owncloud/android/lib/operations/common/ShareRemoteFile.java +++ /dev/null @@ -1,273 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -package com.owncloud.android.lib.operations.common; - -import com.owncloud.android.lib.network.webdav.WebdavEntry; -import com.owncloud.android.lib.utils.FileUtils; - -import android.os.Parcel; -import android.os.Parcelable; -import android.util.Log; - - -/** - * Contains the data of a Share Remote File from the Share API - * - * @author masensio - * - */ -public class ShareRemoteFile extends RemoteFile { - - /** - * Generated - should be refreshed every time the class changes!! - */ - private static final long serialVersionUID = -5916376011588784325L; - - private static final String TAG = ShareRemoteFile.class.getSimpleName(); - - private long mFileSource; - private long mItemSource; - private ShareType mShareType; - private String mShareWith; - private String mPath; - private int mPermissions; - private long mSharedDate; - private long mExpirationDate; - private String mToken; - private String mSharedWithDisplayName; - private boolean mIsDirectory; - private long mUserId; - private long mIdRemoteShared; - - public ShareRemoteFile() { - super(); - resetData(); - } - - public ShareRemoteFile(String path) { - super(path); - resetData(); - if (path == null || path.length() <= 0 || !path.startsWith(FileUtils.PATH_SEPARATOR)) { - Log.e(TAG, "Trying to create a OCShare with a non valid path"); - throw new IllegalArgumentException("Trying to create a OCShare with a non valid path: " + path); - } - mPath = path; - } - - public ShareRemoteFile(WebdavEntry we) { - super(we); - // TODO Auto-generated constructor stub - } - - /** - * Used internally. Reset all file properties - */ - private void resetData() { - mFileSource = 0; - mItemSource = 0; - mShareType = ShareType.NO_SHARED; - mShareWith = null; - mPath = null; - mPermissions = -1; - mSharedDate = 0; - mExpirationDate = 0; - mToken = null; - mSharedWithDisplayName = null; - mIsDirectory = false; - mUserId = -1; - mIdRemoteShared = -1; - } - - /// Getters and Setters - public long getFileSource() { - return mFileSource; - } - - public void setFileSource(long fileSource) { - this.mFileSource = fileSource; - } - - public long getItemSource() { - return mItemSource; - } - - public void setItemSource(long itemSource) { - this.mItemSource = itemSource; - } - - public ShareType getShareType() { - return mShareType; - } - - public void setShareType(ShareType shareType) { - this.mShareType = shareType; - } - - public String getShareWith() { - return mShareWith; - } - - public void setShareWith(String shareWith) { - this.mShareWith = shareWith; - } - - public String getPath() { - return mPath; - } - - public void setPath(String path) { - this.mPath = path; - } - - public int getPermissions() { - return mPermissions; - } - - public void setPermissions(int permissions) { - this.mPermissions = permissions; - } - - public long getSharedDate() { - return mSharedDate; - } - - public void setSharedDate(long sharedDate) { - this.mSharedDate = sharedDate; - } - - public long getExpirationDate() { - return mExpirationDate; - } - - public void setExpirationDate(long expirationDate) { - this.mExpirationDate = expirationDate; - } - - public String getToken() { - return mToken; - } - - public void setToken(String token) { - this.mToken = token; - } - - public String getSharedWithDisplayName() { - return mSharedWithDisplayName; - } - - public void setSharedWithDisplayName(String sharedWithDisplayName) { - this.mSharedWithDisplayName = sharedWithDisplayName; - } - - public boolean isDirectory() { - return mIsDirectory; - } - - public void setIsDirectory(boolean isDirectory) { - this.mIsDirectory = isDirectory; - } - - public long getUserId() { - return mUserId; - } - - public void setUserId(long userId) { - this.mUserId = userId; - } - - public long getIdRemoteShared() { - return mIdRemoteShared; - } - - public void setIdRemoteShared(long idRemoteShared) { - this.mIdRemoteShared = idRemoteShared; - } - - /** - * Parcelable Methods - */ - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - @Override - public ShareRemoteFile createFromParcel(Parcel source) { - return new ShareRemoteFile(source); - } - - @Override - public ShareRemoteFile[] newArray(int size) { - return new ShareRemoteFile[size]; - } - }; - - /** - * Reconstruct from parcel - * - * @param source The source parcel - */ - protected ShareRemoteFile(Parcel source) { - super(source); - } - - public void readFromParcel(Parcel source) { - super.readFromParcel(source); - - mFileSource = source.readLong(); - mItemSource = source.readLong(); - try { - mShareType = ShareType.valueOf(source.readString()); - } catch (IllegalArgumentException x) { - mShareType = ShareType.NO_SHARED; - } - mShareWith = source.readString(); - mPath = source.readString(); - mPermissions = source.readInt(); - mSharedDate = source.readLong(); - mExpirationDate = source.readLong(); - mToken = source.readString(); - mSharedWithDisplayName = source.readString(); - mIsDirectory = source.readInt() == 0; - mUserId = source.readLong(); - mIdRemoteShared = source.readLong(); - } - - - @Override - public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - - dest.writeLong(mFileSource); - dest.writeLong(mItemSource); - dest.writeString((mShareType == null) ? "" : mShareType.name()); - dest.writeString(mShareWith); - dest.writeString(mPath); - dest.writeInt(mPermissions); - dest.writeLong(mSharedDate); - dest.writeLong(mExpirationDate); - dest.writeString(mToken); - dest.writeString(mSharedWithDisplayName); - dest.writeInt(mIsDirectory ? 1 : 0); - dest.writeLong(mUserId); - dest.writeLong(mIdRemoteShared); - } -} diff --git a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java index b09bc1ba..b3935ed2 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java @@ -38,7 +38,7 @@ import com.owncloud.android.lib.network.OwnCloudClient; import com.owncloud.android.lib.operations.common.RemoteOperation; import com.owncloud.android.lib.operations.common.RemoteOperationResult; import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; -import com.owncloud.android.lib.operations.common.ShareRemoteFile; +import com.owncloud.android.lib.operations.common.OCShare; import com.owncloud.android.lib.utils.ShareXMLParser; import android.util.Log; @@ -58,11 +58,11 @@ public class GetRemoteSharesOperation extends RemoteOperation { // OCS Route private static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files_sharing/api/v1/shares"; - private ArrayList mShares; // List of shares for result + private ArrayList mShares; // List of shares for result private String mUrlServer; - public ArrayList getShares() { + public ArrayList getShares() { return mShares; } @@ -93,7 +93,7 @@ public class GetRemoteSharesOperation extends RemoteOperation { ShareXMLParser xmlParser = new ShareXMLParser(); mShares = xmlParser.parseXMLResponse(is); if (mShares != null) { - Log.d(TAG, "Shared Files: " + mShares.size()); + Log.d(TAG, "Shares: " + mShares.size()); result = new RemoteOperationResult(ResultCode.OK); } } diff --git a/src/com/owncloud/android/lib/utils/ShareXMLParser.java b/src/com/owncloud/android/lib/utils/ShareXMLParser.java index d24b881a..c764dd83 100644 --- a/src/com/owncloud/android/lib/utils/ShareXMLParser.java +++ b/src/com/owncloud/android/lib/utils/ShareXMLParser.java @@ -35,7 +35,7 @@ import org.xmlpull.v1.XmlPullParserFactory; import android.util.Log; import android.util.Xml; -import com.owncloud.android.lib.operations.common.ShareRemoteFile; +import com.owncloud.android.lib.operations.common.OCShare; import com.owncloud.android.lib.operations.common.ShareType; /** @@ -111,7 +111,7 @@ public class ShareXMLParser { * @throws XmlPullParserException * @throws IOException */ - public ArrayList parseXMLResponse(InputStream is) throws XmlPullParserException, IOException { + public ArrayList parseXMLResponse(InputStream is) throws XmlPullParserException, IOException { try { // XMLPullParser @@ -136,8 +136,8 @@ public class ShareXMLParser { * @throws XmlPullParserException * @throws IOException */ - private ArrayList readOCS (XmlPullParser parser) throws XmlPullParserException, IOException { - ArrayList shares = new ArrayList(); + private ArrayList readOCS (XmlPullParser parser) throws XmlPullParserException, IOException { + ArrayList shares = new ArrayList(); parser.require(XmlPullParser.START_TAG, ns , NODE_OCS); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { @@ -194,8 +194,8 @@ public class ShareXMLParser { * @throws XmlPullParserException * @throws IOException */ - private ArrayList readData(XmlPullParser parser) throws XmlPullParserException, IOException { - ArrayList shares = new ArrayList(); + private ArrayList readData(XmlPullParser parser) throws XmlPullParserException, IOException { + ArrayList shares = new ArrayList(); parser.require(XmlPullParser.START_TAG, ns, NODE_DATA); Log.d(TAG, "---- NODE DATA ---"); @@ -222,10 +222,10 @@ public class ShareXMLParser { * @throws XmlPullParserException * @throws IOException */ - private ShareRemoteFile readElement(XmlPullParser parser) throws XmlPullParserException, IOException { + private OCShare readElement(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT); - ShareRemoteFile share = new ShareRemoteFile(); + OCShare share = new OCShare(); Log.d(TAG, "---- NODE ELEMENT ---"); while (parser.next() != XmlPullParser.END_TAG) { From b1ae56cbeabbfd53171addcb17aabfcafdfb2e86 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 11:43:07 +0100 Subject: [PATCH 5/9] OC-2746: OCShare in library --- .../lib/operations/common/OCShare.java | 278 ++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 src/com/owncloud/android/lib/operations/common/OCShare.java diff --git a/src/com/owncloud/android/lib/operations/common/OCShare.java b/src/com/owncloud/android/lib/operations/common/OCShare.java new file mode 100644 index 00000000..06d2b919 --- /dev/null +++ b/src/com/owncloud/android/lib/operations/common/OCShare.java @@ -0,0 +1,278 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.operations.common; + +import com.owncloud.android.lib.utils.FileUtils; + +import android.os.Parcel; +import android.os.Parcelable; +import android.util.Log; + + +/** + * Contains the data of a Share from the Share API + * + * @author masensio + * + */ +public class OCShare implements Parcelable{ + + private static final String TAG = OCShare.class.getSimpleName(); + + private long mId; + private long mFileSource; + private long mItemSource; + private ShareType mShareType; + private String mShareWith; + private String mPath; + private int mPermissions; + private long mSharedDate; + private long mExpirationDate; + private String mToken; + private String mSharedWithDisplayName; + private boolean mIsDirectory; + private long mUserId; + private long mIdRemoteShared; + + public OCShare() { + super(); + resetData(); + } + + public OCShare(String path) { + resetData(); + if (path == null || path.length() <= 0 || !path.startsWith(FileUtils.PATH_SEPARATOR)) { + Log.e(TAG, "Trying to create a OCShare with a non valid path"); + throw new IllegalArgumentException("Trying to create a OCShare with a non valid path: " + path); + } + mPath = path; + } + + /** + * Used internally. Reset all file properties + */ + private void resetData() { + mId = -1; + mFileSource = 0; + mItemSource = 0; + mShareType = ShareType.NO_SHARED; + mShareWith = null; + mPath = null; + mPermissions = -1; + mSharedDate = 0; + mExpirationDate = 0; + mToken = null; + mSharedWithDisplayName = null; + mIsDirectory = false; + mUserId = -1; + mIdRemoteShared = -1; + } + + /// Getters and Setters + + public long getId() { + return mId; + } + + public void setId(long id){ + mId = id; + } + + public long getFileSource() { + return mFileSource; + } + + public void setFileSource(long fileSource) { + this.mFileSource = fileSource; + } + + public long getItemSource() { + return mItemSource; + } + + public void setItemSource(long itemSource) { + this.mItemSource = itemSource; + } + + public ShareType getShareType() { + return mShareType; + } + + public void setShareType(ShareType shareType) { + this.mShareType = shareType; + } + + public String getShareWith() { + return mShareWith; + } + + public void setShareWith(String shareWith) { + this.mShareWith = shareWith; + } + + public String getPath() { + return mPath; + } + + public void setPath(String path) { + this.mPath = path; + } + + public int getPermissions() { + return mPermissions; + } + + public void setPermissions(int permissions) { + this.mPermissions = permissions; + } + + public long getSharedDate() { + return mSharedDate; + } + + public void setSharedDate(long sharedDate) { + this.mSharedDate = sharedDate; + } + + public long getExpirationDate() { + return mExpirationDate; + } + + public void setExpirationDate(long expirationDate) { + this.mExpirationDate = expirationDate; + } + + public String getToken() { + return mToken; + } + + public void setToken(String token) { + this.mToken = token; + } + + public String getSharedWithDisplayName() { + return mSharedWithDisplayName; + } + + public void setSharedWithDisplayName(String sharedWithDisplayName) { + this.mSharedWithDisplayName = sharedWithDisplayName; + } + + public boolean isDirectory() { + return mIsDirectory; + } + + public void setIsDirectory(boolean isDirectory) { + this.mIsDirectory = isDirectory; + } + + public long getUserId() { + return mUserId; + } + + public void setUserId(long userId) { + this.mUserId = userId; + } + + public long getIdRemoteShared() { + return mIdRemoteShared; + } + + public void setIdRemoteShared(long idRemoteShared) { + this.mIdRemoteShared = idRemoteShared; + } + + /** + * Parcelable Methods + */ + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + @Override + public OCShare createFromParcel(Parcel source) { + return new OCShare(source); + } + + @Override + public OCShare[] newArray(int size) { + return new OCShare[size]; + } + }; + + /** + * Reconstruct from parcel + * + * @param source The source parcel + */ + protected OCShare(Parcel source) { + readFromParcel(source); + } + + public void readFromParcel(Parcel source) { + mId = source.readLong(); + + mFileSource = source.readLong(); + mItemSource = source.readLong(); + try { + mShareType = ShareType.valueOf(source.readString()); + } catch (IllegalArgumentException x) { + mShareType = ShareType.NO_SHARED; + } + mShareWith = source.readString(); + mPath = source.readString(); + mPermissions = source.readInt(); + mSharedDate = source.readLong(); + mExpirationDate = source.readLong(); + mToken = source.readString(); + mSharedWithDisplayName = source.readString(); + mIsDirectory = source.readInt() == 0; + mUserId = source.readLong(); + mIdRemoteShared = source.readLong(); + } + + + @Override + public int describeContents() { + return this.hashCode(); + } + + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeLong(mId); + dest.writeLong(mFileSource); + dest.writeLong(mItemSource); + dest.writeString((mShareType == null) ? "" : mShareType.name()); + dest.writeString(mShareWith); + dest.writeString(mPath); + dest.writeInt(mPermissions); + dest.writeLong(mSharedDate); + dest.writeLong(mExpirationDate); + dest.writeString(mToken); + dest.writeString(mSharedWithDisplayName); + dest.writeInt(mIsDirectory ? 1 : 0); + dest.writeLong(mUserId); + dest.writeLong(mIdRemoteShared); + } + +} From 492b76230b0794ae5b0f48218f10d0f1c81f6a58 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 13:07:29 +0100 Subject: [PATCH 6/9] OC-2746: RemoteOperationResult return ArrayList instead of ArrayList --- .../lib/operations/common/RemoteOperationResult.java | 6 +++--- .../lib/operations/remote/GetRemoteSharesOperation.java | 8 +++++--- .../lib/operations/remote/ReadRemoteFileOperation.java | 2 +- .../lib/operations/remote/ReadRemoteFolderOperation.java | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java b/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java index df6d7471..21e714c1 100644 --- a/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java +++ b/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java @@ -106,7 +106,7 @@ public class RemoteOperationResult implements Serializable { private ResultCode mCode = ResultCode.UNKNOWN_ERROR; private String mRedirectedLocation; - private ArrayList mFiles; + private ArrayList mFiles; public RemoteOperationResult(ResultCode code) { mCode = code; @@ -207,11 +207,11 @@ public class RemoteOperationResult implements Serializable { } - public void setData(ArrayList files){ + public void setData(ArrayList files){ mFiles = files; } - public ArrayList getData(){ + public ArrayList getData(){ return mFiles; } diff --git a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java index b3935ed2..da54fadb 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java @@ -62,9 +62,6 @@ public class GetRemoteSharesOperation extends RemoteOperation { private String mUrlServer; - public ArrayList getShares() { - return mShares; - } public GetRemoteSharesOperation(String urlServer) { mUrlServer = urlServer; @@ -95,6 +92,11 @@ public class GetRemoteSharesOperation extends RemoteOperation { if (mShares != null) { Log.d(TAG, "Shares: " + mShares.size()); result = new RemoteOperationResult(ResultCode.OK); + ArrayList sharesObjects = new ArrayList(); + for (OCShare share: mShares) { + sharesObjects.add(share); + } + result.setData(sharesObjects); } } } catch (HttpException e) { diff --git a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java index 0410cd62..e0017dc6 100644 --- a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java @@ -89,7 +89,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { MultiStatus resp = propfind.getResponseBodyAsMultiStatus(); WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath()); RemoteFile remoteFile = new RemoteFile(we); - ArrayList files = new ArrayList(); + ArrayList files = new ArrayList(); files.add(remoteFile); // Result of the operation diff --git a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java index 521d73e7..cb9551ad 100644 --- a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java @@ -52,7 +52,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation { private static final String TAG = ReadRemoteFolderOperation.class.getSimpleName(); private String mRemotePath; - private ArrayList mFolderAndFiles; + private ArrayList mFolderAndFiles; /** * Constructor @@ -134,7 +134,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation { * @return */ private void readData(MultiStatus dataInServer, OwnCloudClient client) { - mFolderAndFiles = new ArrayList(); + mFolderAndFiles = new ArrayList(); // parse data from remote folder WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath()); From 79995d6522bd88b75c0db6c91cf3e6c9b1ac14ff Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 13:43:27 +0100 Subject: [PATCH 7/9] OC-2746: Update Sample Library for changes in library --- .../com/owncloud/android/lib/sampleclient/MainActivity.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java index 0ae446b6..7e4e6ed0 100644 --- a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -28,6 +28,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -212,7 +213,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, private void onSuccessfulRefresh(ReadRemoteFolderOperation operation, RemoteOperationResult result) { mFilesAdapter.clear(); - List files = result.getData(); + List files = new ArrayList(); + for(Object obj: result.getData()) { + files.add((RemoteFile) obj); + } if (files != null) { Iterator it = files.iterator(); while (it.hasNext()) { From b4d67521b24bbd517cc1c91b37715ad650688bc3 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 28 Jan 2014 14:39:36 +0100 Subject: [PATCH 8/9] OC-2746: Changes from comments: new method to construct base url server --- .../android/lib/accounts/AccountUtils.java | 17 +++++++++++++++++ .../android/lib/network/OwnCloudClient.java | 15 ++++++++++++++- .../lib/network/OwnCloudClientFactory.java | 15 ++++++++++----- .../ChunkedUploadRemoteFileOperation.java | 2 +- .../remote/CreateRemoteFolderOperation.java | 2 +- .../remote/DownloadRemoteFileOperation.java | 2 +- .../remote/ExistenceCheckRemoteOperation.java | 6 +++--- .../remote/GetUserNameRemoteOperation.java | 4 ++-- .../remote/OwnCloudServerCheckOperation.java | 4 ++-- .../remote/ReadRemoteFileOperation.java | 4 ++-- .../remote/ReadRemoteFolderOperation.java | 6 +++--- .../remote/RemoveRemoteFileOperation.java | 2 +- .../remote/RenameRemoteFileOperation.java | 4 ++-- .../remote/UploadRemoteFileOperation.java | 2 +- 14 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/com/owncloud/android/lib/accounts/AccountUtils.java b/src/com/owncloud/android/lib/accounts/AccountUtils.java index 79983814..fbb840cf 100644 --- a/src/com/owncloud/android/lib/accounts/AccountUtils.java +++ b/src/com/owncloud/android/lib/accounts/AccountUtils.java @@ -92,6 +92,23 @@ public class AccountUtils { return baseurl + webdavpath; } + /** + * Extracts url server from the account + * @param context + * @param account + * @return url server or null on failure + * @throws AccountNotFoundException When 'account' is unknown for the AccountManager + */ + public static String constructBasicURLForAccount(Context context, Account account) throws AccountNotFoundException { + AccountManager ama = AccountManager.get(context); + String baseurl = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_BASE_URL); + + if (baseurl == null ) + throw new AccountNotFoundException(account, "Account not found", null); + + return baseurl; + } + public static class AccountNotFoundException extends AccountsException { diff --git a/src/com/owncloud/android/lib/network/OwnCloudClient.java b/src/com/owncloud/android/lib/network/OwnCloudClient.java index 9ec8867f..2e8fb3d2 100644 --- a/src/com/owncloud/android/lib/network/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/network/OwnCloudClient.java @@ -57,6 +57,7 @@ public class OwnCloudClient extends HttpClient { private static final int MAX_REDIRECTIONS_COUNT = 3; private Uri mUri; + private Uri mWebdavUri; private Credentials mCredentials; private boolean mFollowRedirects; private String mSsoSessionCookie; @@ -117,7 +118,7 @@ public class OwnCloudClient extends HttpClient { * @throws Exception When the existence could not be determined */ public boolean existsFile(String path) throws IOException, HttpException { - HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path)); + HeadMethod head = new HeadMethod(mWebdavUri.toString() + WebdavUtils.encodePath(path)); try { int status = executeMethod(head); Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":"")); @@ -224,6 +225,18 @@ public class OwnCloudClient extends HttpClient { getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout); } + /** + * Sets the Webdav URI for the helper methods that receive paths as parameters, instead of full URLs + * @param uri + */ + public void setWebdavUri(Uri uri) { + mWebdavUri = uri; + } + + public Uri getWebdavUri() { + return mWebdavUri; + } + /** * Sets the base URI for the helper methods that receive paths as parameters, instead of full URLs * @param uri diff --git a/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java index daea819c..a41f5528 100644 --- a/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java @@ -70,11 +70,14 @@ public class OwnCloudClientFactory { public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); - Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri webdavUri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri uri = Uri.parse(AccountUtils.constructBasicURLForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(uri, appContext, !isSamlSso); + OwnCloudClient client = createOwnCloudClient(webdavUri, appContext, !isSamlSso); + client.setBaseUri(uri); + if (isOauth2) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false); client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token @@ -95,11 +98,13 @@ public class OwnCloudClientFactory { public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { - Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri webdavUri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri uri = Uri.parse(AccountUtils.constructBasicURLForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(uri, appContext, !isSamlSso); + OwnCloudClient client = createOwnCloudClient(webdavUri, appContext, !isSamlSso); + client.setBaseUri(uri); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), null, currentActivity, null, null); @@ -148,7 +153,7 @@ public class OwnCloudClientFactory { OwnCloudClient client = new OwnCloudClient(NetworkUtils.getMultiThreadedConnManager()); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); - client.setBaseUri(uri); + client.setWebdavUri(uri); client.setFollowRedirects(followRedirects); return client; diff --git a/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java index 9fde7c0f..51e2bfcd 100644 --- a/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java @@ -70,7 +70,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation } long offset = 0; - String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ; + String uriPrefix = client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ; long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE); for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) { if (mPutMethod != null) { diff --git a/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java b/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java index f0a325ed..c2389e9b 100644 --- a/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java @@ -80,7 +80,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { boolean noInvalidChars = FileUtils.isValidPath(mRemotePath); if (noInvalidChars) { try { - mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + mkcol = new MkColMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) { result = createParentFolder(FileUtils.getParentPath(mRemotePath), client); diff --git a/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java index a964abc4..12e84765 100644 --- a/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java @@ -98,7 +98,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { protected int downloadFile(OwnCloudClient client, File targetFile) throws HttpException, IOException, OperationCancelledException { int status = -1; boolean savedFile = false; - mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); Iterator it = null; FileOutputStream fos = null; diff --git a/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java b/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java index d4b49f91..8406fb0b 100644 --- a/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java @@ -75,16 +75,16 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { RemoteOperationResult result = null; HeadMethod head = null; try { - head = new HeadMethod(client.getBaseUri() + WebdavUtils.encodePath(mPath)); + head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath)); int status = client.executeMethod(head, TIMEOUT, TIMEOUT); client.exhaustResponse(head.getResponseBodyAsStream()); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); result = new RemoteOperationResult(success, status, head.getResponseHeaders()); - Log.d(TAG, "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":"")); + Log.d(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":"")); } catch (Exception e) { result = new RemoteOperationResult(e); - Log.e(TAG, "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); + Log.e(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); } finally { if (head != null) diff --git a/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java b/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java index 9f5c0ff6..be76976f 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java @@ -75,8 +75,8 @@ public class GetUserNameRemoteOperation extends RemoteOperation { int status = -1; // Get Method - GetMethod get = new GetMethod(client.getBaseUri() + OCS_ROUTE); - Log.d(TAG, "URL ------> " + client.getBaseUri() + OCS_ROUTE); + GetMethod get = new GetMethod(client.getWebdavUri() + OCS_ROUTE); + Log.d(TAG, "URL ------> " + client.getWebdavUri() + OCS_ROUTE); // Add the Header get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE); diff --git a/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java b/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java index 16708194..15e54a3e 100644 --- a/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java @@ -158,11 +158,11 @@ public class OwnCloudServerCheckOperation extends RemoteOperation { tryConnection(client, mUrl + AccountUtils.STATUS_PATH); } else { - client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH)); + client.setWebdavUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH)); boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH); if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) { Log.d(TAG, "establishing secure connection failed, trying non secure connection"); - client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH)); + client.setWebdavUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH)); tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH); } } diff --git a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java index e0017dc6..631b5d11 100644 --- a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java @@ -77,7 +77,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { /// take the duty of check the server for the current state of the file there try { - propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath), + propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_0); int status; @@ -87,7 +87,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { if (isMultiStatus) { // Parse response MultiStatus resp = propfind.getResponseBodyAsMultiStatus(); - WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath()); + WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getWebdavUri().getPath()); RemoteFile remoteFile = new RemoteFile(we); ArrayList files = new ArrayList(); files.add(remoteFile); diff --git a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java index cb9551ad..4dfb04b2 100644 --- a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java @@ -75,7 +75,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation { try { // remote request - query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath), + query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1); int status = client.executeMethod(query); @@ -137,14 +137,14 @@ public class ReadRemoteFolderOperation extends RemoteOperation { mFolderAndFiles = new ArrayList(); // parse data from remote folder - WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath()); + WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getWebdavUri().getPath()); mFolderAndFiles.add(fillOCFile(we)); // loop to update every child RemoteFile remoteFile = null; for (int i = 1; i < dataInServer.getResponses().length; ++i) { /// new OCFile instance with the data from the server - we = new WebdavEntry(dataInServer.getResponses()[i], client.getBaseUri().getPath()); + we = new WebdavEntry(dataInServer.getResponses()[i], client.getWebdavUri().getPath()); remoteFile = fillOCFile(we); mFolderAndFiles.add(remoteFile); } diff --git a/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java index b80fa526..2677a981 100644 --- a/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java @@ -68,7 +68,7 @@ public class RemoveRemoteFileOperation extends RemoteOperation { DeleteMethod delete = null; try { - delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT); delete.getResponseBodyAsString(); // exhaust the response, although not interesting diff --git a/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java index 7bc5f0cc..d4fa7c89 100644 --- a/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java @@ -104,8 +104,8 @@ public class RenameRemoteFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); } - move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath), - client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath)); + move = new LocalMoveMethod( client.getWebdavUri() + WebdavUtils.encodePath(mOldRemotePath), + client.getWebdavUri() + WebdavUtils.encodePath(mNewRemotePath)); int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT); move.getResponseBodyAsString(); // exhaust response, although not interesting diff --git a/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java index 4872aaf1..c15350df 100644 --- a/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java @@ -80,7 +80,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { if (mCancellationRequested.get()) { throw new OperationCancelledException(); } else { - mPutMethod = new PutMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); } } From bc8d0bd125fc0921dbec718a4170ba56cb9f242e Mon Sep 17 00:00:00 2001 From: masensio Date: Wed, 29 Jan 2014 10:17:51 +0100 Subject: [PATCH 9/9] OC-2746: Changes from comments in PR#1 in android-library repo --- .../lib/operations/common/RemoteOperationResult.java | 8 ++++---- .../lib/operations/remote/GetRemoteSharesOperation.java | 9 +++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java b/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java index 21e714c1..ba5dd2c0 100644 --- a/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java +++ b/src/com/owncloud/android/lib/operations/common/RemoteOperationResult.java @@ -106,12 +106,12 @@ public class RemoteOperationResult implements Serializable { private ResultCode mCode = ResultCode.UNKNOWN_ERROR; private String mRedirectedLocation; - private ArrayList mFiles; + private ArrayList mData; public RemoteOperationResult(ResultCode code) { mCode = code; mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL); - mFiles = null; + mData = null; } private RemoteOperationResult(boolean success, int httpCode) { @@ -208,11 +208,11 @@ public class RemoteOperationResult implements Serializable { public void setData(ArrayList files){ - mFiles = files; + mData = files; } public ArrayList getData(){ - return mFiles; + return mData; } public boolean isSuccess() { diff --git a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java index da54fadb..3149a045 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetRemoteSharesOperation.java @@ -60,11 +60,8 @@ public class GetRemoteSharesOperation extends RemoteOperation { private ArrayList mShares; // List of shares for result - private String mUrlServer; - - public GetRemoteSharesOperation(String urlServer) { - mUrlServer = urlServer; + public GetRemoteSharesOperation() { } @Override @@ -73,8 +70,8 @@ public class GetRemoteSharesOperation extends RemoteOperation { int status = -1; // Get Method - GetMethod get = new GetMethod(mUrlServer + SHAREAPI_ROUTE); - Log.d(TAG, "URL ------> " + mUrlServer + SHAREAPI_ROUTE); + GetMethod get = new GetMethod(client.getBaseUri() + SHAREAPI_ROUTE); + Log.d(TAG, "URL ------> " + client.getBaseUri() + SHAREAPI_ROUTE); // Get the response try{