Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@ class UpdateShareRemoteOperationIT : AbstractIT() {
assertTrue(RemoveFileRemoteOperation("/note/").execute(client).isSuccess)
}

@Test
fun updateMultipleParams() {
assertTrue(CreateFolderRemoteOperation("/label/", true).execute(client).isSuccess)

val createOperationResult =
CreateShareRemoteOperation(
"/label/",
ShareType.PUBLIC_LINK,
"",
true,
"",
OCShare.READ_PERMISSION_FLAG
).execute(client)

assertTrue(createOperationResult.isSuccess)

val share = createOperationResult.resultData[0]

val sut = UpdateShareRemoteOperation(share.remoteId)
val label = "test & test"
sut.setLabel(label)

val note = "test note"
sut.setNote(note)

val password = "test_pass_%_90"
sut.setPassword(password)

assertTrue(sut.execute(client).isSuccess)

val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client)
assertTrue(getShareOperationResult.isSuccess)
val updatedShare = getShareOperationResult.resultData[0]

assertEquals(label, updatedShare.label)
assertEquals(true, updatedShare.isPasswordProtected)
assertEquals(note, updatedShare.note)

assertTrue(RemoveFileRemoteOperation("/label/").execute(client).isSuccess)
}

@Test
fun updateLabel() {
val label = "test & test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2018-2021 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-FileCopyrightText: 2015 ownCloud Inc.
* SPDX-FileCopyrightText: 2015 David A. Velasco <dvelasco@solidgear.es>
Expand All @@ -11,8 +12,8 @@
package com.owncloud.android.lib.resources.shares;

import android.net.Uri;
import android.util.Pair;

import com.google.gson.JsonObject;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand All @@ -22,18 +23,16 @@
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;


/**
* Updates parameters of an existing Share resource, known its remote ID.
*
* <p>
* Allow updating several parameters, triggering a request to the server per parameter.
*/
public class UpdateShareRemoteOperation extends RemoteOperation {
Expand All @@ -47,15 +46,14 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
private static final String PARAM_HIDE_DOWNLOAD = "hideDownload";
private static final String PARAM_LABEL = "label";
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_CONTENT_TYPE = "application/json";
private static final String ENTITY_CHARSET = "UTF-8";
private static final String PARAM_ATTRIBUTES = "attributes";


/**
* Identifier of the share to update
*/
private long remoteId;
private final long remoteId;

/**
* Password to set for the public link
Expand Down Expand Up @@ -147,106 +145,83 @@ public void setNote(String note) {
this.note = note;
}

@Override
protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
RemoteOperationResult<List<OCShare>> result = null;
int status;

/// prepare array of parameters to update
List<Pair<String, String>> parametersToUpdate = new ArrayList<>();
private String getRequestBody() {
JsonObject params = new JsonObject();
if (password != null) {
parametersToUpdate.add(new Pair<>(PARAM_PASSWORD, password));
params.addProperty(PARAM_PASSWORD, password);
}

if (expirationDateInMillis < 0) {
// clear expiration date
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, ""));
params.addProperty(PARAM_EXPIRATION_DATE, "");
} else if (expirationDateInMillis > 0) {
// set expiration date
DateFormat dateFormat = new SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.US);
Calendar expirationDate = Calendar.getInstance();
expirationDate.setTimeInMillis(expirationDateInMillis);
String formattedExpirationDate = dateFormat.format(expirationDate.getTime());
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, formattedExpirationDate));
params.addProperty(PARAM_EXPIRATION_DATE, formattedExpirationDate);
}

if (permissions > 0) {
// set permissions
parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(permissions)));
params.addProperty(PARAM_PERMISSIONS, Integer.toString(permissions));
}

if (hideFileDownload != null) {
parametersToUpdate.add(new Pair<>(PARAM_HIDE_DOWNLOAD, Boolean.toString(hideFileDownload)));
params.addProperty(PARAM_HIDE_DOWNLOAD, Boolean.toString(hideFileDownload));
}

if (note != null) {
parametersToUpdate.add(new Pair<>(PARAM_NOTE, URLEncoder.encode(note)));
params.addProperty(PARAM_NOTE, note);
}

if (label != null) {
parametersToUpdate.add(new Pair<>(PARAM_LABEL, URLEncoder.encode(label)));
params.addProperty(PARAM_LABEL, label);
}

if (attributes != null) {
parametersToUpdate.add(new Pair<>(PARAM_ATTRIBUTES, URLEncoder.encode(attributes)));
params.addProperty(PARAM_ATTRIBUTES, attributes);
}

/// perform required PUT requests
PutMethod put = null;
String uriString;
return params.toString();
}

@Override
protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
RemoteOperationResult<List<OCShare>> result;
String requestBody = getRequestBody();

PutMethod put = null;
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
uriBuilder.appendEncodedPath(Long.toString(remoteId));
uriString = uriBuilder.build().toString();

for (Pair<String, String> parameter : parametersToUpdate) {
if (put != null) {
put.releaseConnection();
}
put = new PutMethod(uriString);
put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
put.setRequestEntity(new StringRequestEntity(
parameter.first + "=" + parameter.second,
ENTITY_CONTENT_TYPE,
ENTITY_CHARSET
));

status = client.executeMethod(put);

if (status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST) {
String response = put.getResponseBodyAsString();

// Parse xml response
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser()
);
parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response);

} else {
result = new RemoteOperationResult<>(false, put);
}
if (!result.isSuccess()) {
break;
}
String uriString = uriBuilder.build().toString();

put = new PutMethod(uriString);
put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
put.setRequestEntity(new StringRequestEntity(requestBody, ENTITY_CONTENT_TYPE, ENTITY_CHARSET));

int status = client.executeMethod(put);
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST) {
String response = put.getResponseBodyAsString();
final var shareXMLParser = new ShareXMLParser();
final var parser = new ShareToRemoteOperationResultParser(shareXMLParser);
parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response);
} else {
result = new RemoteOperationResult<>(false, put);
}

} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while updating remote share ", e);
if (put != null) {
put.releaseConnection();
}

} finally {
if (put != null) {
put.releaseConnection();
}
}

return result;
}

}
Loading