Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky committed Oct 31, 2024
1 parent 0bb570a commit 1787523
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
import com.owncloud.android.lib.resources.status.GetStatusRemoteOperation
import com.owncloud.android.lib.resources.status.NextcloudVersion
import com.owncloud.android.lib.resources.status.OwnCloudVersion
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assume
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test

class CreateShareRemoteOperationIT : AbstractIT() {
@Before
fun before() {
val result = GetStatusRemoteOperation(context).execute(client)
Assert.assertTrue(result.isSuccess)
val data = result.data as ArrayList<Any>
val ownCloudVersion = data[0] as OwnCloudVersion
Assume.assumeTrue(ownCloudVersion.isNewerOrEqual(NextcloudVersion.nextcloud_24))
assertTrue(result.isSuccess)

val data = result.resultData
val ownCloudVersion = data?.first
assumeTrue(ownCloudVersion?.isNewerOrEqual(NextcloudVersion.nextcloud_24) == true)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,26 +495,6 @@ class RemoteOperationResult<T> : Serializable {
}
}

@get:Deprecated("use getResultData() instead")
@set:Deprecated("use setResultData() instead")
@Suppress("TooGenericExceptionThrown")
var data: java.util.ArrayList<out Any>?
get() {
if (!isSuccess) {
throw RuntimeException("Accessing result data after operation failed!")
}
return if (mData != null) {
mData
} else if (resultData is ArrayList<*>) {
resultData as ArrayList<*>?
} else {
null
}
}
set(files) {
mData = files as ArrayList<Any>?
}

val isCancelled = code == ResultCode.CANCELLED

val isSslRecoverableException = code == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
import org.apache.commons.httpclient.methods.Utf8PostMethod;
import org.json.JSONObject;

import java.util.ArrayList;


/**
* Remote operation to store the folder metadata
*/

public class StoreMetadataRemoteOperation extends RemoteOperation {
public class StoreMetadataRemoteOperation extends RemoteOperation<String> {

private static final String TAG = StoreMetadataRemoteOperation.class.getSimpleName();
private static final int SYNC_READ_TIMEOUT = 40000;
Expand Down Expand Up @@ -51,9 +49,9 @@ public StoreMetadataRemoteOperation(long fileId, String encryptedMetadataJson) {
* @param client Client object
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
protected RemoteOperationResult<String> run(OwnCloudClient client) {
Utf8PostMethod postMethod = null;
RemoteOperationResult result;
RemoteOperationResult<String> result;

try {
// remote request
Expand All @@ -71,16 +69,14 @@ protected RemoteOperationResult run(OwnCloudClient client) {
String metadata = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA)
.get(NODE_META_DATA);

result = new RemoteOperationResult(true, postMethod);
ArrayList<Object> keys = new ArrayList<>();
keys.add(metadata);
result.setData(keys);
result = new RemoteOperationResult<>(true, postMethod);
result.setResultData(metadata);
} else {
result = new RemoteOperationResult(false, postMethod);
result = new RemoteOperationResult<>(false, postMethod);
client.exhaustResponse(postMethod.getResponseBodyAsStream());
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Storing of metadata for folder " + fileId + " failed: " + result.getLogMessage(),
result.getException());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;


/**
* Remote operation to update the folder metadata
*/

public class UpdateMetadataRemoteOperation extends RemoteOperation {
public class UpdateMetadataRemoteOperation extends RemoteOperation<String> {

private static final String TAG = UpdateMetadataRemoteOperation.class.getSimpleName();
private static final int SYNC_READ_TIMEOUT = 40000;
Expand Down Expand Up @@ -56,9 +55,9 @@ public UpdateMetadataRemoteOperation(long fileId, String encryptedMetadataJson,
* @param client Client object
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
protected RemoteOperationResult<String> run(OwnCloudClient client) {
PutMethod putMethod = null;
RemoteOperationResult result;
RemoteOperationResult<String> result;

try {
// remote request
Expand All @@ -85,16 +84,14 @@ protected RemoteOperationResult run(OwnCloudClient client) {
String metadata = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA)
.get(NODE_META_DATA);

result = new RemoteOperationResult(true, putMethod);
ArrayList<Object> keys = new ArrayList<>();
keys.add(metadata);
result.setData(keys);
result = new RemoteOperationResult<>(true, putMethod);
result.setResultData(metadata);
} else {
result = new RemoteOperationResult(false, putMethod);
result = new RemoteOperationResult<>(false, putMethod);
client.exhaustResponse(putMethod.getResponseBodyAsStream());
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Storing of metadata for folder " + fileId + " failed: " + result.getLogMessage(),
result.getException());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected RemoteOperationResult<List<OCShare>> run(OwnCloudClient client) {
result = parser.parse(response);

if (result.isSuccess()) {
Log_OC.d(TAG, "Got " + result.getData().size() + " shares");
Log_OC.d(TAG, "Got " + result.getResultData().size() + " shares");
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* Allow updating several parameters, triggering a request to the server per parameter.
*/
public class UpdateShareRemoteOperation extends RemoteOperation {
public class UpdateShareRemoteOperation extends RemoteOperation<List<OCShare>> {

private static final String TAG = GetShareRemoteOperation.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.util.Pair;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
Expand All @@ -29,15 +30,13 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
* Checks if the server is valid and if the server supports the Share API
*
* @author David A. Velasco
* @author masensio
*/
public class GetStatusRemoteOperation extends RemoteOperation {
public class GetStatusRemoteOperation extends RemoteOperation<Pair<OwnCloudVersion, Boolean>> {

/**
* Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs.
Expand All @@ -53,7 +52,7 @@ public class GetStatusRemoteOperation extends RemoteOperation {
private static final String PROTOCOL_HTTP = "http://";
private static final int UNTRUSTED_DOMAIN_ERROR_CODE = 15;

private RemoteOperationResult mLatestResult;
private RemoteOperationResult<Pair<OwnCloudVersion, Boolean>> mLatestResult;
private Context mContext;

public GetStatusRemoteOperation(Context context) {
Expand All @@ -74,7 +73,7 @@ private boolean tryConnection(OwnCloudClient client) {
client.setFollowRedirects(false);
boolean isRedirectToNonSecureConnection = false;
int status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get);
mLatestResult = new RemoteOperationResult<>((status == HttpStatus.SC_OK), get);

String redirectedLocation = mLatestResult.getRedirectedLocation();
while (redirectedLocation != null && redirectedLocation.length() > 0
Expand All @@ -87,7 +86,7 @@ private boolean tryConnection(OwnCloudClient client) {
get.releaseConnection();
get = new GetMethod(redirectedLocation);
status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get);
mLatestResult = new RemoteOperationResult<>((status == HttpStatus.SC_OK), get);
redirectedLocation = mLatestResult.getRedirectedLocation();
}

Expand All @@ -96,7 +95,7 @@ private boolean tryConnection(OwnCloudClient client) {
if (status == HttpStatus.SC_OK) {
JSONObject json = new JSONObject(response);
if (!json.getBoolean(NODE_INSTALLED)) {
mLatestResult = new RemoteOperationResult(
mLatestResult = new RemoteOperationResult<>(
RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
} else {
boolean extendedSupport = false;
Expand All @@ -108,24 +107,21 @@ private boolean tryConnection(OwnCloudClient client) {
OwnCloudVersion ocVersion = new OwnCloudVersion(version);

if (!ocVersion.isVersionValid()) {
mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
} else {
// success
if (isRedirectToNonSecureConnection) {
mLatestResult = new RemoteOperationResult(
mLatestResult = new RemoteOperationResult<>(
RemoteOperationResult.ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION);
} else {
mLatestResult = new RemoteOperationResult(
mLatestResult = new RemoteOperationResult<>(
baseUrlSt.startsWith(PROTOCOL_HTTPS) ?
RemoteOperationResult.ResultCode.OK_SSL :
RemoteOperationResult.ResultCode.OK_NO_SSL
);
}

ArrayList<Object> data = new ArrayList<>();
data.add(ocVersion);
data.add(extendedSupport);
mLatestResult.setData(data);
mLatestResult.setResultData(new Pair<>(ocVersion, extendedSupport));
retval = true;
}
}
Expand All @@ -134,22 +130,22 @@ private boolean tryConnection(OwnCloudClient client) {
JSONObject json = new JSONObject(response);

if (json.getInt("code") == UNTRUSTED_DOMAIN_ERROR_CODE) {
mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNTRUSTED_DOMAIN);
mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.UNTRUSTED_DOMAIN);
} else {
mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
mLatestResult = new RemoteOperationResult<>(false, status, get.getResponseHeaders());
}
} catch (JSONException e) {
mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
mLatestResult = new RemoteOperationResult<>(false, status, get.getResponseHeaders());
}
} else {
mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
mLatestResult = new RemoteOperationResult<>(false, status, get.getResponseHeaders());
}

} catch (JSONException e) {
mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);

} catch (Exception e) {
mLatestResult = new RemoteOperationResult(e);
mLatestResult = new RemoteOperationResult<>(e);

} finally {
if (get != null)
Expand Down Expand Up @@ -178,9 +174,9 @@ private boolean isOnline() {
}

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
protected RemoteOperationResult<Pair<OwnCloudVersion, Boolean>> run(OwnCloudClient client) {
if (!isOnline()) {
return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
return new RemoteOperationResult<>(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
}
String baseUriStr = client.getBaseUri().toString();
if (baseUriStr.startsWith(PROTOCOL_HTTP) || baseUriStr.startsWith(PROTOCOL_HTTPS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
* Gets avatar about the user logged in, if available
*/
public class GetUserAvatarRemoteOperation extends RemoteOperation {
public class GetUserAvatarRemoteOperation extends RemoteOperation<GetUserAvatarRemoteOperation.ResultData> {

private static final String TAG = GetUserAvatarRemoteOperation.class.getSimpleName();

Expand All @@ -51,8 +50,8 @@ public GetUserAvatarRemoteOperation(int dimension, String currentEtag) {
}

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
protected RemoteOperationResult<ResultData> run(OwnCloudClient client) {
RemoteOperationResult<ResultData> result = null;
GetMethod get = null;
InputStream inputStream = null;
BufferedInputStream bis = null;
Expand Down Expand Up @@ -92,7 +91,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
Header contentType = get.getResponseHeader(CONTENT_TYPE);
if (contentType == null || !contentType.getValue().startsWith("image")) {
Log_OC.e(TAG, "Not an image, failing with no avatar");
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.FILE_NOT_FOUND);
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.FILE_NOT_FOUND);
return result;
}
mimeType = contentType.getValue();
Expand All @@ -115,12 +114,12 @@ protected RemoteOperationResult run(OwnCloudClient client) {
result = createResult(get, bos.toByteArray(), mimeType);

} else {
result = new RemoteOperationResult(false, get);
result = new RemoteOperationResult<>(false, get);
client.exhaustResponse(get.getResponseBodyAsStream());
}

} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while getting OC user avatar", e);

} finally {
Expand Down Expand Up @@ -151,18 +150,16 @@ protected RemoteOperationResult run(OwnCloudClient client) {
return result;
}

private RemoteOperationResult createResult(GetMethod get, byte[] avatarData, String mimeType) throws IOException {
private RemoteOperationResult<ResultData> createResult(GetMethod get, byte[] avatarData, String mimeType) throws IOException {
// find out etag
String etag = WebdavUtils.INSTANCE.getEtagFromResponse(get);
if (etag.length() == 0) {
if (etag.isEmpty()) {
Log_OC.w(TAG, "Could not read Etag from avatar");
}

RemoteOperationResult result = new RemoteOperationResult(true, get);
RemoteOperationResult<ResultData> result = new RemoteOperationResult<>(true, get);
ResultData resultData = new ResultData(avatarData, mimeType, etag);
ArrayList<Object> data = new ArrayList<>();
data.add(resultData);
result.setData(data);
result.setResultData(resultData);
return result;
}

Expand Down
Loading

0 comments on commit 1787523

Please sign in to comment.