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

Commit 0e9fffbf authored by narinder Rana's avatar narinder Rana
Browse files

resolved RemoteOperationResult missing classes

parent 6a6b61e1
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.annotation:annotation:1.3.0'


    // for NC Lib
    api 'org.apache.jackrabbit:jackrabbit-webdav:2.12.6+'
    api 'com.squareup.moshi:moshi:1.8.0'
}
+2 −1
Original line number Diff line number Diff line
@@ -23,13 +23,14 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
//import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import java.io.File;
import java.util.ArrayList;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFileState;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.ncLib.resources.users.GetRemoteUserInfoOperation;

/**
 * @author Vincent Bourgmayer
+142 −0
Original line number Diff line number Diff line
package foundation.e.drive.utils.ncLib.resources.users;
import android.text.TextUtils;

import com.nextcloud.android.lib.resources.profile.HoverCard;
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.Quota;
import com.owncloud.android.lib.common.UserInfo;
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 com.owncloud.android.lib.resources.status.OwnCloudVersion;
import com.squareup.moshi.Types;

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

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;

public class GetRemoteUserInfoOperation extends OCSRemoteOperation {

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

    // OCS Route
    private static final String OCS_ROUTE_SELF = "/ocs/v1.php/cloud/user";
    private static final String OCS_ROUTE_SEARCH = "/ocs/v1.php/cloud/users/";

    /**
     * Quota return value for a not computed space value.
     */
    public static final long SPACE_NOT_COMPUTED = -1;

    /**
     * Quota return value for unknown space value.
     */
    public static final long SPACE_UNKNOWN = -2;

    /**
     * Quota return value for unlimited space.
     */
    public static final long SPACE_UNLIMITED = -3;

    /**
     * Quota return value for quota information not available.
     */
    public static final long QUOTA_LIMIT_INFO_NOT_AVAILABLE = Long.MIN_VALUE;

    private String userID;

    public GetRemoteUserInfoOperation() {
    }

    public GetRemoteUserInfoOperation(String userID) {
        this.userID = userID;
    }

    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result = null;
        int status = -1;
        GetMethod get = null;

//        OwnCloudVersion version = client.getOwnCloudVersion();
//        boolean versionWithSelfAPI = version != null && version.isSelfSupported();

        String url = "";

        if (!TextUtils.isEmpty(userID)) {
            url = client.getBaseUri() + OCS_ROUTE_SEARCH + userID;
        } else {
            url = client.getBaseUri() + OCS_ROUTE_SELF;
        }


        OwnCloudBasicCredentials credentials = (OwnCloudBasicCredentials) client.getCredentials();

        //Get the user
        try {

            get = new GetMethod(url);
            get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
            get.setQueryString(new NameValuePair[]{new NameValuePair("format", "json")});
            status = client.executeMethod(get);
//            val hoverCard: HoverCard = getServerResponse<ServerResponse<HoverCard>>(
//                    getMethod,
//                    object : TypeToken<ServerResponse<HoverCard>?>() {}
//                )
            if (isSuccess(status)) {
                ServerResponse<UserInfo> ocsResponse = (ServerResponse<UserInfo>) getServerResponse(get, Types.newParameterizedType(ServerResponse.class, UserInfo.class));

                UserInfo userInfo = ocsResponse.getOcs().getData();

                if (userInfo.getId() == null) {
                    if (TextUtils.isEmpty(userID))
                        userInfo.setId(credentials.getUsername());
                    else
                        userInfo.setId(userID);
                }

                if (userInfo.getQuota() == null) {
                    userInfo.setQuota(new Quota());
                    userInfo.getQuota().setQuota(QUOTA_LIMIT_INFO_NOT_AVAILABLE);
                }

                if (userInfo.getQuota().getQuota() == 0) {
                    userInfo.getQuota().setQuota(QUOTA_LIMIT_INFO_NOT_AVAILABLE);
                }

                result = new RemoteOperationResult(true, get);
                // Username in result.data
                ArrayList<Object> data = new ArrayList<>();
                data.add(userInfo);
                result.setData(data);
            } else {
                result = new RemoteOperationResult(false, get);
                String response = get.getResponseBodyAsString();
                Log_OC.e(TAG, "Failed response while getting user information ");
                if (response != null) {
                    Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
                } else {
                    Log_OC.e(TAG, "*** status code: " + status);
                }
            }
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting OC user information", e);
        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }
        return result;
    }


    private boolean isSuccess(int status) {
        return (status == HttpStatus.SC_OK);
    }
}
 No newline at end of file
+59 −0
Original line number Diff line number Diff line
/* Nextcloud Android Library is available under MIT license
 *
 *   @author Bartosz Przybylski
 *   Copyright (C) 2018 Bartosz Przybylski
 *   Copyright (C) 2018 Nextcloud GmbH
 *   Copyright (C) 2018 Vincent Bourgmayer (/e/ foundation)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 */

package foundation.e.drive.utils.ncLib.resources.users;

import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.ocs.ServerResponse;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

import org.apache.commons.httpclient.HttpMethodBase;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 *
 * Base class for OCS remote operations with convenient methods
 *
 * @author Bartosz Przybylski
 */
public abstract class OCSRemoteOperation extends RemoteOperation {

    public ServerResponse<UserInfo> getServerResponse(HttpMethodBase method, Type type) throws IOException {
        String response = method.getResponseBodyAsString();

        Moshi moshi = new Moshi.Builder().build();
        JsonAdapter<ServerResponse<UserInfo>> jsonAdapter = moshi.adapter(type);

        return jsonAdapter.fromJson(response);

    }
}