Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate OwncloudClient - DirectEditing #1273

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
*/
package com.nextcloud.android.lib.resources.directediting;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class DirectEditingCreateFileRemoteOperationIT extends AbstractIT {
@Test
public void createEmptyFile() {
RemoteOperationResult<String> result = new DirectEditingCreateFileRemoteOperation("/test.md",
"text",
"textdocument")
.execute(client);
.execute(nextcloudClient);
assertTrue(result.isSuccess());

String url = result.getResultData();
Expand All @@ -35,7 +35,7 @@ public void createFileFromTemplate() {
"text",
"textdocument",
"1")
.execute(client);
.execute(nextcloudClient);
assertTrue(result.isSuccess());

String url = result.getResultData();
Expand All @@ -49,7 +49,7 @@ public void createFileWithSpecialCharacterFromTemplate() {
"text",
"textdocument",
"1")
.execute(client);
.execute(nextcloudClient);
assertTrue(result.isSuccess());

String url = result.getResultData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@
*/
package com.nextcloud.android.lib.resources.directediting;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.TemplateList;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;

import org.junit.Test;

import java.util.Objects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class DirectEditingObtainListOfTemplatesRemoteOperationIT extends AbstractIT {

@Test
public void testGetAll() {
RemoteOperationResult result = new DirectEditingObtainListOfTemplatesRemoteOperation("text",
RemoteOperationResult<TemplateList> result = new DirectEditingObtainListOfTemplatesRemoteOperation("text",
"textdocument")
.execute(client);
.execute(nextcloudClient);
assertTrue(result.isSuccess());

TemplateList templateList = (TemplateList) result.getResultData();

assertEquals("Empty file", templateList.getTemplates().get("empty").getTitle());
assertEquals("md", templateList.getTemplates().get("empty").getExtension());
assertEquals("Empty file", Objects.requireNonNull(templateList.getTemplates().get("empty")).getTitle());
assertEquals("md", Objects.requireNonNull(templateList.getTemplates().get("empty")).getExtension());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.junit.Test
class DirectEditingObtainRemoteOperationIT : AbstractIT() {
@Test
fun testGetAll() {
val result = DirectEditingObtainRemoteOperation().execute(client)
val result = DirectEditingObtainRemoteOperation().run(nextcloudClient)
assertTrue(result.isSuccess)

val (editors, creators) = result.resultData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
*/
package com.nextcloud.android.lib.resources.directediting;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
Expand All @@ -21,6 +18,9 @@

import java.io.IOException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class DirectEditingOpenFileRemoteOperationIT extends AbstractIT {
@Test
public void openFile() throws IOException {
Expand All @@ -37,10 +37,11 @@ public void openFile() throws IOException {
TestCase.assertTrue(new ReadFileRemoteOperation(remotePath).execute(client).isSuccess());

// open file
RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(remotePath, "text").execute(client);
RemoteOperationResult<String> result = new DirectEditingOpenFileRemoteOperation(remotePath, "text")
.execute(nextcloudClient);
assertTrue(result.isSuccess());

String url = (String) result.getSingleData();
String url = result.getResultData();

assertFalse(url.isEmpty());
}
Expand All @@ -60,10 +61,11 @@ public void openFileWithSpecialChars() throws IOException {
TestCase.assertTrue(new ReadFileRemoteOperation(remotePath).execute(client).isSuccess());

// open file
RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(remotePath, "text").execute(client);
RemoteOperationResult<String> result = new DirectEditingOpenFileRemoteOperation(remotePath, "text")
.execute(nextcloudClient);
assertTrue(result.isSuccess());

String url = (String) result.getSingleData();
String url = result.getResultData();

assertFalse(url.isEmpty());
}
Expand All @@ -83,10 +85,11 @@ public void openFileWithSpecialChars2() throws IOException {
TestCase.assertTrue(new ReadFileRemoteOperation(remotePath).execute(client).isSuccess());

// open file
RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(remotePath, "text").execute(client);
RemoteOperationResult<String> result = new DirectEditingOpenFileRemoteOperation(remotePath, "text")
.execute(nextcloudClient);
assertTrue(result.isSuccess());

String url = (String) result.getSingleData();
String url = result.getResultData();

assertFalse(url.isEmpty());
}
Expand All @@ -98,7 +101,8 @@ public void openNonExistingFile() {
TestCase.assertFalse(new ReadFileRemoteOperation(remotePath).execute(client).isSuccess());

// open file
RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(remotePath, "text").execute(client);
RemoteOperationResult<String> result = new DirectEditingOpenFileRemoteOperation(remotePath, "text")
.execute(nextcloudClient);
assertFalse(result.isSuccess());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/
package com.nextcloud.android.lib.resources.directediting;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.nextcloud.common.JSONRequestBody;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.PostMethod;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.Utf8PostMethod;
import org.json.JSONObject;

/**
Expand All @@ -22,8 +22,6 @@

public class DirectEditingCreateFileRemoteOperation extends RemoteOperation<String> {
private static final String TAG = DirectEditingCreateFileRemoteOperation.class.getSimpleName();
private static final int SYNC_READ_TIMEOUT = 40000;
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/files/api/v1/directEditing/create";

private final String path;
Expand All @@ -45,45 +43,44 @@ public DirectEditingCreateFileRemoteOperation(String path, String editor, String
this(path, editor, creator, "");
}

protected RemoteOperationResult<String> run(OwnCloudClient client) {
public RemoteOperationResult<String> run(NextcloudClient client) {
RemoteOperationResult<String> result;
Utf8PostMethod postMethod = null;
PostMethod post = null;

try {
postMethod = new Utf8PostMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT);
postMethod.addParameter("path", path);
postMethod.addParameter("editorId", editor);
postMethod.addParameter("creatorId", creator);
// request body
JSONRequestBody jsonRequestBody = new JSONRequestBody("path", path);
jsonRequestBody.put("editorId", editor);
jsonRequestBody.put("creatorId", creator);

if (!template.isEmpty()) {
postMethod.addParameter("templateId", template);
jsonRequestBody.put("templateId", template);
}

// remote request
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
// post request
post = new PostMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT, true, jsonRequestBody.get());

int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
client.execute(post);

if (status == HttpStatus.SC_OK) {
String response = postMethod.getResponseBodyAsString();
if (post.isSuccess()) {
String response = post.getResponseBodyAsString();

// Parse the response
JSONObject respJSON = new JSONObject(response);
String url = (String) respJSON.getJSONObject("ocs").getJSONObject("data").get("url");

result = new RemoteOperationResult<>(true, postMethod);
result = new RemoteOperationResult<>(true, post);
result.setResultData(url);
} else {
result = new RemoteOperationResult<>(false, postMethod);
client.exhaustResponse(postMethod.getResponseBodyAsStream());
result = new RemoteOperationResult<>(false, post);
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Get all direct editing information failed: " + result.getLogMessage(),
result.getException());
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
if (post != null) {
post.releaseConnection();
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@
package com.nextcloud.android.lib.resources.directediting;

import com.google.gson.reflect.TypeToken;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.TemplateList;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.ocs.ServerResponse;
import com.owncloud.android.lib.resources.OCSRemoteOperation;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

/**
* Get templates for an editor
*/

public class DirectEditingObtainListOfTemplatesRemoteOperation extends OCSRemoteOperation<TemplateList> {
private static final String TAG = DirectEditingObtainListOfTemplatesRemoteOperation.class.getSimpleName();
private static final int SYNC_READ_TIMEOUT = 40000;
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/files/api/v1/directEditing/templates/";

private final String editor;
Expand All @@ -36,39 +32,36 @@ public DirectEditingObtainListOfTemplatesRemoteOperation(String editor, String t
this.template = template;
}

protected RemoteOperationResult<TemplateList> run(OwnCloudClient client) {
public RemoteOperationResult<TemplateList> run(NextcloudClient client) {
RemoteOperationResult<TemplateList> result;
GetMethod getMethod = null;
GetMethod get = null;

try {
getMethod = new GetMethod(client.getBaseUri() + DIRECT_ENDPOINT + editor + "/" + template + JSON_FORMAT);

// remote request
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
// get request
get = new GetMethod(client.getBaseUri() + DIRECT_ENDPOINT + editor + "/" + template + JSON_FORMAT, true);

int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
client.execute(get);

if (status == HttpStatus.SC_OK) {
ServerResponse<TemplateList> serverResponse = getServerResponse(getMethod, new TypeToken<>() {});
if (get.isSuccess()) {
ServerResponse<TemplateList> serverResponse = getServerResponse(get, new TypeToken<>() {});

if (serverResponse != null) {
TemplateList templateList = serverResponse.getOcs().getData();
result = new RemoteOperationResult<>(true, getMethod);
result = new RemoteOperationResult<>(true, get);
result.setResultData(templateList);
} else {
result = new RemoteOperationResult<>(false, getMethod);
result = new RemoteOperationResult<>(false, get);
}
} else {
result = new RemoteOperationResult<>(false, getMethod);
client.exhaustResponse(getMethod.getResponseBodyAsStream());
result = new RemoteOperationResult<>(false, get);
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Get all direct editing information failed: " + result.getLogMessage(),
result.getException());
} finally {
if (getMethod != null) {
getMethod.releaseConnection();
if (get != null) {
get.releaseConnection();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,32 @@
package com.nextcloud.android.lib.resources.directediting;

import com.google.gson.reflect.TypeToken;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.DirectEditing;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.ocs.ServerResponse;
import com.owncloud.android.lib.resources.OCSRemoteOperation;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

/**
* Get all editor details from direct editing
*/

public class DirectEditingObtainRemoteOperation extends OCSRemoteOperation<DirectEditing> {
private static final String TAG = DirectEditingObtainRemoteOperation.class.getSimpleName();
private static final int SYNC_READ_TIMEOUT = 40000;
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/files/api/v1/directEditing";

protected RemoteOperationResult<DirectEditing> run(OwnCloudClient client) {
public RemoteOperationResult<DirectEditing> run(NextcloudClient client) {
RemoteOperationResult<DirectEditing> result;
GetMethod getMethod = null;

try {
getMethod = new GetMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT);

// remote request
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
getMethod = new GetMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT, true);

int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
client.execute(getMethod);

if (status == HttpStatus.SC_OK) {
if (getMethod.isSuccess()) {
ServerResponse<DirectEditing> serverResponse = getServerResponse(getMethod,
new TypeToken<>() {
});
Expand All @@ -55,11 +48,10 @@ protected RemoteOperationResult<DirectEditing> run(OwnCloudClient client) {

} else {
result = new RemoteOperationResult<>(false, getMethod);
client.exhaustResponse(getMethod.getResponseBodyAsStream());
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Get all direct editing informations failed: " + result.getLogMessage(),
Log_OC.e(TAG, "Get all direct editing information failed: " + result.getLogMessage(),
result.getException());
} finally {
if (getMethod != null) {
Expand Down
Loading
Loading