mirror of
https://github.com/nextcloud/android-library.git
synced 2025-07-25 16:08:27 +00:00
fix unnecessary calls
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
@ -33,7 +33,7 @@ import java.util.Locale;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
public class UpdateShareRemoteOperation extends RemoteOperation {
|
public class UpdateShareRemoteOperation extends RemoteOperation {
|
||||||
@ -55,7 +55,7 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
|
|||||||
/**
|
/**
|
||||||
* Identifier of the share to update
|
* Identifier of the share to update
|
||||||
*/
|
*/
|
||||||
private long remoteId;
|
private final long remoteId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Password to set for the public link
|
* Password to set for the public link
|
||||||
@ -149,8 +149,7 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
|
protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
|
||||||
RemoteOperationResult<List<OCShare>> result = null;
|
RemoteOperationResult<List<OCShare>> result;
|
||||||
int status;
|
|
||||||
|
|
||||||
/// prepare array of parameters to update
|
/// prepare array of parameters to update
|
||||||
List<Pair<String, String>> parametersToUpdate = new ArrayList<>();
|
List<Pair<String, String>> parametersToUpdate = new ArrayList<>();
|
||||||
@ -171,7 +170,6 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (permissions > 0) {
|
if (permissions > 0) {
|
||||||
// set permissions
|
|
||||||
parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(permissions)));
|
parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(permissions)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,74 +177,61 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
|
|||||||
parametersToUpdate.add(new Pair<>(PARAM_HIDE_DOWNLOAD, Boolean.toString(hideFileDownload)));
|
parametersToUpdate.add(new Pair<>(PARAM_HIDE_DOWNLOAD, Boolean.toString(hideFileDownload)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (note != null) {
|
|
||||||
parametersToUpdate.add(new Pair<>(PARAM_NOTE, URLEncoder.encode(note)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (label != null) {
|
|
||||||
parametersToUpdate.add(new Pair<>(PARAM_LABEL, URLEncoder.encode(label)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (attributes != null) {
|
|
||||||
parametersToUpdate.add(new Pair<>(PARAM_ATTRIBUTES, URLEncoder.encode(attributes)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// perform required PUT requests
|
|
||||||
PutMethod put = null;
|
PutMethod put = null;
|
||||||
String uriString;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (note != null) {
|
||||||
|
parametersToUpdate.add(new Pair<>(PARAM_NOTE, URLEncoder.encode(note, ENTITY_CHARSET)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (label != null) {
|
||||||
|
parametersToUpdate.add(new Pair<>(PARAM_LABEL, URLEncoder.encode(label, ENTITY_CHARSET)));
|
||||||
|
}
|
||||||
|
|
||||||
Uri requestUri = client.getBaseUri();
|
Uri requestUri = client.getBaseUri();
|
||||||
Uri.Builder uriBuilder = requestUri.buildUpon();
|
Uri.Builder uriBuilder = requestUri.buildUpon();
|
||||||
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
|
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
|
||||||
uriBuilder.appendEncodedPath(Long.toString(remoteId));
|
uriBuilder.appendEncodedPath(Long.toString(remoteId));
|
||||||
uriString = uriBuilder.build().toString();
|
String uriString = uriBuilder.build().toString();
|
||||||
|
|
||||||
for (Pair<String, String> parameter : parametersToUpdate) {
|
StringBuilder bodyBuilder = new StringBuilder();
|
||||||
if (put != null) {
|
for (int i = 0; i < parametersToUpdate.size(); i++) {
|
||||||
put.releaseConnection();
|
Pair<String, String> param = parametersToUpdate.get(i);
|
||||||
}
|
bodyBuilder.append(param.first)
|
||||||
put = new PutMethod(uriString);
|
.append("=")
|
||||||
put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
|
.append(param.second);
|
||||||
put.setRequestEntity(new StringRequestEntity(
|
|
||||||
parameter.first + "=" + parameter.second,
|
|
||||||
ENTITY_CONTENT_TYPE,
|
|
||||||
ENTITY_CHARSET
|
|
||||||
));
|
|
||||||
|
|
||||||
status = client.executeMethod(put);
|
if (i < parametersToUpdate.size() - 1) {
|
||||||
|
bodyBuilder.append("&");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
put = new PutMethod(uriString);
|
||||||
|
put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
|
||||||
|
put.setRequestEntity(new StringRequestEntity(
|
||||||
|
bodyBuilder.toString(),
|
||||||
|
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) {
|
} 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 updating remote share ", e);
|
||||||
if (put != null) {
|
|
||||||
put.releaseConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (put != null) {
|
if (put != null) {
|
||||||
put.releaseConnection();
|
put.releaseConnection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user