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

Implement update share using the new wrapper methods in library

This commit is contained in:
davigonz 2018-06-07 16:26:22 +02:00
parent 6d1e2d0434
commit 226e332460

View File

@ -1,6 +1,6 @@
/* ownCloud Android Library is available under MIT license
* @author David A. Velasco
* Copyright (C) 2016 ownCloud GmbH.
*
* Copyright (C) 2018 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
@ -26,29 +26,31 @@
package com.owncloud.android.lib.resources.shares;
import android.net.Uri;
import android.util.Pair;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.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 org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
/**
* Updates parameters of an existing Share resource, known its remote ID.
* <p/>
*
* 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 {
@ -164,21 +166,31 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
int status;
RemoteOperationResult result;
/// prepare array of parameters to update
List<Pair<String, String>> parametersToUpdate = new ArrayList<>();
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
Request request = new Request.Builder()
.url(uriBuilder.build().toString())
.header("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8")
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
FormBody.Builder formBodyBuilder = new FormBody.Builder();
// Parameters to update
if (mName != null) {
parametersToUpdate.add(new Pair<>(PARAM_NAME, mName));
formBodyBuilder.add(PARAM_NAME, mName);
}
if (mPassword != null) {
parametersToUpdate.add(new Pair<>(PARAM_PASSWORD, mPassword));
}
if (mExpirationDateInMillis < 0) {
// clear expiration date
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, ""));
formBodyBuilder.add(PARAM_EXPIRATION_DATE, "");
} else if (mExpirationDateInMillis > 0) {
// set expiration date
@ -186,81 +198,48 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
Calendar expirationDate = Calendar.getInstance();
expirationDate.setTimeInMillis(mExpirationDateInMillis);
String formattedExpirationDate = dateFormat.format(expirationDate.getTime());
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, formattedExpirationDate));
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate);
} // else, ignore - no update
if (mPublicUpload != null) {
parametersToUpdate.add(new Pair<>(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload)));
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
parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(mPermissions)));
formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions));
}
/// perform required PUT requests
PutMethod put = null;
String uriString;
FormBody formBody = formBodyBuilder.build();
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
uriString = uriBuilder.build().toString();
PutMethod putMethod = new PutMethod(client.getOkHttpClient(), request, formBody);
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) {
String response = put.getResponseBodyAsString();
Response response = client.executeHttpMethod(putMethod);
if (isSuccess(response.code())) {
// Parse xml response
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser()
);
parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response);
result = parser.parse(response.body().string());
} else {
result = new RemoteOperationResult(false, put);
}
if (!result.isSuccess() &&
!PARAM_NAME.equals(parameter.first)
// fail in "name" parameter will be ignored; requires OCX, will fail
// fails in previous versions
) {
break;
}
result = new RemoteOperationResult(false, putMethod.getRequest(), response);
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while updating remote share ", e);
if (put != null) {
put.releaseConnection();
Log_OC.e(TAG, "Exception while Creating New Share", e);
}
} finally {
if (put != null) {
put.releaseConnection();
}
}
return result;
}
private boolean isSuccess(int status) {
return (status == HttpConstants.HTTP_OK);
}
}