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 /* ownCloud Android Library is available under MIT license
<<<<<<< HEAD
* @author masensio * @author masensio
* @author David A. Velasco * @author David A. Velasco
* @author David González Verdugo * @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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
@ -203,8 +197,8 @@ public class CreateRemoteShareOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
RemoteOperationResult result = null;
try { try {
Uri requestUri = client.getBaseUri(); Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon(); Uri.Builder uriBuilder = requestUri.buildUpon();
@ -212,7 +206,8 @@ public class CreateRemoteShareOperation extends RemoteOperation {
final Request request = new Request.Builder() final Request request = new Request.Builder()
.url(uriBuilder.build().toString()) .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) .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build(); .build();

View File

@ -1,14 +1,8 @@
/* ownCloud Android Library is available under MIT license /* ownCloud Android Library is available under MIT license
<<<<<<< HEAD
* @author David A. Velasco * @author David A. Velasco
* @author David González Verdugo * @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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
@ -81,7 +75,6 @@ public class GetRemoteShareOperation extends RemoteOperation {
Response response = client.executeHttpMethod(getMethod); Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) { if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()

View File

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

View File

@ -3,23 +3,23 @@
* @author David A. Velasco * @author David A. Velasco
* @author David González Verdugo * @author David González Verdugo
* Copyright (C) 2018 ownCloud GmbH. * Copyright (C) 2018 ownCloud GmbH.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
* *
@ -29,19 +29,25 @@ package com.owncloud.android.lib.resources.shares;
import android.net.Uri; 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.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import org.apache.commons.httpclient.HttpStatus; import okhttp3.HttpUrl;
import org.apache.commons.httpclient.NameValuePair; import okhttp3.Request;
import org.apache.commons.httpclient.methods.GetMethod; import okhttp3.Response;
/** /**
* Provide a list shares for a specific file. * Provide a list shares for a specific file.
* The input is the full path of the desired 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. * 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 { public class GetRemoteSharesForFileOperation extends RemoteOperation {
@ -75,61 +81,53 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result; RemoteOperationResult result;
int status;
GetMethod get = null;
try { try {
Uri requestUri = client.getBaseUri(); Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon(); Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
// Get Method HttpUrl.Builder httpBuilder = HttpUrl
get = new GetMethod(uriBuilder.build().toString()); .parse(uriBuilder.build().toString())
.newBuilder();
// Add Parameters to Get Method httpBuilder.addQueryParameter(PARAM_PATH, mRemoteFilePath);
get.setQueryString(new NameValuePair[]{ httpBuilder.addQueryParameter(PARAM_RESHARES, String.valueOf(mReshares));
new NameValuePair(PARAM_PATH, mRemoteFilePath), httpBuilder.addQueryParameter(PARAM_SUBFILES, String.valueOf(mSubfiles));
new NameValuePair(PARAM_RESHARES, String.valueOf(mReshares)),
new NameValuePair(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)) { Response response = client.executeHttpMethod(getMethod);
String response = get.getResponseBodyAsString();
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
); );
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response); result = parser.parse(response.body().string());
if (result.isSuccess()) { if (result.isSuccess()) {
Log_OC.d(TAG, "Got " + result.getData().size() + " shares"); Log_OC.d(TAG, "Got " + result.getData().size() + " shares");
} }
} else { } else {
result = new RemoteOperationResult(false, get); result = new RemoteOperationResult(false, getMethod.getRequest(), response);
} }
} catch (Exception e) { } catch (Exception e) {
result = new RemoteOperationResult(e); result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting shares", e); Log_OC.e(TAG, "Exception while getting shares", e);
} finally {
if (get != null) {
get.releaseConnection();
}
} }
return result; return result;
} }
private boolean isSuccess(int status) { private boolean isSuccess(int status) {
return (status == HttpStatus.SC_OK); return (status == HttpConstants.HTTP_OK);
} }
} }

View File

@ -1,24 +1,23 @@
/* ownCloud Android Library is available under MIT license /* ownCloud Android Library is available under MIT license
* @author masensio *
* @author David A. Velasco * Copyright (C) 2018 ownCloud GmbH.
* Copyright (C) 2016 ownCloud GmbH. *
*
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
* *
@ -29,45 +28,46 @@ package com.owncloud.android.lib.resources.shares;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.common.OwnCloudClient; 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.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import org.apache.commons.httpclient.HttpStatus; import okhttp3.Request;
import org.apache.commons.httpclient.methods.GetMethod; import okhttp3.Response;
/** /**
* Get the data from the server about ALL the known shares owned by the requester. * 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 { public class GetRemoteSharesOperation extends RemoteOperation {
private static final String TAG = GetRemoteSharesOperation.class.getSimpleName(); private static final String TAG = GetRemoteSharesOperation.class.getSimpleName();
public GetRemoteSharesOperation() {
}
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null; RemoteOperationResult result;
int status = -1;
// Get Method
GetMethod get = null;
// Get the response
try { try {
Uri requestUri = client.getBaseUri(); Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon(); Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
get = new GetMethod(uriBuilder.build().toString()); Request request = new Request.Builder()
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); .url(uriBuilder.build().toString())
status = client.executeMethod(get); .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
if (isSuccess(status)) { GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
String response = get.getResponseBodyAsString();
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
@ -75,26 +75,20 @@ public class GetRemoteSharesOperation extends RemoteOperation {
); );
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response); result = parser.parse(response.body().string());
} else { } else {
result = new RemoteOperationResult(false, get); result = new RemoteOperationResult(false, getMethod.getRequest(), response);
} }
} catch (Exception e) { } catch (Exception e) {
result = new RemoteOperationResult(e); result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting remote shares ", e); Log_OC.e(TAG, "Exception while getting remote shares ", e);
} finally {
if (get != null) {
get.releaseConnection();
}
} }
return result; return result;
} }
private boolean isSuccess(int status) { private boolean isSuccess(int status) {
return (status == HttpStatus.SC_OK); return (status == HttpConstants.HTTP_OK);
} }
}
}