|
| 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