From c15ed18f8745309c67afb23a9ae864f73cc03441 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Wed, 9 Aug 2023 17:05:41 +0200 Subject: [PATCH 1/4] feat: Support access only collaboration --- .../java/com/box/sdk/BoxCollaborationIT.java | 32 +++++++++++- .../java/com/box/sdk/BoxCollaboration.java | 49 ++++++++++++++++++- .../CreateCollaboration201.json | 3 +- .../CreateFileCollaboration201.json | 3 +- .../GetCollaborationInfo200.json | 3 +- .../GetCollaborationOnFolder200.json | 6 ++- .../GetPendingCollaborationInfo200.json | 3 +- .../UpdateCollaboration200.json | 3 +- .../com/box/sdk/BoxCollaborationTest.java | 14 ++++-- 9 files changed, 100 insertions(+), 16 deletions(-) diff --git a/src/intTest/java/com/box/sdk/BoxCollaborationIT.java b/src/intTest/java/com/box/sdk/BoxCollaborationIT.java index 76d5ce36d..a0eb957dc 100644 --- a/src/intTest/java/com/box/sdk/BoxCollaborationIT.java +++ b/src/intTest/java/com/box/sdk/BoxCollaborationIT.java @@ -2,7 +2,6 @@ import static com.box.sdk.BoxApiProvider.jwtApiForServiceAccount; import static com.box.sdk.BoxCollaborationAllowlist.AllowlistDirection.INBOUND; -import static com.box.sdk.CleanupTools.deleteFile; import static com.box.sdk.CleanupTools.deleteFolder; import static com.box.sdk.UniqueTestFolder.getUniqueFolder; import static com.box.sdk.UniqueTestFolder.removeUniqueFolder; @@ -12,7 +11,10 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import com.eclipsesource.json.JsonObject; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -47,6 +49,7 @@ public void updateInfoSucceeds() { BoxCollaboration.Info collabInfo = folder.collaborate(collaboratorLogin, originalRole); assertThat(collabInfo.getRole(), is(equalTo(originalRole))); + assertNotNull(collabInfo.getIsAccessOnly()); BoxCollaboration collab = collabInfo.getResource(); collabInfo.setRole(newRole); @@ -149,7 +152,7 @@ public void singleFileCollabSucceeds() { assertEquals(2, numCollabs); } finally { - deleteFile(uploadedFile); + CleanupTools.deleteFile(uploadedFile); if (allowList != null) { allowList.delete(); } @@ -167,4 +170,29 @@ public void acceptPendingCollaboration() { } } + @Test + public void singleFileCollabWithAccessOnlySucceeds() { + HashMap collabsMap = new HashMap<>(); + BoxAPIConnection api = jwtApiForServiceAccount(); + String fileName = "[singleFileCollabSucceeds] Test File.txt"; + BoxFile uploadedFile = null; + try { + uploadedFile = uploadFileToUniqueFolderWithSomeContent(api, fileName); + JsonObject user = new JsonObject() + .add("login", TestConfig.getCollaborator()) + .add("type", "user"); + JsonObject file = new JsonObject() + .add("id", uploadedFile.getID()) + .add("type", "file"); + + BoxCollaboration.Role originalRole = BoxCollaboration.Role.VIEWER; + BoxCollaboration.Info collabInfo = BoxCollaboration.create(api, user, file, originalRole, + false, false, null, true); + + assertThat(collabInfo.getRole(), is(equalTo(originalRole))); + assertTrue(collabInfo.getIsAccessOnly()); + } finally { + CleanupTools.deleteFile(uploadedFile); + } + } } diff --git a/src/main/java/com/box/sdk/BoxCollaboration.java b/src/main/java/com/box/sdk/BoxCollaboration.java index 926e85b23..1b269aa60 100644 --- a/src/main/java/com/box/sdk/BoxCollaboration.java +++ b/src/main/java/com/box/sdk/BoxCollaboration.java @@ -25,7 +25,7 @@ public class BoxCollaboration extends BoxResource { */ public static final String[] ALL_FIELDS = {"type", "id", "item", "accessible_by", "role", "expires_at", "can_view_path", "status", "acknowledged_at", "created_by", - "created_at", "modified_at"}; + "created_at", "modified_at", "is_access_only"}; /** * Collaborations URL Template. @@ -67,7 +67,7 @@ public BoxCollaboration(BoxAPIConnection api, String id) { */ protected static BoxCollaboration.Info create(BoxAPIConnection api, JsonObject accessibleBy, JsonObject item, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath) { - return create(api, accessibleBy, item, role, notify, canViewPath, null); + return create(api, accessibleBy, item, role, notify, canViewPath, null, null); } /** @@ -91,6 +91,32 @@ protected static BoxCollaboration.Info create( Boolean canViewPath, Date expiresAt ) { + return create(api, accessibleBy, item, role, notify, canViewPath, expiresAt, null); + } + + /** + * Create a new collaboration object. + * + * @param api the API connection used to make the request. + * @param accessibleBy the JSON object describing who should be collaborated. + * @param item the JSON object describing which item to collaborate. + * @param role the role to give the collaborators. + * @param notify the user/group should receive email notification of the collaboration or not. + * @param canViewPath the view path collaboration feature is enabled or not. + * @param expiresAt the date the collaboration expires + * @param isAccessOnly the collaboration is an access only collaboration or not. + * @return info about the new collaboration. + */ + protected static BoxCollaboration.Info create( + BoxAPIConnection api, + JsonObject accessibleBy, + JsonObject item, + BoxCollaboration.Role role, + Boolean notify, + Boolean canViewPath, + Date expiresAt, + Boolean isAccessOnly + ) { String queryString = ""; if (notify != null) { @@ -113,6 +139,9 @@ protected static BoxCollaboration.Info create( if (expiresAt != null) { requestJSON.add("expires_at", BoxDateFormat.format(expiresAt)); } + if (isAccessOnly != null) { + requestJSON.add("is_access_only", isAccessOnly); + } BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); @@ -364,6 +393,7 @@ public class Info extends BoxResource.Info { private BoxItem.Info item; private String inviteEmail; private boolean canViewPath; + private boolean isAccessOnly; /** * Constructs an empty Info object. @@ -453,6 +483,18 @@ public void setCanViewPath(boolean canViewState) { this.addPendingChange("can_view_path", canViewState); } + /** + * Gets a boolean indicator weather "is access only" feature is enabled or not. This field is read only. + * It is used to indicate whether a collaboration is an Access Only Collaboration (AOC). + * When set to true, it separates access from interest by hiding collaborated items from the All Files page + * and the ALF stream. + * This means that users who have been granted access through AOCs will not see these items in their + * regular file view. + */ + public boolean getIsAccessOnly() { + return this.isAccessOnly; + } + /** * The email address used to invite an un-registered collaborator, if they are not a registered user. * @@ -583,6 +625,9 @@ protected void parseJSONMember(JsonObject.Member member) { case "can_view_path": this.canViewPath = value.asBoolean(); break; + case "is_access_only": + this.isAccessOnly = value.asBoolean(); + break; case "invite_email": this.inviteEmail = value.asString(); break; diff --git a/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json b/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json index 68cd5c0fc..e2bb56771 100644 --- a/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json +++ b/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json @@ -25,5 +25,6 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } diff --git a/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json b/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json index 22948ee83..5e149347b 100644 --- a/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json +++ b/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json @@ -31,5 +31,6 @@ "etag": "5", "sha1": "aa810e72823f3c3dad2d1d4488966f6f1e0a8a9d", "name": "1_1-4_bsp_ball_valve.pdf" - } + }, + "is_access_only": false } diff --git a/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json b/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json index f1840da64..19e7ad9a8 100644 --- a/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json +++ b/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json @@ -25,5 +25,6 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } diff --git a/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json b/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json index cd6ab3bbe..e0345e737 100644 --- a/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json +++ b/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json @@ -28,7 +28,8 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false }, { "type": "collaboration", @@ -57,7 +58,8 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } ] } diff --git a/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json b/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json index 07fa2b7c0..8651be3a2 100644 --- a/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json +++ b/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json @@ -22,7 +22,8 @@ }, "role": "editor", "acknowledged_at": null, - "item": null + "item": null, + "is_access_only": false } ], "limit": 100, diff --git a/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json b/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json index 579c48286..2cb48ee8c 100644 --- a/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json +++ b/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json @@ -25,5 +25,6 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } diff --git a/src/test/java/com/box/sdk/BoxCollaborationTest.java b/src/test/java/com/box/sdk/BoxCollaborationTest.java index 214c2ab83..e6231b4e1 100644 --- a/src/test/java/com/box/sdk/BoxCollaborationTest.java +++ b/src/test/java/com/box/sdk/BoxCollaborationTest.java @@ -4,8 +4,7 @@ import static com.box.sdk.http.ContentType.APPLICATION_JSON; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static java.lang.String.format; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.*; import com.eclipsesource.json.JsonObject; import com.github.tomakehurst.wiremock.client.WireMock; @@ -187,11 +186,13 @@ public void testCreateAndEditCollaborationSucceeds() { .add("item", folder) .add("role", BoxCollaboration.Role.EDITOR.toJSONString()) .add("can_view_path", false) - .add("expires_at", BoxDateFormat.format(expiresAt)); + .add("expires_at", BoxDateFormat.format(expiresAt)) + .add("is_access_only", false); JsonObject updateBody = new JsonObject() .add("role", BoxCollaboration.Role.VIEWER.toJSONString()) - .add("expires_at", BoxDateFormat.format(expiresAt)); + .add("expires_at", BoxDateFormat.format(expiresAt)) + .add("is_access_only", true); wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(createCollaborationURL)) .withRequestBody(WireMock.equalToJson(createBody.toString())) @@ -211,7 +212,7 @@ public void testCreateAndEditCollaborationSucceeds() { .withBody(editResult))); BoxCollaboration.Info collabInfo = BoxCollaboration.create(this.api, user, folder, BoxCollaboration.Role.EDITOR, - false, false, expiresAt); + false, false, expiresAt, false); assertEquals(BoxCollaboration.Status.ACCEPTED, collabInfo.getStatus()); assertEquals(BoxCollaboration.Role.EDITOR, collabInfo.getRole()); @@ -219,6 +220,8 @@ public void testCreateAndEditCollaborationSucceeds() { assertEquals(collabID, collabInfo.getID()); assertEquals(itemName, collabInfo.getItem().getName()); assertEquals(expiresAt, collabInfo.getExpiresAt()); + assertFalse(collabInfo.getIsAccessOnly()); + BoxCollaboration collaboration = new BoxCollaboration(this.api, collabID); collabInfo.setRole(BoxCollaboration.Role.VIEWER); @@ -254,6 +257,7 @@ public void testGetCollaborationInfoSucceeds() { assertEquals(createdByEmail, collabInfo.getCreatedBy().getLogin()); assertEquals(collabItemID, collabInfo.getItem().getID()); assertEquals(collabItemName, collabInfo.getItem().getName()); + assertFalse(collabInfo.getIsAccessOnly()); } @Test From 335ecabcb157bef60e5abd8b785253a50ff26af3 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Wed, 9 Aug 2023 17:14:47 +0200 Subject: [PATCH 2/4] Update code --- src/test/java/com/box/sdk/BoxCollaborationTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/box/sdk/BoxCollaborationTest.java b/src/test/java/com/box/sdk/BoxCollaborationTest.java index e6231b4e1..60a3d8981 100644 --- a/src/test/java/com/box/sdk/BoxCollaborationTest.java +++ b/src/test/java/com/box/sdk/BoxCollaborationTest.java @@ -191,8 +191,7 @@ public void testCreateAndEditCollaborationSucceeds() { JsonObject updateBody = new JsonObject() .add("role", BoxCollaboration.Role.VIEWER.toJSONString()) - .add("expires_at", BoxDateFormat.format(expiresAt)) - .add("is_access_only", true); + .add("expires_at", BoxDateFormat.format(expiresAt)); wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(createCollaborationURL)) .withRequestBody(WireMock.equalToJson(createBody.toString())) From 48b697df036669cd3452f34ee2254522dd85b7af Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Thu, 10 Aug 2023 10:57:58 +0200 Subject: [PATCH 3/4] Update docs --- doc/collaborations.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/doc/collaborations.md b/doc/collaborations.md index 296e0bf65..b146840bc 100644 --- a/doc/collaborations.md +++ b/doc/collaborations.md @@ -52,8 +52,25 @@ BoxFolder folder = new BoxFolder(api, "folder-id"); folder.collaborate(group, BoxCollaboration.Role.EDITOR); ``` +You can also create a collaboration with all fields specified in a +[`BoxCollaboration.Info`][box-collaboration-info] object by using [`BoxCollaboration.create(BoxAPIConnection api, JsonObject accessibleBy, JsonObject item, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath, Date expiresAt, Boolean isAccessOnly)`][collaborate3] + +```java +JsonObject user = new JsonObject() + .add("login", "login@box.com") + .add("type", "user"); +JsonObject file = new JsonObject() + .add("id", "123456") + .add("type", "file"); + +BoxCollaboration.Role role = BoxCollaboration.Role.VIEWER; +BoxCollaboration.Info collabInfo = BoxCollaboration.create(api, user, file, role, + false, false, null, true); +``` + [collaborate1]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-com.box.sdk.BoxCollaborator-com.box.sdk.BoxCollaboration.Role- [collaborate2]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-java.lang.String-com.box.sdk.BoxCollaboration.Role- +[collaborate3]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxCollaboration.html#create-com.box.sdk.BoxAPIConnection-com.eclipsesource.json.JsonObject-com.eclipsesource.json.JsonObject-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean-java.util.Date-java.lang.Boolean- Edit a Collaboration -------------------- From efe52bafd6892ae01cfa53c34b9dd49a4909b0c9 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Fri, 11 Aug 2023 14:39:40 +0200 Subject: [PATCH 4/4] Update code --- doc/collaborations.md | 17 ------ doc/files.md | 16 +++-- doc/folders.md | 20 +++++++ src/main/java/com/box/sdk/BoxFile.java | 49 ++++++++++++++-- src/main/java/com/box/sdk/BoxFolder.java | 58 ++++++++++++++++--- .../CreateFileCollaboration201.json | 4 +- .../com/box/sdk/BoxCollaborationTest.java | 5 +- src/test/java/com/box/sdk/BoxFileTest.java | 6 +- src/test/java/com/box/sdk/BoxFolderTest.java | 39 +++++++++++++ 9 files changed, 174 insertions(+), 40 deletions(-) diff --git a/doc/collaborations.md b/doc/collaborations.md index b146840bc..296e0bf65 100644 --- a/doc/collaborations.md +++ b/doc/collaborations.md @@ -52,25 +52,8 @@ BoxFolder folder = new BoxFolder(api, "folder-id"); folder.collaborate(group, BoxCollaboration.Role.EDITOR); ``` -You can also create a collaboration with all fields specified in a -[`BoxCollaboration.Info`][box-collaboration-info] object by using [`BoxCollaboration.create(BoxAPIConnection api, JsonObject accessibleBy, JsonObject item, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath, Date expiresAt, Boolean isAccessOnly)`][collaborate3] - -```java -JsonObject user = new JsonObject() - .add("login", "login@box.com") - .add("type", "user"); -JsonObject file = new JsonObject() - .add("id", "123456") - .add("type", "file"); - -BoxCollaboration.Role role = BoxCollaboration.Role.VIEWER; -BoxCollaboration.Info collabInfo = BoxCollaboration.create(api, user, file, role, - false, false, null, true); -``` - [collaborate1]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-com.box.sdk.BoxCollaborator-com.box.sdk.BoxCollaboration.Role- [collaborate2]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-java.lang.String-com.box.sdk.BoxCollaboration.Role- -[collaborate3]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxCollaboration.html#create-com.box.sdk.BoxAPIConnection-com.eclipsesource.json.JsonObject-com.eclipsesource.json.JsonObject-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean-java.util.Date-java.lang.Boolean- Edit a Collaboration -------------------- diff --git a/doc/files.md b/doc/files.md index 658b70201..00fe12361 100644 --- a/doc/files.md +++ b/doc/files.md @@ -731,7 +731,7 @@ Add a Collaborator ------------------ You can invite another person to collaborate on a file by email with -[`collaborate(String emailAddress, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath)`][share-a-file]. +[`collaborate(String emailAddress, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath, Date expiresAt, Boolean isAccessOnly)`][share-a-file]. The `notify` parameter will determine if the user or group will receive an email notification when being added as a collaborator. This option is only @@ -741,7 +741,13 @@ The `canViewPath` parameter allows the invitee to see the entire list of ancesto folders of the associated file. The user will not gain privileges in any ancestor folder, but will be able to see the whole path to that file in the owner's account. -Both the `notify` and `canViewPath` parameters can be left as `null`. +The `expiresAt` parameter allows the owner to set a date-time in the future when +the collaboration should expire. + +The `isAccessOnly` parameter allows the owner to set the collaboration to be +access only collaboration. + +The `notify`, `canViewPath`, `expiresAt` and `isAccessOnly` parameters can be left as `null`. ```java BoxFile file = new BoxFile(api, "id"); @@ -750,7 +756,7 @@ BoxCollaboration.Info collabInfo = file.collaborate("testuser@example.com", BoxC Alternatively, if you know the user's ID, you can invite them directly without needing to know their email address with the -[`collaborate(BoxCollaborator user, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath)`][share-a-file-userID] +[`collaborate(BoxCollaborator user, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath, Date expiresAt, Boolean isAccessOnly)`][share-a-file-userID] ```java BoxUser collaborator = new BoxUser(api, "user-id"); @@ -758,8 +764,8 @@ BoxFile file = new BoxFile(api, "file-id"); BoxCollaboration.Info collabInfo = file.collaborate(collaborator, BoxCollaboration.Role.EDITOR, true, true); ``` -[share-a-file]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFile.html#collaborate-java.lang.String-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean- -[share-a-file-userID]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFile.html#collaborate-com.box.sdk.BoxCollaborator-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean- +[share-a-file]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFile.html#collaborate-java.lang.String-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean-java.lang.String-java.util.Date-java.lang.Boolean- +[share-a-file-userID]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFile.html#collaborate-com.box.sdk.BoxCollaborator-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean-java.lang.String-java.util.Date-java.lang.Boolean- Get an Embed Link diff --git a/doc/folders.md b/doc/folders.md index 23ff33dfd..e5bb6eb0d 100644 --- a/doc/folders.md +++ b/doc/folders.md @@ -408,8 +408,28 @@ BoxCollaboration.Info collabInfo = folder.collaborate(collaborator, BoxCollaboration.Role.EDITOR); ``` +You can also create a collaboration with all properties set at once by using the +[`collaborate(BoxCollaborator user, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath, Date expiresAt, Boolean isAccessOnly)`][collaborate3] method. + +The `notify` parameter will determine if the user or group will receive an +email notification when being added as a collaborator. This option is only +available to enterprise administrators. + +The `canViewPath` parameter allows the invitee to see the entire list of ancestor +folders of the associated file. The user will not gain privileges in any ancestor +folder, but will be able to see the whole path to that file in the owner's account. + +The `expiresAt` parameter allows the owner to set a date-time in the future when +the collaboration should expire. + +The `isAccessOnly` parameter allows the owner to set the collaboration to be +access only collaboration. + +The `notify`, `canViewPath`, `expiresAt` and `isAccessOnly` parameters can be left as `null`. + [collaborate]: https://box.github.io/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-java.lang.String-com.box.sdk.BoxCollaboration.Role- [collaborate2]: https://box.github.io/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-com.box.sdk.BoxCollaborator-com.box.sdk.BoxCollaboration.Role- +[collaborate3]: https://box.github.io/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#collaborate-com.box.sdk.BoxCollaborator-com.box.sdk.BoxCollaboration.Role-java.lang.Boolean-java.lang.Boolean-java.util.Date-java.lang.Boolean- Get All Collaborations for a Folder ----------------------------------- diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index 5ff65db49..2b747f7b7 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -1389,13 +1389,15 @@ public BoxFile.Info uploadLargeFile(InputStream inputStream, long fileSize, } private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role, - Boolean notify, Boolean canViewPath) { + Boolean notify, Boolean canViewPath, Date expiresAt, + Boolean isAccessOnly) { JsonObject itemField = new JsonObject(); itemField.add("id", this.getID()); itemField.add("type", "file"); - return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath); + return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath, + expiresAt, isAccessOnly); } /** @@ -1405,10 +1407,13 @@ private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxColla * @param role the role of the collaborator. * @param notify determines if the user (or all the users in the group) will receive email notifications. * @param canViewPath whether view path collaboration feature is enabled or not. + * @param expiresAt when the collaboration should expire. + * @param isAccessOnly whether the collaboration is access only or not. * @return info about the new collaboration. */ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role, - Boolean notify, Boolean canViewPath) { + Boolean notify, Boolean canViewPath, + Date expiresAt, Boolean isAccessOnly) { JsonObject accessibleByField = new JsonObject(); accessibleByField.add("id", collaborator.getID()); @@ -1419,7 +1424,21 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab } else { throw new IllegalArgumentException("The given collaborator is of an unknown type."); } - return this.collaborate(accessibleByField, role, notify, canViewPath); + return this.collaborate(accessibleByField, role, notify, canViewPath, expiresAt, isAccessOnly); + } + + /** + * Adds a collaborator to this file. + * + * @param collaborator the collaborator to add. + * @param role the role of the collaborator. + * @param notify determines if the user (or all the users in the group) will receive email notifications. + * @param canViewPath whether view path collaboration feature is enabled or not. + * @return info about the new collaboration. + */ + public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role, + Boolean notify, Boolean canViewPath) { + return this.collaborate(collaborator, role, notify, canViewPath, null, null); } /** @@ -1430,15 +1449,33 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab * @param role the role of the collaborator. * @param notify determines if the user (or all the users in the group) will receive email notifications. * @param canViewPath whether view path collaboration feature is enabled or not. + * @param expiresAt when the collaboration should expire. + * @param isAccessOnly whether the collaboration is access only or not. * @return info about the new collaboration. */ public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role, - Boolean notify, Boolean canViewPath) { + Boolean notify, Boolean canViewPath, + Date expiresAt, Boolean isAccessOnly) { JsonObject accessibleByField = new JsonObject(); accessibleByField.add("login", email); accessibleByField.add("type", "user"); - return this.collaborate(accessibleByField, role, notify, canViewPath); + return this.collaborate(accessibleByField, role, notify, canViewPath, expiresAt, isAccessOnly); + } + + /** + * Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box + * account. + * + * @param email the email address of the collaborator to add. + * @param role the role of the collaborator. + * @param notify determines if the user (or all the users in the group) will receive email notifications. + * @param canViewPath whether view path collaboration feature is enabled or not. + * @return info about the new collaboration. + */ + public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role, + Boolean notify, Boolean canViewPath) { + return this.collaborate(email, role, notify, canViewPath, null, null); } /** diff --git a/src/main/java/com/box/sdk/BoxFolder.java b/src/main/java/com/box/sdk/BoxFolder.java index a8de34f7c..0420a6957 100644 --- a/src/main/java/com/box/sdk/BoxFolder.java +++ b/src/main/java/com/box/sdk/BoxFolder.java @@ -16,6 +16,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.EnumSet; import java.util.Iterator; import java.util.List; @@ -148,7 +149,7 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab throw new IllegalArgumentException("The given collaborator is of an unknown type."); } - return this.collaborate(accessibleByField, role, null, null); + return this.collaborate(accessibleByField, role, null, null, null, null); } /** @@ -164,7 +165,7 @@ public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role rol accessibleByField.add("login", email); accessibleByField.add("type", "user"); - return this.collaborate(accessibleByField, role, null, null); + return this.collaborate(accessibleByField, role, null, null, null, null); } /** @@ -176,10 +177,13 @@ public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role rol * @param canViewPath the view path collaboration feature is enabled or not. * View path collaborations allow the invitee to see the entire ancestral path to the associated * folder. The user will not gain privileges in any ancestral folder. + * @param expiresAt when the collaboration should expire. + * @param isAccessOnly whether the collaboration is access only or not. * @return info about the new collaboration. */ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role, - Boolean notify, Boolean canViewPath) { + Boolean notify, Boolean canViewPath, + Date expiresAt, Boolean isAccessOnly) { JsonObject accessibleByField = new JsonObject(); accessibleByField.add("id", collaborator.getID()); @@ -191,7 +195,23 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab throw new IllegalArgumentException("The given collaborator is of an unknown type."); } - return this.collaborate(accessibleByField, role, notify, canViewPath); + return this.collaborate(accessibleByField, role, notify, canViewPath, expiresAt, isAccessOnly); + } + + /** + * Adds a collaborator to this folder. + * + * @param collaborator the collaborator to add. + * @param role the role of the collaborator. + * @param notify the user/group should receive email notification of the collaboration or not. + * @param canViewPath the view path collaboration feature is enabled or not. + * View path collaborations allow the invitee to see the entire ancestral path to the associated + * folder. The user will not gain privileges in any ancestral folder. + * @return info about the new collaboration. + */ + public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role, + Boolean notify, Boolean canViewPath) { + return this.collaborate(collaborator, role, notify, canViewPath, null, null); } /** @@ -204,25 +224,47 @@ public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollab * @param canViewPath the view path collaboration feature is enabled or not. * View path collaborations allow the invitee to see the entire ancestral path to the associated * folder. The user will not gain privileges in any ancestral folder. + * @param expiresAt when the collaboration should expire. + * @param isAccessOnly whether the collaboration is access only or not. * @return info about the new collaboration. */ public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role, - Boolean notify, Boolean canViewPath) { + Boolean notify, Boolean canViewPath, + Date expiresAt, Boolean isAccessOnly) { JsonObject accessibleByField = new JsonObject(); accessibleByField.add("login", email); accessibleByField.add("type", "user"); - return this.collaborate(accessibleByField, role, notify, canViewPath); + return this.collaborate(accessibleByField, role, notify, canViewPath, expiresAt, isAccessOnly); + } + + /** + * Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box + * account. + * + * @param email the email address of the collaborator to add. + * @param role the role of the collaborator. + * @param notify the user/group should receive email notification of the collaboration or not. + * @param canViewPath the view path collaboration feature is enabled or not. + * View path collaborations allow the invitee to see the entire ancestral path to the associated + * folder. The user will not gain privileges in any ancestral folder. + * @return info about the new collaboration. + */ + public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role, + Boolean notify, Boolean canViewPath) { + return this.collaborate(email, role, notify, canViewPath, null, null); } private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role, - Boolean notify, Boolean canViewPath) { + Boolean notify, Boolean canViewPath, + Date expiresAt, Boolean isAccessOnly) { JsonObject itemField = new JsonObject(); itemField.add("id", this.getID()); itemField.add("type", "folder"); - return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath); + return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath, + expiresAt, isAccessOnly); } /** diff --git a/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json b/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json index 5e149347b..efb6d0645 100644 --- a/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json +++ b/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json @@ -9,7 +9,7 @@ }, "created_at": "2018-04-19T10:43:14-07:00", "modified_at": "2018-04-19T10:43:14-07:00", - "expires_at": null, + "expires_at": "2020-04-07T12:51:30-07:00", "status": "accepted", "accessible_by": { "type": "user", @@ -32,5 +32,5 @@ "sha1": "aa810e72823f3c3dad2d1d4488966f6f1e0a8a9d", "name": "1_1-4_bsp_ball_valve.pdf" }, - "is_access_only": false + "is_access_only": true } diff --git a/src/test/java/com/box/sdk/BoxCollaborationTest.java b/src/test/java/com/box/sdk/BoxCollaborationTest.java index 60a3d8981..fdbbc2b48 100644 --- a/src/test/java/com/box/sdk/BoxCollaborationTest.java +++ b/src/test/java/com/box/sdk/BoxCollaborationTest.java @@ -42,8 +42,9 @@ public void testCreateFileCollaborationSucceeds() { BoxUser collaborator = new BoxUser(this.api, "1111"); BoxFile file = new BoxFile(this.api, "12345"); + Date expiresAt = new Date(1586289090000L); BoxCollaboration.Info collabInfo = file.collaborate( - collaborator, BoxCollaboration.Role.EDITOR, false, false + collaborator, BoxCollaboration.Role.EDITOR, false, false, expiresAt, true ); assertFalse(collabInfo.getCanViewPath()); @@ -52,6 +53,8 @@ public void testCreateFileCollaborationSucceeds() { assertEquals(BoxFile.Info.class, collabInfo.getItem().getClass()); assertEquals(BoxCollaboration.Role.EDITOR, collabInfo.getRole()); assertEquals(BoxCollaboration.Status.ACCEPTED, collabInfo.getStatus()); + assertEquals(expiresAt.getTime(), collabInfo.getExpiresAt().getTime()); + assertTrue(collabInfo.getIsAccessOnly()); } @Test diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index 738a6cbf4..677dec3f4 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Date; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Before; import org.junit.Rule; @@ -118,6 +119,8 @@ public BoxJSONResponse onJSONRequest(BoxJSONRequest request, JsonObject body) { assertEquals(collaboratorLogin, body.get("accessible_by").asObject().get("login").asString()); assertEquals("user", body.get("accessible_by").asObject().get("type").asString()); assertEquals(collaboratorRole.toJSONString(), body.get("role").asString()); + assertEquals("2020-04-07T19:51:30Z", body.get("expires_at").asString()); + assertTrue(body.get("is_access_only").asBoolean()); return new BoxJSONResponse() { @Override @@ -129,7 +132,8 @@ public String getJSON() { }); BoxFile file = new BoxFile(api, fileID); - file.collaborate(collaboratorLogin, collaboratorRole, true, true); + Date expiresAt = new Date(1586289090000L); + file.collaborate(collaboratorLogin, collaboratorRole, true, true, expiresAt, true); } @Test diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index cbd1d5d0a..5d820635a 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -29,6 +29,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -1592,4 +1593,42 @@ public void renameFolder() { new BoxFolder(this.api, folderId).rename("New Name"); } + + @Test + public void collaborateWithOptionalParamsSendsCorrectRequest() { + + final String folderId = "983745"; + final String collaboratorLogin = "boxer@example.com"; + final BoxCollaboration.Role collaboratorRole = BoxCollaboration.Role.VIEWER; + + BoxAPIConnection api = new BoxAPIConnectionForTests(""); + api.setRequestInterceptor(new JSONRequestInterceptor() { + @Override + public BoxJSONResponse onJSONRequest(BoxJSONRequest request, JsonObject body) { + assertEquals( + "https://api.box.com/2.0/collaborations?notify=true", + request.getUrl().toString()); + assertEquals("POST", request.getMethod()); + + assertEquals(folderId, body.get("item").asObject().get("id").asString()); + assertEquals("folder", body.get("item").asObject().get("type").asString()); + assertEquals(collaboratorLogin, body.get("accessible_by").asObject().get("login").asString()); + assertEquals("user", body.get("accessible_by").asObject().get("type").asString()); + assertEquals(collaboratorRole.toJSONString(), body.get("role").asString()); + assertEquals("2020-04-07T19:51:30Z", body.get("expires_at").asString()); + assertTrue(body.get("is_access_only").asBoolean()); + + return new BoxJSONResponse() { + @Override + public String getJSON() { + return "{\"type\":\"collaboration\",\"id\":\"98763245\"}"; + } + }; + } + }); + + BoxFolder folder = new BoxFolder(api, folderId); + Date expiresAt = new Date(1586289090000L); + folder.collaborate(collaboratorLogin, collaboratorRole, true, true, expiresAt, true); + } }