Skip to content

Commit

Permalink
Use resultData
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky committed Apr 20, 2023
1 parent f2a324d commit 62d41cc
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Nextcloud Android Library is available under MIT license
*
* @author Tobias Kaminsky
* Copyright (C) 2023 Tobias Kaminsky
* Copyright (C) 2023 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.owncloud.android.lib.resources.notifications

import com.owncloud.android.AbstractIT
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Test

class NotificationIT : AbstractIT() {
@Test
fun getNotification() {
// get all
val all = GetNotificationsRemoteOperation().execute(client)
assertTrue(all.isSuccess)

// get one
val firstNotification = all.resultData[0]
val first = GetNotificationRemoteOperation(firstNotification.notificationId).execute(
client
)
assertTrue(first.isSuccess)
assertEquals(firstNotification.message, first.resultData.message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import com.owncloud.android.lib.common.network.CertificateCombinedException;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.files.CreateLocalFileException;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.lib.resources.notifications.models.PushResponse;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.Header;
Expand All @@ -58,7 +56,6 @@
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import javax.net.ssl.SSLException;
Expand Down Expand Up @@ -156,8 +153,6 @@ public enum ResultCode {

private ArrayList<Object> mData;
private T resultData;
private List<Notification> mNotificationData;
private PushResponse mPushResponse;

/**
* Public constructor from result code.
Expand Down Expand Up @@ -533,41 +528,6 @@ public Object getSingleData() {
return mData.get(0);
}

/**
* @deprecated use getResultData() instead
*/
public void setNotificationData(List<Notification> notifications) {
mNotificationData = notifications;
}

/**
* @deprecated use getResultData() instead
*/
public PushResponse getPushResponseData() {
if (!mSuccess) {
throw new RuntimeException("Accessing result data after operation failed!");
}
return mPushResponse;
}

/**
* @deprecated use getResultData() instead
*/
public void setPushResponseData(PushResponse pushResponseData) {
mPushResponse = pushResponseData;
}

/**
* @deprecated use getResultData() instead
*/
public List<Notification> getNotificationData() {
if (!mSuccess) {
throw new RuntimeException("Accessing result data after operation failed!");
}
return mNotificationData;
}


public boolean isSuccess() {
return mSuccess;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand All @@ -52,7 +49,7 @@
* 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 {
public class GetNotificationRemoteOperation extends RemoteOperation<Notification> {

// OCS Route
private static final String OCS_ROUTE_LIST_V12_AND_UP =
Expand All @@ -70,14 +67,13 @@ public GetNotificationRemoteOperation(int id) {

@SuppressFBWarnings("HTTP_PARAMETER_POLLUTION")
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
protected RemoteOperationResult<Notification> run(OwnCloudClient client) {
RemoteOperationResult<Notification> result;
int status;
GetMethod get = null;
List<Notification> notifications = new ArrayList<>();
String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP + id + JSON_FORMAT;

// get the notifications
// get the notification
try {
get = new GetMethod(url);
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
Expand All @@ -86,14 +82,14 @@ protected RemoteOperationResult run(OwnCloudClient client) {
String response = get.getResponseBodyAsString();

if (isSuccess(status)) {
result = new RemoteOperationResult(true, status, get.getResponseHeaders());
result = new RemoteOperationResult<>(true, status, get.getResponseHeaders());
Log_OC.d(this, "Successful response: " + response);

// Parse the response
notifications.add(parseResult(response));
result.setNotificationData(notifications);
Notification notification = parseResult(response);
result.setResultData(notification);
} else {
result = new RemoteOperationResult(false, status, get.getResponseHeaders());
result = new RemoteOperationResult<>(false, status, get.getResponseHeaders());
Log_OC.e(this, "Failed response while getting user notifications ");
if (response != null) {
Log_OC.e(this, "*** status code: " + status + " ; response message: " + response);
Expand All @@ -102,7 +98,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(this, "Exception while getting remote notifications", e);
} finally {
if (get != null) {
Expand All @@ -113,9 +109,8 @@ protected RemoteOperationResult run(OwnCloudClient client) {
return result;
}

private Notification parseResult(String response) throws JSONException {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject) jsonParser.parse(response);
private Notification parseResult(String response) {
JsonObject jo = (JsonObject) JsonParser.parseString(response);
JsonObject jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA);

Gson gson = new Gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.List;
Expand All @@ -52,7 +51,7 @@
* 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 {
public class GetNotificationsRemoteOperation extends RemoteOperation<List<Notification>> {

// OCS Route
private static final String OCS_ROUTE_LIST_V12_AND_UP =
Expand All @@ -66,9 +65,9 @@ public class GetNotificationsRemoteOperation extends RemoteOperation {
private static final String NODE_DATA = "data";

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
int status = -1;
protected RemoteOperationResult<List<Notification>> run(OwnCloudClient client) {
RemoteOperationResult<List<Notification>> result;
int status;
GetMethod get = null;
List<Notification> notifications;
String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP;
Expand All @@ -82,14 +81,14 @@ protected RemoteOperationResult run(OwnCloudClient client) {
String response = get.getResponseBodyAsString();

if (isSuccess(status)) {
result = new RemoteOperationResult(true, status, get.getResponseHeaders());
result = new RemoteOperationResult<>(true, status, get.getResponseHeaders());
Log_OC.d(TAG, "Successful response: " + response);

// Parse the response
notifications = parseResult(response);
result.setNotificationData(notifications);
result.setResultData(notifications);
} else {
result = new RemoteOperationResult(false, status, get.getResponseHeaders());
result = new RemoteOperationResult<>(false, status, get.getResponseHeaders());
Log_OC.e(TAG, "Failed response while getting user notifications ");
if (response != null) {
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
Expand All @@ -98,7 +97,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while getting remote notifications", e);
} finally {
if (get != null) {
Expand All @@ -109,13 +108,13 @@ protected RemoteOperationResult run(OwnCloudClient client) {
return result;
}

private List<Notification> parseResult(String response) throws JSONException {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(response);
private List<Notification> 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<List<Notification>>(){}.getType();
Type listType = new TypeToken<List<Notification>>() {
}.getType();

return gson.fromJson(jsonDataArray, listType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.Utf8PostMethod;
import org.json.JSONException;

import java.lang.reflect.Type;

public class RegisterAccountDeviceForNotificationsOperation extends RemoteOperation {
public class RegisterAccountDeviceForNotificationsOperation extends RemoteOperation<PushResponse> {
// OCS Route
private static final String OCS_ROUTE =
"/ocs/v2.php/apps/notifications/api/v2/push" + JSON_FORMAT;
Expand All @@ -60,9 +59,9 @@ public class RegisterAccountDeviceForNotificationsOperation extends RemoteOperat
private static final String PROXY_SERVER = "proxyServer";
private static final String INVALID_SESSION_TOKEN = "INVALID_SESSION_TOKEN";

private String pushTokenHash;
private String devicePublicKey;
private String proxyServer;
private final String pushTokenHash;
private final String devicePublicKey;
private final String proxyServer;

public RegisterAccountDeviceForNotificationsOperation(String pushTokenHash,
String devicePublicKey,
Expand All @@ -73,10 +72,9 @@ public RegisterAccountDeviceForNotificationsOperation(String pushTokenHash,
}

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
protected RemoteOperationResult<PushResponse> run(OwnCloudClient client) {
RemoteOperationResult<PushResponse> result;
int status;
PushResponse pushResponse;
Utf8PostMethod post = null;

try {
Expand All @@ -85,28 +83,28 @@ protected RemoteOperationResult run(OwnCloudClient client) {
post.setParameter(PUSH_TOKEN_HASH, pushTokenHash);
post.setParameter(DEVICE_PUBLIC_KEY, devicePublicKey);
post.setParameter(PROXY_SERVER, proxyServer);

post.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

status = client.executeMethod(post);
String response = post.getResponseBodyAsString();

if (isSuccess(status)) {
result = new RemoteOperationResult(true, status, post.getResponseHeaders());
result = new RemoteOperationResult<>(true, status, post.getResponseHeaders());
Log_OC.d(TAG, "Successful response: " + response);

// Parse the response
pushResponse = parseResult(response);
result.setPushResponseData(pushResponse);
PushResponse pushResponse = parseResult(response);
result.setResultData(pushResponse);
} else {
if (isInvalidSessionToken(response)) {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD);
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD);
} else {
result = new RemoteOperationResult(false, status, post.getResponseHeaders());
result = new RemoteOperationResult<>(false, status, post.getResponseHeaders());
}
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while registering device for notifications", e);
} finally {
if (post != null) {
Expand All @@ -116,20 +114,19 @@ protected RemoteOperationResult run(OwnCloudClient client) {
return result;
}

private PushResponse parseResult(String response) throws JSONException {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(response);
private PushResponse parseResult(String response) {
JsonObject jo = (JsonObject) JsonParser.parseString(response);
JsonObject jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA);

Gson gson = new Gson();
Type pushResponseType = new TypeToken<PushResponse>(){}.getType();
Type pushResponseType = new TypeToken<PushResponse>() {
}.getType();

return gson.fromJson(jsonDataObject, pushResponseType);
}

private boolean isInvalidSessionToken(String response) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject)jsonParser.parse(response);
JsonObject jsonObject = (JsonObject) JsonParser.parseString(response);
String message = jsonObject.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA).get(MESSAGE).getAsString();

return INVALID_SESSION_TOKEN.equals(message);
Expand Down
2 changes: 1 addition & 1 deletion scripts/analysis/findbugs-results.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140
134

0 comments on commit 62d41cc

Please sign in to comment.