Skip to content

Commit 935cb2f

Browse files
committed
Update download file from shared links
1 parent 6d885b3 commit 935cb2f

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/main/java/com/box/sdk/BoxFile.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,14 @@ public static void downloadFromSharedLink(
359359
public static void downloadFromSharedLink(
360360
BoxAPIConnection api, OutputStream output, String sharedLink, String password, ProgressListener listener
361361
) {
362-
BoxItem.Info item = BoxItem.getSharedItem(api, sharedLink, password, "download_url");
363-
URL url;
364-
try {
365-
url = new URL(item.getDownloadUrl());
366-
} catch (MalformedURLException e) {
367-
throw new RuntimeException(
368-
String.format("Invalid download URL %s for shared link %s", item.getDownloadUrl(), sharedLink), e
369-
);
362+
BoxItem.Info item = BoxItem.getSharedItem(api, sharedLink, password, "id");
363+
if (!(item instanceof BoxFile.Info)) {
364+
throw new BoxAPIException("The shared link provided is not a shared link for a file.");
370365
}
366+
BoxFile sharedFile = new BoxFile(api, item.getID());
367+
URL url = sharedFile.getDownloadUrl();
371368
BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
369+
request.addHeader("BoxApi", BoxSharedLink.getSharedLinkHeaderValue(sharedLink, password));
372370
BoxAPIResponse response = request.send();
373371
writeStream(response, output, listener);
374372
}

src/test/java/com/box/sdk/BoxFileTest.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -735,36 +735,43 @@ public void createEditableSharedLinkSucceeds() {
735735
@Test
736736
public void testDownloadFromSharedLinkWithPassword() {
737737
final String sharedItemsURL = "/2.0/shared_items";
738+
final String fileContentURL = "/2.0/files/12345/content";
738739
final String sharedLink = "https://app.box.com/s/abcdef123456";
739740
final String password = "password";
740741
final byte[] fileContent = "This is a test file content".getBytes();
741742
final String expectedSharedLinkHeaderValue = "shared_link=" + sharedLink + "&shared_link_password=" + password;
742743
final String expectedDownloadPath = "/shared/static/rh935iit6ewrmw0unyul.jpeg";
743744
final String expectedDownloadUrl = format("https://localhost:%d%s", wireMockRule.httpsPort(), expectedDownloadPath);
744745

745-
String sharedItemsResponse = format(
746-
"{ \"download_url\": \"%s\", \"type\": \"file\", \"id\": \"12345\" }",
747-
expectedDownloadUrl
748-
);
746+
String sharedItemsResponse = "{ \"type\": \"file\", \"id\": \"12345\" }";
749747

750748
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(sharedItemsURL))
751749
.willReturn(WireMock.aResponse()
752750
.withHeader("Content-Type", APPLICATION_JSON)
753751
.withBody(sharedItemsResponse)));
754752

755-
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(expectedDownloadPath))
753+
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(fileContentURL))
754+
.withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue))
756755
.willReturn(WireMock.aResponse()
757-
.withHeader("Content-Type", "application/octet-stream")
758-
.withBody(fileContent)));
756+
.withStatus(302)
757+
.withHeader("Location", expectedDownloadUrl)));
758+
759+
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(expectedDownloadPath))
760+
.willReturn(WireMock.aResponse()
761+
.withHeader("Content-Type", "application/octet-stream")
762+
.withBody(fileContent)));
759763

760764

761765
ByteArrayOutputStream output = new ByteArrayOutputStream();
762766
BoxFile.downloadFromSharedLink(api, output, sharedLink, password);
763767

764768
verify(1, getRequestedFor(
765-
urlEqualTo("/2.0/shared_items?fields=download_url")).
769+
urlEqualTo("/2.0/shared_items?fields=id")).
766770
withHeader("BoxApi", WireMock.equalTo(expectedSharedLinkHeaderValue)));
767771

772+
verify(1, getRequestedFor(urlEqualTo(fileContentURL)).
773+
withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue)));
774+
768775
verify(1, getRequestedFor(urlEqualTo(expectedDownloadPath)));
769776

770777
assertArrayEquals(fileContent, output.toByteArray());
@@ -773,6 +780,7 @@ public void testDownloadFromSharedLinkWithPassword() {
773780
@Test
774781
public void testDownloadFromSharedLinkWithProgressListener() {
775782
final String sharedItemsURL = "/2.0/shared_items";
783+
final String fileContentURL = "/2.0/files/12345/content";
776784
final String sharedLink = "https://app.box.com/s/abcdef123456";
777785
final byte[] fileContent = "This is a test file content".getBytes();
778786
final String expectedSharedLinkHeaderValue = "shared_link=" + sharedLink;
@@ -791,6 +799,12 @@ public void testDownloadFromSharedLinkWithProgressListener() {
791799
.withHeader("Content-Type", APPLICATION_JSON)
792800
.withBody(sharedItemsResponse)));
793801

802+
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(fileContentURL))
803+
.withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue))
804+
.willReturn(WireMock.aResponse()
805+
.withStatus(302)
806+
.withHeader("Location", expectedDownloadUrl)));
807+
794808
wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(expectedDownloadPath))
795809
.willReturn(WireMock.aResponse()
796810
.withHeader("Content-Type", "application/octet-stream")
@@ -804,9 +818,12 @@ public void testDownloadFromSharedLinkWithProgressListener() {
804818
BoxFile.downloadFromSharedLink(api, output, sharedLink, listener);
805819

806820
verify(1, getRequestedFor(
807-
urlEqualTo("/2.0/shared_items?fields=download_url")).
821+
urlEqualTo("/2.0/shared_items?fields=id")).
808822
withHeader("BoxApi", WireMock.equalTo(expectedSharedLinkHeaderValue)));
809823

824+
verify(1, getRequestedFor(urlEqualTo(fileContentURL)).
825+
withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue)));
826+
810827
verify(1, getRequestedFor(urlEqualTo(expectedDownloadPath)));
811828

812829
assertArrayEquals(fileContent, output.toByteArray());

0 commit comments

Comments
 (0)