Skip to content

Commit 56ff4f3

Browse files
committed
Update download file from shared link
1 parent 6d885b3 commit 56ff4f3

File tree

3 files changed

+74
-51
lines changed

3 files changed

+74
-51
lines changed
Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
11
package com.box.sdk.example;
22

3-
import com.box.sdk.BoxAPIConnection;
4-
import com.box.sdk.BoxFolder;
5-
import com.box.sdk.BoxItem;
6-
import com.box.sdk.BoxLogger;
7-
import com.box.sdk.BoxUser;
3+
import com.box.sdk.*;
4+
5+
import com.eclipsesource.json.JsonObject;
6+
import java.io.FileNotFoundException;
7+
import java.io.FileReader;
8+
import java.io.IOException;
9+
import java.io.Reader;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.List;
814

915
public final class Main {
10-
private static final String DEVELOPER_TOKEN = "";
1116
private static final int MAX_DEPTH = 1;
1217

13-
private Main() {
14-
}
15-
16-
public static void main(String[] args) {
17-
// Limit logging messages to prevent polluting the output.
18-
BoxLogger.defaultLogger().setLevelToWarning();
19-
20-
BoxAPIConnection api = new BoxAPIConnection(DEVELOPER_TOKEN);
18+
public static void main(String[] args) throws IOException {
19+
Reader reader = new FileReader("/Users/mcong/Downloads/jwt-lukasz.json");
20+
BoxConfig boxConfig = BoxConfig.readFrom(reader);
21+
IAccessTokenCache tokenCache = new InMemoryLRUAccessTokenCache(100);
22+
BoxDeveloperEditionAPIConnection entApi = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, tokenCache);
23+
BoxDeveloperEditionAPIConnection api = BoxDeveloperEditionAPIConnection.getUserConnection("17815542202", boxConfig, tokenCache);
2124

22-
BoxUser.Info userInfo = BoxUser.getCurrentUser(api).getInfo();
25+
BoxUser.Info userInfo = BoxUser.getCurrentUser(entApi).getInfo();
2326
System.out.format("Welcome, %s <%s>!\n\n", userInfo.getName(), userInfo.getLogin());
2427

25-
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
26-
listFolder(rootFolder, 0);
27-
}
28-
29-
private static void listFolder(BoxFolder folder, int depth) {
30-
for (BoxItem.Info itemInfo : folder) {
31-
StringBuilder indent = new StringBuilder();
32-
for (int i = 0; i < depth; i++) {
33-
indent.append(" ");
34-
}
35-
36-
System.out.println(indent + itemInfo.getName());
37-
if (itemInfo instanceof BoxFolder.Info) {
38-
BoxFolder childFolder = (BoxFolder) itemInfo.getResource();
39-
if (depth < MAX_DEPTH) {
40-
listFolder(childFolder, depth + 1);
41-
}
42-
}
43-
}
28+
// BoxFolder rootFolder = BoxFolder.getRootFolder(api);
29+
//
30+
// for (BoxSignRequest.Info sr : BoxSignRequest.getAll(api)) {
31+
// System.out.println(sr.getJson());
32+
// }
33+
//
34+
// BoxSignRequest sr = new BoxSignRequest(api, "091e3a0c-5e44-452b-b256-39d8aaaaa151");
35+
// BoxSignRequest.Info info = sr.getInfo();
36+
// sr.resend();
37+
38+
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(entApi, BoxAIAgent.Mode.EXTRACT, "en-US", null);
39+
BoxAIAgentExtract agentExtract = (BoxAIAgentExtract) agent;
40+
41+
BoxAIResponse response = BoxAI.extractMetadataFreeform(entApi,
42+
"firstName, lastName, location, yearOfBirth, company",
43+
Collections.singletonList(new BoxAIItem("1664251333348", BoxAIItem.Type.FILE)),
44+
agentExtract);
45+
// JsonObject response = BoxAI.extractMetadataStructured(entApi,
46+
// Collections.singletonList(new BoxAIItem("1664220697964", BoxAIItem.Type.FILE)),
47+
// new BoxAIExtractMetadataTemplate("key374c5378-822e-11ef-8b99-0a95d678162f", "enterprise"),
48+
// null,
49+
// null);
50+
//
51+
System.out.println(response);
4452
}
4553
}

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)