diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index f9a8a1541..70a8dec40 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -159,6 +159,26 @@ public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareD return info.getSharedLink(); } + /** + * Creates new SharedLink for a BoxFile with a password. + * + * @param access The access level of the shared link. + * @param unshareDate A specified date to unshare the Box file. + * @param permissions The permissions to set on the shared link for the Box file. + * @param password Password set on the shared link to give access to the Box file. + * @return information about the newly created shared link. + */ + public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate, + BoxSharedLink.Permissions permissions, String password) { + + BoxSharedLink sharedLink = new BoxSharedLink(access, unshareDate, permissions, password); + Info info = new Info(); + info.setSharedLink(sharedLink); + + this.updateInfo(info); + return info.getSharedLink(); + } + /** * Adds new {@link BoxWebHook} to this {@link BoxFile}. * diff --git a/src/main/java/com/box/sdk/BoxFolder.java b/src/main/java/com/box/sdk/BoxFolder.java index 3b8a5b711..3831ab3d7 100644 --- a/src/main/java/com/box/sdk/BoxFolder.java +++ b/src/main/java/com/box/sdk/BoxFolder.java @@ -220,6 +220,26 @@ public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareD return info.getSharedLink(); } + /** + * Creates new SharedLink for a BoxFolder with a password. + * + * @param access The access level of the shared link. + * @param unshareDate A specified date to unshare the Box folder. + * @param permissions The permissions to set on the shared link for the Box folder. + * @param password Password set on the shared link to give access to the Box folder. + * @return information about the newly created shared link. + */ + public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate, + BoxSharedLink.Permissions permissions, String password) { + + BoxSharedLink sharedLink = new BoxSharedLink(access, unshareDate, permissions, password); + Info info = new Info(); + info.setSharedLink(sharedLink); + + this.updateInfo(info); + return info.getSharedLink(); + } + /** * Gets information about all of the collaborations for this folder. * diff --git a/src/main/java/com/box/sdk/BoxSharedLink.java b/src/main/java/com/box/sdk/BoxSharedLink.java index b3737b59d..6c3ead84d 100644 --- a/src/main/java/com/box/sdk/BoxSharedLink.java +++ b/src/main/java/com/box/sdk/BoxSharedLink.java @@ -14,6 +14,7 @@ public class BoxSharedLink extends BoxJSONObject { private String downloadUrl; private String vanityUrl; private boolean isPasswordEnabled; + private String password; private Date unsharedAt; private long downloadCount; private long previewCount; @@ -46,6 +47,16 @@ public BoxSharedLink(String json) { this.setUnsharedDate(unshareDate); } } + BoxSharedLink(BoxSharedLink.Access access, Date unshareDate, BoxSharedLink.Permissions permissions, + String password) { + this.setAccess(access); + this.setPermissions(permissions); + this.setPassword(password); + + if (unshareDate != null) { + this.setUnsharedDate(unshareDate); + } + } /** * Get the URL of this shared link. @@ -129,6 +140,15 @@ public void setAccess(Access access) { this.addPendingChange("access", access.toJSONValue()); } + /** + * Sets the password of this shared link. + * @param password the password of this shared link. + */ + public void setPassword(String password) { + this.password = password; + this.addPendingChange("password", password.toString()); + } + /** * Gets the effective access level of this shared link. * @return the effective access level of this shared link. diff --git a/src/main/java/com/box/sdk/BoxWebLink.java b/src/main/java/com/box/sdk/BoxWebLink.java index e701a8109..9522d511c 100644 --- a/src/main/java/com/box/sdk/BoxWebLink.java +++ b/src/main/java/com/box/sdk/BoxWebLink.java @@ -56,6 +56,26 @@ public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareD return info.getSharedLink(); } + /** + * Creates new SharedLink for a BoxWebLink with a password. + * + * @param access The access level of the shared link. + * @param unshareDate A specified date to unshare the Box web link. + * @param permissions The permissions to set on the shared link for the Box web link. + * @param password Password set on the shared link to give access to the Box web link. + * @return information about the newly created shared link. + */ + public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate, + BoxSharedLink.Permissions permissions, String password) { + + BoxSharedLink sharedLink = new BoxSharedLink(access, unshareDate, permissions, password); + Info info = new Info(); + info.setSharedLink(sharedLink); + + this.updateInfo(info); + return info.getSharedLink(); + } + @Override public BoxWebLink.Info copy(BoxFolder destination) { return this.copy(destination, null); diff --git a/src/test/Fixtures/BoxFile/CreateSharedLinkWithPassword201.json b/src/test/Fixtures/BoxFile/CreateSharedLinkWithPassword201.json new file mode 100644 index 000000000..c3f46cc09 --- /dev/null +++ b/src/test/Fixtures/BoxFile/CreateSharedLinkWithPassword201.json @@ -0,0 +1,82 @@ +{ + "type": "file", + "id": "1111", + "file_version": { + "type": "file_version", + "id": "2222", + "sha1": "a9225c82fdce3e5701111976de462c22a76df7ee" + }, + "sequence_id": "1", + "etag": "1", + "sha1": "a9225c82fdce3e5701111976de462c22a76df7ee", + "name": "test.png", + "description": "", + "size": 2882774, + "path_collection": { + "total_count": 2, + "entries": [ + { + "type": "folder", + "id": "0", + "sequence_id": null, + "etag": null, + "name": "All Files" + }, + { + "type": "folder", + "id": "3333", + "sequence_id": "0", + "etag": "0", + "name": "Test Folder" + } + ] + }, + "created_at": "2018-07-02T10:29:20-07:00", + "modified_at": "2018-07-02T10:29:22-07:00", + "trashed_at": null, + "purged_at": null, + "content_created_at": "2018-04-30T15:36:09-07:00", + "content_modified_at": "2018-04-30T15:36:09-07:00", + "created_by": { + "type": "user", + "id": "1111", + "name": "Test User", + "login": "test@example.com" + }, + "modified_by": { + "type": "user", + "id": "1111", + "name": "Test User", + "login": "test@example.com" + }, + "owned_by": { + "type": "user", + "id": "1111", + "name": "Test User", + "login": "test@example.com" + }, + "shared_link": { + "url": "https://testt3t2t2t2.box.com/s/12345", + "download_url": "https://testt3t2t2t2.box.com/shared/static/12345.png", + "vanity_url": null, + "effective_access": "open", + "effective_permission": "can_download", + "is_password_enabled": true, + "unshared_at": null, + "download_count": 0, + "preview_count": 0, + "access": "open", + "permissions": { + "can_preview": true, + "can_download": true + } + }, + "parent": { + "type": "folder", + "id": "5555", + "sequence_id": "0", + "etag": "0", + "name": "Test Folder" + }, + "item_status": "active" +} diff --git a/src/test/Fixtures/BoxSharedLink/CreateSharedLink201.json b/src/test/Fixtures/BoxSharedLink/CreateSharedLink201.json new file mode 100644 index 000000000..5129998d4 --- /dev/null +++ b/src/test/Fixtures/BoxSharedLink/CreateSharedLink201.json @@ -0,0 +1,18 @@ +{ + "shared_link": { + "url": "https://testt3t2t2t2.box.com/s/12345", + "download_url": "https://testt3t2t2t2.box.com/shared/static/12345.png", + "vanity_url": null, + "effective_access": "open", + "effective_permission": "can_download", + "is_password_enabled": true, + "unshared_at": null, + "download_count": 0, + "preview_count": 0, + "access": "open", + "permissions": { + "can_preview": true, + "can_download": true + } + } +} diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index 89e1aa474..10182f1b2 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -1394,6 +1394,43 @@ public void testUploadNewVersionReturnsCorrectInfo() throws IOException { Assert.assertEquals(fileName, info.getName()); } + @Test + @Category(UnitTest.class) + public void createSharedLinkSucceeds() throws IOException { + final String fileID = "1111"; + final String password = "test1"; + String result = ""; + + JsonObject permissionsObject = new JsonObject() + .add("can_download", true) + .add("can_preview", true); + + JsonObject innerObject = new JsonObject() + .add("password", password) + .add("access", "open") + .add("permissions", permissionsObject); + + JsonObject sharedLinkObject = new JsonObject() + .add("shared_link", innerObject); + + result = TestConfig.getFixture("BoxSharedLink/CreateSharedLink201"); + + WIRE_MOCK_CLASS_RULE.stubFor(WireMock.put(WireMock.urlPathEqualTo("/files/" + fileID)) + .withRequestBody(WireMock.equalToJson(sharedLinkObject.toString())) + .willReturn(WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withBody(result))); + + BoxFile file = new BoxFile(this.api, fileID); + BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions(); + + permissions.setCanDownload(true); + permissions.setCanPreview(true); + BoxSharedLink sharedLink = file.createSharedLink(BoxSharedLink.Access.OPEN, null, permissions, + password); + Assert.assertEquals(true, sharedLink.getIsPasswordEnabled()); + } + private BoxFile.Info parallelMuliputUpload(File file, BoxFolder folder, String fileName) throws IOException, InterruptedException { FileInputStream newStream = new FileInputStream(file); diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index 93938eec6..784ff1b8a 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -944,6 +944,44 @@ public void testGetAllMetadataSucceeds() throws IOException { Assert.assertEquals(scope, metadata.getScope()); } + @Test + @Category(UnitTest.class) + public void createSharedLinkSucceeds() throws IOException { + final String folderID = "1111"; + final String password = "test1"; + String result = ""; + + JsonObject permissionsObject = new JsonObject() + .add("can_download", true) + .add("can_preview", true); + + JsonObject innerObject = new JsonObject() + .add("password", password) + .add("access", "open") + .add("permissions", permissionsObject); + + JsonObject sharedLinkObject = new JsonObject() + .add("shared_link", innerObject); + + result = TestConfig.getFixture("BoxSharedLink/CreateSharedLink201"); + + WIRE_MOCK_CLASS_RULE.stubFor(WireMock.put(WireMock.urlPathEqualTo("/folders/" + folderID)) + .withRequestBody(WireMock.equalToJson(sharedLinkObject.toString())) + .willReturn(WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withBody(result))); + + BoxFolder folder = new BoxFolder(this.api, folderID); + BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions(); + + permissions.setCanDownload(true); + permissions.setCanPreview(true); + BoxSharedLink sharedLink = folder.createSharedLink(BoxSharedLink.Access.OPEN, null, permissions, + password); + + Assert.assertEquals(true, sharedLink.getIsPasswordEnabled()); + } + private void getUploadSessionStatus(BoxFileUploadSession session) { BoxFileUploadSession.Info sessionInfo = session.getStatus(); Assert.assertNotNull(sessionInfo.getSessionExpiresAt()); diff --git a/src/test/java/com/box/sdk/BoxWebLinkTest.java b/src/test/java/com/box/sdk/BoxWebLinkTest.java index 8a1779661..5ee18cf79 100644 --- a/src/test/java/com/box/sdk/BoxWebLinkTest.java +++ b/src/test/java/com/box/sdk/BoxWebLinkTest.java @@ -257,4 +257,42 @@ public void testDeleteWebLinkSucceeds() throws IOException { BoxWebLink webLink = new BoxWebLink(this.api, webLinkID); webLink.delete(); } + + @Test + @Category(UnitTest.class) + public void createSharedLinkSucceeds() throws IOException { + final String webLinkID = "1111"; + final String password = "test1"; + String result = ""; + + JsonObject permissionsObject = new JsonObject() + .add("can_download", true) + .add("can_preview", true); + + JsonObject innerObject = new JsonObject() + .add("password", password) + .add("access", "open") + .add("permissions", permissionsObject); + + JsonObject sharedLinkObject = new JsonObject() + .add("shared_link", innerObject); + + result = TestConfig.getFixture("BoxSharedLink/CreateSharedLink201"); + + WIRE_MOCK_CLASS_RULE.stubFor(WireMock.put(WireMock.urlPathEqualTo("/web_links/" + webLinkID)) + .withRequestBody(WireMock.equalToJson(sharedLinkObject.toString())) + .willReturn(WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withBody(result))); + + BoxWebLink webLink = new BoxWebLink(this.api, webLinkID); + BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions(); + + permissions.setCanDownload(true); + permissions.setCanPreview(true); + BoxSharedLink sharedLink = webLink.createSharedLink(BoxSharedLink.Access.OPEN, null, permissions, + password); + + Assert.assertEquals(true, sharedLink.getIsPasswordEnabled()); + } }