Skip to content

Commit f28477f

Browse files
Add direct endpoint api
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
1 parent 27fe732 commit f28477f

17 files changed

+943
-0
lines changed

.drone.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ services:
9797
- su www-data -c "php /var/www/html/occ group:add users"
9898
- su www-data -c "php /var/www/html/occ group:adduser users user1"
9999
- su www-data -c "php /var/www/html/occ group:adduser users user2"
100+
- su www-data -c "git clone -b master https://github.com/nextcloud/text.git /var/www/html/apps/text/"
101+
- su www-data -c "php /var/www/html/occ app:enable text"
100102
- /run.sh
101103

102104
trigger:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.AbstractIT;
31+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
32+
33+
import org.junit.Test;
34+
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
public class DirectEditingCreateFileRemoteOperationTest extends AbstractIT {
39+
@Test
40+
public void createEmptyFile() {
41+
RemoteOperationResult result = new DirectEditingCreateFileRemoteOperation("/test.md",
42+
"text",
43+
"textdocumenttemplate")
44+
.execute(client);
45+
assertTrue(result.isSuccess());
46+
47+
String url = (String) result.getSingleData();
48+
49+
assertFalse(url.isEmpty());
50+
}
51+
52+
@Test
53+
public void createFileFromTemplate() {
54+
RemoteOperationResult result = new DirectEditingCreateFileRemoteOperation("/test.md",
55+
"text",
56+
"textdocumenttemplate",
57+
"1")
58+
.execute(client);
59+
assertTrue(result.isSuccess());
60+
61+
String url = (String) result.getSingleData();
62+
63+
assertFalse(url.isEmpty());
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.AbstractIT;
31+
import com.owncloud.android.lib.common.TemplateList;
32+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
33+
34+
import org.junit.Test;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertTrue;
38+
39+
public class DirectEditingObtainListOfTemplatesRemoteOperationTest extends AbstractIT {
40+
41+
@Test
42+
public void testGetAll() {
43+
RemoteOperationResult result = new DirectEditingObtainListOfTemplatesRemoteOperation("text",
44+
"textdocumenttemplate")
45+
.execute(client);
46+
assertTrue(result.isSuccess());
47+
48+
TemplateList templateList = (TemplateList) result.getSingleData();
49+
50+
assertEquals("Weekly ToDo", templateList.templates.get("1").name);
51+
assertEquals("md", templateList.templates.get("1").extension);
52+
53+
assertEquals("Meeting notes", templateList.templates.get("2").name);
54+
assertEquals("md", templateList.templates.get("2").extension);
55+
}
56+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
*
3+
* * Nextcloud Android client application
4+
* *
5+
* * @author Tobias Kaminsky
6+
* * Copyright (C) 2019 Tobias Kaminsky
7+
* * Copyright (C) 2019 Nextcloud GmbH
8+
* *
9+
* * This program is free software: you can redistribute it and/or modify
10+
* * it under the terms of the GNU Affero General Public License as published by
11+
* * the Free Software Foundation, either version 3 of the License, or
12+
* * (at your option) any later version.
13+
* *
14+
* * This program is distributed in the hope that it will be useful,
15+
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* * GNU Affero General Public License for more details.
18+
* *
19+
* * You should have received a copy of the GNU Affero General Public License
20+
* * along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package com.nextcloud.android.lib.resources.directediting;
25+
26+
import com.owncloud.android.AbstractIT;
27+
import com.owncloud.android.lib.common.DirectEditing;
28+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
29+
30+
import org.junit.Test;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertFalse;
34+
import static org.junit.Assert.assertTrue;
35+
36+
public class DirectEditingObtainRemoteOperationTest extends AbstractIT {
37+
38+
@Test
39+
public void testGetAll() {
40+
RemoteOperationResult result = new DirectEditingObtainRemoteOperation().execute(client);
41+
assertTrue(result.isSuccess());
42+
43+
DirectEditing directEditing = (DirectEditing) result.getSingleData();
44+
45+
assertEquals("Nextcloud Text", directEditing.editors.get("text").name);
46+
assertEquals("text/plain", directEditing.editors.get("text").optionalMimetypes.get(0));
47+
48+
assertEquals("New text document", directEditing.creators.get("textdocument").name);
49+
assertFalse(directEditing.creators.get("textdocument").templates);
50+
51+
assertEquals("New text document from template", directEditing.creators.get("textdocumenttemplate").name);
52+
assertTrue(directEditing.creators.get("textdocumenttemplate").templates);
53+
}
54+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.AbstractIT;
31+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
32+
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
33+
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
34+
35+
import junit.framework.TestCase;
36+
37+
import org.junit.Test;
38+
39+
import java.io.IOException;
40+
41+
import static org.junit.Assert.assertFalse;
42+
import static org.junit.Assert.assertTrue;
43+
44+
public class DirectEditingOpenFileRemoteOperationTest extends AbstractIT {
45+
@Test
46+
public void createFileFromTemplate() throws IOException {
47+
// create file
48+
String filePath = createFile("text");
49+
String remotePath = "/text.md";
50+
TestCase.assertTrue(new UploadFileRemoteOperation(filePath, remotePath, "text/markdown", "123")
51+
.execute(client).isSuccess());
52+
53+
TestCase.assertTrue(new ReadFileRemoteOperation(remotePath).execute(client).isSuccess());
54+
55+
// open file
56+
RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(remotePath, "text").execute(client);
57+
assertTrue(result.isSuccess());
58+
59+
String url = (String) result.getSingleData();
60+
61+
assertFalse(url.isEmpty());
62+
}
63+
}

src/androidTest/java/com/owncloud/android/GetCapabilitiesTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public void testGetRemoteCapabilitiesOperation() {
5353
OCCapability capability = (OCCapability) result.getData().get(0);
5454

5555
Assert.assertSame(capability.getRichDocuments(), CapabilityBooleanType.FALSE);
56+
57+
Assert.assertFalse(capability.getDirectEditingEtag().isEmpty());
58+
5659
// TODO assert basic capabilities
5760
}
5861
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.lib.common.OwnCloudClient;
31+
import com.owncloud.android.lib.common.operations.RemoteOperation;
32+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
33+
import com.owncloud.android.lib.common.utils.Log_OC;
34+
35+
import org.apache.commons.httpclient.HttpStatus;
36+
import org.apache.commons.httpclient.methods.PostMethod;
37+
import org.json.JSONObject;
38+
39+
/**
40+
* Create file with direct editing api
41+
*/
42+
43+
public class DirectEditingCreateFileRemoteOperation extends RemoteOperation {
44+
private static final String TAG = DirectEditingCreateFileRemoteOperation.class.getSimpleName();
45+
private static final int SYNC_READ_TIMEOUT = 40000;
46+
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
47+
private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/files/api/v1/directEditing/create";
48+
49+
private static final String JSON_FORMAT = "?format=json";
50+
51+
private String path;
52+
private String editor;
53+
private String creator;
54+
private String template;
55+
56+
public DirectEditingCreateFileRemoteOperation(String path, String editor, String creator) {
57+
this(path, editor, creator, "");
58+
}
59+
60+
public DirectEditingCreateFileRemoteOperation(String path, String editor, String creator, String template) {
61+
this.path = path;
62+
this.editor = editor;
63+
this.creator = creator;
64+
this.template = template;
65+
}
66+
67+
protected RemoteOperationResult run(OwnCloudClient client) {
68+
RemoteOperationResult result;
69+
PostMethod postMethod = null;
70+
71+
try {
72+
postMethod = new PostMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT);
73+
postMethod.addParameter("path", path);
74+
postMethod.addParameter("editorId", editor);
75+
postMethod.addParameter("creatorId", creator);
76+
77+
if (!template.isEmpty()) {
78+
postMethod.addParameter("templateId", template);
79+
}
80+
81+
// remote request
82+
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
83+
84+
int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
85+
86+
if (status == HttpStatus.SC_OK) {
87+
String response = postMethod.getResponseBodyAsString();
88+
89+
// Parse the response
90+
JSONObject respJSON = new JSONObject(response);
91+
String url = (String) respJSON.getJSONObject("ocs").getJSONObject("data").get("url");
92+
93+
result = new RemoteOperationResult(true, postMethod);
94+
result.setSingleData(url);
95+
} else {
96+
result = new RemoteOperationResult(false, postMethod);
97+
client.exhaustResponse(postMethod.getResponseBodyAsStream());
98+
}
99+
} catch (Exception e) {
100+
result = new RemoteOperationResult(e);
101+
Log_OC.e(TAG, "Get all direct editing informations failed: " + result.getLogMessage(),
102+
result.getException());
103+
} finally {
104+
if (postMethod != null) {
105+
postMethod.releaseConnection();
106+
}
107+
}
108+
return result;
109+
}
110+
}

0 commit comments

Comments
 (0)