1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-07-23 09:56:02 +00:00

Implement get shares operations using the new wrapper methods

This commit is contained in:
davigonz 2018-06-07 13:13:56 +02:00
parent 1a7e6a475d
commit 7d534c90ab
5 changed files with 97 additions and 117 deletions

View File

@ -1,15 +1,9 @@
/* ownCloud Android Library is available under MIT license
<<<<<<< HEAD
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
* Copyright (C) 2018 ownCloud GmbH.
* Copyright (C) 2018 ownCloud GmbH
*
=======
*
* Copyright (C) 2018 ownCloud GmbH.
*
>>>>>>> Use the new wrapper methods with CreateRemoteShareOperation and GetRemoteShareOperation
* 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
@ -203,8 +197,8 @@ public class CreateRemoteShareOperation extends RemoteOperation {
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
RemoteOperationResult result = null;
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
@ -212,7 +206,8 @@ public class CreateRemoteShareOperation extends RemoteOperation {
final Request request = new Request.Builder()
.url(uriBuilder.build().toString())
.header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
.header("Content-Type", "application/x-www-form-urlencoded; " +
"charset=utf-8")
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();

View File

@ -1,14 +1,8 @@
/* ownCloud Android Library is available under MIT license
<<<<<<< HEAD
* @author David A. Velasco
* @author David González Verdugo
* Copyright (C) 2018 ownCloud GmbH.
*
=======
*
* Copyright (C) 2018 ownCloud GmbH.
*
>>>>>>> Use the new wrapper methods with CreateRemoteShareOperation and GetRemoteShareOperation
* 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
@ -81,7 +75,6 @@ public class GetRemoteShareOperation extends RemoteOperation {
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser()

View File

@ -31,17 +31,22 @@ package com.owncloud.android.lib.resources.shares;
import android.net.Uri;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.nonwebdav.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 org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
/**
* Created by masensio on 08/10/2015.
*
@ -62,6 +67,10 @@ import java.util.ArrayList;
*
* Status codes:
* 100 - successful
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
public class GetRemoteShareesOperation extends RemoteOperation{
@ -81,7 +90,6 @@ public class GetRemoteShareesOperation extends RemoteOperation{
private static final String VALUE_FORMAT = "json";
private static final String VALUE_ITEM_TYPE = "file"; // to get the server search for users / groups
// JSON Node names
private static final String NODE_OCS = "ocs";
private static final String NODE_DATA = "data";
@ -114,8 +122,6 @@ public class GetRemoteShareesOperation extends RemoteOperation{
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
int status;
GetMethod get = null;
try{
Uri requestUri = client.getBaseUri();
@ -127,18 +133,19 @@ public class GetRemoteShareesOperation extends RemoteOperation{
uriBuilder.appendQueryParameter(PARAM_PAGE, String.valueOf(mPage));
uriBuilder.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(mPerPage));
// Get Method
get = new GetMethod(uriBuilder.build().toString());
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
Request request = new Request.Builder()
.url(uriBuilder.build().toString())
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
status = client.executeMethod(get);
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
Response response = client.executeHttpMethod(getMethod);
if(isSuccess(status)) {
String response = get.getResponseBodyAsString();
if(isSuccess(response.code())) {
Log_OC.d(TAG, "Successful response: " + response);
// Parse the response
JSONObject respJSON = new JSONObject(response);
JSONObject respJSON = new JSONObject(response.body().string());
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
JSONObject respData = respOCS.getJSONObject(NODE_DATA);
JSONObject respExact = respData.getJSONObject(NODE_EXACT);
@ -166,36 +173,29 @@ public class GetRemoteShareesOperation extends RemoteOperation{
}
}
// Result
result = new RemoteOperationResult(true, get);
result = new RemoteOperationResult(OK);
result.setData(data);
Log_OC.d(TAG, "*** Get Users or groups completed " );
} else {
result = new RemoteOperationResult(false, get);
String response = get.getResponseBodyAsString();
result = new RemoteOperationResult(false, getMethod.getRequest(), response);
Log_OC.e(TAG, "Failed response while getting users/groups from the server ");
if (response != null) {
Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response);
Log_OC.e(TAG, "*** status code: " + response.code() + "; response message: " + response);
} else {
Log_OC.e(TAG, "*** status code: " + status);
Log_OC.e(TAG, "*** status code: " + response.code());
}
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting users/groups", e);
}
} finally {
if (get != null) {
get.releaseConnection();
}
}
return result;
}
private boolean isSuccess(int status) {
return (status == HttpStatus.SC_OK);
return (status == HttpConstants.HTTP_OK);
}
}

View File

@ -29,19 +29,25 @@ package com.owncloud.android.lib.resources.shares;
import android.net.Uri;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
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 org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
/**
* Provide a list shares for a specific file.
* The input is the full path of the desired file.
* The output is a list of everyone who has the file shared with them.
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
public class GetRemoteSharesForFileOperation extends RemoteOperation {
@ -75,61 +81,53 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation {
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
int status;
GetMethod get = null;
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
// Get Method
get = new GetMethod(uriBuilder.build().toString());
HttpUrl.Builder httpBuilder = HttpUrl
.parse(uriBuilder.build().toString())
.newBuilder();
// Add Parameters to Get Method
get.setQueryString(new NameValuePair[]{
new NameValuePair(PARAM_PATH, mRemoteFilePath),
new NameValuePair(PARAM_RESHARES, String.valueOf(mReshares)),
new NameValuePair(PARAM_SUBFILES, String.valueOf(mSubfiles))
});
httpBuilder.addQueryParameter(PARAM_PATH, mRemoteFilePath);
httpBuilder.addQueryParameter(PARAM_RESHARES, String.valueOf(mReshares));
httpBuilder.addQueryParameter(PARAM_SUBFILES, String.valueOf(mSubfiles));
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
Request request = new Request.Builder()
.url(httpBuilder.build())
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
status = client.executeMethod(get);
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
if (isSuccess(status)) {
String response = get.getResponseBodyAsString();
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser()
);
parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response);
result = parser.parse(response.body().string());
if (result.isSuccess()) {
Log_OC.d(TAG, "Got " + result.getData().size() + " shares");
}
} else {
result = new RemoteOperationResult(false, get);
result = new RemoteOperationResult(false, getMethod.getRequest(), response);
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting shares", e);
}
} finally {
if (get != null) {
get.releaseConnection();
}
}
return result;
}
private boolean isSuccess(int status) {
return (status == HttpStatus.SC_OK);
return (status == HttpConstants.HTTP_OK);
}
}

View File

@ -1,7 +1,6 @@
/* ownCloud Android Library is available under MIT license
* @author masensio
* @author David A. Velasco
* Copyright (C) 2016 ownCloud GmbH.
*
* Copyright (C) 2018 ownCloud 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
@ -29,45 +28,46 @@ package com.owncloud.android.lib.resources.shares;
import android.net.Uri;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.nonwebdav.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 org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import okhttp3.Request;
import okhttp3.Response;
/**
* Get the data from the server about ALL the known shares owned by the requester.
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
public class GetRemoteSharesOperation extends RemoteOperation {
private static final String TAG = GetRemoteSharesOperation.class.getSimpleName();
public GetRemoteSharesOperation() {
}
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
int status = -1;
RemoteOperationResult result;
// Get Method
GetMethod get = null;
// Get the response
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
get = new GetMethod(uriBuilder.build().toString());
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
status = client.executeMethod(get);
Request request = new Request.Builder()
.url(uriBuilder.build().toString())
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
if (isSuccess(status)) {
String response = get.getResponseBodyAsString();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
@ -75,26 +75,20 @@ public class GetRemoteSharesOperation extends RemoteOperation {
);
parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response);
result = parser.parse(response.body().string());
} else {
result = new RemoteOperationResult(false, get);
result = new RemoteOperationResult(false, getMethod.getRequest(), response);
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting remote shares ", e);
}
} finally {
if (get != null) {
get.releaseConnection();
}
}
return result;
}
private boolean isSuccess(int status) {
return (status == HttpStatus.SC_OK);
return (status == HttpConstants.HTTP_OK);
}
}