Skip to content

Commit 618b3ec

Browse files
committed
improve get user info
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 7188e2c commit 618b3ec

1 file changed

Lines changed: 75 additions & 90 deletions

File tree

library/src/main/java/com/owncloud/android/lib/resources/users/GetUserInfoRemoteOperation.kt

Lines changed: 75 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,99 @@
22
* Nextcloud Android Library
33
*
44
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
56
* SPDX-FileCopyrightText: 2017 Tobias Kaminsky <tobias@kaminsky.me>
67
* SPDX-License-Identifier: MIT
78
*/
8-
package com.owncloud.android.lib.resources.users;
9-
10-
import com.google.gson.reflect.TypeToken;
11-
import com.nextcloud.common.NextcloudClient;
12-
import com.nextcloud.operations.GetMethod;
13-
import com.owncloud.android.lib.common.Quota;
14-
import com.owncloud.android.lib.common.UserInfo;
15-
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
16-
import com.owncloud.android.lib.common.utils.Log_OC;
17-
import com.owncloud.android.lib.ocs.ServerResponse;
18-
import com.owncloud.android.lib.resources.OCSRemoteOperation;
19-
20-
import org.apache.commons.httpclient.HttpStatus;
21-
22-
import java.util.HashMap;
9+
package com.owncloud.android.lib.resources.users
10+
11+
import com.google.gson.reflect.TypeToken
12+
import com.nextcloud.common.NextcloudClient
13+
import com.nextcloud.operations.GetMethod
14+
import com.owncloud.android.lib.common.Quota
15+
import com.owncloud.android.lib.common.UserInfo
16+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
17+
import com.owncloud.android.lib.common.utils.Log_OC
18+
import com.owncloud.android.lib.ocs.ServerResponse
19+
import com.owncloud.android.lib.resources.OCSRemoteOperation
20+
import org.apache.commons.httpclient.HttpStatus
2321

2422
/**
25-
* Gets information (id, display name, and e-mail address and many other things) about the user logged in.
26-
*
27-
* @author masensio
28-
* @author David A. Velasco
29-
* @author Mario Danic
23+
* Gets information (id, display name, e-mail address and many other things) about the user logged in.
3024
*/
31-
public class GetUserInfoRemoteOperation extends OCSRemoteOperation<UserInfo> {
32-
33-
private static final String TAG = GetUserInfoRemoteOperation.class.getSimpleName();
34-
35-
// OCS Route
36-
private static final String OCS_ROUTE_SELF = "/ocs/v2.php/cloud/user";
25+
class GetUserInfoRemoteOperation : OCSRemoteOperation<UserInfo>() {
26+
@Suppress("TooGenericExceptionCaught")
27+
override fun run(client: NextcloudClient): RemoteOperationResult<UserInfo> {
28+
val getMethod = GetMethod(client.baseUri.toString() + OCS_ROUTE_SELF + JSON_FORMAT, true)
29+
getMethod.addRequestHeader(CONTENT_TYPE, JSON_UTF8_ENCODED)
30+
31+
return try {
32+
val status = client.execute(getMethod)
33+
if (status != HttpStatus.SC_OK) {
34+
return failure(getMethod, status)
35+
}
3736

38-
/**
39-
* Quota return value for a not computed space value.
40-
*/
41-
public static final long SPACE_NOT_COMPUTED = -1;
37+
parseUserInfo(getMethod)?.let { success(getMethod, it) } ?: missingUserInfoFailure(getMethod)
38+
} catch (e: Exception) {
39+
failure(e)
40+
} finally {
41+
getMethod.releaseConnection()
42+
}
43+
}
4244

43-
/**
44-
* Quota return value for unknown space value.
45-
*/
46-
public static final long SPACE_UNKNOWN = -2;
45+
private fun parseUserInfo(method: GetMethod): UserInfo? =
46+
getServerResponse(method, object : TypeToken<ServerResponse<UserInfo>>() {})?.ocs?.data
4747

48-
/**
49-
* Quota return value for unlimited space.
50-
*/
51-
public static final long SPACE_UNLIMITED = -3;
48+
private fun success(
49+
method: GetMethod,
50+
userInfo: UserInfo
51+
): RemoteOperationResult<UserInfo> =
52+
RemoteOperationResult<UserInfo>(true, method).apply {
53+
resultData = userInfo.withResolvedQuota()
54+
}
5255

5356
/**
54-
* Quota return value for quota information not available.
57+
* A missing quota, or a quota of 0, means the server did not report one — it is not an actual limit of zero.
5558
*/
56-
public static final long QUOTA_LIMIT_INFO_NOT_AVAILABLE = Long.MIN_VALUE;
57-
58-
@Override
59-
public RemoteOperationResult<UserInfo> run(NextcloudClient client) {
60-
RemoteOperationResult<UserInfo> result;
61-
int status;
62-
GetMethod get = null;
63-
64-
String url = client.getBaseUri() + OCS_ROUTE_SELF;
65-
66-
// get the user
67-
try {
59+
private fun UserInfo.withResolvedQuota(): UserInfo =
60+
copy(quota = quota?.takeIf { it.quota != 0L } ?: Quota(QUOTA_LIMIT_INFO_NOT_AVAILABLE))
61+
62+
private fun failure(
63+
method: GetMethod,
64+
status: Int
65+
): RemoteOperationResult<UserInfo> {
66+
val response = method.getResponseBodyAsString()
67+
val message = if (response.isEmpty()) "" else "; response message: $response"
68+
Log_OC.e(TAG, "Failed response while getting user information, status code: $status$message")
69+
return RemoteOperationResult(false, method)
70+
}
6871

69-
get = new GetMethod(url, true);
70-
HashMap<String, String> map = new HashMap<>();
71-
map.put("format", "json");
72+
private fun missingUserInfoFailure(method: GetMethod): RemoteOperationResult<UserInfo> {
73+
Log_OC.e(TAG, "User information missing in response")
74+
return RemoteOperationResult(false, method)
75+
}
7276

73-
get.setQueryString(map);
74-
status = client.execute(get);
77+
@Suppress("DEPRECATION")
78+
private fun failure(e: Exception): RemoteOperationResult<UserInfo> =
79+
RemoteOperationResult<UserInfo>(e).also {
80+
Log_OC.e(TAG, "Exception while getting user information: " + it.logMessage, it.exception)
81+
}
7582

76-
if (isSuccess(status)) {
77-
ServerResponse<UserInfo> ocsResponse = getServerResponse(get, new TypeToken<>() {});
83+
companion object {
84+
private val TAG = GetUserInfoRemoteOperation::class.java.simpleName
7885

79-
if (ocsResponse != null) {
80-
UserInfo userInfo = ocsResponse.ocs.data;
86+
private const val OCS_ROUTE_SELF = "/ocs/v2.php/cloud/user"
8187

82-
if (userInfo.getQuota() == null || userInfo.getQuota().getQuota() == 0) {
83-
userInfo.setQuota(new Quota(QUOTA_LIMIT_INFO_NOT_AVAILABLE));
84-
}
88+
private const val JSON_UTF8_ENCODED = "application/json; charset=utf-8"
8589

86-
result = new RemoteOperationResult<>(true, get);
87-
result.setResultData(userInfo);
88-
} else {
89-
result = new RemoteOperationResult<>(false, get);
90-
}
91-
} else {
92-
result = new RemoteOperationResult<>(false, get);
93-
String response = get.getResponseBodyAsString();
94-
Log_OC.e(TAG, "Failed response while getting user information");
95-
if (response.isEmpty()) {
96-
Log_OC.e(TAG, "*** status code: " + status);
97-
} else {
98-
Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response);
99-
}
100-
}
101-
} catch (Exception e) {
102-
result = new RemoteOperationResult<>(e);
103-
Log_OC.e(TAG, "Exception while getting OC user information", e);
104-
} finally {
105-
if (get != null) {
106-
get.releaseConnection();
107-
}
108-
}
109-
return result;
110-
}
90+
/**
91+
* Quota return value for unlimited space.
92+
*/
93+
const val SPACE_UNLIMITED = -3L
11194

112-
private boolean isSuccess(int status) {
113-
return (status == HttpStatus.SC_OK);
95+
/**
96+
* Quota return value for quota information not available.
97+
*/
98+
const val QUOTA_LIMIT_INFO_NOT_AVAILABLE = Long.MIN_VALUE
11499
}
115100
}

0 commit comments

Comments
 (0)