1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

2nd code review changes

Apply pending change
This commit is contained in:
davigonz 2019-03-05 18:28:40 +01:00
parent af6b18e042
commit c2d8e68ecc
9 changed files with 219 additions and 273 deletions

View File

@ -33,6 +33,7 @@ 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.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_EXPIRATION_DATE_IN_MILLIS
import okhttp3.FormBody import okhttp3.FormBody
import java.net.URL import java.net.URL
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -75,7 +76,7 @@ class CreateRemoteShareOperation
var password: String?, // Password to set for the public link var password: String?, // Password to set for the public link
var permissions: Int // Access permissions for the file bound to the share var permissions: Int // Access permissions for the file bound to the share
) : RemoteOperation<ShareParserResult>() { ) : RemoteOperation<ShareParserResult>() {
var getShareDetails: Boolean = false var getShareDetails: Boolean = false // To retrieve more info about the just created share
/** /**
* Name to set for the public link * Name to set for the public link
@ -85,7 +86,7 @@ class CreateRemoteShareOperation
/** /**
* Expiration date to set for the public link * Expiration date to set for the public link
*/ */
var expirationDateInMillis: Long = INITIAL_EXPIRATION_DATE_IN_MILLIS var expirationDateInMillis: Long = INIT_EXPIRATION_DATE_IN_MILLIS
init { init {
getShareDetails = false // defaults to false for backwards compatibility getShareDetails = false // defaults to false for backwards compatibility
@ -104,7 +105,7 @@ class CreateRemoteShareOperation
formBodyBuilder.add(PARAM_NAME, name) formBodyBuilder.add(PARAM_NAME, name)
} }
if (expirationDateInMillis > INITIAL_EXPIRATION_DATE_IN_MILLIS) { if (expirationDateInMillis > INIT_EXPIRATION_DATE_IN_MILLIS) {
val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault())
val expirationDate = Calendar.getInstance() val expirationDate = Calendar.getInstance()
expirationDate.timeInMillis = expirationDateInMillis expirationDate.timeInMillis = expirationDateInMillis
@ -116,7 +117,7 @@ class CreateRemoteShareOperation
formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString()) formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString())
} }
if (!password.isNullOrEmpty()) { if (!password.isNullOrEmpty()) {
formBodyBuilder.add(PARAM_PASSWORD, password!!) formBodyBuilder.add(PARAM_PASSWORD, password)
} }
if (RemoteShare.DEFAULT_PERMISSION != permissions) { if (RemoteShare.DEFAULT_PERMISSION != permissions) {
formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(permissions)) formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(permissions))
@ -168,24 +169,19 @@ class CreateRemoteShareOperation
return result return result
} }
private fun isSuccess(status: Int): Boolean { private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
return status == HttpConstants.HTTP_OK
}
companion object { companion object {
private val TAG = CreateRemoteShareOperation::class.java.simpleName private val TAG = CreateRemoteShareOperation::class.java.simpleName
private val PARAM_NAME = "name" private const val PARAM_NAME = "name"
private val PARAM_PASSWORD = "password" private const val PARAM_PASSWORD = "password"
private val PARAM_EXPIRATION_DATE = "expireDate" private const val PARAM_EXPIRATION_DATE = "expireDate"
private val PARAM_PUBLIC_UPLOAD = "publicUpload" private const val PARAM_PUBLIC_UPLOAD = "publicUpload"
private val PARAM_PATH = "path" private const val PARAM_PATH = "path"
private val PARAM_SHARE_TYPE = "shareType" private const val PARAM_SHARE_TYPE = "shareType"
private val PARAM_SHARE_WITH = "shareWith" private const val PARAM_SHARE_WITH = "shareWith"
private val PARAM_PERMISSIONS = "permissions" private const val PARAM_PERMISSIONS = "permissions"
private val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"
const val INITIAL_EXPIRATION_DATE_IN_MILLIS : Long = 0
} }
} }

View File

@ -1,124 +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.GetMethod;
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;
/**
* Provide a list shares for a specific file.
* The input is the full path of the desired file.
* The output is a list of everyone who has the file shared with them.
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
public class GetRemoteSharesForFileOperation extends RemoteOperation<ShareParserResult> {
private static final String TAG = GetRemoteSharesForFileOperation.class.getSimpleName();
private static final String PARAM_PATH = "path";
private static final String PARAM_RESHARES = "reshares";
private static final String PARAM_SUBFILES = "subfiles";
private String mRemoteFilePath;
private boolean mReshares;
private boolean mSubfiles;
/**
* Constructor
*
* @param remoteFilePath Path to file or folder
* @param reshares If set to false (default), only shares owned by the current user are
* returned.
* If set to true, shares owned by any user from the given file are returned.
* @param subfiles If set to false (default), lists only the folder being shared
* If set to true, all shared files within the folder are returned.
*/
public GetRemoteSharesForFileOperation(String remoteFilePath, boolean reshares,
boolean subfiles) {
mRemoteFilePath = remoteFilePath;
mReshares = reshares;
mSubfiles = subfiles;
}
@Override
protected RemoteOperationResult<ShareParserResult> run(OwnCloudClient client) {
RemoteOperationResult<ShareParserResult> result;
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
uriBuilder.appendQueryParameter(PARAM_PATH, mRemoteFilePath);
uriBuilder.appendQueryParameter(PARAM_RESHARES, String.valueOf(mReshares));
uriBuilder.appendQueryParameter(PARAM_SUBFILES, String.valueOf(mSubfiles));
GetMethod getMethod = new GetMethod(new URL(uriBuilder.build().toString()));
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
int status = client.executeHttpMethod(getMethod);
if (isSuccess(status)) {
// Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser()
);
parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(getMethod.getResponseBodyAsString());
if (result.isSuccess()) {
Log_OC.d(TAG, "Got " + result.getData().getShares().size() + " shares");
}
} else {
result = new RemoteOperationResult<>(getMethod);
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while getting shares", e);
}
return result;
}
private boolean isSuccess(int status) {
return (status == HttpConstants.HTTP_OK);
}
}

View File

@ -0,0 +1,115 @@
/* 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.GetMethod
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
/**
* Provide a list shares for a specific file.
* The input is the full path of the desired file.
* The output is a list of everyone who has the file shared with them.
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
/**
* Constructor
*
* @param remoteFilePath Path to file or folder
* @param reshares If set to false (default), only shares owned by the current user are
* returned.
* If set to true, shares owned by any user from the given file are returned.
* @param subfiles If set to false (default), lists only the folder being shared
* If set to true, all shared files within the folder are returned.
*/
class GetRemoteSharesForFileOperation(
private val remoteFilePath: String,
private val reshares: Boolean,
private val subfiles: Boolean
) : RemoteOperation<ShareParserResult>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> {
var result: RemoteOperationResult<ShareParserResult>
try {
val requestUri = client.baseUri
val uriBuilder = requestUri.buildUpon()
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH)
uriBuilder.appendQueryParameter(PARAM_PATH, remoteFilePath)
uriBuilder.appendQueryParameter(PARAM_RESHARES, reshares.toString())
uriBuilder.appendQueryParameter(PARAM_SUBFILES, subfiles.toString())
val getMethod = GetMethod(URL(uriBuilder.build().toString()))
getMethod.addRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE)
val status = client.executeHttpMethod(getMethod)
if (isSuccess(status)) {
// Parse xml response and obtain the list of shares
val parser = ShareToRemoteOperationResultParser(
ShareXMLParser()
)
parser.ownCloudVersion = client.ownCloudVersion
parser.serverBaseUri = client.baseUri
result = parser.parse(getMethod.responseBodyAsString)
if (result.isSuccess) {
Log_OC.d(TAG, "Got " + result.data.shares.size + " shares")
}
} else {
result = RemoteOperationResult(getMethod)
}
} catch (e: Exception) {
result = RemoteOperationResult(e)
Log_OC.e(TAG, "Exception while getting shares", e)
}
return result
}
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
companion object {
private val TAG = GetRemoteSharesForFileOperation::class.java.simpleName
private const val PARAM_PATH = "path"
private const val PARAM_RESHARES = "reshares"
private const val PARAM_SUBFILES = "subfiles"
}
}

View File

@ -48,13 +48,12 @@ class RemoteShare : Parcelable, Serializable {
var fileSource: Long = 0 var fileSource: Long = 0
var itemSource: Long = 0 var itemSource: Long = 0
var shareType: ShareType? = null var shareType: ShareType? = null
var permissions: Int = 0 var permissions: Int = INIT_PERMISSION
var sharedDate: Long = 0 var sharedDate: Long = INIT_SHARED_DATE
var expirationDate: Long = 0 var expirationDate: Long = INIT_EXPIRATION_DATE_IN_MILLIS
var isFolder: Boolean = false var isFolder: Boolean = false
var userId: Long = 0 var userId: Long = 0
var remoteId: Long = 0 var remoteId: Long = 0
private set
val isPasswordProtected: Boolean val isPasswordProtected: Boolean
get() = ShareType.PUBLIC_LINK == shareType && shareWith.isNotEmpty() get() = ShareType.PUBLIC_LINK == shareType && shareWith.isNotEmpty()
@ -65,7 +64,7 @@ class RemoteShare : Parcelable, Serializable {
constructor(path: String?) { constructor(path: String?) {
resetData() resetData()
if (path == null || path.isEmpty() || !path.startsWith(FileUtils.PATH_SEPARATOR)) { if (path.isNullOrEmpty() || !path.startsWith(FileUtils.PATH_SEPARATOR)) {
Log_OC.e(TAG, "Trying to create a RemoteShare with a non valid path") 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!!) throw IllegalArgumentException("Trying to create a RemoteShare with a non valid path: " + path!!)
} }
@ -87,18 +86,14 @@ class RemoteShare : Parcelable, Serializable {
itemSource = 0 itemSource = 0
shareType = ShareType.NO_SHARED shareType = ShareType.NO_SHARED
permissions = DEFAULT_PERMISSION permissions = DEFAULT_PERMISSION
sharedDate = 0 sharedDate = INIT_SHARED_DATE
expirationDate = 0 expirationDate = INIT_EXPIRATION_DATE_IN_MILLIS
sharedWithAdditionalInfo = "" sharedWithAdditionalInfo = ""
isFolder = false isFolder = false
userId = -1 userId = -1
remoteId = -1 remoteId = -1
} }
fun setIdRemoteShared(remoteId: Long) {
this.remoteId = remoteId
}
/** /**
* Reconstruct from parcel * Reconstruct from parcel
* *
@ -131,9 +126,7 @@ class RemoteShare : Parcelable, Serializable {
remoteId = source.readLong() remoteId = source.readLong()
} }
override fun describeContents(): Int { override fun describeContents(): Int = this.hashCode()
return this.hashCode()
}
override fun writeToParcel(dest: Parcel, flags: Int) { override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(shareWith) dest.writeString(shareWith)
@ -145,7 +138,7 @@ class RemoteShare : Parcelable, Serializable {
dest.writeString(shareLink) dest.writeString(shareLink)
dest.writeLong(fileSource) dest.writeLong(fileSource)
dest.writeLong(itemSource) dest.writeLong(itemSource)
dest.writeString(if (shareType == null) "" else shareType!!.name) dest.writeString(shareType?.name ?: "")
dest.writeInt(permissions) dest.writeInt(permissions)
dest.writeLong(sharedDate) dest.writeLong(sharedDate)
dest.writeLong(expirationDate) dest.writeLong(expirationDate)
@ -163,29 +156,34 @@ class RemoteShare : Parcelable, Serializable {
private val TAG = RemoteShare::class.java.simpleName private val TAG = RemoteShare::class.java.simpleName
val DEFAULT_PERMISSION = -1 const val INIT_PERMISSION = 0
val READ_PERMISSION_FLAG = 1 const val DEFAULT_PERMISSION = -1
val UPDATE_PERMISSION_FLAG = 2 const val READ_PERMISSION_FLAG = 1
val CREATE_PERMISSION_FLAG = 4 const val UPDATE_PERMISSION_FLAG = 2
val DELETE_PERMISSION_FLAG = 8 const val CREATE_PERMISSION_FLAG = 4
val SHARE_PERMISSION_FLAG = 16 const val DELETE_PERMISSION_FLAG = 8
val MAXIMUM_PERMISSIONS_FOR_FILE = READ_PERMISSION_FLAG + const val SHARE_PERMISSION_FLAG = 16
const val MAXIMUM_PERMISSIONS_FOR_FILE = READ_PERMISSION_FLAG +
UPDATE_PERMISSION_FLAG + UPDATE_PERMISSION_FLAG +
SHARE_PERMISSION_FLAG SHARE_PERMISSION_FLAG
val MAXIMUM_PERMISSIONS_FOR_FOLDER = MAXIMUM_PERMISSIONS_FOR_FILE + const val MAXIMUM_PERMISSIONS_FOR_FOLDER = MAXIMUM_PERMISSIONS_FOR_FILE +
CREATE_PERMISSION_FLAG + CREATE_PERMISSION_FLAG +
DELETE_PERMISSION_FLAG DELETE_PERMISSION_FLAG
val FEDERATED_PERMISSIONS_FOR_FILE_UP_TO_OC9 = READ_PERMISSION_FLAG + UPDATE_PERMISSION_FLAG const val FEDERATED_PERMISSIONS_FOR_FILE_UP_TO_OC9 = READ_PERMISSION_FLAG + UPDATE_PERMISSION_FLAG
val FEDERATED_PERMISSIONS_FOR_FILE_AFTER_OC9 = READ_PERMISSION_FLAG + const val FEDERATED_PERMISSIONS_FOR_FILE_AFTER_OC9 = READ_PERMISSION_FLAG +
UPDATE_PERMISSION_FLAG + UPDATE_PERMISSION_FLAG +
SHARE_PERMISSION_FLAG SHARE_PERMISSION_FLAG
val FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 = READ_PERMISSION_FLAG + const val FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 = READ_PERMISSION_FLAG +
UPDATE_PERMISSION_FLAG + UPDATE_PERMISSION_FLAG +
CREATE_PERMISSION_FLAG + CREATE_PERMISSION_FLAG +
DELETE_PERMISSION_FLAG DELETE_PERMISSION_FLAG
val FEDERATED_PERMISSIONS_FOR_FOLDER_AFTER_OC9 = const val FEDERATED_PERMISSIONS_FOR_FOLDER_AFTER_OC9 =
FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 + SHARE_PERMISSION_FLAG FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9 + SHARE_PERMISSION_FLAG
const val INIT_EXPIRATION_DATE_IN_MILLIS : Long = 0
const val INIT_SHARED_DATE : Long = 0
/** /**
* Parcelable Methods * Parcelable Methods
*/ */

View File

@ -43,13 +43,12 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class RemoveRemoteShareOperation
/** /**
* Constructor * Constructor
* *
* @param remoteShareId Share ID * @param remoteShareId Share ID
*/ */
(private val mRemoteShareId: Long) : RemoteOperation<ShareParserResult>() { class RemoveRemoteShareOperation(private val remoteShareId: Long) : RemoteOperation<ShareParserResult>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> {
var result: RemoteOperationResult<ShareParserResult> var result: RemoteOperationResult<ShareParserResult>
@ -58,7 +57,7 @@ class RemoveRemoteShareOperation
val requestUri = client.baseUri val requestUri = client.baseUri
val uriBuilder = requestUri.buildUpon() val uriBuilder = requestUri.buildUpon()
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH)
uriBuilder.appendEncodedPath(mRemoteShareId.toString()) uriBuilder.appendEncodedPath(remoteShareId.toString())
val deleteMethod = DeleteMethod( val deleteMethod = DeleteMethod(
URL(uriBuilder.build().toString()) URL(uriBuilder.build().toString())
@ -76,7 +75,7 @@ class RemoveRemoteShareOperation
) )
result = parser.parse(deleteMethod.responseBodyAsString) result = parser.parse(deleteMethod.responseBodyAsString)
Log_OC.d(TAG, "Unshare " + mRemoteShareId + ": " + result.logMessage) Log_OC.d(TAG, "Unshare " + remoteShareId + ": " + result.logMessage)
} else { } else {
result = RemoteOperationResult(deleteMethod) result = RemoteOperationResult(deleteMethod)

View File

@ -32,7 +32,7 @@ package com.owncloud.android.lib.resources.shares
class SharePermissionsBuilder { class SharePermissionsBuilder {
/** Set of permissions */ /** Set of permissions */
private var mPermissions = RemoteShare.READ_PERMISSION_FLAG // READ is minimum permission private var permissions = RemoteShare.READ_PERMISSION_FLAG // READ is minimum permission
/** /**
* Sets or clears permission to reshare a file or folder. * Sets or clears permission to reshare a file or folder.
@ -87,10 +87,10 @@ class SharePermissionsBuilder {
private fun updatePermission(permissionsFlag: Int, enable: Boolean) { private fun updatePermission(permissionsFlag: Int, enable: Boolean) {
if (enable) { if (enable) {
// add permission // add permission
mPermissions = mPermissions or permissionsFlag permissions = permissions or permissionsFlag
} else { } else {
// delete permission // delete permission
mPermissions = mPermissions and permissionsFlag.inv() permissions = permissions and permissionsFlag.inv()
} }
} }
@ -99,7 +99,5 @@ class SharePermissionsBuilder {
* *
* @return An int value representing the accumulated set of permissions. * @return An int value representing the accumulated set of permissions.
*/ */
fun build(): Int { fun build(): Int = permissions
return mPermissions
}
} }

View File

@ -47,7 +47,7 @@ class ShareToRemoteOperationResultParser(private var shareXmlParser: ShareXMLPar
} }
var result: RemoteOperationResult<ShareParserResult> var result: RemoteOperationResult<ShareParserResult>
val resultData = ArrayList<RemoteShare>() var resultData: List<RemoteShare>?
try { try {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
@ -61,25 +61,25 @@ class ShareToRemoteOperationResultParser(private var shareXmlParser: ShareXMLPar
if (shareXmlParser?.isSuccess!!) { if (shareXmlParser?.isSuccess!!) {
if (!shares.isNullOrEmpty() || !oneOrMoreSharesRequired) { if (!shares.isNullOrEmpty() || !oneOrMoreSharesRequired) {
result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
if (shares != null) {
for (share in shares) { resultData = shares?.map { share ->
resultData.add(share) if (share.shareType != ShareType.PUBLIC_LINK ||
// build the share link if not in the response share.shareLink.isNotEmpty() ||
// (needed for OC servers < 9.0.0, see ShareXMLParser.java#line256) share.token.isEmpty()) {
if (share.shareType == ShareType.PUBLIC_LINK return@map share
&& share.shareLink.isEmpty()
&& share.token.isNotEmpty()
) {
if (serverBaseUri != null) {
val sharingLinkPath = ShareUtils.getSharingLinkPath(ownCloudVersion)
share.shareLink = serverBaseUri.toString() + sharingLinkPath + share.token
} else {
Log_OC.e(TAG, "Couldn't build link for public share :(")
}
}
} }
if (serverBaseUri != null) {
val sharingLinkPath = ShareUtils.getSharingLinkPath(ownCloudVersion)
share.shareLink = serverBaseUri.toString() + sharingLinkPath + share.token
} else {
Log_OC.e(TAG, "Couldn't build link for public share :(")
}
share
} }
result.setData(ShareParserResult(resultData, ""))
result.setData(ShareParserResult(ArrayList(resultData), ""))
} else { } else {
result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE)

View File

@ -179,7 +179,7 @@ class ShareXMLParser {
} else if (name.equals(NODE_ID, ignoreCase = true)) {// Parse Create XML Response } else if (name.equals(NODE_ID, ignoreCase = true)) {// Parse Create XML Response
share = RemoteShare() share = RemoteShare()
val value = readNode(parser, NODE_ID) val value = readNode(parser, NODE_ID)
share.setIdRemoteShared(Integer.parseInt(value).toLong()) share.remoteId = Integer.parseInt(value).toLong()
} else if (name.equals(NODE_URL, ignoreCase = true)) { } else if (name.equals(NODE_URL, ignoreCase = true)) {
// NOTE: this field is received in all the public shares from OC 9.0.0 // NOTE: this field is received in all the public shares from OC 9.0.0
@ -235,7 +235,7 @@ class ShareXMLParser {
readElement(parser, shares) readElement(parser, shares)
} else if (name.equals(NODE_ID, ignoreCase = true)) { } else if (name.equals(NODE_ID, ignoreCase = true)) {
share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID)).toLong()) share.remoteId = Integer.parseInt(readNode(parser, NODE_ID)).toLong()
} else if (name.equals(NODE_ITEM_TYPE, ignoreCase = true)) { } else if (name.equals(NODE_ITEM_TYPE, ignoreCase = true)) {
share.isFolder = readNode(parser, NODE_ITEM_TYPE).equals(TYPE_FOLDER, ignoreCase = true) share.isFolder = readNode(parser, NODE_ITEM_TYPE).equals(TYPE_FOLDER, ignoreCase = true)

View File

@ -31,6 +31,7 @@ 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.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_PERMISSION
import okhttp3.FormBody import okhttp3.FormBody
import java.net.URL import java.net.URL
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -53,84 +54,34 @@ class UpdateRemoteShareOperation
* *
* @param remoteId Identifier of the share to update. * @param remoteId Identifier of the share to update.
*/ */
( (private val remoteId: Long) : RemoteOperation<ShareParserResult>() {
/**
* Identifier of the share to update
*/
private val remoteId: Long
) : RemoteOperation<ShareParserResult>() {
/** /**
* Password to set for the public link * Password to update in Share resource.
*/
private var password: String? = null
/**
* Expiration date to set for the public link
*/
private var expirationDateInMillis: Long = 0
/**
* Access permissions for the file bound to the share
*/
private var permissions: Int = 0
/**
* Upload permissions for the public link (only folders)
*/
private var publicUpload: Boolean? = null
private var name: String? = null
init {
password = null // no update
expirationDateInMillis = 0 // no update
publicUpload = null
permissions = 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.name = name
}
/**
* Set password to update in Share resource.
* *
* @param password Password to set to the target share. * @param password Password to set to the target share.
* Empty string clears the current password. * Empty string clears the current password.
* Null results in no update applied to the password. * Null results in no update applied to the password.
*/ */
fun setPassword(password: String) { var password: String? = null
this.password = password
}
/** /**
* Set expiration date to update in Share resource. * Expiration date to update in Share resource.
* *
* @param expirationDateInMillis Expiration date to set to the target share. * @param expirationDateInMillis Expiration date to set to the target share.
* A negative value clears the current expiration date. * A negative value clears the current expiration date.
* Zero value (start-of-epoch) results in no update done on * Zero value (start-of-epoch) results in no update done on
* the expiration date. * the expiration date.
*/ */
fun setExpirationDate(expirationDateInMillis: Long) { var expirationDateInMillis: Long = INITIAL_EXPIRATION_DATE_IN_MILLIS
this.expirationDateInMillis = expirationDateInMillis
}
/** /**
* Set permissions to update in Share resource. * Permissions to update in Share resource.
* *
* @param permissions Permissions to set to the target share. * @param permissions Permissions to set to the target share.
* Values <= 0 result in no update applied to the permissions. * Values <= 0 result in no update applied to the permissions.
*/ */
fun setPermissions(permissions: Int) { var permissions: Int = INIT_PERMISSION
this.permissions = permissions
}
/** /**
* Enable upload permissions to update in Share resource. * Enable upload permissions to update in Share resource.
@ -138,8 +89,21 @@ class UpdateRemoteShareOperation
* @param publicUpload Upload permission to set to the target share. * @param publicUpload Upload permission to set to the target share.
* Null results in no update applied to the upload permission. * Null results in no update applied to the upload permission.
*/ */
fun setPublicUpload(publicUpload: Boolean?) { var publicUpload: Boolean? = null
this.publicUpload = publicUpload /**
* 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.
*/
var name: String? = null
init {
password = null // no update
expirationDateInMillis = INITIAL_EXPIRATION_DATE_IN_MILLIS // no update
publicUpload = null
permissions = RemoteShare.DEFAULT_PERMISSION
} }
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> {
@ -153,11 +117,11 @@ class UpdateRemoteShareOperation
formBodyBuilder.add(PARAM_NAME, name!!) formBodyBuilder.add(PARAM_NAME, name!!)
} }
if (expirationDateInMillis < 0) { if (expirationDateInMillis < INITIAL_EXPIRATION_DATE_IN_MILLIS) {
// clear expiration date // clear expiration date
formBodyBuilder.add(PARAM_EXPIRATION_DATE, "") formBodyBuilder.add(PARAM_EXPIRATION_DATE, "")
} else if (expirationDateInMillis > 0) { } else if (expirationDateInMillis > INITIAL_EXPIRATION_DATE_IN_MILLIS) {
// set expiration date // set expiration date
val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault())
val expirationDate = Calendar.getInstance() val expirationDate = Calendar.getInstance()
@ -172,7 +136,7 @@ class UpdateRemoteShareOperation
// IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter, // 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 // otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated
if (permissions > 0) { if (permissions > INIT_PERMISSION) {
// set permissions // set permissions
formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString()) formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString())
} }
@ -212,21 +176,21 @@ class UpdateRemoteShareOperation
return result return result
} }
private fun isSuccess(status: Int): Boolean { private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
return status == HttpConstants.HTTP_OK
}
companion object { companion object {
private val TAG = GetRemoteShareOperation::class.java.simpleName private val TAG = GetRemoteShareOperation::class.java.simpleName
private val PARAM_NAME = "name" private const val PARAM_NAME = "name"
private val PARAM_PASSWORD = "password" private const val PARAM_PASSWORD = "password"
private val PARAM_EXPIRATION_DATE = "expireDate" private const val PARAM_EXPIRATION_DATE = "expireDate"
private val PARAM_PERMISSIONS = "permissions" private const val PARAM_PERMISSIONS = "permissions"
private val PARAM_PUBLIC_UPLOAD = "publicUpload" private const val PARAM_PUBLIC_UPLOAD = "publicUpload"
private val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"
private val ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded" private const val ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"
private val ENTITY_CHARSET = "UTF-8" private const val ENTITY_CHARSET = "UTF-8"
private const val INITIAL_EXPIRATION_DATE_IN_MILLIS : Long = 0
} }
} }