Search code examples
javasdkbox-api

Box java sdk - generating a Box URL for a BoxItem


Here is what I'm using to generate a box URL given a box item.

 private String generateURL(BoxItem item) {
     if (item instanceof BoxFolder) {
         if (item.getInfo().getParent() == null) {
             return "https://app.box.com/files/0";
         } else {
             return String.format("https://app.box.com/files/0/f/%s", item.getInfo().getParent().getID());
         }
     } else {
         return String.format(
                 "https://app.box.com/files/0/f/%s/1/f_%s", item.getInfo().getParent().getID(), item.getID());
     }
 }

I generate this URL when there is no shared URL for a box item. Otherwise we have no viable URL to use when getting files from the box java sdk.

Is this OK? Is there any problems with it? Is there something in the SDK maybe that already does what this function does?


Solution

  • The URL format has changed (for the better) in the new Box UI:

     private String generateURL(BoxItem item) {
         if (item instanceof BoxFolder) {
             if (item.getInfo().getParent() == null) {
                 return "https://app.box.com/folder/0";
             } else {
                 return String.format("https://app.box.com/folder/%s", item.getInfo().getParent().getID());
             }
         } else {
             return String.format("https://app.box.com/file/%s", item.getID());
         }
     }