diff --git a/pom.xml b/pom.xml index e811ffda..0254f0f6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,21 +4,21 @@ 4.0.0 com.owncloud.android - oc_framework + owncloud-android-library ${owncloud.version} jar - oc_framework for Owncloud Android + owncloud-android-library for Owncloud Android 1.5.1-SNAPSHOT 1.6 - 4.4_r1 + 4.4.2_r2 19 - oc_framwork for Owncloud for Android + owncloud-android-library for Owncloud for Android diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index a68b43db..abfa049c 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -97,7 +97,8 @@ public class RemoteOperationResult implements Serializable { ACCOUNT_EXCEPTION, ACCOUNT_NOT_NEW, ACCOUNT_NOT_THE_SAME, - INVALID_CHARACTER_IN_NAME + INVALID_CHARACTER_IN_NAME, + SHARE_NOT_FOUND } private boolean mSuccess = false; diff --git a/src/com/owncloud/android/lib/operations/remote/RemoveRemoteShareOperation.java b/src/com/owncloud/android/lib/operations/remote/RemoveRemoteShareOperation.java new file mode 100644 index 00000000..67ebc7a1 --- /dev/null +++ b/src/com/owncloud/android/lib/operations/remote/RemoveRemoteShareOperation.java @@ -0,0 +1,118 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * 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.operations.remote; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.apache.commons.httpclient.HttpStatus; +import org.apache.jackrabbit.webdav.client.methods.DeleteMethod; + +import android.util.Log; + +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.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.resources.shares.ShareUtils; +import com.owncloud.android.lib.resources.shares.ShareXMLParser; + +/** + * Remove a share + * + * @author masensio + * + */ + +public class RemoveRemoteShareOperation extends RemoteOperation { + + private static final String TAG = RemoveRemoteShareOperation.class.getSimpleName(); + + private int mIdShare; + + /** + * Constructor + * + * @param idShare Share ID + */ + + public RemoveRemoteShareOperation(int idShare) { + mIdShare = idShare; + + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + int status = -1; + + DeleteMethod delete = null; + + try { + String id = "/" + String.valueOf(mIdShare); + delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE + id); + Log.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE + id); + + status = client.executeMethod(delete); + + if(isSuccess(status)) { + String response = delete.getResponseBodyAsString(); + Log.d(TAG, "Successful response: " + response); + + result = new RemoteOperationResult(ResultCode.OK); + + // Parse xml response + // convert String into InputStream + InputStream is = new ByteArrayInputStream(response.getBytes()); + ShareXMLParser xmlParser = new ShareXMLParser(); + xmlParser.parseXMLResponse(is); + if (xmlParser.isSuccess()) { + result = new RemoteOperationResult(ResultCode.OK); + } else if (xmlParser.isFileNotFound()){ + result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); + } else { + result = new RemoteOperationResult(false, status, delete.getResponseHeaders()); + } + + Log.i(TAG, "Unshare " + id + ": " + result.getLogMessage()); + } else { + result = new RemoteOperationResult(false, status, delete.getResponseHeaders()); + } + } catch (Exception e) { + result = new RemoteOperationResult(e); + Log.e(TAG, "Unshare Link Exception " + result.getLogMessage(), e); + + } finally { + if (delete != null) + delete.releaseConnection(); + } + return result; + } + + + private boolean isSuccess(int status) { + return (status == HttpStatus.SC_OK); + } +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java index 1b470448..d6ebc343 100644 --- a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java @@ -139,7 +139,7 @@ public class CreateRemoteShareOperation extends RemoteOperation { result.setData(sharesObjects); } } else if (xmlParser.isFileNotFound()){ - result = new RemoteOperationResult(ResultCode.FILE_NOT_FOUND); + result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); } else { result = new RemoteOperationResult(false, status, post.getResponseHeaders()); diff --git a/tests/src/com/owncloud/android/lib/test_project/TestActivity.java b/tests/src/com/owncloud/android/lib/test_project/TestActivity.java index 07bfaec8..2beda565 100644 --- a/tests/src/com/owncloud/android/lib/test_project/TestActivity.java +++ b/tests/src/com/owncloud/android/lib/test_project/TestActivity.java @@ -37,7 +37,10 @@ import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation; import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; +import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation; import com.owncloud.android.lib.resources.shares.GetRemoteSharesOperation; +import com.owncloud.android.lib.operations.remote.RemoveRemoteShareOperation; +import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.test_project.R; import android.net.Uri; @@ -190,6 +193,11 @@ public class TestActivity extends Activity { return result; } + + /** Access to the library method to Get Shares + * + * @return + */ public RemoteOperationResult getShares(){ GetRemoteSharesOperation getOperation = new GetRemoteSharesOperation(); @@ -197,4 +205,47 @@ public class TestActivity extends Activity { return result; } + + /** Access to the library method to Create Share + * @param path Full path of the file/folder being shared. Mandatory argument + * @param shareType ‘0’ = user, ‘1’ = group, ‘3’ = Public link. Mandatory argument + * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1 + * @param publicUpload If ‘false’ (default) public cannot upload to a public shared folder. + * If ‘true’ public can upload to a shared folder. Only available for public link shares + * @param password Password to protect a public link share. Only available for public link shares + * @param permissions 1 - Read only – Default for “public” shares + * 2 - Update + * 4 - Create + * 8 - Delete + * 16- Re-share + * 31- All above – Default for “private” shares + * For user or group shares. + * To obtain combinations, add the desired values together. + * For instance, for “Re-Share”, “delete”, “read”, “update”, add 16+8+2+1 = 27. + * + * @return + */ + public RemoteOperationResult createShare(String path, ShareType shareType, String shareWith, boolean publicUpload, + String password, int permissions){ + + CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith, publicUpload, password, permissions); + RemoteOperationResult result = createOperation.execute(mClient); + + return result; + } + + + /** + * Access to the library method to Remove Share + * + * @param idShare Share ID + */ + + public RemoteOperationResult removeShare(int idShare) { + RemoveRemoteShareOperation removeOperation = new RemoveRemoteShareOperation(idShare); + RemoteOperationResult result = removeOperation.execute(mClient); + + return result; + + } } diff --git a/tests/test_cases/src/com/owncloud/android/lib/test_project/test/CreateShareTest.java b/tests/test_cases/src/com/owncloud/android/lib/test_project/test/CreateShareTest.java new file mode 100644 index 00000000..927f472a --- /dev/null +++ b/tests/test_cases/src/com/owncloud/android/lib/test_project/test/CreateShareTest.java @@ -0,0 +1,59 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * 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.test_project.test; + +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.resources.shares.ShareType; +import com.owncloud.android.lib.test_project.TestActivity; + +import android.test.ActivityInstrumentationTestCase2; + +public class CreateShareTest extends ActivityInstrumentationTestCase2 { + + /* File to share.*/ + private final String mFileToShare = "/fileToShare.png"; + + private TestActivity mActivity; + + public CreateShareTest() { + super(TestActivity.class); + + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + setActivityInitialTouchMode(false); + mActivity = getActivity(); + } + + /** + * Test Create Share: the server must support SHARE API + */ + public void testCreateShare() { + RemoteOperationResult result = mActivity.createShare(mFileToShare, ShareType.PUBLIC_LINK, "", false, "", 1); + assertTrue(result.isSuccess()); + } +} diff --git a/tests/test_cases/src/com/owncloud/android/lib/test_project/test/GetSharesTest.java b/tests/test_cases/src/com/owncloud/android/lib/test_project/test/GetSharesTest.java index 45577883..bc6e3804 100644 --- a/tests/test_cases/src/com/owncloud/android/lib/test_project/test/GetSharesTest.java +++ b/tests/test_cases/src/com/owncloud/android/lib/test_project/test/GetSharesTest.java @@ -24,7 +24,7 @@ package com.owncloud.android.lib.test_project.test; -import com.owncloud.android.lib.operations.common.RemoteOperationResult; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.test_project.TestActivity; import android.test.ActivityInstrumentationTestCase2; diff --git a/tests/test_cases/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java b/tests/test_cases/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java new file mode 100644 index 00000000..ce0ca1bd --- /dev/null +++ b/tests/test_cases/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java @@ -0,0 +1,74 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) + * + * 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.test_project.test; + +import com.owncloud.android.lib.resources.shares.OCShare; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.test_project.TestActivity; + +import android.test.ActivityInstrumentationTestCase2; +import android.util.Log; + +public class RemoveShareTest extends ActivityInstrumentationTestCase2 { + + private static final String TAG = RemoveShareTest.class.getSimpleName(); + + private TestActivity mActivity; + + public RemoveShareTest() { + super(TestActivity.class); + + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + setActivityInitialTouchMode(false); + mActivity = getActivity(); + } + + /** + * Test Remove Share: the server must support SHARE API + */ + public void testRemoveShare() { + // Get the shares + RemoteOperationResult result = mActivity.getShares(); + if (result.isSuccess()) { + int size = result.getData().size(); + + if (size > 0) { + OCShare share = ((OCShare) result.getData().get(size -1)); + long id = share.getIdRemoteShared(); + Log.d(TAG, "File to unshare: " + share.getPath() ); + result = mActivity.removeShare((int) id); // Unshare + assertTrue(result.isSuccess()); + } else { + assertTrue(true); + } + } else { + assertTrue(true); + } + } +}