diff --git a/.drone.yml b/.drone.yml index 30eec7f8c7..f2ad8f2fe7 100644 --- a/.drone.yml +++ b/.drone.yml @@ -99,9 +99,9 @@ services: - su www-data -c "php /var/www/html/occ app:enable -f groupfolders" - su www-data -c "php /var/www/html/occ groupfolders:create groupfolder" - su www-data -c "php /var/www/html/occ groupfolders:group 1 users" - - su www-data -c "git clone --depth 1 https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/" + - su www-data -c "git clone --depth 1 -b master https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/" - su www-data -c "php /var/www/html/occ app:enable -f notifications" - - su www-data -c "php /var/www/html/occ notification:generate test -d test" + - su www-data -c "php /var/www/html/occ notification:generate 'test' 'test message'" - su www-data -c "git clone --depth 1 https://github.com/nextcloud/photos.git /var/www/html/apps/photos/" - su www-data -c "cd /var/www/html/apps/photos; composer install --no-dev" - su www-data -c "php /var/www/html/occ app:enable -f photos" @@ -216,7 +216,7 @@ services: - su www-data -c "php /var/www/html/occ groupfolders:group 1 users" - su www-data -c "git clone --depth 1 -b $SERVER_VERSION https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/" - su www-data -c "php /var/www/html/occ app:enable -f notifications" - - su www-data -c "php /var/www/html/occ notification:generate test test" + - su www-data -c "php /var/www/html/occ notification:generate 'test' 'test message'" - su www-data -c "git clone --depth 1 -b $SERVER_VERSION https://github.com/nextcloud/photos.git /var/www/html/apps/photos/" - su www-data -c "cd /var/www/html/apps/photos; composer install --no-dev" - su www-data -c "php /var/www/html/occ app:enable -f photos" @@ -235,6 +235,6 @@ trigger: - pull_request --- kind: signature -hmac: 3e71a44f6f57a4d4d853c586c0c322bf0b718d96627906b92864e12353e5a014 +hmac: 889fb47b6c7f4ba0f7b0297f697e29152099b18b6f5073ad02a503ab9046fbcd ... diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index 629747cce3..7e2d1aba9d 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -70,6 +70,7 @@ public abstract class AbstractIT { public static OwnCloudClient client; public static OwnCloudClient client2; protected static NextcloudClient nextcloudClient; + protected static NextcloudClient nextcloudClientAdmin; protected static Context context; protected static Uri url; @@ -109,6 +110,12 @@ public static void beforeAll() throws InterruptedException, String credentials = Credentials.basic(loginName, password); nextcloudClient = new NextcloudClient(url, userId, credentials, context); + nextcloudClientAdmin = new NextcloudClient( + url, + "admin", + Credentials.basic("admin", "admin"), + context); + waitForServer(client, url); testConnection(); } diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperationIT.kt new file mode 100644 index 0000000000..aa9f6e51f6 --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperationIT.kt @@ -0,0 +1,38 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.owncloud.android.AbstractIT +import org.junit.Assert.assertTrue +import org.junit.Test + +class DeleteAllNotificationsRemoteOperationIT : AbstractIT() { + @Test + fun testDeleteAllNotification() { + // create one notification + assertTrue( + nextcloudClientAdmin.execute( + CreateNotificationRemoteOperation( + nextcloudClient.userId, + "test" + ) + ).isSuccess + ) + + var result = nextcloudClient.execute(GetNotificationsRemoteOperation()) + assertTrue(result.isSuccess) + assertTrue(result.resultData.isNotEmpty()) + + assertTrue(nextcloudClient.execute(DeleteAllNotificationsRemoteOperation()).isSuccess) + + result = nextcloudClient.execute(GetNotificationsRemoteOperation()) + assertTrue(result.isSuccess) + assertTrue(result.resultData.isEmpty()) + } +} diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperationIT.kt new file mode 100644 index 0000000000..ecd952e34d --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperationIT.kt @@ -0,0 +1,40 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.owncloud.android.AbstractIT +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class DeleteNotificationRemoteOperationIT : AbstractIT() { + @Test + fun deleteOneNotification() { + // create one notification + assertTrue( + nextcloudClientAdmin.execute( + CreateNotificationRemoteOperation( + nextcloudClient.userId, + "test" + ) + ).isSuccess + ) + + val result = nextcloudClient.execute(GetNotificationsRemoteOperation()) + assertTrue(result.isSuccess) + assertTrue(result.resultData.isNotEmpty()) + + val firstNotificationId = result.resultData.first().notificationId + + assertTrue(DeleteNotificationRemoteOperation(firstNotificationId).execute(nextcloudClient).isSuccess) + + val getResult = GetNotificationRemoteOperation(firstNotificationId).execute(nextcloudClient) + assertFalse(getResult.isSuccess) + } +} diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperationIT.kt new file mode 100644 index 0000000000..f3e8fa769a --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperationIT.kt @@ -0,0 +1,51 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.owncloud.android.AbstractIT +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class GetNotificationRemoteOperationIT : AbstractIT() { + @Test + fun testSingleNotification() { + // create one notification + assertTrue( + nextcloudClientAdmin.execute( + CreateNotificationRemoteOperation( + nextcloudClient.userId, + "test" + ) + ).isSuccess + ) + + // get all notifications + val resultAll = nextcloudClient.execute(GetNotificationsRemoteOperation()) + assertTrue(resultAll.isSuccess) + + val allNotifications = resultAll.resultData + val firstNotificationId = allNotifications.first().notificationId + + // check one specific + val result = nextcloudClient.execute(GetNotificationRemoteOperation(firstNotificationId)) + assertTrue(result.isSuccess) + + val notification = result.resultData + assertNotNull(notification) + assertEquals(firstNotificationId, notification.notificationId) + } + + @Test + fun testNonExisting() { + assertFalse(GetNotificationRemoteOperation(-1).execute(nextcloudClient).isSuccess) + } +} diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperationIT.kt new file mode 100644 index 0000000000..f228b5423b --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperationIT.kt @@ -0,0 +1,33 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.owncloud.android.AbstractIT +import junit.framework.Assert.assertTrue +import org.junit.Assert +import org.junit.Test + +class GetNotificationsRemoteOperationIT : AbstractIT() { + @Test + fun testNotifications() { + // create one notification + Assert.assertTrue( + nextcloudClientAdmin.execute( + CreateNotificationRemoteOperation( + nextcloudClient.userId, + "test" + ) + ).isSuccess + ) + + val result = nextcloudClient.execute(GetNotificationsRemoteOperation()) + assertTrue(result.isSuccess) + assertTrue(result.resultData.isNotEmpty()) + } +} diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt index ba34b46fba..c0e39786c0 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt @@ -22,6 +22,7 @@ class NotificationIT : AbstractIT() { val count = all.resultData.size // get one + assertTrue(count > 0) val firstNotification = all.resultData[0] val first = GetNotificationRemoteOperation(firstNotification.notificationId).execute(nextcloudClient) assertTrue(first.isSuccess) diff --git a/library/src/main/java/com/nextcloud/operations/PostMethod.kt b/library/src/main/java/com/nextcloud/operations/PostMethod.kt index 28110f7268..c4b1f4dd5d 100644 --- a/library/src/main/java/com/nextcloud/operations/PostMethod.kt +++ b/library/src/main/java/com/nextcloud/operations/PostMethod.kt @@ -5,6 +5,7 @@ * SPDX-FileCopyrightText: 2023 Tobias Kaminsky * SPDX-License-Identifier: MIT */ + package com.nextcloud.operations import com.nextcloud.common.OkHttpMethodBase @@ -15,12 +16,12 @@ import okhttp3.RequestBody * HTTP POST method that uses OkHttp with new NextcloudClient * UTF8 by default */ -class PostMethod( +open class PostMethod( uri: String, useOcsApiRequestHeader: Boolean, - val body: RequestBody + val body: RequestBody? ) : OkHttpMethodBase(uri, useOcsApiRequestHeader) { override fun applyType(temp: Request.Builder) { - temp.post(body) + body?.let { temp.post(it) } } } diff --git a/library/src/main/java/com/nextcloud/operations/Utf8PostMethod.kt b/library/src/main/java/com/nextcloud/operations/Utf8PostMethod.kt new file mode 100644 index 0000000000..1c322af6b0 --- /dev/null +++ b/library/src/main/java/com/nextcloud/operations/Utf8PostMethod.kt @@ -0,0 +1,25 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.nextcloud.operations + +import okhttp3.Request +import okhttp3.RequestBody + +/** + * HTTP POST method that uses OkHttp with new NextcloudClient + */ +class Utf8PostMethod( + uri: String, + useOcsApiRequestHeader: Boolean, + body: RequestBody? +) : PostMethod(uri, useOcsApiRequestHeader, body) { + override fun applyType(temp: Request.Builder) { + temp.addHeader("Content-Type", "charset=utf-8") + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/CreateNotificationRemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/notifications/CreateNotificationRemoteOperation.kt new file mode 100644 index 0000000000..e97ab474cb --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/CreateNotificationRemoteOperation.kt @@ -0,0 +1,69 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2022 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.nextcloud.common.NextcloudClient +import com.nextcloud.operations.PostMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.notifications.models.Notification +import okhttp3.FormBody +import org.apache.commons.httpclient.HttpStatus +import java.io.IOException + +/** + * Provides the remote notifications from the server handling the following data structure accessible via the + * notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at {@link + * "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. + */ +class CreateNotificationRemoteOperation(private val userId: String, private val message: String) : + RemoteOperation() { + override fun run(client: NextcloudClient): RemoteOperationResult { + var result: RemoteOperationResult + val status: Int + var post: PostMethod? = null + val url = + client.baseUri.toString() + "/ocs/v2.php/apps/notifications/api/v2/admin_notifications/" + + try { + val bodyRequest = + FormBody + .Builder() + .add("shortMessage", message) + .build() + + post = PostMethod(url + userId, true, bodyRequest) + status = client.execute(post) + val response = post.getResponseBodyAsString() + if (isSuccess(status)) { + result = RemoteOperationResult(true, post) + Log_OC.d(TAG, "Successful response: $response") + } else { + result = RemoteOperationResult(false, post) + Log_OC.e(TAG, "Failed response while getting user notifications ") + Log_OC.e(TAG, "*** status code: $status ; response message: $response") + } + } catch (e: IOException) { + result = RemoteOperationResult(e) + Log_OC.e(TAG, "Exception while getting remote notifications", e) + } finally { + post?.releaseConnection() + } + return result + } + + private fun isSuccess(status: Int): Boolean { + return status == HttpStatus.SC_OK + } + + companion object { + private val TAG = CreateNotificationRemoteOperation::class.java.simpleName + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperation.java deleted file mode 100644 index 641de60eea..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperation.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2019 Tobias Kaminsky - * SPDX-License-Identifier: MIT - */ -package com.owncloud.android.lib.resources.notifications; - -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.DeleteMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; - -/** - * Delete all notification, specified at - * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. - */ -public class DeleteAllNotificationsRemoteOperation extends RemoteOperation { - - // OCS Route - private static final String OCS_ROUTE_LIST_V12_AND_UP = - "/ocs/v2.php/apps/notifications/api/v2/notifications"; - - @Override - public RemoteOperationResult run(NextcloudClient client) { - RemoteOperationResult result; - int status; - DeleteMethod delete = null; - String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP; - - try { - delete = new DeleteMethod(url, true); - - status = client.execute(delete); - String response = delete.getResponseBodyAsString(); - - if (delete.isSuccess()) { - result = new RemoteOperationResult<>(true, delete); - Log_OC.d(this, "Successful response: " + response); - } else { - result = new RemoteOperationResult<>(false, delete); - Log_OC.e(this, "Failed response while getting user notifications "); - Log_OC.e(this, "*** status code: " + status + " ;response message: " + response); - } - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(this, "Exception while getting remote notifications", e); - } finally { - if (delete != null) { - delete.releaseConnection(); - } - } - - return result; - } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperation.kt new file mode 100644 index 0000000000..67fa3fe858 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteAllNotificationsRemoteOperation.kt @@ -0,0 +1,55 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2019 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ +package com.owncloud.android.lib.resources.notifications + +import com.nextcloud.common.NextcloudClient +import com.nextcloud.operations.DeleteMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import java.io.IOException + +/** + * Delete all notification, specified at + * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. + */ +class DeleteAllNotificationsRemoteOperation : RemoteOperation() { + override fun run(client: NextcloudClient): RemoteOperationResult { + var result: RemoteOperationResult + val status: Int + var delete: DeleteMethod? = null + val url = client.baseUri.toString() + OCS_ROUTE_LIST_V12_AND_UP + try { + delete = DeleteMethod(url, true) + + status = client.execute(delete) + val response = delete.getResponseBodyAsString() + + if (delete.isSuccess()) { + result = RemoteOperationResult(true, delete) + Log_OC.d(this, "Successful response: $response") + } else { + result = RemoteOperationResult(false, delete) + Log_OC.e(this, "Failed response while deleting all user notifications") + Log_OC.e(this, "*** status code: $status ;response message: $response") + } + } catch (e: IOException) { + result = RemoteOperationResult(e) + Log_OC.e(this, "Exception while getting remote notifications", e) + } finally { + delete?.releaseConnection() + } + return result + } + + companion object { + // OCS Route + private const val OCS_ROUTE_LIST_V12_AND_UP = + "/ocs/v2.php/apps/notifications/api/v2/notifications" + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperation.java deleted file mode 100644 index 36d7e463cc..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperation.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2019 Tobias Kaminsky - * SPDX-License-Identifier: MIT - */ -package com.owncloud.android.lib.resources.notifications; - -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.DeleteMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; - -/** - * Delete a notification, specified at - * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. - */ -public class DeleteNotificationRemoteOperation extends RemoteOperation { - - // OCS Route - private static final String OCS_ROUTE_LIST_V12_AND_UP = - "/ocs/v2.php/apps/notifications/api/v2/notifications/"; - - private final int id; - - public DeleteNotificationRemoteOperation(int id) { - this.id = id; - } - - @Override - public RemoteOperationResult run(NextcloudClient client) { - RemoteOperationResult result; - int status; - DeleteMethod delete = null; - String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP; - - try { - delete = new DeleteMethod(url + id, true); - - status = client.execute(delete); - String response = delete.getResponseBodyAsString(); - - if (delete.isSuccess()) { - result = new RemoteOperationResult<>(true, delete); - Log_OC.d(this, "Successful response: " + response); - } else { - result = new RemoteOperationResult<>(false, delete); - Log_OC.e(this, "Failed response while getting user notifications"); - Log_OC.e(this, "*** status code: " + status + " ;response message: " + response); - } - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(this, "Exception while getting remote notifications", e); - } finally { - if (delete != null) { - delete.releaseConnection(); - } - } - - return result; - } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperation.kt new file mode 100644 index 0000000000..af900f82e1 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/DeleteNotificationRemoteOperation.kt @@ -0,0 +1,54 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2019 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ +package com.owncloud.android.lib.resources.notifications + +import com.nextcloud.common.NextcloudClient +import com.nextcloud.operations.DeleteMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import java.io.IOException + +/** + * Delete a notification, specified at + * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. + */ +class DeleteNotificationRemoteOperation(private val id: Int) : RemoteOperation() { + override fun run(client: NextcloudClient): RemoteOperationResult { + var result: RemoteOperationResult + val status: Int + var delete: DeleteMethod? = null + val url = client.baseUri.toString() + OCS_ROUTE_LIST_V12_AND_UP + try { + delete = DeleteMethod(url + id, true) + status = client.execute(delete) + + val response = delete.getResponseBodyAsString() + + if (delete.isSuccess()) { + result = RemoteOperationResult(true, delete) + Log_OC.d(this, "Successful response: $response") + } else { + result = RemoteOperationResult(false, delete) + Log_OC.e(this, "Failed response while deleting user notification: $id ") + Log_OC.e(this, "*** status code: $status ;response message: $response") + } + } catch (e: IOException) { + result = RemoteOperationResult(e) + Log_OC.e(this, "Exception while getting remote notifications", e) + } finally { + delete?.releaseConnection() + } + return result + } + + companion object { + // OCS Route + private const val OCS_ROUTE_LIST_V12_AND_UP = "/ocs/v2.php/apps/notifications/api/v2/notifications/" + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java deleted file mode 100644 index 4730e45753..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2019 Tobias Kaminsky - * SPDX-License-Identifier: MIT - */ -package com.owncloud.android.lib.resources.notifications; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.reflect.TypeToken; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.notifications.models.Notification; - -import java.lang.reflect.Type; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - -/** - * Provides the remote notifications from the server handling the following data structure - * accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at - * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. - */ -public class GetNotificationRemoteOperation extends RemoteOperation { - - // OCS Route - private static final String OCS_ROUTE_LIST_V12_AND_UP = - "/ocs/v2.php/apps/notifications/api/v2/notifications/"; - - // JSON Node names - private static final String NODE_OCS = "ocs"; - private static final String NODE_DATA = "data"; - - private final int id; - - public GetNotificationRemoteOperation(int id) { - this.id = id; - } - - @SuppressFBWarnings("HTTP_PARAMETER_POLLUTION") - @Override - public RemoteOperationResult run(NextcloudClient client) { - RemoteOperationResult result; - int status; - GetMethod get = null; - String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP + id + JSON_FORMAT; - - // get the notification - try { - get = new GetMethod(url, true); - - status = client.execute(get); - String response = get.getResponseBodyAsString(); - - if (get.isSuccess()) { - result = new RemoteOperationResult<>(true, get); - Log_OC.d(this, "Successful response: " + response); - - // Parse the response - Notification notification = parseResult(response); - result.setResultData(notification); - } else { - result = new RemoteOperationResult<>(false, get); - Log_OC.e(this, "Failed response while getting user notifications "); - Log_OC.e(this, "*** status code: " + status + " ; response message: " + response); - } - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(this, "Exception while getting remote notifications", e); - } finally { - if (get != null) { - get.releaseConnection(); - } - } - - return result; - } - - private Notification parseResult(String response) { - JsonObject jo = (JsonObject) JsonParser.parseString(response); - JsonObject jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA); - - Gson gson = new Gson(); - Type type = new TypeToken() { - }.getType(); - - return gson.fromJson(jsonDataObject, type); - } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.kt new file mode 100644 index 0000000000..74c5c375ef --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.kt @@ -0,0 +1,87 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2019 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.google.gson.reflect.TypeToken +import com.nextcloud.common.NextcloudClient +import com.nextcloud.operations.GetMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.notifications.models.Notification +import org.apache.commons.httpclient.HttpStatus +import org.json.JSONException +import java.io.IOException + +/** + * Provides the remote notifications from the server handling the following data structure accessible via the + * notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at {@link + * "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. + */ +class GetNotificationRemoteOperation(private val id: Int) : RemoteOperation() { + override fun run(client: NextcloudClient): RemoteOperationResult { + var result: RemoteOperationResult + val status: Int + var get: GetMethod? = null + val url = client.baseUri.toString() + OCS_ROUTE_LIST_V12_AND_UP + id + JSON_FORMAT + + // get the notifications + try { + get = GetMethod(url, true) + status = client.execute(get) + val response = get.getResponseBodyAsString() + if (isSuccess(status)) { + result = RemoteOperationResult(true, get) + Log_OC.d(TAG, "Successful response: $response") + + // Parse the response + result.setResultData(parseResult(response)) + } else { + result = RemoteOperationResult(false, get) + Log_OC.e(TAG, "Failed response while getting user notifications ") + Log_OC.e(TAG, "*** status code: $status ; response message: $response") + } + } catch (e: IOException) { + result = RemoteOperationResult(e) + Log_OC.e(TAG, "Exception while getting remote notifications", e) + } finally { + get?.releaseConnection() + } + return result + } + + @Throws(JSONException::class) + private fun parseResult(response: String): Notification { + val jo = JsonParser.parseString(response) as JsonObject + val jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA) + val gson = Gson() + val type = object : TypeToken() {}.type + + return gson.fromJson(jsonDataObject, type) + } + + private fun isSuccess(status: Int): Boolean { + return status == HttpStatus.SC_OK + } + + companion object { + private val TAG = GetNotificationRemoteOperation::class.java.simpleName + + // OCS Route + private const val OCS_ROUTE_LIST_V12_AND_UP = + "/ocs/v2.php/apps/notifications/api/v2/notifications/" + + // JSON Node names + private const val NODE_OCS = "ocs" + private const val NODE_DATA = "data" + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java deleted file mode 100644 index ae2da9e508..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2017 Andy Scherzinger - * SPDX-FileCopyrightText: 2017 Mario Danic - * SPDX-License-Identifier: MIT - */ -package com.owncloud.android.lib.resources.notifications; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.reflect.TypeToken; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.notifications.models.Notification; - -import java.lang.reflect.Type; -import java.util.List; - -/** - * Provides the remote notifications from the server handling the following data structure - * accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at - * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. - */ -public class GetNotificationsRemoteOperation extends RemoteOperation> { - - // OCS Route - private static final String OCS_ROUTE_LIST_V12_AND_UP = - "/ocs/v2.php/apps/notifications/api/v2/notifications" + JSON_FORMAT; - - private static final String TAG = GetNotificationsRemoteOperation.class.getSimpleName(); - - // JSON Node names - private static final String NODE_OCS = "ocs"; - - private static final String NODE_DATA = "data"; - - @Override - public RemoteOperationResult> run(NextcloudClient client) { - RemoteOperationResult> result; - int status; - GetMethod get = null; - List notifications; - String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP; - - // get the notifications - try { - get = new GetMethod(url, true); - - status = client.execute(get); - String response = get.getResponseBodyAsString(); - - if (get.isSuccess()) { - result = new RemoteOperationResult<>(true, get); - Log_OC.d(TAG, "Successful response: " + response); - - // Parse the response - notifications = parseResult(response); - result.setResultData(notifications); - } else { - result = new RemoteOperationResult<>(false, get); - Log_OC.e(TAG, "Failed response while getting user notifications "); - Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); - } - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Log_OC.e(TAG, "Exception while getting remote notifications", e); - } finally { - if (get != null) { - get.releaseConnection(); - } - } - - return result; - } - - private List parseResult(String response) { - JsonObject jo = (JsonObject) JsonParser.parseString(response); - JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA); - - Gson gson = new Gson(); - Type listType = new TypeToken>() { - }.getType(); - - return gson.fromJson(jsonDataArray, listType); - } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.kt new file mode 100644 index 0000000000..13f02a8258 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.kt @@ -0,0 +1,86 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2017 Mario Danic + * SPDX-FileCopyrightText: 2017 Andy Scherzinger + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.notifications + +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.google.gson.reflect.TypeToken +import com.nextcloud.common.NextcloudClient +import com.nextcloud.operations.GetMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.notifications.models.Notification +import org.apache.commons.httpclient.HttpStatus +import java.io.IOException + +/** + * Provides the remote notifications from the server handling the following data structure + * accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at + * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. + */ +class GetNotificationsRemoteOperation : RemoteOperation>() { + override fun run(client: NextcloudClient): RemoteOperationResult> { + var result: RemoteOperationResult> + val status: Int + var get: GetMethod? = null + val notifications: List + val url = client.baseUri.toString() + OCS_ROUTE_LIST_V12_AND_UP + + // get the notifications + try { + get = GetMethod(url, true) + status = client.execute(get) + val response = get.getResponseBodyAsString() + if (isSuccess(status)) { + result = RemoteOperationResult(true, get) + Log_OC.d(TAG, "Successful response: $response") + + // Parse the response + notifications = parseResult(response) + result.setResultData(notifications) + } else { + result = RemoteOperationResult(false, get) + Log_OC.e(TAG, "Failed response while getting user notifications ") + Log_OC.e(TAG, "*** status code: $status ; response message: $response") + } + } catch (e: IOException) { + result = RemoteOperationResult(e) + Log_OC.e(TAG, "Exception while getting remote notifications", e) + } finally { + get?.releaseConnection() + } + return result + } + + private fun parseResult(response: String): List { + val jo = JsonParser.parseString(response) as JsonObject + val jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA) + val gson = Gson() + val listType = object : TypeToken?>() {}.type + return gson.fromJson(jsonDataArray, listType) + } + + private fun isSuccess(status: Int): Boolean { + return status == HttpStatus.SC_OK + } + + companion object { + // OCS Route + private const val OCS_ROUTE_LIST_V12_AND_UP = + "/ocs/v2.php/apps/notifications/api/v2/notifications$JSON_FORMAT" + private val TAG = GetNotificationsRemoteOperation::class.java.simpleName + + // JSON Node names + private const val NODE_OCS = "ocs" + private const val NODE_DATA = "data" + } +} diff --git a/scripts/analysis/findbugs-results.txt b/scripts/analysis/findbugs-results.txt index 2efea5198b..897bdc8200 100644 --- a/scripts/analysis/findbugs-results.txt +++ b/scripts/analysis/findbugs-results.txt @@ -1 +1 @@ -167 \ No newline at end of file +139