mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +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; |         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 |      * @param rawEtag | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* 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 |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   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. |  * The result of a remote operation required to an ownCloud server. | ||||||
|  * <p/> |  * | ||||||
|  * Provides a common classification of remote operation results for all the |  * Provides a common classification of remote operation results for all the | ||||||
|  * application. |  * application. | ||||||
|  * |  * | ||||||
|  * @author David A. Velasco |  * @author David A. Velasco | ||||||
|  |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| public class RemoteOperationResult implements Serializable { | public class RemoteOperationResult implements Serializable { | ||||||
| 
 | 
 | ||||||
| @ -378,8 +379,8 @@ public class RemoteOperationResult implements Serializable { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setData(ArrayList<Object> files) { |     public void setData(ArrayList<Object> items) { | ||||||
|         mData = files; |         mData = items; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public ArrayList<Object> getData() { |     public ArrayList<Object> getData() { | ||||||
|  | |||||||
| @ -150,7 +150,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | |||||||
|             remoteFile = fillOCFile(we); |             remoteFile = fillOCFile(we); | ||||||
|             mFolderAndFiles.add(remoteFile); |             mFolderAndFiles.add(remoteFile); | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* 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) 2015 Bartosz Przybylski | ||||||
|  *   Copyright (C) 2014 Marcello Steiner |  *   Copyright (C) 2014 Marcello Steiner | ||||||
|  * |  * | ||||||
| @ -28,20 +28,23 @@ | |||||||
| package com.owncloud.android.lib.resources.users; | package com.owncloud.android.lib.resources.users; | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | 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.RemoteOperation; | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
| 
 | 
 | ||||||
| import org.apache.commons.httpclient.HttpStatus; | import org.apache.commons.httpclient.HttpStatus; | ||||||
| import org.apache.commons.httpclient.NameValuePair; | import org.apache.jackrabbit.webdav.DavConstants; | ||||||
| import org.apache.commons.httpclient.methods.GetMethod; | import org.apache.jackrabbit.webdav.MultiStatus; | ||||||
| import org.json.JSONObject; | import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; | ||||||
| 
 | 
 | ||||||
|  | import java.math.BigDecimal; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @author marcello |  * @author marcello | ||||||
|  |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| public class GetRemoteUserQuotaOperation extends RemoteOperation { | 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 TAG = GetRemoteUserQuotaOperation.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
|     private static final String NODE_OCS = "ocs"; |     private String mRemotePath; | ||||||
|     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"; |  | ||||||
| 
 | 
 | ||||||
|     // 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 |     @Override | ||||||
|     protected RemoteOperationResult run(OwnCloudClient client) { |     protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
|         RemoteOperationResult result = null; |         RemoteOperationResult result = null; | ||||||
|         int status; |         PropFindMethod query = null; | ||||||
|         GetMethod get = null; |  | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|         //Get the user |  | ||||||
|         try { |         try { | ||||||
|             OwnCloudCredentials credentials = client.getCredentials(); |             // remote request | ||||||
|             String url = client.getBaseUri() + OCS_ROUTE + credentials.getUsername(); |             query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), | ||||||
|  |                     WebdavUtils.getQuotaPropSet(), | ||||||
|  |                     DavConstants.DEPTH_0); | ||||||
| 
 | 
 | ||||||
|             get = new GetMethod(url); |             int status = client.executeMethod(query); | ||||||
|             get.setQueryString(new NameValuePair[]{new NameValuePair("format","json")}); |  | ||||||
|             get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); |  | ||||||
|             status = client.executeMethod(get); |  | ||||||
| 
 | 
 | ||||||
|             if(isSuccess(status)) { |             if (isSuccess(status)) { | ||||||
|                 String response = get.getResponseBodyAsString(); |                 // get data from remote folder | ||||||
|  |                 MultiStatus dataInServer = query.getResponseBodyAsMultiStatus(); | ||||||
|  |                 Quota quota = readData(dataInServer, client); | ||||||
| 
 | 
 | ||||||
|                 // Parse the response |                 // Result of the operation | ||||||
|                 JSONObject respJSON = new JSONObject(response); |                 result = new RemoteOperationResult(true, query); | ||||||
|                 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); |  | ||||||
| 
 | 
 | ||||||
|  |                 ArrayList<Object> data = new ArrayList<>(); | ||||||
|  |                 data.add(quota); | ||||||
| 
 | 
 | ||||||
|                 // Result |                 // Add data to the result | ||||||
|                 result = new RemoteOperationResult(true, get); |                 if (result.isSuccess()) { | ||||||
|                 //Quota data in data collection |  | ||||||
|                 ArrayList<Object> data = new ArrayList<Object>(); |  | ||||||
|                 data.add(new Quota(quotaFree, quotaUsed, quotaTotal, quotaRelative)); |  | ||||||
|                     result.setData(data); |                     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) { |         } catch (Exception e) { | ||||||
|             result = new RemoteOperationResult(e); |             result = new RemoteOperationResult(e); | ||||||
|             Log_OC.e(TAG, "Exception while getting OC user information", e); | 
 | ||||||
| 
 | 
 | ||||||
|         } finally { |         } finally { | ||||||
|             if (get != null) { |             if (query != null) | ||||||
|                 get.releaseConnection(); |                 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) { |     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