From 91ffc87014dc7b7995beea443ab24b5d85101dfe Mon Sep 17 00:00:00 2001 From: davigonz Date: Fri, 1 Mar 2019 12:20:08 +0100 Subject: [PATCH] Convert used classes to Kotlin --- owncloudComLibrary/build.gradle | 4 +- .../shares/CreateRemoteShareOperation.java | 269 ----------- .../shares/CreateRemoteShareOperation.kt | 252 ++++++++++ .../lib/resources/shares/RemoteShare.java | 343 -------------- .../lib/resources/shares/RemoteShare.kt | 241 ++++++++++ ...ion.java => RemoveRemoteShareOperation.kt} | 93 ++-- ...ParserResult.java => ShareParserResult.kt} | 22 +- ...uilder.java => SharePermissionsBuilder.kt} | 44 +- .../ShareToRemoteOperationResultParser.java | 140 ------ .../ShareToRemoteOperationResultParser.kt | 142 ++++++ .../lib/resources/shares/ShareXMLParser.java | 441 ------------------ .../lib/resources/shares/ShareXMLParser.kt | 415 ++++++++++++++++ .../shares/UpdateRemoteShareOperation.java | 233 --------- .../shares/UpdateRemoteShareOperation.kt | 232 +++++++++ .../lib/resources/status/OwnCloudVersion.java | 213 --------- .../lib/resources/status/OwnCloudVersion.kt | 202 ++++++++ 16 files changed, 1555 insertions(+), 1731 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/{RemoveRemoteShareOperation.java => RemoveRemoteShareOperation.kt} (50%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/{ShareParserResult.java => ShareParserResult.kt} (71%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/{SharePermissionsBuilder.java => SharePermissionsBuilder.kt} (77%) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 3dce97b1..3b04c31f 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -1,8 +1,10 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' dependencies { api 'com.squareup.okhttp3:okhttp:3.12.0' - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.11" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java deleted file mode 100644 index 9faeff67..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java +++ /dev/null @@ -1,269 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2019 ownCloud GmbH - * - * 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.resources.shares; - -import android.net.Uri; - -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import okhttp3.FormBody; - -import java.net.URL; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Locale; - -/** - * Creates a new share. This allows sharing with a user or group or as a link. - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - */ -public class CreateRemoteShareOperation extends RemoteOperation { - - private static final String TAG = CreateRemoteShareOperation.class.getSimpleName(); - - private static final String PARAM_NAME = "name"; - private static final String PARAM_PASSWORD = "password"; - private static final String PARAM_EXPIRATION_DATE = "expireDate"; - private static final String PARAM_PUBLIC_UPLOAD = "publicUpload"; - private static final String PARAM_PATH = "path"; - private static final String PARAM_SHARE_TYPE = "shareType"; - private static final String PARAM_SHARE_WITH = "shareWith"; - private static final String PARAM_PERMISSIONS = "permissions"; - private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"; - - private String mRemoteFilePath; - private ShareType mShareType; - private String mShareWith; - private boolean mGetShareDetails; - - /** - * Name to set for the public link - */ - private String mName = ""; - - /** - * Password to set for the public link - */ - private String mPassword; - - /** - * Expiration date to set for the public link - */ - private long mExpirationDateInMillis = 0; - - /** - * Access permissions for the file bound to the share - */ - private int mPermissions; - - /** - * Upload permissions for the public link (only folders) - */ - private Boolean mPublicUpload; - - /** - * Constructor - * - * @param remoteFilePath Full path of the file/folder being shared. Mandatory argument - * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument - * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType - * of 0 or 1 - * @param publicUpload If false (default) public cannot upload to a public shared folder. - * If true public can upload to a shared folder. Only available for public link shares - * @param password Password to protect a public link share. Only available for public link shares - * @param permissions 1 - Read only Default for public shares - * 2 - Update - * 4 - Create - * 8 - Delete - * 16- Re-share - * 31- All above Default for private shares - * For user or group shares. - * To obtain combinations, add the desired values together. - * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27. - */ - public CreateRemoteShareOperation( - String remoteFilePath, - ShareType shareType, - String shareWith, - boolean publicUpload, - String password, - int permissions - ) { - - mRemoteFilePath = remoteFilePath; - mShareType = shareType; - mShareWith = shareWith; - mPublicUpload = publicUpload; - mPassword = password; - mPermissions = permissions; - mGetShareDetails = false; // defaults to false for backwards compatibility - } - - /** - * Set name to create in Share resource. Ignored by servers previous to version 10.0.0 - * - * @param name Name to set to the target share. - * Null or empty string result in no value set for the name. - */ - public void setName(String name) { - this.mName = (name == null) ? "" : name; - } - - /** - * Set password to create in Share resource. - * - * @param password Password to set to the target share. - * Null or empty string result in no value set for the password. - */ - public void setPassword(String password) { - mPassword = password; - } - - /** - * Set expiration date to create in Share resource. - * - * @param expirationDateInMillis Expiration date to set to the target share. - * Zero or negative value results in no value sent for expiration date. - */ - public void setExpirationDate(long expirationDateInMillis) { - mExpirationDateInMillis = expirationDateInMillis; - } - - /** - * Set permissions to create in Share resource. - * - * @param permissions Permissions to set to the target share. - * Values <= 0 result in value set to the permissions. - */ - public void setPermissions(int permissions) { - mPermissions = permissions; - } - - /** - * * Enable upload permissions to create in Share resource. - * * - * * @param publicUpload Upload permission to set to the target share. - * * Null results in no update applied to the upload permission. - */ - public void setPublicUpload(Boolean publicUpload) { - mPublicUpload = publicUpload; - } - - public void setGetShareDetails(boolean set) { - mGetShareDetails = set; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; - - try { - FormBody.Builder formBodyBuilder = new FormBody.Builder() - .add(PARAM_PATH, mRemoteFilePath) - .add(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())) - .add(PARAM_SHARE_WITH, mShareWith); - - if (mName.length() > 0) { - formBodyBuilder.add(PARAM_NAME, mName); - } - - if (mExpirationDateInMillis > 0) { - DateFormat dateFormat = new SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()); - Calendar expirationDate = Calendar.getInstance(); - expirationDate.setTimeInMillis(mExpirationDateInMillis); - String formattedExpirationDate = dateFormat.format(expirationDate.getTime()); - formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate); - } - - if (mPublicUpload) { - formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); - } - if (mPassword != null && mPassword.length() > 0) { - formBodyBuilder.add(PARAM_PASSWORD, mPassword); - } - if (RemoteShare.DEFAULT_PERMISSION != mPermissions) { - formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)); - } - - Uri requestUri = client.getBaseUri(); - Uri.Builder uriBuilder = requestUri.buildUpon(); - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); - - PostMethod postMethod = new PostMethod(new URL(uriBuilder.build().toString())); - - postMethod.setRequestBody(formBodyBuilder.build()); - - postMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8); - postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - - int status = client.executeHttpMethod(postMethod); - - ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( - new ShareXMLParser() - ); - - if (isSuccess(status)) { - parser.setOneOrMoreSharesRequired(true); - parser.setOwnCloudVersion(client.getOwnCloudVersion()); - parser.setServerBaseUri(client.getBaseUri()); - result = parser.parse(postMethod.getResponseBodyAsString()); - - if (result.isSuccess() && mGetShareDetails) { - - // TODO Use executeHttpMethod - // retrieve more info - POST only returns the index of the new share - RemoteShare emptyShare = result.getData().getShares().get(0); - GetRemoteShareOperation getInfo = new GetRemoteShareOperation( - emptyShare.getRemoteId() - ); - result = getInfo.execute(client); - } - - } else { - result = parser.parse(postMethod.getResponseBodyAsString()); - } - - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(TAG, "Exception while Creating New Share", e); - } - return result; - } - - private boolean isSuccess(int status) { - return (status == HttpConstants.HTTP_OK); - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt new file mode 100644 index 00000000..01cbfc3a --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -0,0 +1,252 @@ +/* ownCloud Android Library is available under MIT license + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * Copyright (C) 2019 ownCloud GmbH + * + * 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.resources.shares + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import okhttp3.FormBody +import java.net.URL +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale + +/** + * Creates a new share. This allows sharing with a user or group or as a link. + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + */ +class CreateRemoteShareOperation +/** + * Constructor + * + * @param remoteFilePath Full path of the file/folder being shared. Mandatory argument + * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument + * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType + * of 0 or 1 + * @param publicUpload If false (default) public cannot upload to a public shared folder. + * If true public can upload to a shared folder. Only available for public link shares + * @param password Password to protect a public link share. Only available for public link shares + * @param permissions 1 - Read only Default for public shares + * 2 - Update + * 4 - Create + * 8 - Delete + * 16- Re-share + * 31- All above Default for private shares + * For user or group shares. + * To obtain combinations, add the desired values together. + * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27. + */ + ( + private val mRemoteFilePath: String, + private val mShareType: ShareType, + private val mShareWith: String, + /** + * Upload permissions for the public link (only folders) + */ + private var mPublicUpload: Boolean?, + /** + * Password to set for the public link + */ + private var mPassword: String?, + /** + * Access permissions for the file bound to the share + */ + private var mPermissions: Int +) : RemoteOperation() { + private var mGetShareDetails: Boolean = false + + /** + * Name to set for the public link + */ + private var mName = "" + + /** + * Expiration date to set for the public link + */ + private var mExpirationDateInMillis: Long = 0 + + init { + mGetShareDetails = false // defaults to false for backwards compatibility + } + + /** + * Set name to create in Share resource. Ignored by servers previous to version 10.0.0 + * + * @param name Name to set to the target share. + * Null or empty string result in no value set for the name. + */ + fun setName(name: String?) { + this.mName = name ?: "" + } + + /** + * Set password to create in Share resource. + * + * @param password Password to set to the target share. + * Null or empty string result in no value set for the password. + */ + fun setPassword(password: String) { + mPassword = password + } + + /** + * Set expiration date to create in Share resource. + * + * @param expirationDateInMillis Expiration date to set to the target share. + * Zero or negative value results in no value sent for expiration date. + */ + fun setExpirationDate(expirationDateInMillis: Long) { + mExpirationDateInMillis = expirationDateInMillis + } + + /** + * Set permissions to create in Share resource. + * + * @param permissions Permissions to set to the target share. + * Values <= 0 result in value set to the permissions. + */ + fun setPermissions(permissions: Int) { + mPermissions = permissions + } + + /** + * * Enable upload permissions to create in Share resource. + * * + * * @param publicUpload Upload permission to set to the target share. + * * Null results in no update applied to the upload permission. + */ + fun setPublicUpload(publicUpload: Boolean?) { + mPublicUpload = publicUpload + } + + fun setGetShareDetails(set: Boolean) { + mGetShareDetails = set + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult + + try { + val formBodyBuilder = FormBody.Builder() + .add(PARAM_PATH, mRemoteFilePath) + .add(PARAM_SHARE_TYPE, Integer.toString(mShareType.value)) + .add(PARAM_SHARE_WITH, mShareWith) + + if (mName.length > 0) { + formBodyBuilder.add(PARAM_NAME, mName) + } + + if (mExpirationDateInMillis > 0) { + val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) + val expirationDate = Calendar.getInstance() + expirationDate.timeInMillis = mExpirationDateInMillis + val formattedExpirationDate = dateFormat.format(expirationDate.time) + formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate) + } + + if (mPublicUpload!!) { + formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, java.lang.Boolean.toString(true)) + } + if (mPassword != null && mPassword!!.length > 0) { + formBodyBuilder.add(PARAM_PASSWORD, mPassword!!) + } + if (RemoteShare.DEFAULT_PERMISSION != mPermissions) { + formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)) + } + + val requestUri = client.baseUri + val uriBuilder = requestUri.buildUpon() + uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) + + val postMethod = PostMethod(URL(uriBuilder.build().toString())) + + postMethod.setRequestBody(formBodyBuilder.build()) + + postMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) + postMethod.addRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE) + + val status = client.executeHttpMethod(postMethod) + + val parser = ShareToRemoteOperationResultParser( + ShareXMLParser() + ) + + if (isSuccess(status)) { + parser.setOneOrMoreSharesRequired(true) + parser.setOwnCloudVersion(client.ownCloudVersion) + parser.setServerBaseUri(client.baseUri) + result = parser.parse(postMethod.responseBodyAsString) + + if (result.isSuccess && mGetShareDetails) { + + // TODO Use executeHttpMethod + // retrieve more info - POST only returns the index of the new share + val emptyShare = result.data.shares[0] + val getInfo = GetRemoteShareOperation( + emptyShare.remoteId + ) + result = getInfo.execute(client) + } + + } else { + result = parser.parse(postMethod.responseBodyAsString) + } + + } catch (e: Exception) { + result = RemoteOperationResult(e) + Log_OC.e(TAG, "Exception while Creating New Share", e) + } + + return result + } + + private fun isSuccess(status: Int): Boolean { + return status == HttpConstants.HTTP_OK + } + + companion object { + + private val TAG = CreateRemoteShareOperation::class.java.simpleName + + private val PARAM_NAME = "name" + private val PARAM_PASSWORD = "password" + private val PARAM_EXPIRATION_DATE = "expireDate" + private val PARAM_PUBLIC_UPLOAD = "publicUpload" + private val PARAM_PATH = "path" + private val PARAM_SHARE_TYPE = "shareType" + private val PARAM_SHARE_WITH = "shareWith" + private val PARAM_PERMISSIONS = "permissions" + private val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.java deleted file mode 100644 index 5d340524..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.java +++ /dev/null @@ -1,343 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2019 ownCloud GmbH. - * - * 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.resources.shares; - -import java.io.File; -import java.io.Serializable; - -import android.os.Parcel; -import android.os.Parcelable; - -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.files.FileUtils; - - - -/** - * Contains the data of a Share from the Share API - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - */ -public class RemoteShare implements Parcelable, Serializable { - - /** - * Generated - should be refreshed every time the class changes!! - */ - private static final long serialVersionUID = 4124975224281327921L; - - private static final String TAG = RemoteShare.class.getSimpleName(); - - public static final int DEFAULT_PERMISSION = -1; - public static final int READ_PERMISSION_FLAG = 1; - public static final int UPDATE_PERMISSION_FLAG = 2; - public static final int CREATE_PERMISSION_FLAG = 4; - public static final int DELETE_PERMISSION_FLAG = 8; - public static final int SHARE_PERMISSION_FLAG = 16; - public static final int MAXIMUM_PERMISSIONS_FOR_FILE = - READ_PERMISSION_FLAG + - UPDATE_PERMISSION_FLAG + - SHARE_PERMISSION_FLAG; - public static final int MAXIMUM_PERMISSIONS_FOR_FOLDER = - MAXIMUM_PERMISSIONS_FOR_FILE + - CREATE_PERMISSION_FLAG + - DELETE_PERMISSION_FLAG; - public static final int FEDERATED_PERMISSIONS_FOR_FILE_UP_TO_OC9 = - READ_PERMISSION_FLAG + - UPDATE_PERMISSION_FLAG; - public static final int FEDERATED_PERMISSIONS_FOR_FILE_AFTER_OC9 = - READ_PERMISSION_FLAG + - UPDATE_PERMISSION_FLAG + - SHARE_PERMISSION_FLAG; - public static final int FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 = - READ_PERMISSION_FLAG + - UPDATE_PERMISSION_FLAG + - CREATE_PERMISSION_FLAG + - DELETE_PERMISSION_FLAG; - public static final int FEDERATED_PERMISSIONS_FOR_FOLDER_AFTER_OC9 = - FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 + - SHARE_PERMISSION_FLAG; - - 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 String mSharedWithAdditionalInfo; - private String mName; - private boolean mIsFolder; - private long mUserId; - private long mRemoteId; - private String mShareLink; - - public RemoteShare() { - super(); - resetData(); - } - - public RemoteShare(String path) { - resetData(); - if (path == null || path.length() <= 0 || !path.startsWith(FileUtils.PATH_SEPARATOR)) { - Log_OC.e(TAG, "Trying to create a RemoteShare with a non valid path"); - throw new IllegalArgumentException("Trying to create a RemoteShare with a non valid path: " + path); - } - mPath = path; - } - - /** - * Used internally. Reset all file properties - */ - private void resetData() { - mFileSource = 0; - mItemSource = 0; - mShareType = ShareType.NO_SHARED; - mShareWith = ""; - mPath = ""; - mPermissions = -1; - mSharedDate = 0; - mExpirationDate = 0; - mToken = ""; - mSharedWithDisplayName = ""; - mSharedWithAdditionalInfo = ""; - mIsFolder = false; - mUserId = -1; - mRemoteId = -1; - mShareLink = ""; - mName = ""; - } - - /// 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 != null) ? shareWith : ""; - } - - public String getPath() { - return mPath; - } - - public void setPath(String path) { - this.mPath = (path != null) ? 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 != null) ? token : ""; - } - - public String getSharedWithDisplayName() { - return mSharedWithDisplayName; - } - - public void setSharedWithDisplayName(String sharedWithDisplayName) { - this.mSharedWithDisplayName = (sharedWithDisplayName != null) ? sharedWithDisplayName : ""; - } - - public String getSharedWithAdditionalInfo() { - return mSharedWithAdditionalInfo; - } - - public void setSharedWithAdditionalInfo(String sharedWithAdditionalInfo) { - this.mSharedWithAdditionalInfo = sharedWithAdditionalInfo; - } - - public String getName() { - return mName; - } - - public void setName(String name) { - mName = (name != null) ? name : ""; - } - - public boolean isFolder() { - return mIsFolder; - } - - public void setIsFolder(boolean isFolder) { - this.mIsFolder = isFolder; - } - - public long getUserId() { - return mUserId; - } - - public void setUserId(long userId) { - this.mUserId = userId; - } - - public long getRemoteId() { - return mRemoteId; - } - - public void setIdRemoteShared(long remoteId) { - this.mRemoteId = remoteId; - } - - public String getShareLink() { - return this.mShareLink; - } - - public void setShareLink(String shareLink) { - this.mShareLink = (shareLink != null) ? shareLink : ""; - } - - public boolean isPasswordProtected() { - return ShareType.PUBLIC_LINK.equals(mShareType) && mShareWith.length() > 0; - } - - /** - * Parcelable Methods - */ - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - @Override - public RemoteShare createFromParcel(Parcel source) { - return new RemoteShare(source); - } - - @Override - public RemoteShare[] newArray(int size) { - return new RemoteShare[size]; - } - }; - - /** - * Reconstruct from parcel - * - * @param source The source parcel - */ - protected RemoteShare(Parcel source) { - readFromParcel(source); - } - - public void readFromParcel(Parcel 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(); - mSharedWithAdditionalInfo = source.readString(); - mIsFolder = source.readInt() == 0; - mUserId = source.readLong(); - mRemoteId = source.readLong(); - mShareLink = source.readString(); - mName = source.readString(); - } - - - @Override - public int describeContents() { - return this.hashCode(); - } - - - @Override - public void writeToParcel(Parcel dest, int 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.writeString(mSharedWithAdditionalInfo); - dest.writeInt(mIsFolder ? 1 : 0); - dest.writeLong(mUserId); - dest.writeLong(mRemoteId); - dest.writeString(mShareLink); - dest.writeString(mName); - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt new file mode 100644 index 00000000..64baee1b --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt @@ -0,0 +1,241 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2019 ownCloud GmbH. + * + * 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.resources.shares + +import android.os.Parcel +import android.os.Parcelable +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.files.FileUtils +import java.io.Serializable + +/** + * Contains the data of a Share from the Share API + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + */ +class RemoteShare : Parcelable, Serializable { + + /// Getters and Setters + var fileSource: Long = 0 + var itemSource: Long = 0 + var shareType: ShareType? = null + private var mShareWith: String? = null + private var mPath: String? = null + var permissions: Int = 0 + var sharedDate: Long = 0 + var expirationDate: Long = 0 + private var mToken: String? = null + private var mSharedWithDisplayName: String? = null + var sharedWithAdditionalInfo: String? = null + private var mName: String? = null + var isFolder: Boolean = false + var userId: Long = 0 + var remoteId: Long = 0 + private set + private var mShareLink: String? = null + + var shareWith: String? + get() = mShareWith + set(shareWith) { + this.mShareWith = shareWith ?: "" + } + + var path: String? + get() = mPath + set(path) { + this.mPath = path ?: "" + } + + var token: String? + get() = mToken + set(token) { + this.mToken = token ?: "" + } + + var sharedWithDisplayName: String? + get() = mSharedWithDisplayName + set(sharedWithDisplayName) { + this.mSharedWithDisplayName = sharedWithDisplayName ?: "" + } + + var name: String? + get() = mName + set(name) { + mName = name ?: "" + } + + var shareLink: String? + get() = this.mShareLink + set(shareLink) { + this.mShareLink = shareLink ?: "" + } + + val isPasswordProtected: Boolean + get() = ShareType.PUBLIC_LINK == shareType && mShareWith!!.length > 0 + + constructor() : super() { + resetData() + } + + constructor(path: String?) { + resetData() + if (path == null || path.length <= 0 || !path.startsWith(FileUtils.PATH_SEPARATOR)) { + Log_OC.e(TAG, "Trying to create a RemoteShare with a non valid path") + throw IllegalArgumentException("Trying to create a RemoteShare with a non valid path: " + path!!) + } + mPath = path + } + + /** + * Used internally. Reset all file properties + */ + private fun resetData() { + fileSource = 0 + itemSource = 0 + shareType = ShareType.NO_SHARED + mShareWith = "" + mPath = "" + permissions = -1 + sharedDate = 0 + expirationDate = 0 + mToken = "" + mSharedWithDisplayName = "" + sharedWithAdditionalInfo = "" + isFolder = false + userId = -1 + remoteId = -1 + mShareLink = "" + mName = "" + } + + fun setIdRemoteShared(remoteId: Long) { + this.remoteId = remoteId + } + + /** + * Reconstruct from parcel + * + * @param source The source parcel + */ + protected constructor(source: Parcel) { + readFromParcel(source) + } + + fun readFromParcel(source: Parcel) { + fileSource = source.readLong() + itemSource = source.readLong() + try { + shareType = ShareType.valueOf(source.readString()) + } catch (x: IllegalArgumentException) { + shareType = ShareType.NO_SHARED + } + + mShareWith = source.readString() + mPath = source.readString() + permissions = source.readInt() + sharedDate = source.readLong() + expirationDate = source.readLong() + mToken = source.readString() + mSharedWithDisplayName = source.readString() + sharedWithAdditionalInfo = source.readString() + isFolder = source.readInt() == 0 + userId = source.readLong() + remoteId = source.readLong() + mShareLink = source.readString() + mName = source.readString() + } + + override fun describeContents(): Int { + return this.hashCode() + } + + override fun writeToParcel(dest: Parcel, flags: Int) { + dest.writeLong(fileSource) + dest.writeLong(itemSource) + dest.writeString(if (shareType == null) "" else shareType!!.name) + dest.writeString(mShareWith) + dest.writeString(mPath) + dest.writeInt(permissions) + dest.writeLong(sharedDate) + dest.writeLong(expirationDate) + dest.writeString(mToken) + dest.writeString(mSharedWithDisplayName) + dest.writeString(sharedWithAdditionalInfo) + dest.writeInt(if (isFolder) 1 else 0) + dest.writeLong(userId) + dest.writeLong(remoteId) + dest.writeString(mShareLink) + dest.writeString(mName) + } + + companion object { + + /** + * Generated - should be refreshed every time the class changes!! + */ + private const val serialVersionUID = 4124975224281327921L + + private val TAG = RemoteShare::class.java.simpleName + + val DEFAULT_PERMISSION = -1 + val READ_PERMISSION_FLAG = 1 + val UPDATE_PERMISSION_FLAG = 2 + val CREATE_PERMISSION_FLAG = 4 + val DELETE_PERMISSION_FLAG = 8 + val SHARE_PERMISSION_FLAG = 16 + val MAXIMUM_PERMISSIONS_FOR_FILE = READ_PERMISSION_FLAG + + UPDATE_PERMISSION_FLAG + + SHARE_PERMISSION_FLAG + val MAXIMUM_PERMISSIONS_FOR_FOLDER = MAXIMUM_PERMISSIONS_FOR_FILE + + CREATE_PERMISSION_FLAG + + DELETE_PERMISSION_FLAG + val FEDERATED_PERMISSIONS_FOR_FILE_UP_TO_OC9 = READ_PERMISSION_FLAG + UPDATE_PERMISSION_FLAG + val FEDERATED_PERMISSIONS_FOR_FILE_AFTER_OC9 = READ_PERMISSION_FLAG + + UPDATE_PERMISSION_FLAG + + SHARE_PERMISSION_FLAG + val FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 = READ_PERMISSION_FLAG + + UPDATE_PERMISSION_FLAG + + CREATE_PERMISSION_FLAG + + DELETE_PERMISSION_FLAG + val FEDERATED_PERMISSIONS_FOR_FOLDER_AFTER_OC9 = + FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 + SHARE_PERMISSION_FLAG + + /** + * Parcelable Methods + */ + @JvmField + val CREATOR: Parcelable.Creator = object : Parcelable.Creator { + override fun createFromParcel(source: Parcel): RemoteShare { + return RemoteShare(source) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt similarity index 50% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index facf141f..6494a4c1 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -25,18 +25,15 @@ * */ -package com.owncloud.android.lib.resources.shares; +package com.owncloud.android.lib.resources.shares -import android.net.Uri; - -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; - -import java.net.URL; +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import java.net.URL /** * Remove a share @@ -46,63 +43,59 @@ import java.net.URL; * @author David González Verdugo */ -public class RemoveRemoteShareOperation extends RemoteOperation { +class RemoveRemoteShareOperation +/** + * Constructor + * + * @param remoteShareId Share ID + */ + (private val mRemoteShareId: Long) : RemoteOperation() { - private static final String TAG = RemoveRemoteShareOperation.class.getSimpleName(); - - private long mRemoteShareId; - - /** - * Constructor - * - * @param remoteShareId Share ID - */ - - public RemoveRemoteShareOperation(long remoteShareId) { - mRemoteShareId = remoteShareId; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult try { - Uri requestUri = client.getBaseUri(); - Uri.Builder uriBuilder = requestUri.buildUpon(); - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); - uriBuilder.appendEncodedPath(String.valueOf(mRemoteShareId)); + val requestUri = client.baseUri + val uriBuilder = requestUri.buildUpon() + uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) + uriBuilder.appendEncodedPath(mRemoteShareId.toString()) - DeleteMethod deleteMethod = new DeleteMethod( - new URL(uriBuilder.build().toString()) - ); + val deleteMethod = DeleteMethod( + URL(uriBuilder.build().toString()) + ) - deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + deleteMethod.addRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE) - int status = client.executeHttpMethod(deleteMethod); + val status = client.executeHttpMethod(deleteMethod) if (isSuccess(status)) { // Parse xml response and obtain the list of shares - ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( - new ShareXMLParser() - ); - result = parser.parse(deleteMethod.getResponseBodyAsString()); + val parser = ShareToRemoteOperationResultParser( + ShareXMLParser() + ) + result = parser.parse(deleteMethod.responseBodyAsString) - Log_OC.d(TAG, "Unshare " + mRemoteShareId + ": " + result.getLogMessage()); + Log_OC.d(TAG, "Unshare " + mRemoteShareId + ": " + result.logMessage) } else { - result = new RemoteOperationResult<>(deleteMethod); + result = RemoteOperationResult(deleteMethod) } - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(TAG, "Unshare Link Exception " + result.getLogMessage(), e); + } catch (e: Exception) { + result = RemoteOperationResult(e) + Log_OC.e(TAG, "Unshare Link Exception " + result.logMessage, e) } - return result; + return result } - private boolean isSuccess(int status) { - return (status == HttpConstants.HTTP_OK); + private fun isSuccess(status: Int): Boolean { + return status == HttpConstants.HTTP_OK + } + + companion object { + + private val TAG = RemoveRemoteShareOperation::class.java.simpleName } } \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.kt similarity index 71% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.kt index a03a5839..e4c0e9ed 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.kt @@ -23,24 +23,8 @@ * */ -package com.owncloud.android.lib.resources.shares; +package com.owncloud.android.lib.resources.shares -import java.util.ArrayList; +import java.util.ArrayList -public class ShareParserResult { - private ArrayList shares; - private String parserMessage; - - public ShareParserResult(ArrayList shares, String parserMessage) { - this.shares = shares; - this.parserMessage = parserMessage; - } - - public ArrayList getShares() { - return shares; - } - - public String getParserMessage() { - return parserMessage; - } -} \ No newline at end of file +class ShareParserResult(val shares: ArrayList, val parserMessage: String) \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.kt similarity index 77% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.kt index a38d7970..18be72d7 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.kt @@ -1,6 +1,6 @@ /* ownCloud Android Library is available under MIT license * @author David A. Velasco - * Copyright (C) 2016 ownCloud GmbH. + * Copyright (C) 2019 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,16 +23,16 @@ * */ -package com.owncloud.android.lib.resources.shares; +package com.owncloud.android.lib.resources.shares /** * Provides method to define a set of share permissions and calculate the appropiate * int value representing it. */ -public class SharePermissionsBuilder { +class SharePermissionsBuilder { - /** Set of permissions */ - private int mPermissions = RemoteShare.READ_PERMISSION_FLAG; // READ is minimum permission + /** Set of permissions */ + private var mPermissions = RemoteShare.READ_PERMISSION_FLAG // READ is minimum permission /** * Sets or clears permission to reshare a file or folder. @@ -40,9 +40,9 @@ public class SharePermissionsBuilder { * @param enabled 'True' to set, 'false' to clear. * @return Instance to builder itself, to allow consecutive calls to setters */ - public SharePermissionsBuilder setSharePermission(boolean enabled) { - updatePermission(RemoteShare.SHARE_PERMISSION_FLAG, enabled); - return this; + fun setSharePermission(enabled: Boolean): SharePermissionsBuilder { + updatePermission(RemoteShare.SHARE_PERMISSION_FLAG, enabled) + return this } /** @@ -51,9 +51,9 @@ public class SharePermissionsBuilder { * @param enabled 'True' to set, 'false' to clear. * @return Instance to builder itself, to allow consecutive calls to setters */ - public SharePermissionsBuilder setUpdatePermission(boolean enabled) { - updatePermission(RemoteShare.UPDATE_PERMISSION_FLAG, enabled); - return this; + fun setUpdatePermission(enabled: Boolean): SharePermissionsBuilder { + updatePermission(RemoteShare.UPDATE_PERMISSION_FLAG, enabled) + return this } /** @@ -62,9 +62,9 @@ public class SharePermissionsBuilder { * @param enabled 'True' to set, 'false' to clear. * @return Instance to builder itself, to allow consecutive calls to setters */ - public SharePermissionsBuilder setCreatePermission(boolean enabled) { - updatePermission(RemoteShare.CREATE_PERMISSION_FLAG, enabled); - return this; + fun setCreatePermission(enabled: Boolean): SharePermissionsBuilder { + updatePermission(RemoteShare.CREATE_PERMISSION_FLAG, enabled) + return this } /** @@ -73,9 +73,9 @@ public class SharePermissionsBuilder { * @param enabled 'True' to set, 'false' to clear. * @return Instance to builder itself, to allow consecutive calls to setters */ - public SharePermissionsBuilder setDeletePermission(boolean enabled) { - updatePermission(RemoteShare.DELETE_PERMISSION_FLAG, enabled); - return this; + fun setDeletePermission(enabled: Boolean): SharePermissionsBuilder { + updatePermission(RemoteShare.DELETE_PERMISSION_FLAG, enabled) + return this } /** @@ -84,13 +84,13 @@ public class SharePermissionsBuilder { * @param permissionsFlag Flag for the permission to update. * @param enable 'True' to set, 'false' to clear. */ - private void updatePermission(int permissionsFlag, boolean enable) { + private fun updatePermission(permissionsFlag: Int, enable: Boolean) { if (enable) { // add permission - mPermissions |= permissionsFlag; + mPermissions = mPermissions or permissionsFlag } else { // delete permission - mPermissions &= ~permissionsFlag; + mPermissions = mPermissions and permissionsFlag.inv() } } @@ -99,7 +99,7 @@ public class SharePermissionsBuilder { * * @return An int value representing the accumulated set of permissions. */ - public int build() { - return mPermissions; + fun build(): Int { + return mPermissions } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.java deleted file mode 100644 index 8292ca02..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.java +++ /dev/null @@ -1,140 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * @author David A. Velasco - * @author David González Verdugo - * @author Christian Schabesberger - * Copyright (C) 2019 ownCloud GmbH. - * - * 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.resources.shares; - -import android.net.Uri; - -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.status.OwnCloudVersion; -import org.xmlpull.v1.XmlPullParserException; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -public class ShareToRemoteOperationResultParser { - - private static final String TAG = ShareToRemoteOperationResultParser.class.getSimpleName(); - - private ShareXMLParser mShareXmlParser = null; - private boolean mOneOrMoreSharesRequired = false; - private OwnCloudVersion mOwnCloudVersion = null; - private Uri mServerBaseUri = null; - - public ShareToRemoteOperationResultParser(ShareXMLParser shareXmlParser) { - mShareXmlParser = shareXmlParser; - } - - public void setOneOrMoreSharesRequired(boolean oneOrMoreSharesRequired) { - mOneOrMoreSharesRequired = oneOrMoreSharesRequired; - } - - public void setOwnCloudVersion(OwnCloudVersion ownCloudVersion) { - mOwnCloudVersion = ownCloudVersion; - } - - public void setServerBaseUri(Uri serverBaseURi) { - mServerBaseUri = serverBaseURi; - } - - public RemoteOperationResult parse(String serverResponse) { - if (serverResponse == null || serverResponse.length() == 0) { - return new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); - } - - RemoteOperationResult result; - final ArrayList resultData = new ArrayList<>(); - - try { - // Parse xml response and obtain the list of shares - InputStream is = new ByteArrayInputStream(serverResponse.getBytes()); - if (mShareXmlParser == null) { - Log_OC.w(TAG, "No ShareXmlParser provided, creating new instance "); - mShareXmlParser = new ShareXMLParser(); - } - List shares = mShareXmlParser.parseXMLResponse(is); - - if (mShareXmlParser.isSuccess()) { - if ((shares != null && shares.size() > 0) || !mOneOrMoreSharesRequired) { - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK); - if (shares != null) { - for (RemoteShare share : shares) { - resultData.add(share); - // build the share link if not in the response - // (needed for OC servers < 9.0.0, see ShareXMLParser.java#line256) - if (share.getShareType() == ShareType.PUBLIC_LINK - && (share.getShareLink() == null - || share.getShareLink().length() <= 0) - && share.getToken().length() > 0) { - if (mServerBaseUri != null) { - String sharingLinkPath = ShareUtils.getSharingLinkPath(mOwnCloudVersion); - share.setShareLink(mServerBaseUri + sharingLinkPath + share.getToken()); - } else { - Log_OC.e(TAG, "Couldn't build link for public share :("); - } - } - } - } - result.setData(new ShareParserResult(resultData, "")); - - } else { - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); - Log_OC.e(TAG, "Successful status with no share in the response"); - } - - } else if (mShareXmlParser.isWrongParameter()) { - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.SHARE_WRONG_PARAMETER); - result.setData(new ShareParserResult(null, mShareXmlParser.getMessage())); - - } else if (mShareXmlParser.isNotFound()) { - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND); - result.setData(new ShareParserResult(null, mShareXmlParser.getMessage())); - - } else if (mShareXmlParser.isForbidden()) { - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.SHARE_FORBIDDEN); - result.setData(new ShareParserResult(null, mShareXmlParser.getMessage())); - - } else { - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); - } - - } catch (XmlPullParserException e) { - Log_OC.e(TAG, "Error parsing response from server ", e); - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); - - } catch (IOException e) { - Log_OC.e(TAG, "Error reading response from server ", e); - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); - } - - return result; - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt new file mode 100644 index 00000000..55786c41 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt @@ -0,0 +1,142 @@ +/* ownCloud Android Library is available under MIT license + * @author David A. Velasco + * @author David González Verdugo + * @author Christian Schabesberger + * Copyright (C) 2019 ownCloud GmbH. + * + * 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.resources.shares + +import android.net.Uri + +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.status.OwnCloudVersion +import org.xmlpull.v1.XmlPullParserException + +import java.io.ByteArrayInputStream +import java.io.IOException +import java.io.InputStream +import java.util.ArrayList + +class ShareToRemoteOperationResultParser(shareXmlParser: ShareXMLParser) { + + private var mShareXmlParser: ShareXMLParser? = null + private var mOneOrMoreSharesRequired = false + private var mOwnCloudVersion: OwnCloudVersion? = null + private var mServerBaseUri: Uri? = null + + init { + mShareXmlParser = shareXmlParser + } + + fun setOneOrMoreSharesRequired(oneOrMoreSharesRequired: Boolean) { + mOneOrMoreSharesRequired = oneOrMoreSharesRequired + } + + fun setOwnCloudVersion(ownCloudVersion: OwnCloudVersion) { + mOwnCloudVersion = ownCloudVersion + } + + fun setServerBaseUri(serverBaseURi: Uri) { + mServerBaseUri = serverBaseURi + } + + fun parse(serverResponse: String?): RemoteOperationResult { + if (serverResponse == null || serverResponse.length == 0) { + return RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) + } + + var result: RemoteOperationResult + val resultData = ArrayList() + + try { + // Parse xml response and obtain the list of shares + val `is` = ByteArrayInputStream(serverResponse.toByteArray()) + if (mShareXmlParser == null) { + Log_OC.w(TAG, "No ShareXmlParser provided, creating new instance ") + mShareXmlParser = ShareXMLParser() + } + val shares = mShareXmlParser!!.parseXMLResponse(`is`) + + if (mShareXmlParser!!.isSuccess) { + if (shares != null && shares.size > 0 || !mOneOrMoreSharesRequired) { + result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + if (shares != null) { + for (share in shares) { + resultData.add(share) + // build the share link if not in the response + // (needed for OC servers < 9.0.0, see ShareXMLParser.java#line256) + if (share.shareType == ShareType.PUBLIC_LINK + && (share.shareLink == null || share.shareLink!!.length <= 0) + && share.token!!.length > 0 + ) { + if (mServerBaseUri != null) { + val sharingLinkPath = ShareUtils.getSharingLinkPath(mOwnCloudVersion) + share.shareLink = mServerBaseUri.toString() + sharingLinkPath + share.token + } else { + Log_OC.e(TAG, "Couldn't build link for public share :(") + } + } + } + } + result.setData(ShareParserResult(resultData, "")) + + } else { + result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) + Log_OC.e(TAG, "Successful status with no share in the response") + } + + } else if (mShareXmlParser!!.isWrongParameter) { + result = RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_WRONG_PARAMETER) + result.setData(ShareParserResult(null!!, mShareXmlParser!!.message!!)) + + } else if (mShareXmlParser!!.isNotFound) { + result = RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND) + result.setData(ShareParserResult(null!!, mShareXmlParser!!.message!!)) + + } else if (mShareXmlParser!!.isForbidden) { + result = RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_FORBIDDEN) + result.setData(ShareParserResult(null!!, mShareXmlParser!!.message!!)) + + } else { + result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) + } + + } catch (e: XmlPullParserException) { + Log_OC.e(TAG, "Error parsing response from server ", e) + result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) + + } catch (e: IOException) { + Log_OC.e(TAG, "Error reading response from server ", e) + result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) + } + + return result + } + + companion object { + + private val TAG = ShareToRemoteOperationResultParser::class.java.simpleName + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.java deleted file mode 100644 index fb8e877c..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.java +++ /dev/null @@ -1,441 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2019 ownCloud GmbH. - * - * 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.resources.shares; - -import android.util.Xml; - -import com.owncloud.android.lib.common.network.WebdavUtils; -import com.owncloud.android.lib.resources.files.FileUtils; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; -import org.xmlpull.v1.XmlPullParserFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; - -/** - * Parser for Share API Response - * - * @author masensio - * @author David González Verdugo - */ - -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_displayname"; - private static final String NODE_SHARE_WITH_ADDITIONAL_INFO = "share_with_additional_info"; - private static final String NODE_NAME = "name"; - - private static final String NODE_URL = "url"; - - private static final String TYPE_FOLDER = "folder"; - - private static final int SUCCESS = 200; - private static final int ERROR_WRONG_PARAMETER = 400; - private static final int ERROR_FORBIDDEN = 403; - private static final int ERROR_NOT_FOUND = 404; - - private String mStatus; - private int mStatusCode; - private String mMessage; - - // 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; - } - - public String getMessage() { - return mMessage; - } - - public void setMessage(String message) { - this.mMessage = message; - } - - // Constructor - public ShareXMLParser() { - mStatusCode = -1; - } - - public boolean isSuccess() { - return mStatusCode == SUCCESS; - } - - public boolean isForbidden() { - return mStatusCode == ERROR_FORBIDDEN; - } - - public boolean isNotFound() { - return mStatusCode == ERROR_NOT_FOUND; - } - - public boolean isWrongParameter() { - return mStatusCode == ERROR_WRONG_PARAMETER; - } - - /** - * 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 shares = 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)) { - shares = readData(parser); - } else { - skip(parser); - } - - } - return shares; - } - - /** - * 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_OC.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 if (name.equalsIgnoreCase(NODE_MESSAGE)) { - setMessage(readNode(parser, NODE_MESSAGE)); - - } else { - skip(parser); - } - - } - } - - /** - * Parse Data node - * @param parser - * @return - * @throws XmlPullParserException - * @throws IOException - */ - private ArrayList readData(XmlPullParser parser) throws XmlPullParserException, - IOException { - ArrayList shares = new ArrayList(); - RemoteShare share = null; - - parser.require(XmlPullParser.START_TAG, ns, NODE_DATA); - //Log_OC.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)) { - readElement(parser, shares); - - } else if (name.equalsIgnoreCase(NODE_ID)) {// Parse Create XML Response - share = new RemoteShare(); - String value = readNode(parser, NODE_ID); - share.setIdRemoteShared(Integer.parseInt(value)); - - } else if (name.equalsIgnoreCase(NODE_URL)) { - // NOTE: this field is received in all the public shares from OC 9.0.0 - // in previous versions, it's received in the result of POST requests, but not - // in GET requests - share.setShareType(ShareType.PUBLIC_LINK); - String value = readNode(parser, NODE_URL); - share.setShareLink(value); - - } else if (name.equalsIgnoreCase(NODE_TOKEN)) { - share.setToken(readNode(parser, NODE_TOKEN)); - - } else { - skip(parser); - - } - } - - if (share != null) { - // this is the response of a request for creation; don't pass to isValidShare() - shares.add(share); - } - - return shares; - - } - - - /** - * Parse Element node - * @param parser - * @return - * @throws XmlPullParserException - * @throws IOException - */ - private void readElement(XmlPullParser parser, ArrayList shares) - throws XmlPullParserException, IOException { - parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT); - - RemoteShare share = new RemoteShare(); - - //Log_OC.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_ELEMENT)) { - // patch to work around servers responding with extra surrounding all - // the shares on the same file before - // https://github.com/owncloud/core/issues/6992 was fixed - readElement(parser, shares); - - } else if (name.equalsIgnoreCase(NODE_ID)) { - share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); - - } else if (name.equalsIgnoreCase(NODE_ITEM_TYPE)) { - share.setIsFolder(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); - fixPathForFolder(share); - - } else if (name.equalsIgnoreCase(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)); - share.setShareType(ShareType.fromValue(value)); - - } else if (name.equalsIgnoreCase(NODE_SHARE_WITH)) { - share.setShareWith(readNode(parser, NODE_SHARE_WITH)); - - } else if (name.equalsIgnoreCase(NODE_FILE_SOURCE)) { - share.setFileSource(Long.parseLong(readNode(parser, NODE_FILE_SOURCE))); - - } else if (name.equalsIgnoreCase(NODE_PATH)) { - share.setPath(readNode(parser, NODE_PATH)); - fixPathForFolder(share); - - } else if (name.equalsIgnoreCase(NODE_PERMISSIONS)) { - share.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); - - } else if (name.equalsIgnoreCase(NODE_STIME)) { - share.setSharedDate(Long.parseLong(readNode(parser, NODE_STIME))); - - } else if (name.equalsIgnoreCase(NODE_EXPIRATION)) { - String value = readNode(parser, NODE_EXPIRATION); - if (!(value.length() == 0)) { - share.setExpirationDate(WebdavUtils.parseResponseDate(value).getTime()); - } - - } else if (name.equalsIgnoreCase(NODE_TOKEN)) { - share.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)) { - share.setSharedWithDisplayName(readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME)); - - } else if (name.equalsIgnoreCase(NODE_URL)) { - String value = readNode(parser, NODE_URL); - share.setShareLink(value); - - } else if (name.equalsIgnoreCase(NODE_NAME)) { - share.setName(readNode(parser, NODE_NAME)); - - } else { - skip(parser); - } - } - - if (isValidShare(share)) { - shares.add(share); - } - } - - private boolean isValidShare(RemoteShare share) { - return (share.getRemoteId() > -1); - } - - private void fixPathForFolder(RemoteShare share) { - if (share.isFolder() && share.getPath() != null && share.getPath().length() > 0 && - !share.getPath().endsWith(FileUtils.PATH_SEPARATOR)) { - share.setPath(share.getPath() + FileUtils.PATH_SEPARATOR); - } - } - - /** - * 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_OC.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; - } - } - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt new file mode 100644 index 00000000..23f9435f --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt @@ -0,0 +1,415 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2019 ownCloud GmbH. + * + * 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.resources.shares + +import android.util.Xml + +import com.owncloud.android.lib.common.network.WebdavUtils +import com.owncloud.android.lib.resources.files.FileUtils + +import org.xmlpull.v1.XmlPullParser +import org.xmlpull.v1.XmlPullParserException +import org.xmlpull.v1.XmlPullParserFactory + +import java.io.IOException +import java.io.InputStream +import java.util.ArrayList + +/** + * Parser for Share API Response + * + * @author masensio + * @author David González Verdugo + */ + +class ShareXMLParser { + + // Getters and Setters + var status: String? = null + var statusCode: Int = 0 + var message: String? = null + + val isSuccess: Boolean + get() = statusCode == SUCCESS + + val isForbidden: Boolean + get() = statusCode == ERROR_FORBIDDEN + + val isNotFound: Boolean + get() = statusCode == ERROR_NOT_FOUND + + val isWrongParameter: Boolean + get() = statusCode == ERROR_WRONG_PARAMETER + + // Constructor + init { + statusCode = -1 + } + + /** + * Parse is as response of Share API + * @param is + * @return List of ShareRemoteFiles + * @throws XmlPullParserException + * @throws IOException + */ + @Throws(XmlPullParserException::class, IOException::class) + fun parseXMLResponse(`is`: InputStream): ArrayList { + + try { + // XMLPullParser + val factory = XmlPullParserFactory.newInstance() + factory.isNamespaceAware = true + + val 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 + */ + @Throws(XmlPullParserException::class, IOException::class) + private fun readOCS(parser: XmlPullParser): ArrayList { + var shares = ArrayList() + parser.require(XmlPullParser.START_TAG, ns, NODE_OCS) + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + val name = parser.name + // read NODE_META and NODE_DATA + if (name.equals(NODE_META, ignoreCase = true)) { + readMeta(parser) + } else if (name.equals(NODE_DATA, ignoreCase = true)) { + shares = readData(parser) + } else { + skip(parser) + } + + } + return shares + } + + /** + * Parse Meta node + * @param parser + * @throws XmlPullParserException + * @throws IOException + */ + @Throws(XmlPullParserException::class, IOException::class) + private fun readMeta(parser: XmlPullParser) { + parser.require(XmlPullParser.START_TAG, ns, NODE_META) + //Log_OC.d(TAG, "---- NODE META ---"); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + val name = parser.name + + if (name.equals(NODE_STATUS, ignoreCase = true)) { + status = readNode(parser, NODE_STATUS) + + } else if (name.equals(NODE_STATUS_CODE, ignoreCase = true)) { + statusCode = Integer.parseInt(readNode(parser, NODE_STATUS_CODE)) + + } else if (name.equals(NODE_MESSAGE, ignoreCase = true)) { + message = readNode(parser, NODE_MESSAGE) + + } else { + skip(parser) + } + + } + } + + /** + * Parse Data node + * @param parser + * @return + * @throws XmlPullParserException + * @throws IOException + */ + @Throws(XmlPullParserException::class, IOException::class) + private fun readData(parser: XmlPullParser): ArrayList { + val shares = ArrayList() + var share: RemoteShare? = null + + parser.require(XmlPullParser.START_TAG, ns, NODE_DATA) + //Log_OC.d(TAG, "---- NODE DATA ---"); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + val name = parser.name + if (name.equals(NODE_ELEMENT, ignoreCase = true)) { + readElement(parser, shares) + + } else if (name.equals(NODE_ID, ignoreCase = true)) {// Parse Create XML Response + share = RemoteShare() + val value = readNode(parser, NODE_ID) + share.setIdRemoteShared(Integer.parseInt(value).toLong()) + + } else if (name.equals(NODE_URL, ignoreCase = true)) { + // NOTE: this field is received in all the public shares from OC 9.0.0 + // in previous versions, it's received in the result of POST requests, but not + // in GET requests + share!!.shareType = ShareType.PUBLIC_LINK + val value = readNode(parser, NODE_URL) + share.shareLink = value + + } else if (name.equals(NODE_TOKEN, ignoreCase = true)) { + share!!.token = readNode(parser, NODE_TOKEN) + + } else { + skip(parser) + + } + } + + if (share != null) { + // this is the response of a request for creation; don't pass to isValidShare() + shares.add(share) + } + + return shares + + } + + /** + * Parse Element node + * @param parser + * @return + * @throws XmlPullParserException + * @throws IOException + */ + @Throws(XmlPullParserException::class, IOException::class) + private fun readElement(parser: XmlPullParser, shares: ArrayList) { + parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT) + + val share = RemoteShare() + + //Log_OC.d(TAG, "---- NODE ELEMENT ---"); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + + val name = parser.name + + if (name.equals(NODE_ELEMENT, ignoreCase = true)) { + // patch to work around servers responding with extra surrounding all + // the shares on the same file before + // https://github.com/owncloud/core/issues/6992 was fixed + readElement(parser, shares) + + } else if (name.equals(NODE_ID, ignoreCase = true)) { + share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID)).toLong()) + + } else if (name.equals(NODE_ITEM_TYPE, ignoreCase = true)) { + share.isFolder = readNode(parser, NODE_ITEM_TYPE).equals(TYPE_FOLDER, ignoreCase = true) + fixPathForFolder(share) + + } else if (name.equals(NODE_ITEM_SOURCE, ignoreCase = true)) { + share.itemSource = java.lang.Long.parseLong(readNode(parser, NODE_ITEM_SOURCE)) + + } else if (name.equals(NODE_PARENT, ignoreCase = true)) { + readNode(parser, NODE_PARENT) + + } else if (name.equals(NODE_SHARE_TYPE, ignoreCase = true)) { + val value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE)) + share.shareType = ShareType.fromValue(value) + + } else if (name.equals(NODE_SHARE_WITH, ignoreCase = true)) { + share.shareWith = readNode(parser, NODE_SHARE_WITH) + + } else if (name.equals(NODE_FILE_SOURCE, ignoreCase = true)) { + share.fileSource = java.lang.Long.parseLong(readNode(parser, NODE_FILE_SOURCE)) + + } else if (name.equals(NODE_PATH, ignoreCase = true)) { + share.path = readNode(parser, NODE_PATH) + fixPathForFolder(share) + + } else if (name.equals(NODE_PERMISSIONS, ignoreCase = true)) { + share.permissions = Integer.parseInt(readNode(parser, NODE_PERMISSIONS)) + + } else if (name.equals(NODE_STIME, ignoreCase = true)) { + share.sharedDate = java.lang.Long.parseLong(readNode(parser, NODE_STIME)) + + } else if (name.equals(NODE_EXPIRATION, ignoreCase = true)) { + val value = readNode(parser, NODE_EXPIRATION) + if (value.length != 0) { + share.expirationDate = WebdavUtils.parseResponseDate(value)!!.time + } + + } else if (name.equals(NODE_TOKEN, ignoreCase = true)) { + share.token = readNode(parser, NODE_TOKEN) + + } else if (name.equals(NODE_STORAGE, ignoreCase = true)) { + readNode(parser, NODE_STORAGE) + } else if (name.equals(NODE_MAIL_SEND, ignoreCase = true)) { + readNode(parser, NODE_MAIL_SEND) + + } else if (name.equals(NODE_SHARE_WITH_DISPLAY_NAME, ignoreCase = true)) { + share.sharedWithDisplayName = readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME) + + } else if (name.equals(NODE_URL, ignoreCase = true)) { + val value = readNode(parser, NODE_URL) + share.shareLink = value + + } else if (name.equals(NODE_NAME, ignoreCase = true)) { + share.name = readNode(parser, NODE_NAME) + + } else { + skip(parser) + } + } + + if (isValidShare(share)) { + shares.add(share) + } + } + + private fun isValidShare(share: RemoteShare): Boolean { + return share.remoteId > -1 + } + + private fun fixPathForFolder(share: RemoteShare) { + if (share.isFolder && share.path != null && share.path!!.length > 0 && + !share.path!!.endsWith(FileUtils.PATH_SEPARATOR) + ) { + share.path = share.path!! + FileUtils.PATH_SEPARATOR + } + } + + /** + * Parse a node, to obtain its text. Needs readText method + * @param parser + * @param node + * @return Text of the node + * @throws XmlPullParserException + * @throws IOException + */ + @Throws(XmlPullParserException::class, IOException::class) + private fun readNode(parser: XmlPullParser, node: String): String { + parser.require(XmlPullParser.START_TAG, ns, node) + val value = readText(parser) + //Log_OC.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 + */ + @Throws(IOException::class, XmlPullParserException::class) + private fun readText(parser: XmlPullParser): String { + var result = "" + if (parser.next() == XmlPullParser.TEXT) { + result = parser.text + parser.nextTag() + } + return result + } + + /** + * Skip tags in parser procedure + * @param parser + * @throws XmlPullParserException + * @throws IOException + */ + @Throws(XmlPullParserException::class, IOException::class) + private fun skip(parser: XmlPullParser) { + if (parser.eventType != XmlPullParser.START_TAG) { + throw IllegalStateException() + } + var depth = 1 + while (depth != 0) { + when (parser.next()) { + XmlPullParser.END_TAG -> depth-- + XmlPullParser.START_TAG -> depth++ + } + } + } + + companion object { + + //private static final String TAG = ShareXMLParser.class.getSimpleName(); + + // No namespaces + private val ns: String? = null + + // NODES for XML Parser + private val NODE_OCS = "ocs" + + private val NODE_META = "meta" + private val NODE_STATUS = "status" + private val NODE_STATUS_CODE = "statuscode" + private val NODE_MESSAGE = "message" + + private val NODE_DATA = "data" + private val NODE_ELEMENT = "element" + private val NODE_ID = "id" + private val NODE_ITEM_TYPE = "item_type" + private val NODE_ITEM_SOURCE = "item_source" + private val NODE_PARENT = "parent" + private val NODE_SHARE_TYPE = "share_type" + private val NODE_SHARE_WITH = "share_with" + private val NODE_FILE_SOURCE = "file_source" + private val NODE_PATH = "path" + private val NODE_PERMISSIONS = "permissions" + private val NODE_STIME = "stime" + private val NODE_EXPIRATION = "expiration" + private val NODE_TOKEN = "token" + private val NODE_STORAGE = "storage" + private val NODE_MAIL_SEND = "mail_send" + private val NODE_SHARE_WITH_DISPLAY_NAME = "share_with_displayname" + private val NODE_SHARE_WITH_ADDITIONAL_INFO = "share_with_additional_info" + private val NODE_NAME = "name" + + private val NODE_URL = "url" + + private val TYPE_FOLDER = "folder" + + private val SUCCESS = 200 + private val ERROR_WRONG_PARAMETER = 400 + private val ERROR_FORBIDDEN = 403 + private val ERROR_NOT_FOUND = 404 + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java deleted file mode 100644 index 83bb4d37..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java +++ /dev/null @@ -1,233 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * - * Copyright (C) 2019 ownCloud GmbH. - * - * 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.resources.shares; - -import android.net.Uri; - -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.nonwebdav.PutMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import okhttp3.FormBody; - -import java.net.URL; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Locale; - -/** - * Updates parameters of an existing Share resource, known its remote ID. - *

- * Allow updating several parameters, triggering a request to the server per parameter. - * - * @author David A. Velasco - * @author David González Verdugo - */ - -public class UpdateRemoteShareOperation extends RemoteOperation { - - private static final String TAG = GetRemoteShareOperation.class.getSimpleName(); - - private static final String PARAM_NAME = "name"; - private static final String PARAM_PASSWORD = "password"; - private static final String PARAM_EXPIRATION_DATE = "expireDate"; - private static final String PARAM_PERMISSIONS = "permissions"; - private static final String PARAM_PUBLIC_UPLOAD = "publicUpload"; - private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"; - private static final String ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private static final String ENTITY_CHARSET = "UTF-8"; - - /** - * Identifier of the share to update - */ - private long mRemoteId; - - /** - * Password to set for the public link - */ - private String mPassword; - - /** - * Expiration date to set for the public link - */ - private long mExpirationDateInMillis; - - /** - * Access permissions for the file bound to the share - */ - private int mPermissions; - - /** - * Upload permissions for the public link (only folders) - */ - private Boolean mPublicUpload; - private String mName; - - /** - * Constructor. No update is initialized by default, need to be applied with setters below. - * - * @param remoteId Identifier of the share to update. - */ - public UpdateRemoteShareOperation(long remoteId) { - mRemoteId = remoteId; - mPassword = null; // no update - mExpirationDateInMillis = 0; // no update - mPublicUpload = null; - mPermissions = RemoteShare.DEFAULT_PERMISSION; - } - - /** - * Set name to update in Share resource. Ignored by servers previous to version 10.0.0 - * - * @param name Name to set to the target share. - * Empty string clears the current name. - * Null results in no update applied to the name. - */ - public void setName(String name) { - this.mName = name; - } - - /** - * Set password to update in Share resource. - * - * @param password Password to set to the target share. - * Empty string clears the current password. - * Null results in no update applied to the password. - */ - public void setPassword(String password) { - mPassword = password; - } - - /** - * Set expiration date to update in Share resource. - * - * @param expirationDateInMillis Expiration date to set to the target share. - * A negative value clears the current expiration date. - * Zero value (start-of-epoch) results in no update done on - * the expiration date. - */ - public void setExpirationDate(long expirationDateInMillis) { - mExpirationDateInMillis = expirationDateInMillis; - } - - /** - * Set permissions to update in Share resource. - * - * @param permissions Permissions to set to the target share. - * Values <= 0 result in no update applied to the permissions. - */ - public void setPermissions(int permissions) { - mPermissions = permissions; - } - - /** - * Enable upload permissions to update in Share resource. - * - * @param publicUpload Upload permission to set to the target share. - * Null results in no update applied to the upload permission. - */ - public void setPublicUpload(Boolean publicUpload) { - mPublicUpload = publicUpload; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; - - try { - FormBody.Builder formBodyBuilder = new FormBody.Builder(); - - // Parameters to update - if (mName != null) { - formBodyBuilder.add(PARAM_NAME, mName); - } - - if (mExpirationDateInMillis < 0) { - // clear expiration date - formBodyBuilder.add(PARAM_EXPIRATION_DATE, ""); - - } else if (mExpirationDateInMillis > 0) { - // set expiration date - DateFormat dateFormat = new SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.GERMAN); - Calendar expirationDate = Calendar.getInstance(); - expirationDate.setTimeInMillis(mExpirationDateInMillis); - String formattedExpirationDate = dateFormat.format(expirationDate.getTime()); - formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate); - } // else, ignore - no update - - if (mPublicUpload != null) { - formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload)); - } - - // IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter, - // otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated - if (mPermissions > 0) { - // set permissions - formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)); - } - - Uri requestUri = client.getBaseUri(); - Uri.Builder uriBuilder = requestUri.buildUpon(); - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); - uriBuilder.appendEncodedPath(Long.toString(mRemoteId)); - - PutMethod putMethod = new PutMethod(new URL(uriBuilder.build().toString())); - - putMethod.setRequestBody(formBodyBuilder.build()); - - putMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8); - putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - - int status = client.executeHttpMethod(putMethod); - - if (isSuccess(status)) { - // Parse xml response - ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( - new ShareXMLParser() - ); - parser.setOwnCloudVersion(client.getOwnCloudVersion()); - parser.setServerBaseUri(client.getBaseUri()); - result = parser.parse(putMethod.getResponseBodyAsString()); - - } else { - result = new RemoteOperationResult<>(putMethod); - } - - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(TAG, "Exception while Creating New Share", e); - } - - return result; - } - - private boolean isSuccess(int status) { - return (status == HttpConstants.HTTP_OK); - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt new file mode 100644 index 00000000..64217546 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -0,0 +1,232 @@ +/* ownCloud Android Library is available under MIT license + * + * Copyright (C) 2019 ownCloud GmbH. + * + * 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.resources.shares + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.nonwebdav.PutMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import okhttp3.FormBody +import java.net.URL +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale + +/** + * Updates parameters of an existing Share resource, known its remote ID. + * + * + * Allow updating several parameters, triggering a request to the server per parameter. + * + * @author David A. Velasco + * @author David González Verdugo + */ + +class UpdateRemoteShareOperation +/** + * Constructor. No update is initialized by default, need to be applied with setters below. + * + * @param remoteId Identifier of the share to update. + */ + ( + /** + * Identifier of the share to update + */ + private val mRemoteId: Long +) : RemoteOperation() { + + /** + * Password to set for the public link + */ + private var mPassword: String? = null + + /** + * Expiration date to set for the public link + */ + private var mExpirationDateInMillis: Long = 0 + + /** + * Access permissions for the file bound to the share + */ + private var mPermissions: Int = 0 + + /** + * Upload permissions for the public link (only folders) + */ + private var mPublicUpload: Boolean? = null + private var mName: String? = null + + init { + mPassword = null // no update + mExpirationDateInMillis = 0 // no update + mPublicUpload = null + mPermissions = RemoteShare.DEFAULT_PERMISSION + } + + /** + * Set name to update in Share resource. Ignored by servers previous to version 10.0.0 + * + * @param name Name to set to the target share. + * Empty string clears the current name. + * Null results in no update applied to the name. + */ + fun setName(name: String) { + this.mName = name + } + + /** + * Set password to update in Share resource. + * + * @param password Password to set to the target share. + * Empty string clears the current password. + * Null results in no update applied to the password. + */ + fun setPassword(password: String) { + mPassword = password + } + + /** + * Set expiration date to update in Share resource. + * + * @param expirationDateInMillis Expiration date to set to the target share. + * A negative value clears the current expiration date. + * Zero value (start-of-epoch) results in no update done on + * the expiration date. + */ + fun setExpirationDate(expirationDateInMillis: Long) { + mExpirationDateInMillis = expirationDateInMillis + } + + /** + * Set permissions to update in Share resource. + * + * @param permissions Permissions to set to the target share. + * Values <= 0 result in no update applied to the permissions. + */ + fun setPermissions(permissions: Int) { + mPermissions = permissions + } + + /** + * Enable upload permissions to update in Share resource. + * + * @param publicUpload Upload permission to set to the target share. + * Null results in no update applied to the upload permission. + */ + fun setPublicUpload(publicUpload: Boolean?) { + mPublicUpload = publicUpload + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult + + try { + val formBodyBuilder = FormBody.Builder() + + // Parameters to update + if (mName != null) { + formBodyBuilder.add(PARAM_NAME, mName!!) + } + + if (mExpirationDateInMillis < 0) { + // clear expiration date + formBodyBuilder.add(PARAM_EXPIRATION_DATE, "") + + } else if (mExpirationDateInMillis > 0) { + // set expiration date + val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.GERMAN) + val expirationDate = Calendar.getInstance() + expirationDate.timeInMillis = mExpirationDateInMillis + val formattedExpirationDate = dateFormat.format(expirationDate.time) + formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate) + } // else, ignore - no update + + if (mPublicUpload != null) { + formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, java.lang.Boolean.toString(mPublicUpload!!)) + } + + // IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter, + // otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated + if (mPermissions > 0) { + // set permissions + formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)) + } + + val requestUri = client.baseUri + val uriBuilder = requestUri.buildUpon() + uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) + uriBuilder.appendEncodedPath(java.lang.Long.toString(mRemoteId)) + + val putMethod = PutMethod(URL(uriBuilder.build().toString())) + + putMethod.setRequestBody(formBodyBuilder.build()) + + putMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) + putMethod.addRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE) + + val status = client.executeHttpMethod(putMethod) + + if (isSuccess(status)) { + // Parse xml response + val parser = ShareToRemoteOperationResultParser( + ShareXMLParser() + ) + parser.setOwnCloudVersion(client.ownCloudVersion) + parser.setServerBaseUri(client.baseUri) + result = parser.parse(putMethod.responseBodyAsString) + + } else { + result = RemoteOperationResult(putMethod) + } + + } catch (e: Exception) { + result = RemoteOperationResult(e) + Log_OC.e(TAG, "Exception while Creating New Share", e) + } + + return result + } + + private fun isSuccess(status: Int): Boolean { + return status == HttpConstants.HTTP_OK + } + + companion object { + + private val TAG = GetRemoteShareOperation::class.java.simpleName + + private val PARAM_NAME = "name" + private val PARAM_PASSWORD = "password" + private val PARAM_EXPIRATION_DATE = "expireDate" + private val PARAM_PERMISSIONS = "permissions" + private val PARAM_PUBLIC_UPLOAD = "publicUpload" + private val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" + private val ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded" + private val ENTITY_CHARSET = "UTF-8" + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.java deleted file mode 100644 index c8fd34a4..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.java +++ /dev/null @@ -1,213 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2016 ownCloud GmbH. - * Copyright (C) 2012 Bartek Przybylski - * - * 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.resources.status; - -import android.os.Parcel; -import android.os.Parcelable; - -public class OwnCloudVersion implements Comparable, Parcelable { - - public static final int MINIMUN_VERSION_FOR_CHUNKED_UPLOADS = 0x04050000; // 4.5 - - public static final int MINIMUM_VERSION_FOR_SHARING_API = 0x05001B00; // 5.0.27 - - public static final int MINIMUM_VERSION_WITH_FORBIDDEN_CHARS = 0x08010000; // 8.1 - - public static final int MINIMUM_SERVER_VERSION_FOR_REMOTE_THUMBNAILS = 0x07080000; // 7.8.0 - - public static final int MINIMUM_VERSION_FOR_SEARCHING_USERS = 0x08020000; //8.2 - - public static final int VERSION_8 = 0x08000000; // 8.0 - - public static final int MINIMUM_VERSION_CAPABILITIES_API = 0x08010000; // 8.1 - - private static final int MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED = 0x09010000; // 9.1 - - private static final int MINIMUM_VERSION_WITH_SESSION_MONITORING = 0x09010000; // 9.1 - - private static final int MINIMUM_VERSION_WITH_SESSION_MONITORING_WORKING_IN_PREEMPTIVE_MODE = 0x09010301; - // 9.1.3.1, final 9.1.3: https://github.com/owncloud/core/commit/f9a867b70c217463289a741d4d26079eb2a80dfd - - private static final int MINIMUM_VERSION_WITH_MULTIPLE_PUBLIC_SHARING = 0xA000000; // 10.0.0 - - private static final int MINIMUM_VERSION_WITH_WRITE_ONLY_PUBLIC_SHARING = 0xA000100; // 10.0.1 - - private static final String INVALID_ZERO_VERSION = "0.0.0"; - - private static final int MAX_DOTS = 3; - - // format is in version - // 0xAABBCCDD - // for version AA.BB.CC.DD - // ie version 2.0.3 will be stored as 0x02000300 - private int mVersion; - private boolean mIsValid; - - public OwnCloudVersion(String version) { - mVersion = 0; - mIsValid = false; - int countDots = version.length() - version.replace(".", "").length(); - - // Complete the version. Version must have 3 dots - for (int i = countDots; i < MAX_DOTS; i++) { - version = version + ".0"; - } - - parseVersion(version); - - } - - public String toString() { - // gets the first digit of version, shifting hexadecimal version to right 'til max position - String versionToString = String.valueOf((mVersion >> (8 * MAX_DOTS)) % 256); - for (int i = MAX_DOTS - 1; i >= 0; i--) { - // gets another digit of version, shifting hexadecimal version to right 8*i bits and... - // ...discarding left part with mod 256 - versionToString = versionToString + "." + String.valueOf((mVersion >> (8 * i)) % 256); - } - if (!mIsValid) { - versionToString += " INVALID"; - } - return versionToString; - } - - public String getVersion() { - if (mIsValid) { - return toString(); - } else { - return INVALID_ZERO_VERSION; - } - } - - public boolean isVersionValid() { - return mIsValid; - } - - @Override - public int compareTo(OwnCloudVersion another) { - return another.mVersion == mVersion ? 0 - : another.mVersion < mVersion ? 1 : -1; - } - - private void parseVersion(String version) { - try { - mVersion = getParsedVersion(version); - mIsValid = true; - - } catch (Exception e) { - mIsValid = false; - // if invalid, the instance will respond as if server is 8.1, minimum with capabilities API, - // and "dead" : https://github.com/owncloud/core/wiki/Maintenance-and-Release-Schedule - mVersion = MINIMUM_VERSION_CAPABILITIES_API; - } - } - - private int getParsedVersion(String version) throws NumberFormatException { - int versionValue = 0; - - // get only numeric part - version = version.replaceAll("[^\\d.]", ""); - - String[] nums = version.split("\\."); - for (int i = 0; i < nums.length && i <= MAX_DOTS; i++) { - versionValue += Integer.parseInt(nums[i]); - if (i < nums.length - 1) { - versionValue = versionValue << 8; - } - } - - return versionValue; - } - - public boolean isChunkedUploadSupported() { - return (mVersion >= MINIMUN_VERSION_FOR_CHUNKED_UPLOADS); - } - - public boolean isSharedSupported() { - return (mVersion >= MINIMUM_VERSION_FOR_SHARING_API); - } - - public boolean isVersionWithForbiddenCharacters() { - return (mVersion >= MINIMUM_VERSION_WITH_FORBIDDEN_CHARS); - } - - public boolean supportsRemoteThumbnails() { - return (mVersion >= MINIMUM_SERVER_VERSION_FOR_REMOTE_THUMBNAILS); - } - - public boolean isAfter8Version() { - return (mVersion >= VERSION_8); - } - - public boolean isSearchUsersSupported() { - return (mVersion >= MINIMUM_VERSION_FOR_SEARCHING_USERS); - } - - public boolean isVersionWithCapabilitiesAPI() { - return (mVersion >= MINIMUM_VERSION_CAPABILITIES_API); - } - - public boolean isNotReshareableFederatedSupported() { - return (mVersion >= MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED); - } - - public boolean isSessionMonitoringSupported() { - return (mVersion >= MINIMUM_VERSION_WITH_SESSION_MONITORING); - } - - /** - * From OC 9.1 session tracking is a feature, but to get it working in the OC app we need the preemptive - * mode of basic authentication is disabled. This changes in OC 9.1.3, where preemptive mode is compatible - * with session tracking again. - * - * @return True for every version before 9.1 and from 9.1.3, false otherwise - */ - public boolean isPreemptiveAuthenticationPreferred() { - return ( - (mVersion < MINIMUM_VERSION_WITH_SESSION_MONITORING) || - (mVersion >= MINIMUM_VERSION_WITH_SESSION_MONITORING_WORKING_IN_PREEMPTIVE_MODE) - ); - } - - public boolean isMultiplePublicSharingSupported() { - return (mVersion >= MINIMUM_VERSION_WITH_MULTIPLE_PUBLIC_SHARING); - } - - public boolean isPublicSharingWriteOnlySupported() { - return (mVersion >= MINIMUM_VERSION_WITH_WRITE_ONLY_PUBLIC_SHARING); - } - - @Override - public int describeContents() { - return super.hashCode(); - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(mVersion); - dest.writeInt(mIsValid ? 1 : 0); - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt new file mode 100644 index 00000000..80b8814e --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt @@ -0,0 +1,202 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2016 ownCloud GmbH. + * Copyright (C) 2012 Bartek Przybylski + * + * 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.resources.status + +import android.os.Parcel +import android.os.Parcelable + +class OwnCloudVersion(version: String) : Comparable, Parcelable { + + // format is in version + // 0xAABBCCDD + // for version AA.BB.CC.DD + // ie version 2.0.3 will be stored as 0x02000300 + private var mVersion: Int = 0 + var isVersionValid: Boolean = false + private set + + val version: String + get() = if (isVersionValid) { + toString() + } else { + INVALID_ZERO_VERSION + } + + val isChunkedUploadSupported: Boolean + get() = mVersion >= MINIMUN_VERSION_FOR_CHUNKED_UPLOADS + + val isSharedSupported: Boolean + get() = mVersion >= MINIMUM_VERSION_FOR_SHARING_API + + val isVersionWithForbiddenCharacters: Boolean + get() = mVersion >= MINIMUM_VERSION_WITH_FORBIDDEN_CHARS + + val isAfter8Version: Boolean + get() = mVersion >= VERSION_8 + + val isSearchUsersSupported: Boolean + get() = mVersion >= MINIMUM_VERSION_FOR_SEARCHING_USERS + + val isVersionWithCapabilitiesAPI: Boolean + get() = mVersion >= MINIMUM_VERSION_CAPABILITIES_API + + val isNotReshareableFederatedSupported: Boolean + get() = mVersion >= MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED + + val isSessionMonitoringSupported: Boolean + get() = mVersion >= MINIMUM_VERSION_WITH_SESSION_MONITORING + + /** + * From OC 9.1 session tracking is a feature, but to get it working in the OC app we need the preemptive + * mode of basic authentication is disabled. This changes in OC 9.1.3, where preemptive mode is compatible + * with session tracking again. + * + * @return True for every version before 9.1 and from 9.1.3, false otherwise + */ + val isPreemptiveAuthenticationPreferred: Boolean + get() = mVersion < MINIMUM_VERSION_WITH_SESSION_MONITORING || mVersion >= MINIMUM_VERSION_WITH_SESSION_MONITORING_WORKING_IN_PREEMPTIVE_MODE + + val isMultiplePublicSharingSupported: Boolean + get() = mVersion >= MINIMUM_VERSION_WITH_MULTIPLE_PUBLIC_SHARING + + val isPublicSharingWriteOnlySupported: Boolean + get() = mVersion >= MINIMUM_VERSION_WITH_WRITE_ONLY_PUBLIC_SHARING + + init { + var version = version + mVersion = 0 + isVersionValid = false + val countDots = version.length - version.replace(".", "").length + + // Complete the version. Version must have 3 dots + for (i in countDots until MAX_DOTS) { + version = "$version.0" + } + + parseVersion(version) + + } + + override fun toString(): String { + // gets the first digit of version, shifting hexadecimal version to right 'til max position + var versionToString = ((mVersion shr 8 * MAX_DOTS) % 256).toString() + for (i in MAX_DOTS - 1 downTo 0) { + // gets another digit of version, shifting hexadecimal version to right 8*i bits and... + // ...discarding left part with mod 256 + versionToString = versionToString + "." + ((mVersion shr 8 * i) % 256).toString() + } + if (!isVersionValid) { + versionToString += " INVALID" + } + return versionToString + } + + override fun compareTo(another: OwnCloudVersion): Int { + return if (another.mVersion == mVersion) + 0 + else if (another.mVersion < mVersion) 1 else -1 + } + + private fun parseVersion(version: String) { + try { + mVersion = getParsedVersion(version) + isVersionValid = true + + } catch (e: Exception) { + isVersionValid = false + // if invalid, the instance will respond as if server is 8.1, minimum with capabilities API, + // and "dead" : https://github.com/owncloud/core/wiki/Maintenance-and-Release-Schedule + mVersion = MINIMUM_VERSION_CAPABILITIES_API + } + + } + + @Throws(NumberFormatException::class) + private fun getParsedVersion(version: String): Int { + var version = version + var versionValue = 0 + + // get only numeric part + version = version.replace("[^\\d.]".toRegex(), "") + + val nums = version.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + var i = 0 + while (i < nums.size && i <= MAX_DOTS) { + versionValue += Integer.parseInt(nums[i]) + if (i < nums.size - 1) { + versionValue = versionValue shl 8 + } + i++ + } + + return versionValue + } + + fun supportsRemoteThumbnails(): Boolean { + return mVersion >= MINIMUM_SERVER_VERSION_FOR_REMOTE_THUMBNAILS + } + + override fun describeContents(): Int { + return super.hashCode() + } + + override fun writeToParcel(dest: Parcel, flags: Int) { + dest.writeInt(mVersion) + dest.writeInt(if (isVersionValid) 1 else 0) + } + + companion object { + + val MINIMUN_VERSION_FOR_CHUNKED_UPLOADS = 0x04050000 // 4.5 + + val MINIMUM_VERSION_FOR_SHARING_API = 0x05001B00 // 5.0.27 + + val MINIMUM_VERSION_WITH_FORBIDDEN_CHARS = 0x08010000 // 8.1 + + val MINIMUM_SERVER_VERSION_FOR_REMOTE_THUMBNAILS = 0x07080000 // 7.8.0 + + val MINIMUM_VERSION_FOR_SEARCHING_USERS = 0x08020000 //8.2 + + val VERSION_8 = 0x08000000 // 8.0 + + val MINIMUM_VERSION_CAPABILITIES_API = 0x08010000 // 8.1 + + private val MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED = 0x09010000 // 9.1 + + private val MINIMUM_VERSION_WITH_SESSION_MONITORING = 0x09010000 // 9.1 + + private val MINIMUM_VERSION_WITH_SESSION_MONITORING_WORKING_IN_PREEMPTIVE_MODE = 0x09010301 + // 9.1.3.1, final 9.1.3: https://github.com/owncloud/core/commit/f9a867b70c217463289a741d4d26079eb2a80dfd + + private val MINIMUM_VERSION_WITH_MULTIPLE_PUBLIC_SHARING = 0xA000000 // 10.0.0 + + private val MINIMUM_VERSION_WITH_WRITE_ONLY_PUBLIC_SHARING = 0xA000100 // 10.0.1 + + private val INVALID_ZERO_VERSION = "0.0.0" + + private val MAX_DOTS = 3 + } +}