From 7b5d6cdef791cd27a573fcaa9c8214970753843c Mon Sep 17 00:00:00 2001 From: masensio Date: Mon, 27 Jan 2014 13:44:24 +0100 Subject: [PATCH] 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; + } + } + } +}