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:
parent
6d1e2d0434
commit
226e332460
@ -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,103 +166,80 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
|
||||
|
||||
@Override
|
||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||
RemoteOperationResult result = null;
|
||||
int status;
|
||||
|
||||
/// prepare array of parameters to update
|
||||
List<Pair<String, String>> parametersToUpdate = new ArrayList<>();
|
||||
if (mName != null) {
|
||||
parametersToUpdate.add(new Pair<>(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, ""));
|
||||
|
||||
} 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());
|
||||
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, formattedExpirationDate));
|
||||
|
||||
} // else, ignore - no update
|
||||
|
||||
if (mPublicUpload != null) {
|
||||
parametersToUpdate.add(new Pair<>(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)));
|
||||
}
|
||||
|
||||
/// perform required PUT requests
|
||||
PutMethod put = null;
|
||||
String uriString;
|
||||
RemoteOperationResult result;
|
||||
|
||||
try {
|
||||
Uri requestUri = client.getBaseUri();
|
||||
Uri.Builder uriBuilder = requestUri.buildUpon();
|
||||
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
|
||||
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
|
||||
uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
|
||||
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
|
||||
));
|
||||
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();
|
||||
|
||||
status = client.executeMethod(put);
|
||||
FormBody.Builder formBodyBuilder = new FormBody.Builder();
|
||||
|
||||
if (status == HttpStatus.SC_OK) {
|
||||
String response = put.getResponseBodyAsString();
|
||||
// Parameters to update
|
||||
if (mName != null) {
|
||||
formBodyBuilder.add(PARAM_NAME, mName);
|
||||
}
|
||||
|
||||
// Parse xml response
|
||||
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
|
||||
new ShareXMLParser()
|
||||
);
|
||||
parser.setOwnCloudVersion(client.getOwnCloudVersion());
|
||||
parser.setServerBaseUri(client.getBaseUri());
|
||||
result = parser.parse(response);
|
||||
if (mExpirationDateInMillis < 0) {
|
||||
// clear expiration date
|
||||
formBodyBuilder.add(PARAM_EXPIRATION_DATE, "");
|
||||
|
||||
} 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;
|
||||
}
|
||||
} 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));
|
||||
}
|
||||
|
||||
FormBody formBody = formBodyBuilder.build();
|
||||
|
||||
PutMethod putMethod = new PutMethod(client.getOkHttpClient(), request, formBody);
|
||||
|
||||
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.body().string());
|
||||
|
||||
} else {
|
||||
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();
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (put != null) {
|
||||
put.releaseConnection();
|
||||
}
|
||||
Log_OC.e(TAG, "Exception while Creating New Share", e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSuccess(int status) {
|
||||
return (status == HttpConstants.HTTP_OK);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user