mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-11-04 12:28:25 +00:00 
			
		
		
		
	Update operation to get remote user quota
This commit is contained in:
		
							parent
							
								
									ad0adaaf5c
								
							
						
					
					
						commit
						12429b7ab9
					
				@ -140,6 +140,17 @@ public class WebdavUtils {
 | 
			
		||||
        return propSet;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Builds a DavPropertyNameSet with properties for user quotas
 | 
			
		||||
     * @return set of quota properties
 | 
			
		||||
     */
 | 
			
		||||
    public static DavPropertyNameSet getQuotaPropSet() {
 | 
			
		||||
        DavPropertyNameSet propSet = new DavPropertyNameSet();
 | 
			
		||||
        propSet.add(DavPropertyName.create(WebdavEntry.PROPERTY_QUOTA_AVAILABLE_BYTES));
 | 
			
		||||
        propSet.add(DavPropertyName.create(WebdavEntry.PROPERTY_QUOTA_USED_BYTES));
 | 
			
		||||
        return propSet;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @param rawEtag
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
/* ownCloud Android Library is available under MIT license
 | 
			
		||||
 *   Copyright (C) 2016 ownCloud GmbH.
 | 
			
		||||
 *   Copyright (C) 2018 ownCloud GmbH.
 | 
			
		||||
 *
 | 
			
		||||
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 *   of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
@ -55,11 +55,12 @@ import javax.net.ssl.SSLException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The result of a remote operation required to an ownCloud server.
 | 
			
		||||
 * <p/>
 | 
			
		||||
 *
 | 
			
		||||
 * Provides a common classification of remote operation results for all the
 | 
			
		||||
 * application.
 | 
			
		||||
 *
 | 
			
		||||
 * @author David A. Velasco
 | 
			
		||||
 * @author David González Verdugo
 | 
			
		||||
 */
 | 
			
		||||
public class RemoteOperationResult implements Serializable {
 | 
			
		||||
 | 
			
		||||
@ -378,8 +379,8 @@ public class RemoteOperationResult implements Serializable {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setData(ArrayList<Object> files) {
 | 
			
		||||
        mData = files;
 | 
			
		||||
    public void setData(ArrayList<Object> items) {
 | 
			
		||||
        mData = items;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ArrayList<Object> getData() {
 | 
			
		||||
 | 
			
		||||
@ -150,7 +150,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
 | 
			
		||||
            remoteFile = fillOCFile(we);
 | 
			
		||||
            mFolderAndFiles.add(remoteFile);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
/* ownCloud Android Library is available under MIT license
 | 
			
		||||
 *
 | 
			
		||||
 *   Copyright (C) 2015 ownCloud Inc.
 | 
			
		||||
 *   Copyright (C) 2018 ownCloud Inc.
 | 
			
		||||
 *   Copyright (C) 2015 Bartosz Przybylski
 | 
			
		||||
 *   Copyright (C) 2014 Marcello Steiner
 | 
			
		||||
 *
 | 
			
		||||
@ -28,20 +28,23 @@
 | 
			
		||||
package com.owncloud.android.lib.resources.users;
 | 
			
		||||
 | 
			
		||||
import com.owncloud.android.lib.common.OwnCloudClient;
 | 
			
		||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
 | 
			
		||||
import com.owncloud.android.lib.common.network.WebdavEntry;
 | 
			
		||||
import com.owncloud.android.lib.common.network.WebdavUtils;
 | 
			
		||||
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.NameValuePair;
 | 
			
		||||
import org.apache.commons.httpclient.methods.GetMethod;
 | 
			
		||||
import org.json.JSONObject;
 | 
			
		||||
import org.apache.jackrabbit.webdav.DavConstants;
 | 
			
		||||
import org.apache.jackrabbit.webdav.MultiStatus;
 | 
			
		||||
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
 | 
			
		||||
 | 
			
		||||
import java.math.BigDecimal;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author marcello
 | 
			
		||||
 * @author David González Verdugo
 | 
			
		||||
 */
 | 
			
		||||
public class GetRemoteUserQuotaOperation extends RemoteOperation {
 | 
			
		||||
 | 
			
		||||
@ -64,72 +67,66 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation {
 | 
			
		||||
 | 
			
		||||
    private static final String TAG = GetRemoteUserQuotaOperation.class.getSimpleName();
 | 
			
		||||
 | 
			
		||||
    private static final String NODE_OCS = "ocs";
 | 
			
		||||
    private static final String NODE_DATA = "data";
 | 
			
		||||
    private static final String NODE_QUOTA = "quota";
 | 
			
		||||
    private static final String NODE_QUOTA_FREE = "free";
 | 
			
		||||
    private static final String NODE_QUOTA_USED = "used";
 | 
			
		||||
    private static final String NODE_QUOTA_TOTAL = "total";
 | 
			
		||||
    private static final String NODE_QUOTA_RELATIVE = "relative";
 | 
			
		||||
    private String mRemotePath;
 | 
			
		||||
 | 
			
		||||
    // OCS Route
 | 
			
		||||
    private static final String OCS_ROUTE ="/ocs/v1.php/cloud/users/";
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructor
 | 
			
		||||
     *
 | 
			
		||||
     * @param remotePath Remote path of the file.
 | 
			
		||||
     */
 | 
			
		||||
    public GetRemoteUserQuotaOperation(String remotePath) {
 | 
			
		||||
        mRemotePath = remotePath;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected RemoteOperationResult run(OwnCloudClient client) {
 | 
			
		||||
        RemoteOperationResult result = null;
 | 
			
		||||
        int status;
 | 
			
		||||
        GetMethod get = null;
 | 
			
		||||
        PropFindMethod query = null;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        //Get the user
 | 
			
		||||
        try {
 | 
			
		||||
            OwnCloudCredentials credentials = client.getCredentials();
 | 
			
		||||
            String url = client.getBaseUri() + OCS_ROUTE + credentials.getUsername();
 | 
			
		||||
            // remote request
 | 
			
		||||
            query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
 | 
			
		||||
                    WebdavUtils.getQuotaPropSet(),
 | 
			
		||||
                    DavConstants.DEPTH_0);
 | 
			
		||||
 | 
			
		||||
            get = new GetMethod(url);
 | 
			
		||||
            get.setQueryString(new NameValuePair[]{new NameValuePair("format","json")});
 | 
			
		||||
            get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
 | 
			
		||||
            status = client.executeMethod(get);
 | 
			
		||||
            int status = client.executeMethod(query);
 | 
			
		||||
 | 
			
		||||
            if(isSuccess(status)) {
 | 
			
		||||
                String response = get.getResponseBodyAsString();
 | 
			
		||||
            if (isSuccess(status)) {
 | 
			
		||||
                // get data from remote folder
 | 
			
		||||
                MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
 | 
			
		||||
                Quota quota = readData(dataInServer, client);
 | 
			
		||||
 | 
			
		||||
                // Parse the response
 | 
			
		||||
                JSONObject respJSON = new JSONObject(response);
 | 
			
		||||
                JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
 | 
			
		||||
                JSONObject respData = respOCS.getJSONObject(NODE_DATA);
 | 
			
		||||
                JSONObject quota    = respData.getJSONObject(NODE_QUOTA);
 | 
			
		||||
                final Long quotaFree = quota.getLong(NODE_QUOTA_FREE);
 | 
			
		||||
                final Long quotaUsed = quota.getLong(NODE_QUOTA_USED);
 | 
			
		||||
                final Long quotaTotal = quota.getLong(NODE_QUOTA_TOTAL);
 | 
			
		||||
                final Double quotaRelative = quota.getDouble(NODE_QUOTA_RELATIVE);
 | 
			
		||||
                // Result of the operation
 | 
			
		||||
                result = new RemoteOperationResult(true, query);
 | 
			
		||||
 | 
			
		||||
                ArrayList<Object> data = new ArrayList<>();
 | 
			
		||||
                data.add(quota);
 | 
			
		||||
 | 
			
		||||
                // Result
 | 
			
		||||
                result = new RemoteOperationResult(true, get);
 | 
			
		||||
                //Quota data in data collection
 | 
			
		||||
                ArrayList<Object> data = new ArrayList<Object>();
 | 
			
		||||
                data.add(new Quota(quotaFree, quotaUsed, quotaTotal, quotaRelative));
 | 
			
		||||
                // Add data to the result
 | 
			
		||||
                if (result.isSuccess()) {
 | 
			
		||||
                    result.setData(data);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                // synchronization failed
 | 
			
		||||
                result = new RemoteOperationResult(false, query);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            } else {
 | 
			
		||||
                result = new RemoteOperationResult(false, get);
 | 
			
		||||
                String response = get.getResponseBodyAsString();
 | 
			
		||||
                Log_OC.e(TAG, "Failed response while getting user quota 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();
 | 
			
		||||
            if (query != null)
 | 
			
		||||
                query.releaseConnection();  // let the connection available for other methods
 | 
			
		||||
            if (result.isSuccess()) {
 | 
			
		||||
                Log_OC.i(TAG, "Get quota from " + mRemotePath + ": " + result.getLogMessage());
 | 
			
		||||
            } else {
 | 
			
		||||
                if (result.isException()) {
 | 
			
		||||
                    Log_OC.e(TAG, "Get quota from " + mRemotePath + ": " + result.getLogMessage(),
 | 
			
		||||
                            result.getException());
 | 
			
		||||
                } else {
 | 
			
		||||
                    Log_OC.e(TAG, "Get quota from " + mRemotePath + ": " + result.getLogMessage());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -137,8 +134,32 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean isSuccess(int status) {
 | 
			
		||||
        return (status == HttpStatus.SC_OK);
 | 
			
		||||
        return status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Read the data retrieved from the server about the quota
 | 
			
		||||
     *
 | 
			
		||||
     * @param remoteData Full response got from the server with the data of the quota
 | 
			
		||||
     * @param client     Client instance to the remote server where the data were retrieved
 | 
			
		||||
     * @return new Quota instance representing the data read from the server
 | 
			
		||||
     */
 | 
			
		||||
    private Quota readData(MultiStatus remoteData, OwnCloudClient client) {
 | 
			
		||||
 | 
			
		||||
        // parse data from remote folder
 | 
			
		||||
        WebdavEntry we = new WebdavEntry(remoteData.getResponses()[0], client.getWebdavUri().getPath());
 | 
			
		||||
 | 
			
		||||
        BigDecimal totalQuota = we.quotaAvailableBytes().add(we.quotaUsedBytes());
 | 
			
		||||
 | 
			
		||||
        BigDecimal relativeQuota = we.quotaUsedBytes().multiply(new BigDecimal(100)).divide(totalQuota);
 | 
			
		||||
 | 
			
		||||
        Quota quota = new Quota(
 | 
			
		||||
                we.quotaAvailableBytes().longValue(),
 | 
			
		||||
                we.quotaUsedBytes().longValue(),
 | 
			
		||||
                totalQuota.longValue(),
 | 
			
		||||
                relativeQuota.doubleValue()
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return quota;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user