Skip to content

Commit 55c0b6d

Browse files
wip
Signed-off-by: tobiasKaminsky <[email protected]>
1 parent 6ec0364 commit 55c0b6d

File tree

13 files changed

+633
-35
lines changed

13 files changed

+633
-35
lines changed

library/src/androidTest/java/com/owncloud/android/lib/common/accounts/ExternalLinksOperationIT.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727
package com.owncloud.android.lib.common.accounts
2828

2929
import com.owncloud.android.AbstractIT
30-
import com.owncloud.android.lib.common.ExternalLink
3130
import junit.framework.Assert.assertEquals
3231
import junit.framework.Assert.assertTrue
3332
import org.junit.Test
3433

3534
class ExternalLinksOperationIT : AbstractIT() {
3635
@Test
3736
fun retrieveExternalLinks() {
38-
val result = ExternalLinksOperation().execute(client)
37+
val result = ExternalLinksOperation().execute(nextcloudClient)
3938
assertTrue(result.isSuccess)
4039

41-
val data = result.data as ArrayList<ExternalLink>
40+
val data = result.resultData
4241
assertEquals(2, data.size)
4342

4443
assertEquals("Nextcloud", data[0].name)

library/src/main/java/com/nextcloud/android/lib/richWorkspace/RichWorkspaceDirectEditingRemoteOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import com.google.gson.GsonBuilder;
3131
import com.owncloud.android.lib.common.OwnCloudClient;
32-
import com.owncloud.android.lib.common.operations.RemoteOperation;
32+
import com.owncloud.android.lib.common.operations.LegacyRemoteOperation;
3333
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
3434
import com.owncloud.android.lib.common.utils.Log_OC;
3535

@@ -45,7 +45,7 @@
4545
* Get direct editing url for rich workspace
4646
*/
4747

48-
public class RichWorkspaceDirectEditingRemoteOperation extends RemoteOperation {
48+
public class RichWorkspaceDirectEditingRemoteOperation extends LegacyRemoteOperation {
4949
private static final String TAG = RichWorkspaceDirectEditingRemoteOperation.class.getSimpleName();
5050
private static final int SYNC_READ_TIMEOUT = 40000;
5151
private static final int SYNC_CONNECTION_TIMEOUT = 5000;

library/src/main/java/com/owncloud/android/lib/common/accounts/ExternalLinksOperation.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* gets external links provided by 'external' app
4949
*/
5050

51-
public class ExternalLinksOperation extends NextcloudRemoteOperation {
51+
public class ExternalLinksOperation extends NextcloudRemoteOperation<ArrayList<ExternalLink>> {
5252

5353
private static final String TAG = ExternalLinksOperation.class.getSimpleName();
5454

@@ -68,8 +68,8 @@ public class ExternalLinksOperation extends NextcloudRemoteOperation {
6868

6969

7070
@Override
71-
public RemoteOperationResult run(NextcloudClient client) {
72-
RemoteOperationResult result = null;
71+
public RemoteOperationResult<ArrayList<ExternalLink>> run(NextcloudClient client) {
72+
RemoteOperationResult<ArrayList<ExternalLink>> result = null;
7373
int status = -1;
7474
GetMethod get = null;
7575
String ocsUrl = client.getBaseUri() + OCS_ROUTE_EXTERNAL_LINKS;
@@ -96,13 +96,13 @@ public RemoteOperationResult run(NextcloudClient client) {
9696
// parse
9797
JSONArray links = new JSONObject(response).getJSONObject(NODE_OCS).getJSONArray(NODE_DATA);
9898

99-
ArrayList<Object> resultLinks = new ArrayList<>();
99+
ArrayList<ExternalLink> resultLinks = new ArrayList<>();
100100

101101
for (int i = 0; i < links.length(); i++) {
102102
JSONObject link = links.getJSONObject(i);
103103

104104
if (link != null) {
105-
Integer id = link.getInt(NODE_ID);
105+
int id = link.getInt(NODE_ID);
106106
String iconUrl = link.getString(NODE_ICON);
107107
String language = "";
108108
if (link.has(NODE_LANGUAGE)) {
@@ -139,26 +139,22 @@ public RemoteOperationResult run(NextcloudClient client) {
139139
}
140140
}
141141

142-
result = new RemoteOperationResult(true, get);
143-
result.setData(resultLinks);
142+
result = new RemoteOperationResult<>(true, get);
143+
result.setResultData(resultLinks);
144144

145145
} else {
146-
result = new RemoteOperationResult(false, get);
146+
result = new RemoteOperationResult<>(false, get);
147147
String response = get.getResponseBodyAsString();
148148
Log_OC.e(TAG, "Failed response while getting external links ");
149-
if (response != null) {
150-
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
151-
} else {
152-
Log_OC.e(TAG, "*** status code: " + status);
153-
}
149+
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
154150
}
155151
} else {
156-
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.NOT_AVAILABLE);
152+
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.NOT_AVAILABLE);
157153
Log_OC.d(TAG, "External links disabled");
158154
}
159155

160156
} catch (Exception e) {
161-
result = new RemoteOperationResult(e);
157+
result = new RemoteOperationResult<>(e);
162158
Log_OC.e(TAG, "Exception while getting external links ", e);
163159
} finally {
164160
if (get != null) {

library/src/main/java/com/owncloud/android/lib/common/operations/NextcloudRemoteOperation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ abstract class NextcloudRemoteOperation<T> : RemoteOperation<T>() {
4848
* @param context Android context for the component calling the method.
4949
* @return Result of the operation.
5050
*/
51-
fun executeNextcloudClient(account: Account, context: Context): RemoteOperationResult<T>? {
51+
private fun executeNextcloudClient(account: Account, context: Context): RemoteOperationResult<T> {
5252
mAccount = account
5353
mContext = context.applicationContext
5454
clientNew = try {
@@ -66,7 +66,7 @@ abstract class NextcloudRemoteOperation<T> : RemoteOperation<T>() {
6666
* This is a transitional wrapper around [.executeNextcloudClient]
6767
* using modern [User] interface instead of platform [Account]
6868
*/
69-
fun executeNextcloudClient(user: User, context: Context): RemoteOperationResult<T>? {
69+
fun executeNextcloudClient(user: User, context: Context): RemoteOperationResult<T> {
7070
return executeNextcloudClient(user.toPlatformAccount(), context)
7171
}
7272

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2018 Tobias Kaminsky
6+
* Copyright (C) 2018 Nextcloud GmbH.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.owncloud.android.lib.resources.files;
23+
24+
import com.owncloud.android.lib.common.OwnCloudClient;
25+
import com.owncloud.android.lib.common.operations.LegacyRemoteOperation;
26+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
27+
import com.owncloud.android.lib.common.utils.Log_OC;
28+
import com.owncloud.android.lib.resources.comments.CommentFileRemoteOperation;
29+
30+
/**
31+
* Comment file
32+
*/
33+
public class CommentFileOperation extends LegacyRemoteOperation {
34+
35+
private final String message;
36+
private final long fileId;
37+
38+
/**
39+
* Constructor
40+
*
41+
* @param message Comment to store
42+
*/
43+
public CommentFileOperation(String message, long fileId) {
44+
this.message = message;
45+
this.fileId = fileId;
46+
}
47+
48+
/**
49+
* Performs the operation.
50+
*
51+
* @param client Client object to communicate with the remote ownCloud server.
52+
*/
53+
@Override
54+
protected RemoteOperationResult run(OwnCloudClient client) {
55+
RemoteOperationResult result = new CommentFileRemoteOperation(message, fileId).execute(client);
56+
57+
if (!result.isSuccess()) {
58+
Log_OC.e(this, "File with Id " + fileId + " could not be commented");
59+
}
60+
61+
return result;
62+
}
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2018 Tobias Kaminsky
6+
* Copyright (C) 2018 Nextcloud GmbH.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
package com.owncloud.android.lib.resources.files;
22+
23+
import com.owncloud.android.lib.common.OwnCloudClient;
24+
import com.owncloud.android.lib.common.operations.LegacyRemoteOperation;
25+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
26+
import com.owncloud.android.lib.common.utils.Log_OC;
27+
28+
import org.apache.commons.httpclient.HttpStatus;
29+
import org.apache.commons.httpclient.methods.Utf8PostMethod;
30+
import org.json.JSONObject;
31+
32+
import java.util.ArrayList;
33+
34+
public class CreateFileFromTemplateOperation extends LegacyRemoteOperation {
35+
private static final String TAG = CreateFileFromTemplateOperation.class.getSimpleName();
36+
private static final int SYNC_READ_TIMEOUT = 40000;
37+
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
38+
private static final String NEW_FROM_TEMPLATE_URL = "/ocs/v2.php/apps/richdocuments/api/v1/templates/new";
39+
40+
private String path;
41+
private long templateId;
42+
43+
// JSON node names
44+
private static final String NODE_OCS = "ocs";
45+
private static final String NODE_DATA = "data";
46+
private static final String JSON_FORMAT = "?format=json";
47+
48+
public CreateFileFromTemplateOperation(String path, long templateId) {
49+
this.path = path;
50+
this.templateId = templateId;
51+
}
52+
53+
protected RemoteOperationResult run(OwnCloudClient client) {
54+
RemoteOperationResult result;
55+
Utf8PostMethod postMethod = null;
56+
57+
try {
58+
59+
postMethod = new Utf8PostMethod(client.getBaseUri() + NEW_FROM_TEMPLATE_URL + JSON_FORMAT);
60+
postMethod.setParameter("path", path);
61+
postMethod.setParameter("template", String.valueOf(templateId));
62+
63+
// remote request
64+
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
65+
66+
int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
67+
68+
if (status == HttpStatus.SC_OK) {
69+
String response = postMethod.getResponseBodyAsString();
70+
71+
// Parse the response
72+
JSONObject respJSON = new JSONObject(response);
73+
String url = respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString("url");
74+
75+
ArrayList<Object> templateArray = new ArrayList<>();
76+
templateArray.add(url);
77+
78+
result = new RemoteOperationResult(true, postMethod);
79+
result.setData(templateArray);
80+
} else {
81+
result = new RemoteOperationResult(false, postMethod);
82+
client.exhaustResponse(postMethod.getResponseBodyAsStream());
83+
}
84+
} catch (Exception e) {
85+
result = new RemoteOperationResult(e);
86+
Log_OC.e(TAG, "Create file from template " + templateId + " failed: " + result.getLogMessage(),
87+
result.getException());
88+
} finally {
89+
if (postMethod != null) {
90+
postMethod.releaseConnection();
91+
}
92+
}
93+
return result;
94+
}
95+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2018 Tobias Kaminsky
6+
* Copyright (C) 2018 Nextcloud GmbH.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
package com.owncloud.android.lib.resources.files;
22+
23+
import com.owncloud.android.lib.common.OwnCloudClient;
24+
import com.owncloud.android.lib.common.operations.LegacyRemoteOperation;
25+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
26+
import com.owncloud.android.lib.common.utils.Log_OC;
27+
28+
import org.apache.commons.httpclient.HttpStatus;
29+
import org.apache.commons.httpclient.methods.GetMethod;
30+
import org.json.JSONArray;
31+
import org.json.JSONObject;
32+
33+
import java.util.ArrayList;
34+
import java.util.Locale;
35+
36+
public class FetchTemplateRemoteOperation extends LegacyRemoteOperation {
37+
private static final String TAG = FetchTemplateRemoteOperation.class.getSimpleName();
38+
private static final int SYNC_READ_TIMEOUT = 40000;
39+
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
40+
private static final String TEMPLATE_URL = "/ocs/v2.php/apps/richdocuments/api/v1/templates/";
41+
42+
private RichDocumentsTemplateType type;
43+
44+
// JSON node names
45+
private static final String NODE_OCS = "ocs";
46+
private static final String NODE_DATA = "data";
47+
private static final String JSON_FORMAT = "?format=json";
48+
49+
public FetchTemplateRemoteOperation(RichDocumentsTemplateType type) {
50+
this.type = type;
51+
}
52+
53+
protected RemoteOperationResult run(OwnCloudClient client) {
54+
RemoteOperationResult result;
55+
GetMethod getMethod = null;
56+
57+
try {
58+
59+
getMethod = new GetMethod(client.getBaseUri() + TEMPLATE_URL + type.toString().toLowerCase(Locale.ENGLISH) +
60+
JSON_FORMAT);
61+
62+
// remote request
63+
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
64+
65+
int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
66+
67+
if (status == HttpStatus.SC_OK) {
68+
String response = getMethod.getResponseBodyAsString();
69+
70+
// Parse the response
71+
JSONObject respJSON = new JSONObject(response);
72+
JSONArray templates = respJSON.getJSONObject(NODE_OCS).getJSONArray(NODE_DATA);
73+
74+
ArrayList<Object> templateArray = new ArrayList<>();
75+
76+
for (int i = 0; i < templates.length(); i++) {
77+
JSONObject templateObject = templates.getJSONObject(i);
78+
79+
templateArray.add(new Template(templateObject.getLong("id"),
80+
templateObject.getString("name"),
81+
templateObject.optString("preview"),
82+
RichDocumentsTemplateType.valueOf(templateObject.getString("type")
83+
.toUpperCase(Locale.ROOT)),
84+
templateObject.getString("extension")));
85+
}
86+
87+
result = new RemoteOperationResult(true, getMethod);
88+
result.setData(templateArray);
89+
} else {
90+
result = new RemoteOperationResult(false, getMethod);
91+
client.exhaustResponse(getMethod.getResponseBodyAsStream());
92+
}
93+
} catch (Exception e) {
94+
result = new RemoteOperationResult(e);
95+
Log_OC.e(TAG, "Get templates for typ " + type + " failed: " + result.getLogMessage(),
96+
result.getException());
97+
} finally {
98+
if (getMethod != null) {
99+
getMethod.releaseConnection();
100+
}
101+
}
102+
return result;
103+
}
104+
}

0 commit comments

Comments
 (0)