Skip to content

Commit a7b0fcd

Browse files
Add client reporting
Signed-off-by: tobiasKaminsky <[email protected]>
1 parent 5073a84 commit a7b0fcd

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

library/src/main/java/com/owncloud/android/lib/resources/status/GetCapabilitiesRemoteOperation.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public class GetCapabilitiesRemoteOperation extends RemoteOperation {
169169
// drop-account
170170
private static final String NODE_DROP_ACCOUNT = "drop-account";
171171

172+
// security guard
173+
private static final String NODE_SECURITY_GUARD = "security_guard";
174+
private static final String NODE_DIAGNOSTICS = "diagnostics";
172175

173176
private OCCapability currentCapability = null;
174177

@@ -687,6 +690,19 @@ private OCCapability parseResponse(String response) throws JSONException {
687690
capability.setDropAccount(CapabilityBooleanType.FALSE);
688691
}
689692
}
693+
694+
// security guard
695+
if (respCapabilities.has(NODE_SECURITY_GUARD)) {
696+
JSONObject securityGuardCapability = respCapabilities.getJSONObject(NODE_SECURITY_GUARD);
697+
698+
if (securityGuardCapability.getBoolean(NODE_DIAGNOSTICS)) {
699+
capability.setSecurityGuard(CapabilityBooleanType.TRUE);
700+
} else {
701+
capability.setSecurityGuard(CapabilityBooleanType.FALSE);
702+
}
703+
} else {
704+
capability.setSecurityGuard(CapabilityBooleanType.FALSE);
705+
}
690706
}
691707

692708
Log_OC.d(TAG, "*** Get Capabilities completed ");

library/src/main/java/com/owncloud/android/lib/resources/status/OCCapability.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class OCCapability {
113113
// Drop-Account
114114
var dropAccount = CapabilityBooleanType.UNKNOWN
115115

116+
// Security guard
117+
var securityGuard = CapabilityBooleanType.UNKNOWN
118+
116119
// Etag for capabilities
117120
var etag: String? = ""
118121

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2023 Tobias Kaminsky
5+
* Copyright (C) 2023 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.owncloud.android.lib.resources.status
29+
30+
data class Problem(val type: String, val count: Int, val oldestTimestamp: Long) {
31+
fun toJsonWithTypeString(): String {
32+
return """"$type": {"count": $count, "oldest": $oldestTimestamp}"""
33+
}
34+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2023 Tobias Kaminsky
5+
* Copyright (C) 2023 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.owncloud.android.lib.resources.status
29+
30+
import androidx.annotation.VisibleForTesting
31+
import com.nextcloud.common.NextcloudClient
32+
import com.nextcloud.operations.PutMethod
33+
import com.owncloud.android.lib.common.operations.RemoteOperation
34+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
35+
import okhttp3.MediaType.Companion.toMediaTypeOrNull
36+
import okhttp3.RequestBody
37+
import org.apache.commons.httpclient.HttpStatus
38+
39+
class SendClientDiagnosticRemoteOperation(
40+
private val syncConflict: Problem?,
41+
private val problems: List<Problem>?,
42+
private val virusDetected: Problem?,
43+
private val e2eError: Problem?
44+
) : RemoteOperation<Void>() {
45+
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
46+
val request = RequestBody.create("application/json".toMediaTypeOrNull(), generateJSON())
47+
48+
val putMethod = PutMethod(client.baseUri.toString() + URL, true, request)
49+
50+
val status = putMethod.execute(client)
51+
52+
return if (status == HttpStatus.SC_OK) {
53+
RemoteOperationResult<Void>(true, putMethod)
54+
} else {
55+
RemoteOperationResult<Void>(false, putMethod)
56+
}
57+
}
58+
59+
@VisibleForTesting
60+
fun generateJSON(): String {
61+
val map = mutableListOf<String>()
62+
63+
if (syncConflict != null) {
64+
map.add(syncConflict.toJsonWithTypeString())
65+
} else {
66+
map.add(""""$SYNC_CONFLICTS": {}""")
67+
}
68+
69+
if (problems != null) {
70+
val test = problems.map { it.toJsonWithTypeString() }
71+
map.add(""""$PROBLEMS": ${test.joinToString(", ", "{", "}")}""")
72+
} else {
73+
map.add(""""$PROBLEMS": {}""")
74+
}
75+
76+
if (virusDetected != null) {
77+
map.add(virusDetected.toJsonWithTypeString())
78+
} else {
79+
map.add(""""$VIRUS_DETECTED": {}""")
80+
}
81+
82+
if (e2eError != null) {
83+
map.add(e2eError.toJsonWithTypeString())
84+
} else {
85+
map.add(""""$E2E_ERRORS": {}""")
86+
}
87+
88+
return map.joinToString(prefix = "{", postfix = "}")
89+
}
90+
91+
companion object {
92+
const val URL = "/ocs/v2.php/apps/security_guard/diagnostics"
93+
94+
const val SYNC_CONFLICTS = "sync_conflicts"
95+
const val PROBLEMS = "problems"
96+
const val VIRUS_DETECTED = "virus_detected"
97+
const val E2E_ERRORS = "e2e_errors"
98+
}
99+
}

0 commit comments

Comments
 (0)