Skip to content
Open
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 @@ -8,18 +8,17 @@
package com.owncloud.android.lib.common.accounts

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.ExternalLink
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import org.junit.Test

class ExternalLinksOperationIT : AbstractIT() {
@Test
fun retrieveExternalLinks() {
val result = ExternalLinksOperation().execute(client)
val result = ExternalLinksOperation().execute(nextcloudClient)
assertTrue(result.isSuccess)

val data = result.data as ArrayList<ExternalLink>
val data = result.resultData
assertEquals(2, data.size)

assertEquals("Nextcloud", data[0].name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
*/
package com.owncloud.android.lib.common.accounts;

import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.ExternalLink;
import com.owncloud.android.lib.common.ExternalLinkType;
import com.owncloud.android.lib.common.OwnCloudClient;
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 com.owncloud.android.lib.resources.status.GetCapabilitiesRemoteOperation;
import com.owncloud.android.lib.resources.status.OCCapability;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
* gets external links provided by 'external' app
*/

public class ExternalLinksOperation extends RemoteOperation {
public class ExternalLinksOperation extends RemoteOperation<List<ExternalLink>> {

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

Expand All @@ -48,24 +48,21 @@ public class ExternalLinksOperation extends RemoteOperation {


@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
public RemoteOperationResult<List<ExternalLink>> run(NextcloudClient client) {
RemoteOperationResult<List<ExternalLink>> result = null;
int status = -1;
GetMethod get = null;
String ocsUrl = client.getBaseUri() + OCS_ROUTE_EXTERNAL_LINKS;
String ocsUrl = client.getBaseUri() + OCS_ROUTE_EXTERNAL_LINKS + JSON_FORMAT;

try {
// check capabilities
RemoteOperation getCapabilities = new GetCapabilitiesRemoteOperation();
RemoteOperationResult capabilitiesResult = getCapabilities.execute(client);
RemoteOperationResult capabilitiesResult = new GetCapabilitiesRemoteOperation().execute(client);
OCCapability capability = (OCCapability) capabilitiesResult.getData().get(0);

if (capability.getExternalLinks().isTrue()) {

get = new GetMethod(ocsUrl);
get = new GetMethod(ocsUrl, true);
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
get.setQueryString(new NameValuePair[]{new NameValuePair("format", "json")});
status = client.executeMethod(get);
status = client.execute(get);

if (isSuccess(status)) {
String response = get.getResponseBodyAsString();
Expand All @@ -74,34 +71,25 @@ protected RemoteOperationResult run(OwnCloudClient client) {
// parse
JSONArray links = new JSONObject(response).getJSONObject(NODE_OCS).getJSONArray(NODE_DATA);

ArrayList<Object> resultLinks = new ArrayList<>();
ArrayList<ExternalLink> resultLinks = new ArrayList<>();

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

if (link != null) {
Integer id = link.getInt(NODE_ID);
int id = link.getInt(NODE_ID);
String iconUrl = link.getString(NODE_ICON);
String language = "";
if (link.has(NODE_LANGUAGE)) {
language = link.getString(NODE_LANGUAGE);
}

ExternalLinkType type;
switch (link.getString(NODE_TYPE)) {
case "link":
type = ExternalLinkType.LINK;
break;
case "settings":
type = ExternalLinkType.SETTINGS;
break;
case "quota":
type = ExternalLinkType.QUOTA;
break;
default:
type = ExternalLinkType.UNKNOWN;
break;
}
ExternalLinkType type = switch (link.getString(NODE_TYPE)) {
case "link" -> ExternalLinkType.LINK;
case "settings" -> ExternalLinkType.SETTINGS;
case "quota" -> ExternalLinkType.QUOTA;
default -> ExternalLinkType.UNKNOWN;
};


String name = link.getString(NODE_NAME);
Expand All @@ -117,11 +105,11 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}
}

result = new RemoteOperationResult(true, status, get.getResponseHeaders());
result.setData(resultLinks);
result = new RemoteOperationResult<>(true, get);
result.setResultData(resultLinks);

} else {
result = new RemoteOperationResult(false, status, get.getResponseHeaders());
result = new RemoteOperationResult<>(false, get);
String response = get.getResponseBodyAsString();
Log_OC.e(TAG, "Failed response while getting external links ");
if (response != null) {
Expand All @@ -131,12 +119,12 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}
}
} else {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.NOT_AVAILABLE);
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.NOT_AVAILABLE);
Log_OC.d(TAG, "External links disabled");
}

} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while getting external links ", e);
} finally {
if (get != null) {
Expand Down
Loading