blob: bac5b837f33b0a90f77bcecd1b07641686481061 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*
* <!--
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* -->
*/
package com.nextcloud.android.lib.resources.search
import com.google.gson.reflect.TypeToken
import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.GetMethod
import com.owncloud.android.lib.common.SearchProvider
import com.owncloud.android.lib.common.SearchProviders
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.ocs.ServerResponse
import com.owncloud.android.lib.resources.OCSRemoteOperation
import org.apache.commons.httpclient.HttpStatus
/**
* Get all search providers for unified search
*/
@Suppress("TooGenericExceptionCaught")
class UnifiedSearchProvidersRemoteOperation : OCSRemoteOperation<SearchProviders>() {
companion object {
private val TAG = UnifiedSearchProvidersRemoteOperation::class.java.simpleName
private const val ENDPOINT = "/ocs/v2.php/search/providers"
}
override fun run(client: NextcloudClient): RemoteOperationResult<SearchProviders> {
var result: RemoteOperationResult<SearchProviders>
var getMethod: GetMethod? = null
try {
getMethod = GetMethod(client.baseUri.toString() + ENDPOINT + JSON_FORMAT, true)
// remote request
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
val status = client.execute(getMethod)
if (status == HttpStatus.SC_OK) {
val searchProviders = getServerResponse(
getMethod,
object : TypeToken<ServerResponse<ArrayList<SearchProvider>?>?>() {}
)
?.ocs
?.data
?: ArrayList()
val eTag = getMethod.getResponseHeader("ETag") ?: ""
result = RemoteOperationResult(true, getMethod)
result.resultData = SearchProviders(eTag, searchProviders)
} else {
result = RemoteOperationResult(false, getMethod)
}
} catch (e: Exception) {
result = RemoteOperationResult(e)
Log_OC.e(
TAG,
"Get all search provider failed: " + result.logMessage,
result.exception
)
} finally {
getMethod?.releaseConnection()
}
return result
}
}
|