From 7d534c90ab990f8672819b123a17acb2c63cdbcc Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 7 Jun 2018 13:13:56 +0200 Subject: [PATCH] Implement get shares operations using the new wrapper methods --- .../shares/CreateRemoteShareOperation.java | 13 ++-- .../shares/GetRemoteShareOperation.java | 7 -- .../shares/GetRemoteShareesOperation.java | 52 +++++++------- .../GetRemoteSharesForFileOperation.java | 70 +++++++++--------- .../shares/GetRemoteSharesOperation.java | 72 +++++++++---------- 5 files changed, 97 insertions(+), 117 deletions(-) diff --git a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java index d1dd3fd4..921aafea 100644 --- a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java @@ -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(); diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java index 1ed1aeb3..8ae1b74f 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java @@ -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() diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.java index 3d88ceba..3562c3f4 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.java @@ -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); } -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java index 5e4eb7d5..c5141e0f 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java @@ -3,23 +3,23 @@ * @author David A. Velasco * @author David González Verdugo * 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 * 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, + * + * 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 + * 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. * @@ -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); } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java index 66c6615f..95519f06 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java @@ -1,24 +1,23 @@ /* 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 * 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, + * + * 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 + * 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. * @@ -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); } - - -} +} \ No newline at end of file