001package com.box.sdk;
002
003import static com.box.sdk.PagingParameters.DEFAULT_LIMIT;
004import static com.box.sdk.PagingParameters.marker;
005
006import com.box.sdk.internal.utils.Parsers;
007import com.box.sdk.sharedlink.BoxSharedLinkRequest;
008import com.eclipsesource.json.Json;
009import com.eclipsesource.json.JsonArray;
010import com.eclipsesource.json.JsonObject;
011import com.eclipsesource.json.JsonValue;
012import java.io.IOException;
013import java.io.InputStream;
014import java.net.URL;
015import java.util.ArrayList;
016import java.util.Collection;
017import java.util.Date;
018import java.util.EnumSet;
019import java.util.Iterator;
020import java.util.List;
021import java.util.Map;
022import java.util.concurrent.TimeUnit;
023
024/**
025 * Represents a folder on Box. This class can be used to iterate through a folder's contents, collaborate a folder with
026 * another user or group, and perform other common folder operations (move, copy, delete, etc.).
027 * <p>
028 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
029 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
030 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
031 */
032@BoxResourceType("folder")
033public class BoxFolder extends BoxItem implements Iterable<BoxItem.Info> {
034    /**
035     * An array of all possible folder fields that can be requested when calling {@link #getInfo()}.
036     */
037    public static final String[] ALL_FIELDS = {"type", "id", "sequence_id", "etag", "name", "created_at", "modified_at",
038        "description", "size", "path_collection", "created_by", "modified_by", "trashed_at", "purged_at",
039        "content_created_at", "content_modified_at", "owned_by", "shared_link", "folder_upload_email", "parent",
040        "item_status", "item_collection", "sync_state", "has_collaborations", "permissions", "tags",
041        "can_non_owners_invite", "collections", "watermark_info", "metadata", "is_externally_owned",
042        "is_collaboration_restricted_to_enterprise", "allowed_shared_link_access_levels", "allowed_invitee_roles"};
043    /**
044     * Create Folder URL Template.
045     */
046    public static final URLTemplate CREATE_FOLDER_URL = new URLTemplate("folders");
047    /**
048     * Create Web Link URL Template.
049     */
050    public static final URLTemplate CREATE_WEB_LINK_URL = new URLTemplate("web_links");
051    /**
052     * Copy Folder URL Template.
053     */
054    public static final URLTemplate COPY_FOLDER_URL = new URLTemplate("folders/%s/copy");
055    /**
056     * Delete Folder URL Template.
057     */
058    public static final URLTemplate DELETE_FOLDER_URL = new URLTemplate("folders/%s?recursive=%b");
059    /**
060     * Folder Info URL Template.
061     */
062    public static final URLTemplate FOLDER_INFO_URL_TEMPLATE = new URLTemplate("folders/%s");
063    /**
064     * Upload File URL Template.
065     */
066    public static final URLTemplate UPLOAD_FILE_URL = new URLTemplate("files/content");
067    /**
068     * Add Collaboration URL Template.
069     */
070    public static final URLTemplate ADD_COLLABORATION_URL = new URLTemplate("collaborations");
071    /**
072     * Get Collaborations URL Template.
073     */
074    public static final URLTemplate GET_COLLABORATIONS_URL = new URLTemplate("folders/%s/collaborations");
075    /**
076     * Get Items URL Template.
077     */
078    public static final URLTemplate GET_ITEMS_URL = new URLTemplate("folders/%s/items/");
079    /**
080     * Search URL Template.
081     */
082    public static final URLTemplate SEARCH_URL_TEMPLATE = new URLTemplate("search");
083    /**
084     * Metadata URL Template.
085     */
086    public static final URLTemplate METADATA_URL_TEMPLATE = new URLTemplate("folders/%s/metadata/%s/%s");
087    /**
088     * Upload Session URL Template.
089     */
090    public static final URLTemplate UPLOAD_SESSION_URL_TEMPLATE = new URLTemplate("files/upload_sessions");
091    /**
092     * Folder Locks URL Template.
093     */
094    public static final URLTemplate FOLDER_LOCK_URL_TEMPLATE = new URLTemplate("folder_locks");
095
096    /**
097     * Constructs a BoxFolder for a folder with a given ID.
098     *
099     * @param api the API connection to be used by the folder.
100     * @param id  the ID of the folder.
101     */
102    public BoxFolder(BoxAPIConnection api, String id) {
103        super(api, id);
104    }
105
106    /**
107     * Gets the current user's root folder.
108     *
109     * @param api the API connection to be used by the folder.
110     * @return the user's root folder.
111     */
112    public static BoxFolder getRootFolder(BoxAPIConnection api) {
113        return new BoxFolder(api, "0");
114    }
115
116    /**
117     * {@inheritDoc}
118     */
119    @Override
120    protected URL getItemURL() {
121        return FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
122    }
123
124    /**
125     * Adds a collaborator to this folder.
126     *
127     * @param collaborator the collaborator to add.
128     * @param role         the role of the collaborator.
129     * @return info about the new collaboration.
130     */
131    public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role) {
132        JsonObject accessibleByField = new JsonObject();
133        accessibleByField.add("id", collaborator.getID());
134
135        if (collaborator instanceof BoxUser) {
136            accessibleByField.add("type", "user");
137        } else if (collaborator instanceof BoxGroup) {
138            accessibleByField.add("type", "group");
139        } else {
140            throw new IllegalArgumentException("The given collaborator is of an unknown type.");
141        }
142
143        return this.collaborate(accessibleByField, role, null, null);
144    }
145
146    /**
147     * Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box
148     * account.
149     *
150     * @param email the email address of the collaborator to add.
151     * @param role  the role of the collaborator.
152     * @return info about the new collaboration.
153     */
154    public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role) {
155        JsonObject accessibleByField = new JsonObject();
156        accessibleByField.add("login", email);
157        accessibleByField.add("type", "user");
158
159        return this.collaborate(accessibleByField, role, null, null);
160    }
161
162    /**
163     * Adds a collaborator to this folder.
164     *
165     * @param collaborator the collaborator to add.
166     * @param role         the role of the collaborator.
167     * @param notify       the user/group should receive email notification of the collaboration or not.
168     * @param canViewPath  the view path collaboration feature is enabled or not.
169     *                     View path collaborations allow the invitee to see the entire ancestral path to the associated
170     *                     folder. The user will not gain privileges in any ancestral folder.
171     * @return info about the new collaboration.
172     */
173    public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role,
174                                             Boolean notify, Boolean canViewPath) {
175        JsonObject accessibleByField = new JsonObject();
176        accessibleByField.add("id", collaborator.getID());
177
178        if (collaborator instanceof BoxUser) {
179            accessibleByField.add("type", "user");
180        } else if (collaborator instanceof BoxGroup) {
181            accessibleByField.add("type", "group");
182        } else {
183            throw new IllegalArgumentException("The given collaborator is of an unknown type.");
184        }
185
186        return this.collaborate(accessibleByField, role, notify, canViewPath);
187    }
188
189    /**
190     * Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box
191     * account.
192     *
193     * @param email       the email address of the collaborator to add.
194     * @param role        the role of the collaborator.
195     * @param notify      the user/group should receive email notification of the collaboration or not.
196     * @param canViewPath the view path collaboration feature is enabled or not.
197     *                    View path collaborations allow the invitee to see the entire ancestral path to the associated
198     *                    folder. The user will not gain privileges in any ancestral folder.
199     * @return info about the new collaboration.
200     */
201    public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role,
202                                             Boolean notify, Boolean canViewPath) {
203        JsonObject accessibleByField = new JsonObject();
204        accessibleByField.add("login", email);
205        accessibleByField.add("type", "user");
206
207        return this.collaborate(accessibleByField, role, notify, canViewPath);
208    }
209
210    private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role,
211                                              Boolean notify, Boolean canViewPath) {
212
213        JsonObject itemField = new JsonObject();
214        itemField.add("id", this.getID());
215        itemField.add("type", "folder");
216
217        return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath);
218    }
219
220    /**
221     * Creates a new shared link for this item.
222     *
223     * <p>This method is a convenience method for manually creating a new shared link and applying it to this item with
224     * {@link BoxItem.Info#setSharedLink}. You may want to create the shared link manually so that it can be updated along with
225     * other changes to the item's info in a single network request, giving a boost to performance.</p>
226     *
227     * @param access      the access level of the shared link.
228     * @param unshareDate the date and time at which the link will expire. Can be null to create a non-expiring link.
229     * @param permissions the permissions of the shared link. Can be null to use the default permissions.
230     * @return the created shared link.
231     * @deprecated use {@link BoxFolder#createSharedLink(BoxSharedLinkRequest)}
232     */
233    @Override
234    @Deprecated
235    public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate,
236                                          BoxSharedLink.Permissions permissions) {
237
238        return this.createSharedLink(new BoxSharedLink(access, unshareDate, permissions));
239    }
240
241    /**
242     * Creates new SharedLink for a BoxFolder with a password.
243     *
244     * @param access      The access level of the shared link.
245     * @param unshareDate A specified date to unshare the Box folder.
246     * @param permissions The permissions to set on the shared link for the Box folder.
247     * @param password    Password set on the shared link to give access to the Box folder.
248     * @return information about the newly created shared link.
249     * @deprecated Use {@link BoxFolder#createSharedLink(BoxSharedLinkRequest)}
250     */
251    @Deprecated
252    public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate,
253                                          BoxSharedLink.Permissions permissions, String password) {
254
255        return this.createSharedLink(new BoxSharedLink(access, unshareDate, permissions, password));
256    }
257
258    /**
259     * Creates a shared link.
260     *
261     * @param sharedLinkRequest Shared link to create
262     * @return Created shared link.
263     */
264    public BoxSharedLink createSharedLink(BoxSharedLinkRequest sharedLinkRequest) {
265        return createSharedLink(sharedLinkRequest.asSharedLink());
266    }
267
268    private BoxSharedLink createSharedLink(BoxSharedLink sharedLink) {
269        BoxFolder.Info info = new BoxFolder.Info();
270        info.setSharedLink(sharedLink);
271
272        this.updateInfo(info);
273        return info.getSharedLink();
274    }
275
276    /**
277     * Gets information about all of the collaborations for this folder.
278     *
279     * @return a collection of information about the collaborations for this folder.
280     */
281    public Collection<BoxCollaboration.Info> getCollaborations() {
282        BoxAPIConnection api = this.getAPI();
283        URL url = GET_COLLABORATIONS_URL.build(api.getBaseURL(), this.getID());
284
285        BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
286        BoxJSONResponse response = (BoxJSONResponse) request.send();
287        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
288
289        int entriesCount = responseJSON.get("total_count").asInt();
290        Collection<BoxCollaboration.Info> collaborations = new ArrayList<>(entriesCount);
291        JsonArray entries = responseJSON.get("entries").asArray();
292        for (JsonValue entry : entries) {
293            JsonObject entryObject = entry.asObject();
294            BoxCollaboration collaboration = new BoxCollaboration(api, entryObject.get("id").asString());
295            BoxCollaboration.Info info = collaboration.new Info(entryObject);
296            collaborations.add(info);
297        }
298
299        return collaborations;
300    }
301
302    @Override
303    public BoxFolder.Info getInfo() {
304        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
305        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
306        BoxJSONResponse response = (BoxJSONResponse) request.send();
307        return new Info(response.getJSON());
308    }
309
310    @Override
311    public BoxFolder.Info getInfo(String... fields) {
312        String queryString = new QueryStringBuilder().appendParam("fields", fields).toString();
313        URL url = FOLDER_INFO_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
314
315        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
316        BoxJSONResponse response = (BoxJSONResponse) request.send();
317        return new Info(response.getJSON());
318    }
319
320    /**
321     * Updates the information about this folder with any info fields that have been modified locally.
322     *
323     * @param info the updated info.
324     */
325    public void updateInfo(BoxFolder.Info info) {
326        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
327        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
328        request.setBody(info.getPendingChanges());
329        BoxJSONResponse response = (BoxJSONResponse) request.send();
330        JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
331        info.update(jsonObject);
332    }
333
334    @Override
335    public BoxFolder.Info copy(BoxFolder destination) {
336        return this.copy(destination, null);
337    }
338
339    @Override
340    public BoxFolder.Info copy(BoxFolder destination, String newName) {
341        URL url = COPY_FOLDER_URL.build(this.getAPI().getBaseURL(), this.getID());
342        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST");
343
344        JsonObject parent = new JsonObject();
345        parent.add("id", destination.getID());
346
347        JsonObject copyInfo = new JsonObject();
348        copyInfo.add("parent", parent);
349        if (newName != null) {
350            copyInfo.add("name", newName);
351        }
352
353        request.setBody(copyInfo.toString());
354        BoxJSONResponse response = (BoxJSONResponse) request.send();
355        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
356        BoxFolder copiedFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());
357        return copiedFolder.new Info(responseJSON);
358    }
359
360    /**
361     * Creates a new child folder inside this folder.
362     *
363     * @param name the new folder's name.
364     * @return the created folder's info.
365     */
366    public BoxFolder.Info createFolder(String name) {
367        JsonObject parent = new JsonObject();
368        parent.add("id", this.getID());
369
370        JsonObject newFolder = new JsonObject();
371        newFolder.add("name", name);
372        newFolder.add("parent", parent);
373
374        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), CREATE_FOLDER_URL.build(this.getAPI().getBaseURL()),
375            "POST");
376        request.setBody(newFolder.toString());
377        BoxJSONResponse response = (BoxJSONResponse) request.send();
378        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
379
380        BoxFolder createdFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());
381        return createdFolder.new Info(responseJSON);
382    }
383
384    /**
385     * Deletes this folder, optionally recursively deleting all of its contents.
386     *
387     * @param recursive true to recursively delete this folder's contents; otherwise false.
388     */
389    public void delete(boolean recursive) {
390        URL url = DELETE_FOLDER_URL.buildAlpha(this.getAPI().getBaseURL(), this.getID(), recursive);
391        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
392        BoxAPIResponse response = request.send();
393        response.disconnect();
394    }
395
396    @Override
397    public BoxItem.Info move(BoxFolder destination) {
398        return this.move(destination, null);
399    }
400
401    @Override
402    public BoxItem.Info move(BoxFolder destination, String newName) {
403        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
404        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
405
406        JsonObject parent = new JsonObject();
407        parent.add("id", destination.getID());
408
409        JsonObject updateInfo = new JsonObject();
410        updateInfo.add("parent", parent);
411        if (newName != null) {
412            updateInfo.add("name", newName);
413        }
414
415        request.setBody(updateInfo.toString());
416        BoxJSONResponse response = (BoxJSONResponse) request.send();
417        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
418        BoxFolder movedFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());
419        return movedFolder.new Info(responseJSON);
420    }
421
422    /**
423     * Renames this folder.
424     *
425     * @param newName the new name of the folder.
426     */
427    public void rename(String newName) {
428        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
429        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
430
431        JsonObject updateInfo = new JsonObject();
432        updateInfo.add("name", newName);
433
434        request.setBody(updateInfo.toString());
435        BoxJSONResponse response = (BoxJSONResponse) request.send();
436        response.getJSON();
437    }
438
439    /**
440     * Checks if the file can be successfully uploaded by using the preflight check.
441     *
442     * @param name     the name to give the uploaded file.
443     * @param fileSize the size of the file used for account capacity calculations.
444     */
445    public void canUpload(String name, long fileSize) {
446        URL url = UPLOAD_FILE_URL.build(this.getAPI().getBaseURL());
447        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "OPTIONS");
448
449        JsonObject parent = new JsonObject();
450        parent.add("id", this.getID());
451
452        JsonObject preflightInfo = new JsonObject();
453        preflightInfo.add("parent", parent);
454        preflightInfo.add("name", name);
455
456        preflightInfo.add("size", fileSize);
457
458        request.setBody(preflightInfo.toString());
459        BoxAPIResponse response = request.send();
460        response.disconnect();
461    }
462
463    /**
464     * Uploads a new file to this folder.
465     *
466     * @param fileContent a stream containing the contents of the file to upload.
467     * @param name        the name to give the uploaded file.
468     * @return the uploaded file's info.
469     */
470    public BoxFile.Info uploadFile(InputStream fileContent, String name) {
471        FileUploadParams uploadInfo = new FileUploadParams()
472            .setContent(fileContent)
473            .setName(name);
474        return this.uploadFile(uploadInfo);
475    }
476
477    /**
478     * Uploads a new file to this folder.
479     *
480     * @param callback the callback which allows file content to be written on output stream.
481     * @param name     the name to give the uploaded file.
482     * @return the uploaded file's info.
483     */
484    public BoxFile.Info uploadFile(UploadFileCallback callback, String name) {
485        FileUploadParams uploadInfo = new FileUploadParams()
486            .setUploadFileCallback(callback)
487            .setName(name);
488        return this.uploadFile(uploadInfo);
489    }
490
491    /**
492     * Uploads a new file to this folder while reporting the progress to a ProgressListener.
493     *
494     * @param fileContent a stream containing the contents of the file to upload.
495     * @param name        the name to give the uploaded file.
496     * @param fileSize    the size of the file used for determining the progress of the upload.
497     * @param listener    a listener for monitoring the upload's progress.
498     * @return the uploaded file's info.
499     */
500    public BoxFile.Info uploadFile(InputStream fileContent, String name, long fileSize, ProgressListener listener) {
501        FileUploadParams uploadInfo = new FileUploadParams()
502            .setContent(fileContent)
503            .setName(name)
504            .setSize(fileSize)
505            .setProgressListener(listener);
506        return this.uploadFile(uploadInfo);
507    }
508
509    /**
510     * Uploads a new file to this folder with a specified file description.
511     *
512     * @param fileContent a stream containing the contents of the file to upload.
513     * @param name        the name to give the uploaded file.
514     * @param description the description to give the uploaded file.
515     * @return the uploaded file's info.
516     */
517    public BoxFile.Info uploadFile(InputStream fileContent, String name, String description) {
518        FileUploadParams uploadInfo = new FileUploadParams()
519            .setContent(fileContent)
520            .setName(name)
521            .setDescription(description);
522        return this.uploadFile(uploadInfo);
523    }
524
525    /**
526     * Uploads a new file to this folder with custom upload parameters.
527     *
528     * @param uploadParams the custom upload parameters.
529     * @return the uploaded file's info.
530     */
531    public BoxFile.Info uploadFile(FileUploadParams uploadParams) {
532        URL uploadURL = UPLOAD_FILE_URL.build(this.getAPI().getBaseUploadURL());
533        BoxMultipartRequest request = new BoxMultipartRequest(getAPI(), uploadURL);
534
535        JsonObject fieldJSON = new JsonObject();
536        JsonObject parentIdJSON = new JsonObject();
537        parentIdJSON.add("id", getID());
538        fieldJSON.add("name", uploadParams.getName());
539        fieldJSON.add("parent", parentIdJSON);
540
541        if (uploadParams.getCreated() != null) {
542            fieldJSON.add("content_created_at", BoxDateFormat.format(uploadParams.getCreated()));
543        }
544
545        if (uploadParams.getModified() != null) {
546            fieldJSON.add("content_modified_at", BoxDateFormat.format(uploadParams.getModified()));
547        }
548
549        if (uploadParams.getSHA1() != null && !uploadParams.getSHA1().isEmpty()) {
550            request.setContentSHA1(uploadParams.getSHA1());
551        }
552
553        if (uploadParams.getDescription() != null) {
554            fieldJSON.add("description", uploadParams.getDescription());
555        }
556
557        request.putField("attributes", fieldJSON.toString());
558
559        if (uploadParams.getSize() > 0) {
560            request.setFile(uploadParams.getContent(), uploadParams.getName(), uploadParams.getSize());
561        } else if (uploadParams.getContent() != null) {
562            request.setFile(uploadParams.getContent(), uploadParams.getName());
563        } else {
564            request.setUploadFileCallback(uploadParams.getUploadFileCallback(), uploadParams.getName());
565        }
566
567        BoxJSONResponse response;
568        if (uploadParams.getProgressListener() == null) {
569            response = (BoxJSONResponse) request.send();
570        } else {
571            response = (BoxJSONResponse) request.send(uploadParams.getProgressListener());
572        }
573        JsonObject collection = Json.parse(response.getJSON()).asObject();
574        JsonArray entries = collection.get("entries").asArray();
575        JsonObject fileInfoJSON = entries.get(0).asObject();
576        String uploadedFileID = fileInfoJSON.get("id").asString();
577
578        BoxFile uploadedFile = new BoxFile(getAPI(), uploadedFileID);
579        return uploadedFile.new Info(fileInfoJSON);
580    }
581
582    /**
583     * Uploads a new weblink to this folder.
584     *
585     * @param linkURL the URL the weblink points to.
586     * @return the uploaded weblink's info.
587     */
588    public BoxWebLink.Info createWebLink(URL linkURL) {
589        return this.createWebLink(null, linkURL,
590            null);
591    }
592
593    /**
594     * Uploads a new weblink to this folder.
595     *
596     * @param name    the filename for the weblink.
597     * @param linkURL the URL the weblink points to.
598     * @return the uploaded weblink's info.
599     */
600    public BoxWebLink.Info createWebLink(String name, URL linkURL) {
601        return this.createWebLink(name, linkURL,
602            null);
603    }
604
605    /**
606     * Uploads a new weblink to this folder.
607     *
608     * @param linkURL     the URL the weblink points to.
609     * @param description the weblink's description.
610     * @return the uploaded weblink's info.
611     */
612    public BoxWebLink.Info createWebLink(URL linkURL, String description) {
613        return this.createWebLink(null, linkURL, description);
614    }
615
616    /**
617     * Uploads a new weblink to this folder.
618     *
619     * @param name        the filename for the weblink.
620     * @param linkURL     the URL the weblink points to.
621     * @param description the weblink's description.
622     * @return the uploaded weblink's info.
623     */
624    public BoxWebLink.Info createWebLink(String name, URL linkURL, String description) {
625        JsonObject parent = new JsonObject();
626        parent.add("id", this.getID());
627
628        JsonObject newWebLink = new JsonObject();
629        newWebLink.add("name", name);
630        newWebLink.add("parent", parent);
631        newWebLink.add("url", linkURL.toString());
632
633        if (description != null) {
634            newWebLink.add("description", description);
635        }
636
637        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(),
638            CREATE_WEB_LINK_URL.build(this.getAPI().getBaseURL()), "POST");
639        request.setBody(newWebLink.toString());
640        BoxJSONResponse response = (BoxJSONResponse) request.send();
641        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
642
643        BoxWebLink createdWebLink = new BoxWebLink(this.getAPI(), responseJSON.get("id").asString());
644        return createdWebLink.new Info(responseJSON);
645    }
646
647    /**
648     * Returns an iterable containing the items in this folder. Iterating over the iterable returned by this method is
649     * equivalent to iterating over this BoxFolder directly.
650     *
651     * @return an iterable containing the items in this folder.
652     */
653    public Iterable<BoxItem.Info> getChildren() {
654        return this;
655    }
656
657    /**
658     * Returns an iterable containing the items in this folder and specifies which child fields to retrieve from the
659     * API.
660     *
661     * @param fields the fields to retrieve.
662     * @return an iterable containing the items in this folder.
663     */
664    public Iterable<BoxItem.Info> getChildren(final String... fields) {
665        return new Iterable<BoxItem.Info>() {
666            @Override
667            public Iterator<BoxItem.Info> iterator() {
668                String queryString = new QueryStringBuilder().appendParam("fields", fields).toString();
669                URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), queryString, getID());
670                return new BoxItemIterator(getAPI(), url, marker(DEFAULT_LIMIT));
671            }
672        };
673    }
674
675    /**
676     * Returns an iterable containing the items in this folder sorted by name and direction.
677     *
678     * @param sort      the field to sort by, can be set as `name`, `id`, and `date`.
679     * @param direction the direction to display the item results.
680     * @param fields    the fields to retrieve.
681     * @return an iterable containing the items in this folder.
682     */
683    public Iterable<BoxItem.Info> getChildren(String sort, SortDirection direction, final String... fields) {
684        QueryStringBuilder builder = new QueryStringBuilder()
685            .appendParam("sort", sort)
686            .appendParam("direction", direction.toString());
687
688        if (fields.length > 0) {
689            builder.appendParam("fields", fields);
690        }
691        final String query = builder.toString();
692        return new Iterable<BoxItem.Info>() {
693            @Override
694            public Iterator<BoxItem.Info> iterator() {
695                URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), query, getID());
696                return new BoxItemIterator(getAPI(), url, marker(DEFAULT_LIMIT));
697            }
698        };
699    }
700
701    /**
702     * Returns an iterable containing the items in this folder sorted by name and direction.
703     *
704     * @param sort      the field to sort by, can be set as `name`, `id`, and `date`.
705     * @param direction the direction to display the item results.
706     * @param offset    the index of the first child item to retrieve.
707     * @param limit     the maximum number of children to retrieve after the offset.
708     * @param fields    the fields to retrieve.
709     * @return an iterable containing the items in this folder.
710     */
711    public Iterable<BoxItem.Info> getChildren(String sort, SortDirection direction, final long offset, final long limit,
712                                              final String... fields) {
713        QueryStringBuilder builder = new QueryStringBuilder()
714            .appendParam("sort", sort)
715            .appendParam("direction", direction.toString());
716
717        if (fields.length > 0) {
718            builder.appendParam("fields", fields);
719        }
720        final String query = builder.toString();
721        return new Iterable<BoxItem.Info>() {
722            @Override
723            public Iterator<BoxItem.Info> iterator() {
724                URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), query, getID());
725                return new BoxItemIterator(getAPI(), url, limit, offset);
726            }
727        };
728    }
729
730    /**
731     * Retrieves a specific range of child items in this folder.
732     *
733     * @param offset the index of the first child item to retrieve.
734     * @param limit  the maximum number of children to retrieve after the offset.
735     * @param fields the fields to retrieve.
736     * @return a partial collection containing the specified range of child items.
737     */
738    public PartialCollection<BoxItem.Info> getChildrenRange(long offset, long limit, String... fields) {
739        QueryStringBuilder builder = new QueryStringBuilder()
740            .appendParam("limit", limit)
741            .appendParam("offset", offset);
742
743        if (fields.length > 0) {
744            builder.appendParam("fields", fields);
745        }
746
747        URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), builder.toString(), getID());
748        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
749        BoxJSONResponse response = (BoxJSONResponse) request.send();
750        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
751
752        String totalCountString = responseJSON.get("total_count").toString();
753        long fullSize = Double.valueOf(totalCountString).longValue();
754        PartialCollection<BoxItem.Info> children = new PartialCollection<>(offset, limit, fullSize);
755        JsonArray jsonArray = responseJSON.get("entries").asArray();
756        for (JsonValue value : jsonArray) {
757            JsonObject jsonObject = value.asObject();
758            BoxItem.Info parsedItemInfo = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), jsonObject);
759            if (parsedItemInfo != null) {
760                children.add(parsedItemInfo);
761            }
762        }
763        return children;
764    }
765
766    /**
767     * Returns an iterable containing the items in this folder sorted by name and direction.
768     *
769     * @param sortParameters   describes sorting parameters
770     * @param pagingParameters describes paging parameters
771     * @param fields           the fields to retrieve.
772     * @return an iterable containing the items in this folder.
773     */
774    public Iterable<BoxItem.Info> getChildren(
775        final SortParameters sortParameters, final PagingParameters pagingParameters, String... fields
776    ) {
777        QueryStringBuilder builder = sortParameters.asQueryStringBuilder();
778
779        if (fields.length > 0) {
780            builder.appendParam("fields", fields);
781        }
782        final String query = builder.toString();
783        return new Iterable<BoxItem.Info>() {
784            @Override
785            public Iterator<BoxItem.Info> iterator() {
786                URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), query, getID());
787                return new BoxItemIterator(getAPI(), url, pagingParameters);
788            }
789        };
790    }
791
792    /**
793     * Returns an iterator over the items in this folder.
794     *
795     * @return an iterator over the items in this folder.
796     */
797    @Override
798    public Iterator<BoxItem.Info> iterator() {
799        URL url = GET_ITEMS_URL.build(this.getAPI().getBaseURL(), BoxFolder.this.getID());
800        return new BoxItemIterator(BoxFolder.this.getAPI(), url);
801    }
802
803    /**
804     * Adds new {@link BoxWebHook} to this {@link BoxFolder}.
805     *
806     * @param address  {@link BoxWebHook.Info#getAddress()}
807     * @param triggers {@link BoxWebHook.Info#getTriggers()}
808     * @return created {@link BoxWebHook.Info}
809     */
810    public BoxWebHook.Info addWebHook(URL address, BoxWebHook.Trigger... triggers) {
811        return BoxWebHook.create(this, address, triggers);
812    }
813
814    /**
815     * Used to retrieve the watermark for the folder.
816     * If the folder does not have a watermark applied to it, a 404 Not Found will be returned by API.
817     *
818     * @param fields the fields to retrieve.
819     * @return the watermark associated with the folder.
820     */
821    public BoxWatermark getWatermark(String... fields) {
822        return this.getWatermark(FOLDER_INFO_URL_TEMPLATE, fields);
823    }
824
825    /**
826     * Used to apply or update the watermark for the folder.
827     *
828     * @return the watermark associated with the folder.
829     */
830    public BoxWatermark applyWatermark() {
831        return this.applyWatermark(FOLDER_INFO_URL_TEMPLATE, BoxWatermark.WATERMARK_DEFAULT_IMPRINT);
832    }
833
834    /**
835     * Removes a watermark from the folder.
836     * If the folder did not have a watermark applied to it, a 404 Not Found will be returned by API.
837     */
838    public void removeWatermark() {
839        this.removeWatermark(FOLDER_INFO_URL_TEMPLATE);
840    }
841
842    /**
843     * Used to retrieve all metadata associated with the folder.
844     *
845     * @param fields the optional fields to retrieve.
846     * @return An iterable of metadata instances associated with the folder
847     */
848    public Iterable<Metadata> getAllMetadata(String... fields) {
849        return Metadata.getAllMetadata(this, fields);
850    }
851
852    /**
853     * This method is deprecated, please use the {@link BoxSearch} class instead.
854     * Searches this folder and all descendant folders using a given queryPlease use BoxSearch Instead.
855     *
856     * @param query the search query.
857     * @return an Iterable containing the search results.
858     */
859    @Deprecated
860    public Iterable<BoxItem.Info> search(final String query) {
861        return new Iterable<BoxItem.Info>() {
862            @Override
863            public Iterator<BoxItem.Info> iterator() {
864                QueryStringBuilder builder = new QueryStringBuilder();
865                builder.appendParam("query", query);
866                builder.appendParam("ancestor_folder_ids", getID());
867
868                URL url = SEARCH_URL_TEMPLATE.buildWithQuery(getAPI().getBaseURL(), builder.toString());
869                return new BoxItemIterator(getAPI(), url);
870            }
871        };
872    }
873
874    @Override
875    public BoxFolder.Info setCollections(BoxCollection... collections) {
876        JsonArray jsonArray = new JsonArray();
877        for (BoxCollection collection : collections) {
878            JsonObject collectionJSON = new JsonObject();
879            collectionJSON.add("id", collection.getID());
880            jsonArray.add(collectionJSON);
881        }
882        JsonObject infoJSON = new JsonObject();
883        infoJSON.add("collections", jsonArray);
884
885        String queryString = new QueryStringBuilder().appendParam("fields", ALL_FIELDS).toString();
886        URL url = FOLDER_INFO_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
887        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
888        request.setBody(infoJSON.toString());
889        BoxJSONResponse response = (BoxJSONResponse) request.send();
890        JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
891        return new Info(jsonObject);
892    }
893
894    /**
895     * Creates global property metadata on this folder.
896     *
897     * @param metadata the new metadata values.
898     * @return the metadata returned from the server.
899     */
900    public Metadata createMetadata(Metadata metadata) {
901        return this.createMetadata(Metadata.DEFAULT_METADATA_TYPE, metadata);
902    }
903
904    /**
905     * Creates metadata on this folder using a specified template.
906     *
907     * @param templateName the name of the metadata template.
908     * @param metadata     the new metadata values.
909     * @return the metadata returned from the server.
910     */
911    public Metadata createMetadata(String templateName, Metadata metadata) {
912        String scope = Metadata.scopeBasedOnType(templateName);
913        return this.createMetadata(templateName, scope, metadata);
914    }
915
916    /**
917     * Creates metadata on this folder using a specified scope and template.
918     *
919     * @param templateName the name of the metadata template.
920     * @param scope        the scope of the template (usually "global" or "enterprise").
921     * @param metadata     the new metadata values.
922     * @return the metadata returned from the server.
923     */
924    public Metadata createMetadata(String templateName, String scope, Metadata metadata) {
925        URL url = METADATA_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
926        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "POST");
927        request.addHeader("Content-Type", "application/json");
928        request.setBody(metadata.toString());
929        BoxJSONResponse response = (BoxJSONResponse) request.send();
930        return new Metadata(Json.parse(response.getJSON()).asObject());
931    }
932
933    /**
934     * Sets the provided metadata on the folder, overwriting any existing metadata keys already present.
935     *
936     * @param templateName the name of the metadata template.
937     * @param scope        the scope of the template (usually "global" or "enterprise").
938     * @param metadata     the new metadata values.
939     * @return the metadata returned from the server.
940     */
941    public Metadata setMetadata(String templateName, String scope, Metadata metadata) {
942        Metadata metadataValue;
943
944        try {
945            metadataValue = this.createMetadata(templateName, scope, metadata);
946        } catch (BoxAPIException e) {
947            if (e.getResponseCode() == 409) {
948                Metadata metadataToUpdate = new Metadata(scope, templateName);
949                for (JsonValue value : metadata.getOperations()) {
950                    if (value.asObject().get("value").isNumber()) {
951                        metadataToUpdate.add(value.asObject().get("path").asString(),
952                            value.asObject().get("value").asDouble());
953                    } else if (value.asObject().get("value").isString()) {
954                        metadataToUpdate.add(value.asObject().get("path").asString(),
955                            value.asObject().get("value").asString());
956                    } else if (value.asObject().get("value").isArray()) {
957                        ArrayList<String> list = new ArrayList<>();
958                        for (JsonValue jsonValue : value.asObject().get("value").asArray()) {
959                            list.add(jsonValue.asString());
960                        }
961                        metadataToUpdate.add(value.asObject().get("path").asString(), list);
962                    }
963                }
964                metadataValue = this.updateMetadata(metadataToUpdate);
965            } else {
966                throw e;
967            }
968        }
969
970        return metadataValue;
971    }
972
973    /**
974     * Gets the global properties metadata on this folder.
975     *
976     * @return the metadata returned from the server.
977     */
978    public Metadata getMetadata() {
979        return this.getMetadata(Metadata.DEFAULT_METADATA_TYPE);
980    }
981
982    /**
983     * Gets the metadata on this folder associated with a specified template.
984     *
985     * @param templateName the metadata template type name.
986     * @return the metadata returned from the server.
987     */
988    public Metadata getMetadata(String templateName) {
989        String scope = Metadata.scopeBasedOnType(templateName);
990        return this.getMetadata(templateName, scope);
991    }
992
993    /**
994     * Gets the metadata on this folder associated with a specified scope and template.
995     *
996     * @param templateName the metadata template type name.
997     * @param scope        the scope of the template (usually "global" or "enterprise").
998     * @return the metadata returned from the server.
999     */
1000    public Metadata getMetadata(String templateName, String scope) {
1001        URL url = METADATA_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
1002        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
1003        BoxJSONResponse response = (BoxJSONResponse) request.send();
1004        return new Metadata(Json.parse(response.getJSON()).asObject());
1005    }
1006
1007    /**
1008     * Updates the global properties metadata on this folder.
1009     *
1010     * @param metadata the new metadata values.
1011     * @return the metadata returned from the server.
1012     */
1013    public Metadata updateMetadata(Metadata metadata) {
1014        URL url = METADATA_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID(), metadata.getScope(),
1015            metadata.getTemplateName());
1016        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "PUT");
1017        request.addHeader("Content-Type", "application/json-patch+json");
1018        request.setBody(metadata.getPatch());
1019        BoxJSONResponse response = (BoxJSONResponse) request.send();
1020        return new Metadata(Json.parse(response.getJSON()).asObject());
1021    }
1022
1023    /**
1024     * Deletes the global properties metadata on this folder.
1025     */
1026    public void deleteMetadata() {
1027        this.deleteMetadata(Metadata.DEFAULT_METADATA_TYPE);
1028    }
1029
1030    /**
1031     * Deletes the metadata on this folder associated with a specified template.
1032     *
1033     * @param templateName the metadata template type name.
1034     */
1035    public void deleteMetadata(String templateName) {
1036        String scope = Metadata.scopeBasedOnType(templateName);
1037        this.deleteMetadata(templateName, scope);
1038    }
1039
1040    /**
1041     * Deletes the metadata on this folder associated with a specified scope and template.
1042     *
1043     * @param templateName the metadata template type name.
1044     * @param scope        the scope of the template (usually "global" or "enterprise").
1045     */
1046    public void deleteMetadata(String templateName, String scope) {
1047        URL url = METADATA_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
1048        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
1049        BoxAPIResponse response = request.send();
1050        response.disconnect();
1051    }
1052
1053    /**
1054     * Adds a metadata classification to the specified file.
1055     *
1056     * @param classificationType the metadata classification type.
1057     * @return the metadata classification type added to the file.
1058     */
1059    public String addClassification(String classificationType) {
1060        Metadata metadata = new Metadata().add(Metadata.CLASSIFICATION_KEY, classificationType);
1061        Metadata classification = this.createMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY,
1062            "enterprise", metadata);
1063
1064        return classification.getString(Metadata.CLASSIFICATION_KEY);
1065    }
1066
1067    /**
1068     * Updates a metadata classification on the specified file.
1069     *
1070     * @param classificationType the metadata classification type.
1071     * @return the new metadata classification type updated on the file.
1072     */
1073    public String updateClassification(String classificationType) {
1074        Metadata metadata = new Metadata("enterprise", Metadata.CLASSIFICATION_TEMPLATE_KEY);
1075        metadata.replace(Metadata.CLASSIFICATION_KEY, classificationType);
1076        Metadata classification = this.updateMetadata(metadata);
1077
1078        return classification.getString(Metadata.CLASSIFICATION_KEY);
1079    }
1080
1081    /**
1082     * Attempts to add classification to a file. If classification already exists then do update.
1083     *
1084     * @param classificationType the metadata classification type.
1085     * @return the metadata classification type on the file.
1086     */
1087    public String setClassification(String classificationType) {
1088        Metadata metadata = new Metadata().add(Metadata.CLASSIFICATION_KEY, classificationType);
1089        Metadata classification;
1090
1091        try {
1092            classification = this.createMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY, "enterprise", metadata);
1093        } catch (BoxAPIException e) {
1094            if (e.getResponseCode() == 409) {
1095                metadata = new Metadata("enterprise", Metadata.CLASSIFICATION_TEMPLATE_KEY);
1096                metadata.replace(Metadata.CLASSIFICATION_KEY, classificationType);
1097                classification = this.updateMetadata(metadata);
1098            } else {
1099                throw e;
1100            }
1101        }
1102
1103        return classification.getString("/Box__Security__Classification__Key");
1104    }
1105
1106    /**
1107     * Gets the classification type for the specified file.
1108     *
1109     * @return the metadata classification type on the file.
1110     */
1111    public String getClassification() {
1112        Metadata metadata = this.getMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY);
1113        return metadata.getString(Metadata.CLASSIFICATION_KEY);
1114    }
1115
1116    /**
1117     * Deletes the classification on the file.
1118     */
1119    public void deleteClassification() {
1120        this.deleteMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY, "enterprise");
1121    }
1122
1123    /**
1124     * Creates an upload session to create a new file in chunks.
1125     * This will first verify that the file can be created and then open a session for uploading pieces of the file.
1126     *
1127     * @param fileName the name of the file to be created
1128     * @param fileSize the size of the file that will be uploaded
1129     * @return the created upload session instance
1130     */
1131    public BoxFileUploadSession.Info createUploadSession(String fileName, long fileSize) {
1132
1133        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
1134        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST");
1135
1136        JsonObject body = new JsonObject();
1137        body.add("folder_id", this.getID());
1138        body.add("file_name", fileName);
1139        body.add("file_size", fileSize);
1140        request.setBody(body.toString());
1141
1142        BoxJSONResponse response = (BoxJSONResponse) request.send();
1143        JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
1144
1145        String sessionId = jsonObject.get("id").asString();
1146        BoxFileUploadSession session = new BoxFileUploadSession(this.getAPI(), sessionId);
1147
1148        return session.new Info(jsonObject);
1149    }
1150
1151    /**
1152     * Creates a new file.
1153     *
1154     * @param inputStream the stream instance that contains the data.
1155     * @param fileName    the name of the file to be created.
1156     * @param fileSize    the size of the file that will be uploaded.
1157     * @return the created file instance.
1158     * @throws InterruptedException when a thread execution is interrupted.
1159     * @throws IOException          when reading a stream throws exception.
1160     */
1161    public BoxFile.Info uploadLargeFile(InputStream inputStream, String fileName, long fileSize)
1162        throws InterruptedException, IOException {
1163        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
1164        this.canUpload(fileName, fileSize);
1165        return new LargeFileUpload().
1166            upload(this.getAPI(), this.getID(), inputStream, url, fileName, fileSize);
1167    }
1168
1169    /**
1170     * Creates a new file.  Also sets file attributes.
1171     *
1172     * @param inputStream    the stream instance that contains the data.
1173     * @param fileName       the name of the file to be created.
1174     * @param fileSize       the size of the file that will be uploaded.
1175     * @param fileAttributes file attributes to set
1176     * @return the created file instance.
1177     * @throws InterruptedException when a thread execution is interrupted.
1178     * @throws IOException          when reading a stream throws exception.
1179     */
1180    public BoxFile.Info uploadLargeFile(InputStream inputStream, String fileName, long fileSize,
1181                                        Map<String, String> fileAttributes)
1182        throws InterruptedException, IOException {
1183        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
1184        this.canUpload(fileName, fileSize);
1185        return new LargeFileUpload().
1186            upload(this.getAPI(), this.getID(), inputStream, url, fileName, fileSize, fileAttributes);
1187    }
1188
1189    /**
1190     * Creates a new file using specified number of parallel http connections.
1191     *
1192     * @param inputStream          the stream instance that contains the data.
1193     * @param fileName             the name of the file to be created.
1194     * @param fileSize             the size of the file that will be uploaded.
1195     * @param nParallelConnections number of parallel http connections to use
1196     * @param timeOut              time to wait before killing the job
1197     * @param unit                 time unit for the time wait value
1198     * @return the created file instance.
1199     * @throws InterruptedException when a thread execution is interrupted.
1200     * @throws IOException          when reading a stream throws exception.
1201     */
1202    public BoxFile.Info uploadLargeFile(InputStream inputStream, String fileName, long fileSize,
1203                                        int nParallelConnections, long timeOut, TimeUnit unit)
1204        throws InterruptedException, IOException {
1205        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
1206        this.canUpload(fileName, fileSize);
1207        return new LargeFileUpload(nParallelConnections, timeOut, unit).
1208            upload(this.getAPI(), this.getID(), inputStream, url, fileName, fileSize);
1209    }
1210
1211    /**
1212     * Creates a new file using specified number of parallel http connections.  Also sets file attributes.
1213     *
1214     * @param inputStream          the stream instance that contains the data.
1215     * @param fileName             the name of the file to be created.
1216     * @param fileSize             the size of the file that will be uploaded.
1217     * @param nParallelConnections number of parallel http connections to use
1218     * @param timeOut              time to wait before killing the job
1219     * @param unit                 time unit for the time wait value
1220     * @param fileAttributes       file attributes to set
1221     * @return the created file instance.
1222     * @throws InterruptedException when a thread execution is interrupted.
1223     * @throws IOException          when reading a stream throws exception.
1224     */
1225    public BoxFile.Info uploadLargeFile(InputStream inputStream, String fileName, long fileSize,
1226                                        int nParallelConnections, long timeOut, TimeUnit unit,
1227                                        Map<String, String> fileAttributes)
1228        throws InterruptedException, IOException {
1229        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
1230        this.canUpload(fileName, fileSize);
1231        return new LargeFileUpload(nParallelConnections, timeOut, unit).
1232            upload(this.getAPI(), this.getID(), inputStream, url, fileName, fileSize, fileAttributes);
1233    }
1234
1235    /**
1236     * Creates a new Metadata Cascade Policy on a folder.
1237     *
1238     * @param scope       the scope of the metadata cascade policy.
1239     * @param templateKey the key of the template.
1240     * @return information about the Metadata Cascade Policy.
1241     */
1242    public BoxMetadataCascadePolicy.Info addMetadataCascadePolicy(String scope, String templateKey) {
1243
1244        return BoxMetadataCascadePolicy.create(this.getAPI(), this.getID(), scope, templateKey);
1245    }
1246
1247    /**
1248     * Retrieves all Metadata Cascade Policies on a folder.
1249     *
1250     * @param fields optional fields to retrieve for cascade policies.
1251     * @return the Iterable of Box Metadata Cascade Policies in your enterprise.
1252     */
1253    public Iterable<BoxMetadataCascadePolicy.Info> getMetadataCascadePolicies(String... fields) {
1254        return BoxMetadataCascadePolicy.getAll(this.getAPI(), this.getID(), fields);
1255    }
1256
1257    /**
1258     * Retrieves all Metadata Cascade Policies on a folder.
1259     *
1260     * @param enterpriseID the ID of the enterprise to retrieve cascade policies for.
1261     * @param limit        the number of entries of cascade policies to retrieve.
1262     * @param fields       optional fields to retrieve for cascade policies.
1263     * @return the Iterable of Box Metadata Cascade Policies in your enterprise.
1264     */
1265    public Iterable<BoxMetadataCascadePolicy.Info> getMetadataCascadePolicies(String enterpriseID,
1266                                                                              int limit, String... fields) {
1267
1268        return BoxMetadataCascadePolicy.getAll(this.getAPI(), this.getID(), enterpriseID, limit, fields);
1269    }
1270
1271    /**
1272     * Lock this folder.
1273     *
1274     * @return a created folder lock object.
1275     */
1276    public BoxFolderLock.Info lock() {
1277        JsonObject folderObject = new JsonObject();
1278        folderObject.add("type", "folder");
1279        folderObject.add("id", this.getID());
1280
1281        JsonObject lockedOperations = new JsonObject();
1282        lockedOperations.add("move", true);
1283        lockedOperations.add("delete", true);
1284
1285
1286        JsonObject body = new JsonObject();
1287        body.add("folder", folderObject);
1288        body.add("locked_operations", lockedOperations);
1289
1290        BoxJSONRequest request =
1291            new BoxJSONRequest(this.getAPI(), FOLDER_LOCK_URL_TEMPLATE.build(this.getAPI().getBaseURL()),
1292                "POST");
1293        request.setBody(body.toString());
1294        BoxJSONResponse response = (BoxJSONResponse) request.send();
1295        JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
1296
1297        BoxFolderLock createdFolderLock = new BoxFolderLock(this.getAPI(), responseJSON.get("id").asString());
1298        return createdFolderLock.new Info(responseJSON);
1299    }
1300
1301    /**
1302     * Get the lock on this folder.
1303     *
1304     * @return a folder lock object.
1305     */
1306    public Iterable<BoxFolderLock.Info> getLocks() {
1307        String queryString = new QueryStringBuilder().appendParam("folder_id", this.getID()).toString();
1308        final BoxAPIConnection api = this.getAPI();
1309        return new BoxResourceIterable<BoxFolderLock.Info>(api,
1310            FOLDER_LOCK_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), queryString), 100) {
1311            @Override
1312            protected BoxFolderLock.Info factory(JsonObject jsonObject) {
1313                BoxFolderLock folderLock =
1314                    new BoxFolderLock(api, jsonObject.get("id").asString());
1315                return folderLock.new Info(jsonObject);
1316            }
1317        };
1318    }
1319
1320    /**
1321     * Used to specify what direction to sort and display results.
1322     */
1323    public enum SortDirection {
1324        /**
1325         * ASC for ascending order.
1326         */
1327        ASC,
1328
1329        /**
1330         * DESC for descending order.
1331         */
1332        DESC
1333    }
1334
1335    /**
1336     * Enumerates the possible sync states that a folder can have.
1337     */
1338    public enum SyncState {
1339        /**
1340         * The folder is synced.
1341         */
1342        SYNCED("synced"),
1343
1344        /**
1345         * The folder is not synced.
1346         */
1347        NOT_SYNCED("not_synced"),
1348
1349        /**
1350         * The folder is partially synced.
1351         */
1352        PARTIALLY_SYNCED("partially_synced");
1353
1354        private final String jsonValue;
1355
1356        SyncState(String jsonValue) {
1357            this.jsonValue = jsonValue;
1358        }
1359
1360        static SyncState fromJSONValue(String jsonValue) {
1361            return SyncState.valueOf(jsonValue.toUpperCase());
1362        }
1363
1364        String toJSONValue() {
1365            return this.jsonValue;
1366        }
1367    }
1368
1369    /**
1370     * Enumerates the possible permissions that a user can have on a folder.
1371     */
1372    public enum Permission {
1373        /**
1374         * The user can download the folder.
1375         */
1376        CAN_DOWNLOAD("can_download"),
1377
1378        /**
1379         * The user can upload to the folder.
1380         */
1381        CAN_UPLOAD("can_upload"),
1382
1383        /**
1384         * The user can rename the folder.
1385         */
1386        CAN_RENAME("can_rename"),
1387
1388        /**
1389         * The user can delete the folder.
1390         */
1391        CAN_DELETE("can_delete"),
1392
1393        /**
1394         * The user can share the folder.
1395         */
1396        CAN_SHARE("can_share"),
1397
1398        /**
1399         * The user can invite collaborators to the folder.
1400         */
1401        CAN_INVITE_COLLABORATOR("can_invite_collaborator"),
1402
1403        /**
1404         * The user can set the access level for shared links to the folder.
1405         */
1406        CAN_SET_SHARE_ACCESS("can_set_share_access");
1407
1408        private final String jsonValue;
1409
1410        Permission(String jsonValue) {
1411            this.jsonValue = jsonValue;
1412        }
1413
1414        static Permission fromJSONValue(String jsonValue) {
1415            return Permission.valueOf(jsonValue.toUpperCase());
1416        }
1417
1418        String toJSONValue() {
1419            return this.jsonValue;
1420        }
1421    }
1422
1423    /**
1424     * Contains information about a BoxFolder.
1425     */
1426    public class Info extends BoxItem.Info {
1427        private BoxUploadEmail uploadEmail;
1428        private boolean hasCollaborations;
1429        private SyncState syncState;
1430        private EnumSet<Permission> permissions;
1431        private boolean canNonOwnersInvite;
1432        private boolean isWatermarked;
1433        private boolean isCollaborationRestrictedToEnterprise;
1434        private boolean isExternallyOwned;
1435        private Map<String, Map<String, Metadata>> metadataMap;
1436        private List<String> allowedSharedLinkAccessLevels;
1437        private List<String> allowedInviteeRoles;
1438        private BoxClassification classification;
1439
1440        /**
1441         * Constructs an empty Info object.
1442         */
1443        public Info() {
1444            super();
1445        }
1446
1447        /**
1448         * Constructs an Info object by parsing information from a JSON string.
1449         *
1450         * @param json the JSON string to parse.
1451         */
1452        public Info(String json) {
1453            super(json);
1454        }
1455
1456        /**
1457         * Constructs an Info object using an already parsed JSON object.
1458         *
1459         * @param jsonObject the parsed JSON object.
1460         */
1461        public Info(JsonObject jsonObject) {
1462            super(jsonObject);
1463        }
1464
1465        /**
1466         * Gets the upload email for the folder.
1467         *
1468         * @return the upload email for the folder.
1469         */
1470        public BoxUploadEmail getUploadEmail() {
1471            return this.uploadEmail;
1472        }
1473
1474        /**
1475         * Sets the upload email for the folder.
1476         *
1477         * @param uploadEmail the upload email for the folder.
1478         */
1479        public void setUploadEmail(BoxUploadEmail uploadEmail) {
1480            if (this.uploadEmail == uploadEmail) {
1481                return;
1482            }
1483
1484            this.removeChildObject("folder_upload_email");
1485            this.uploadEmail = uploadEmail;
1486
1487            if (uploadEmail == null) {
1488                this.addPendingChange("folder_upload_email", (String) null);
1489            } else {
1490                this.addChildObject("folder_upload_email", uploadEmail);
1491            }
1492        }
1493
1494        /**
1495         * Gets whether or not the folder has any collaborations.
1496         *
1497         * @return true if the folder has collaborations; otherwise false.
1498         */
1499        public boolean getHasCollaborations() {
1500            return this.hasCollaborations;
1501        }
1502
1503        /**
1504         * Gets the sync state of the folder.
1505         *
1506         * @return the sync state of the folder.
1507         */
1508        public SyncState getSyncState() {
1509            return this.syncState;
1510        }
1511
1512        /**
1513         * Sets the sync state of the folder.
1514         *
1515         * @param syncState the sync state of the folder.
1516         */
1517        public void setSyncState(SyncState syncState) {
1518            this.syncState = syncState;
1519            this.addPendingChange("sync_state", syncState.toJSONValue());
1520        }
1521
1522        /**
1523         * Gets the permissions that the current user has on the folder.
1524         *
1525         * @return the permissions that the current user has on the folder.
1526         */
1527        public EnumSet<Permission> getPermissions() {
1528            return this.permissions;
1529        }
1530
1531        /**
1532         * Gets whether or not the non-owners can invite collaborators to the folder.
1533         *
1534         * @return [description]
1535         */
1536        public boolean getCanNonOwnersInvite() {
1537            return this.canNonOwnersInvite;
1538        }
1539
1540        /**
1541         * Sets whether or not non-owners can invite collaborators to the folder.
1542         *
1543         * @param canNonOwnersInvite indicates non-owners can invite collaborators to the folder.
1544         */
1545        public void setCanNonOwnersInvite(boolean canNonOwnersInvite) {
1546            this.canNonOwnersInvite = canNonOwnersInvite;
1547            this.addPendingChange("can_non_owners_invite", canNonOwnersInvite);
1548        }
1549
1550        /**
1551         * Gets whether future collaborations should be restricted to within the enterprise only.
1552         *
1553         * @return indicates whether collaboration is restricted to enterprise only.
1554         */
1555        public boolean getIsCollaborationRestrictedToEnterprise() {
1556            return this.isCollaborationRestrictedToEnterprise;
1557        }
1558
1559        /**
1560         * Sets whether future collaborations should be restricted to within the enterprise only.
1561         *
1562         * @param isRestricted indicates whether there is collaboration restriction within enterprise.
1563         */
1564        public void setIsCollaborationRestrictedToEnterprise(boolean isRestricted) {
1565            this.isCollaborationRestrictedToEnterprise = isRestricted;
1566            this.addPendingChange("is_collaboration_restricted_to_enterprise", isRestricted);
1567        }
1568
1569        /**
1570         * Retrieves the allowed roles for collaborations.
1571         *
1572         * @return the roles allowed for collaboration.
1573         */
1574        public List<String> getAllowedInviteeRoles() {
1575            return this.allowedInviteeRoles;
1576        }
1577
1578        /**
1579         * Retrieves the allowed access levels for a shared link.
1580         *
1581         * @return the allowed access levels for a shared link.
1582         */
1583        public List<String> getAllowedSharedLinkAccessLevels() {
1584            return this.allowedSharedLinkAccessLevels;
1585        }
1586
1587        /**
1588         * Gets flag indicating whether this file is Watermarked.
1589         *
1590         * @return whether the file is watermarked or not
1591         */
1592        public boolean getIsWatermarked() {
1593            return this.isWatermarked;
1594        }
1595
1596        /**
1597         * Gets the metadata on this folder associated with a specified scope and template.
1598         * Makes an attempt to get metadata that was retrieved using getInfo(String ...) method.
1599         *
1600         * @param templateName the metadata template type name.
1601         * @param scope        the scope of the template (usually "global" or "enterprise").
1602         * @return the metadata returned from the server.
1603         */
1604        public Metadata getMetadata(String templateName, String scope) {
1605            try {
1606                return this.metadataMap.get(scope).get(templateName);
1607            } catch (NullPointerException e) {
1608                return null;
1609            }
1610        }
1611
1612        /**
1613         * Get the field is_externally_owned determining whether this folder is owned by a user outside of the
1614         * enterprise.
1615         *
1616         * @return a boolean indicating whether this folder is owned by a user outside the enterprise.
1617         */
1618        public boolean getIsExternallyOwned() {
1619            return this.isExternallyOwned;
1620        }
1621
1622        /**
1623         * Gets the metadata classification type of this folder.
1624         *
1625         * @return the metadata classification type of this folder.
1626         */
1627        public BoxClassification getClassification() {
1628            return this.classification;
1629        }
1630
1631        @Override
1632        public BoxFolder getResource() {
1633            return BoxFolder.this;
1634        }
1635
1636        @Override
1637        protected void parseJSONMember(JsonObject.Member member) {
1638            super.parseJSONMember(member);
1639
1640            String memberName = member.getName();
1641            JsonValue value = member.getValue();
1642            try {
1643                if (memberName.equals("folder_upload_email")) {
1644                    if (this.uploadEmail == null) {
1645                        this.uploadEmail = new BoxUploadEmail(value.asObject());
1646                    } else {
1647                        this.uploadEmail.update(value.asObject());
1648                    }
1649
1650                } else if (memberName.equals("has_collaborations")) {
1651                    this.hasCollaborations = value.asBoolean();
1652
1653                } else if (memberName.equals("sync_state")) {
1654                    this.syncState = SyncState.fromJSONValue(value.asString());
1655
1656                } else if (memberName.equals("permissions")) {
1657                    this.permissions = this.parsePermissions(value.asObject());
1658
1659                } else if (memberName.equals("can_non_owners_invite")) {
1660                    this.canNonOwnersInvite = value.asBoolean();
1661                } else if (memberName.equals("allowed_shared_link_access_levels")) {
1662                    this.allowedSharedLinkAccessLevels = this.parseSharedLinkAccessLevels(value.asArray());
1663                } else if (memberName.equals("allowed_invitee_roles")) {
1664                    this.allowedInviteeRoles = this.parseAllowedInviteeRoles(value.asArray());
1665                } else if (memberName.equals("is_collaboration_restricted_to_enterprise")) {
1666                    this.isCollaborationRestrictedToEnterprise = value.asBoolean();
1667                } else if (memberName.equals("is_externally_owned")) {
1668                    this.isExternallyOwned = value.asBoolean();
1669                } else if (memberName.equals("watermark_info")) {
1670                    JsonObject jsonObject = value.asObject();
1671                    this.isWatermarked = jsonObject.get("is_watermarked").asBoolean();
1672                } else if (memberName.equals("metadata")) {
1673                    JsonObject jsonObject = value.asObject();
1674                    this.metadataMap = Parsers.parseAndPopulateMetadataMap(jsonObject);
1675                } else if (memberName.equals("classification")) {
1676                    if (value.isNull()) {
1677                        this.classification = null;
1678                    } else {
1679                        this.classification = new BoxClassification(value.asObject());
1680                    }
1681                }
1682            } catch (Exception e) {
1683                throw new BoxDeserializationException(memberName, value.toString(), e);
1684            }
1685        }
1686
1687        private EnumSet<Permission> parsePermissions(JsonObject jsonObject) {
1688            EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class);
1689            for (JsonObject.Member member : jsonObject) {
1690                JsonValue value = member.getValue();
1691                if (value.isNull() || !value.asBoolean()) {
1692                    continue;
1693                }
1694
1695                String memberName = member.getName();
1696                if (memberName.equals("can_download")) {
1697                    permissions.add(Permission.CAN_DOWNLOAD);
1698                } else if (memberName.equals("can_upload")) {
1699                    permissions.add(Permission.CAN_UPLOAD);
1700                } else if (memberName.equals("can_rename")) {
1701                    permissions.add(Permission.CAN_RENAME);
1702                } else if (memberName.equals("can_delete")) {
1703                    permissions.add(Permission.CAN_DELETE);
1704                } else if (memberName.equals("can_share")) {
1705                    permissions.add(Permission.CAN_SHARE);
1706                } else if (memberName.equals("can_invite_collaborator")) {
1707                    permissions.add(Permission.CAN_INVITE_COLLABORATOR);
1708                } else if (memberName.equals("can_set_share_access")) {
1709                    permissions.add(Permission.CAN_SET_SHARE_ACCESS);
1710                }
1711            }
1712
1713            return permissions;
1714        }
1715
1716        private List<String> parseSharedLinkAccessLevels(JsonArray jsonArray) {
1717            List<String> accessLevels = new ArrayList<>(jsonArray.size());
1718            for (JsonValue value : jsonArray) {
1719                accessLevels.add(value.asString());
1720            }
1721
1722            return accessLevels;
1723        }
1724
1725        private List<String> parseAllowedInviteeRoles(JsonArray jsonArray) {
1726            List<String> roles = new ArrayList<>(jsonArray.size());
1727            for (JsonValue value : jsonArray) {
1728                roles.add(value.asString());
1729            }
1730
1731            return roles;
1732        }
1733    }
1734}