Skip to content

Commit b5945bd

Browse files
committed
fix unnecessary calls
Signed-off-by: alperozturk <alper_ozturk@proton.me>
1 parent 2d47aa4 commit b5945bd

1 file changed

Lines changed: 40 additions & 51 deletions

File tree

library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
/**
3535
* Updates parameters of an existing Share resource, known its remote ID.
36-
*
36+
* <p>
3737
* Allow updating several parameters, triggering a request to the server per parameter.
3838
*/
3939
public class UpdateShareRemoteOperation extends RemoteOperation {
@@ -54,7 +54,7 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
5454
/**
5555
* Identifier of the share to update
5656
*/
57-
private long remoteId;
57+
private final long remoteId;
5858

5959
/**
6060
* Password to set for the public link
@@ -143,8 +143,7 @@ public void setNote(String note) {
143143

144144
@Override
145145
protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
146-
RemoteOperationResult<List<OCShare>> result = null;
147-
int status;
146+
RemoteOperationResult<List<OCShare>> result;
148147

149148
/// prepare array of parameters to update
150149
List<Pair<String, String>> parametersToUpdate = new ArrayList<>();
@@ -165,78 +164,68 @@ protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
165164
}
166165

167166
if (permissions > 0) {
168-
// set permissions
169167
parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(permissions)));
170168
}
171169

172170
if (hideFileDownload != null) {
173171
parametersToUpdate.add(new Pair<>(PARAM_HIDE_DOWNLOAD, Boolean.toString(hideFileDownload)));
174172
}
175173

176-
if (note != null) {
177-
parametersToUpdate.add(new Pair<>(PARAM_NOTE, URLEncoder.encode(note)));
178-
}
179-
180-
if (label != null) {
181-
parametersToUpdate.add(new Pair<>(PARAM_LABEL, URLEncoder.encode(label)));
182-
}
183-
184-
/// perform required PUT requests
185174
PutMethod put = null;
186-
String uriString;
187-
188175
try {
176+
if (note != null) {
177+
parametersToUpdate.add(new Pair<>(PARAM_NOTE, URLEncoder.encode(note, ENTITY_CHARSET)));
178+
}
179+
180+
if (label != null) {
181+
parametersToUpdate.add(new Pair<>(PARAM_LABEL, URLEncoder.encode(label, ENTITY_CHARSET)));
182+
}
183+
189184
Uri requestUri = client.getBaseUri();
190185
Uri.Builder uriBuilder = requestUri.buildUpon();
191186
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
192187
uriBuilder.appendEncodedPath(Long.toString(remoteId));
193-
uriString = uriBuilder.build().toString();
188+
String uriString = uriBuilder.build().toString();
194189

195-
for (Pair<String, String> parameter : parametersToUpdate) {
196-
if (put != null) {
197-
put.releaseConnection();
198-
}
199-
put = new PutMethod(uriString);
200-
put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
201-
put.setRequestEntity(new StringRequestEntity(
202-
parameter.first + "=" + parameter.second,
203-
ENTITY_CONTENT_TYPE,
204-
ENTITY_CHARSET
205-
));
206-
207-
status = client.executeMethod(put);
208-
209-
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST) {
210-
String response = put.getResponseBodyAsString();
211-
212-
// Parse xml response
213-
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
214-
new ShareXMLParser()
215-
);
216-
parser.setServerBaseUri(client.getBaseUri());
217-
result = parser.parse(response);
218-
219-
} else {
220-
result = new RemoteOperationResult<>(false, put);
221-
}
222-
if (!result.isSuccess()) {
223-
break;
190+
StringBuilder bodyBuilder = new StringBuilder();
191+
for (int i = 0; i < parametersToUpdate.size(); i++) {
192+
Pair<String, String> param = parametersToUpdate.get(i);
193+
bodyBuilder.append(param.first)
194+
.append("=")
195+
.append(param.second);
196+
197+
if (i < parametersToUpdate.size() - 1) {
198+
bodyBuilder.append("&");
224199
}
225200
}
226201

202+
put = new PutMethod(uriString);
203+
put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
204+
put.setRequestEntity(new StringRequestEntity(
205+
bodyBuilder.toString(),
206+
ENTITY_CONTENT_TYPE,
207+
ENTITY_CHARSET
208+
));
209+
210+
int status = client.executeMethod(put);
211+
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST) {
212+
String response = put.getResponseBodyAsString();
213+
final var shareXMLParser = new ShareXMLParser();
214+
final var parser = new ShareToRemoteOperationResultParser(shareXMLParser);
215+
parser.setServerBaseUri(client.getBaseUri());
216+
result = parser.parse(response);
217+
} else {
218+
result = new RemoteOperationResult<>(false, put);
219+
}
227220
} catch (Exception e) {
228221
result = new RemoteOperationResult<>(e);
229222
Log_OC.e(TAG, "Exception while updating remote share ", e);
230-
if (put != null) {
231-
put.releaseConnection();
232-
}
233-
234223
} finally {
235224
if (put != null) {
236225
put.releaseConnection();
237226
}
238227
}
228+
239229
return result;
240230
}
241-
242231
}

0 commit comments

Comments
 (0)