Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3dc3e27 authored by Mario Đanić's avatar Mario Đanić Committed by GitHub
Browse files

Merge pull request #57 from nextcloud/externalLinks

External links
parents 57f13ba5 41a44e80
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
/**
 * Nextcloud Android client application
 *
 * @author Tobias Kaminsky
 * Copyright (C) 2017 Nextcloud GmbH.
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.owncloud.android.lib.common;

import org.parceler.Parcel;

/**
 * Quota data model
 */

@Parcel
public class ExternalLink {
    public Integer id;
    public String iconUrl;
    public String language;
    public ExternalLinkType type;
    public String name;
    public String url;

    public ExternalLink() {

    }

    public ExternalLink(Integer id, String iconUrl, String language, ExternalLinkType type, String name, String url) {
        this.id = id;
        this.iconUrl = iconUrl;
        this.language = language;
        this.type = type;
        this.name = name;
        this.url = url;
    }
}
+5 −0
Original line number Diff line number Diff line
package com.owncloud.android.lib.common;

public enum ExternalLinkType {
    LINK, SETTINGS, QUOTA, UNKNOWN
}
+156 −0
Original line number Diff line number Diff line
/**
 * Nextcloud Android client application
 *
 * @author Tobias Kaminsky
 * Copyright (C) 2017 Nextcloud GmbH.
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.owncloud.android.lib.common.accounts;

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.GetRemoteCapabilitiesOperation;
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;

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

public class ExternalLinksOperation extends RemoteOperation {

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

    // OCS Route
    private static final String OCS_ROUTE_EXTERNAL_LINKS = "/ocs/v2.php/apps/external/api/v1";

    // JSON Node names
    private static final String NODE_OCS = "ocs";
    private static final String NODE_DATA = "data";
    private static final String NODE_ID = "id";
    private static final String NODE_ICON = "icon";
    private static final String NODE_LANGUAGE = "lang";
    private static final String NODE_TYPE = "type";
    private static final String NODE_NAME = "name";
    private static final String NODE_URL = "url";


    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result = null;
        int status = -1;
        GetMethod get = null;
        String ocsUrl = client.getBaseUri() + OCS_ROUTE_EXTERNAL_LINKS;

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

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

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

                if (isSuccess(status)) {
                    String response = get.getResponseBodyAsString();
                    Log_OC.d(TAG, "Successful response: " + response);

                    // parse
                    JSONArray links = new JSONObject(response).getJSONObject(NODE_OCS).getJSONArray(NODE_DATA);

                    ArrayList<Object> 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);
                            String iconUrl = link.getString(NODE_ICON);
                            String 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;
                            }


                            String name = link.getString(NODE_NAME);
                            String url = link.getString(NODE_URL);

                            resultLinks.add(new ExternalLink(id, iconUrl, language, type, name, url));
                        }
                    }

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

                } else {
                    result = new RemoteOperationResult(false, status, get.getResponseHeaders());
                    String response = get.getResponseBodyAsString();
                    Log_OC.e(TAG, "Failed response while getting external links ");
                    if (response != null) {
                        Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
                    } else {
                        Log_OC.e(TAG, "*** status code: " + status);
                    }
                }
            } else {
                result = new RemoteOperationResult(RemoteOperationResult.ResultCode.NOT_AVAILABLE);
                Log_OC.d(TAG, "External links disabled");
            }

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

        return result;
    }

    private boolean isSuccess(int status) {
        return (status == HttpStatus.SC_OK);
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@ public class RemoteOperationResult implements Serializable {
        INVALID_CHARACTER_DETECT_IN_SERVER,
        DELAYED_FOR_WIFI,
        DELAYED_FOR_CHARGING,
        LOCAL_FILE_NOT_FOUND
        LOCAL_FILE_NOT_FOUND,
        NOT_AVAILABLE
    }

    private boolean mSuccess = false;
+21 −0
Original line number Diff line number Diff line
@@ -121,6 +121,11 @@ public class GetRemoteCapabilitiesOperation extends RemoteOperation {
    private static final String PROPERTY_ICONS = "icons";
    private static final String PROPERTY_RICH_STRINGS = "rich-strings";

    // v1 external
    private static final String NODE_EXTERNAL_LINKS = "external";
    private static final String NODE_EXTERNAL_LINKS_V1 = "v1";
    private static final String NODE_EXTERNAL_LINKS_SITES = "sites";

    /**
     * Constructor
     *
@@ -292,6 +297,22 @@ public class GetRemoteCapabilitiesOperation extends RemoteOperation {
                            }
                        }

                        if (respCapabilities.has(NODE_EXTERNAL_LINKS)) {
                            JSONObject respExternalLinks = respCapabilities.getJSONObject(NODE_EXTERNAL_LINKS);

                            if (respExternalLinks.has(NODE_EXTERNAL_LINKS_V1)) {
                                JSONArray respExternalLinksV1 = respExternalLinks.getJSONArray(NODE_EXTERNAL_LINKS_V1);

                                String element = (String) respExternalLinksV1.get(0);

                                if (element.equalsIgnoreCase(NODE_EXTERNAL_LINKS_SITES)) {
                                    capability.setExternalLinks(CapabilityBooleanType.TRUE);
                                } else {
                                    capability.setExternalLinks(CapabilityBooleanType.FALSE);
                                }
                            }
                        }

                    }
                    // Result
                    data.add(capability);
Loading