1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +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 /* 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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -26,29 +26,31 @@
package com.owncloud.android.lib.resources.shares; package com.owncloud.android.lib.resources.shares;
import android.net.Uri; import android.net.Uri;
import android.util.Pair;
import com.owncloud.android.lib.common.OwnCloudClient; 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.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 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.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
/** /**
* Updates parameters of an existing Share resource, known its remote ID. * Updates parameters of an existing Share resource, known its remote ID.
* <p/> *
* Allow updating several parameters, triggering a request to the server per parameter. * 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 { public class UpdateRemoteShareOperation extends RemoteOperation {
@ -164,21 +166,31 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null; RemoteOperationResult result;
int status;
/// prepare array of parameters to update try {
List<Pair<String, String>> parametersToUpdate = new ArrayList<>(); 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) { 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) { if (mExpirationDateInMillis < 0) {
// clear expiration date // clear expiration date
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, "")); formBodyBuilder.add(PARAM_EXPIRATION_DATE, "");
} else if (mExpirationDateInMillis > 0) { } else if (mExpirationDateInMillis > 0) {
// set expiration date // set expiration date
@ -186,81 +198,48 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
Calendar expirationDate = Calendar.getInstance(); Calendar expirationDate = Calendar.getInstance();
expirationDate.setTimeInMillis(mExpirationDateInMillis); expirationDate.setTimeInMillis(mExpirationDateInMillis);
String formattedExpirationDate = dateFormat.format(expirationDate.getTime()); String formattedExpirationDate = dateFormat.format(expirationDate.getTime());
parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, formattedExpirationDate)); formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate);
} // else, ignore - no update } // else, ignore - no update
if (mPublicUpload != null) { 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, // 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 (mPermissions > 0) { if (mPermissions > 0) {
// set permissions // set permissions
parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(mPermissions))); formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions));
} }
/// perform required PUT requests FormBody formBody = formBodyBuilder.build();
PutMethod put = null;
String uriString;
try { PutMethod putMethod = new PutMethod(client.getOkHttpClient(), request, formBody);
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
uriString = uriBuilder.build().toString();
for (Pair<String, String> parameter : parametersToUpdate) { Response response = client.executeHttpMethod(putMethod);
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();
if (isSuccess(response.code())) {
// Parse xml response // Parse xml response
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
); );
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response); result = parser.parse(response.body().string());
} else { } else {
result = new RemoteOperationResult(false, put); result = new RemoteOperationResult(false, putMethod.getRequest(), response);
}
if (!result.isSuccess() &&
!PARAM_NAME.equals(parameter.first)
// fail in "name" parameter will be ignored; requires OCX, will fail
// fails in previous versions
) {
break;
}
} }
} catch (Exception e) { } catch (Exception e) {
result = new RemoteOperationResult(e); result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while updating remote share ", e); Log_OC.e(TAG, "Exception while Creating New Share", e);
if (put != null) {
put.releaseConnection();
} }
} finally {
if (put != null) {
put.releaseConnection();
}
}
return result; return result;
} }
private boolean isSuccess(int status) {
return (status == HttpConstants.HTTP_OK);
}
} }