Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UnifiedSearchRemoteOperation.kt « search « resources « lib « android « nextcloud « com « java « main « src « library - github.com/nextcloud/android-library.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 093adaf0361f04c8939a765d6d24fe56fc552ba6 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * <!--
 *   Nextcloud Android client application
 *
 *   @author Tobias Kaminsky
 *   Copyright (C) 2019 Tobias Kaminsky
 *   Copyright (C) 2019 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.SearchResult
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
import java.net.URLEncoder

/**
 * Get search result by a specific unified search provider
 */
@Suppress("TooGenericExceptionCaught")
class UnifiedSearchRemoteOperation(
    val provider: String,
    val query: String,
    val cursor: Int? = null,
    val limit: Int = 5
) :
    OCSRemoteOperation<SearchResult>() {
    companion object {
        private val TAG = UnifiedSearchRemoteOperation::class.java.simpleName
        private const val ENDPOINT = "/ocs/v2.php/search/providers/"
        private const val SEARCH = "/search"
        private const val TERM = "&term="
        private const val LIMIT = "&limit=%d"
        private const val CURSOR = "&cursor=%d"
    }

    override fun run(client: NextcloudClient): RemoteOperationResult<SearchResult> {
        if (query.isBlank()) {
            return RemoteOperationResult(
                IllegalArgumentException("Query may not be empty or blank!")
            )
        }

        var result: RemoteOperationResult<SearchResult>
        var getMethod: GetMethod? = null
        try {
            var uri = client.baseUri.toString() +
                ENDPOINT +
                provider +
                SEARCH +
                JSON_FORMAT +
                TERM +
                URLEncoder.encode(query, "UTF-8") +
                LIMIT.format(limit)
            cursor?.let {
                uri += CURSOR.format(it)
            }
            getMethod = GetMethod(
                uri,
                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<SearchResult>?>() {}
                )?.ocs?.data

                if (searchProviders != null) {
                    result = RemoteOperationResult(true, getMethod)
                    result.resultData = searchProviders
                } else {
                    result = RemoteOperationResult(false, getMethod)
                }
            } else {
                result = RemoteOperationResult(false, getMethod)
            }
        } catch (e: Exception) {
            result = RemoteOperationResult(e)
            Log_OC.e(
                TAG,
                "Search with " + query + " to " + provider + " failed:" + result.logMessage,
                result.exception
            )
        } finally {
            getMethod?.releaseConnection()
        }
        return result
    }
}