001package com.box.sdk;
002
003import com.box.sdk.http.HttpMethod;
004import com.eclipsesource.json.JsonObject;
005import com.eclipsesource.json.JsonValue;
006import java.net.URL;
007import java.util.Date;
008
009
010/**
011 * Represents a collaboration allowlist between a domain and a Box Enterprise. Collaboration Allowlist enables a Box
012 * Enterprise(only available if you have Box Governance) to manage a set of approved domains that can collaborate
013 * with an enterprise.
014 *
015 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
016 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
017 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
018 */
019@BoxResourceType("collaboration_allowlist_entry")
020public class BoxCollaborationAllowlist extends BoxResource {
021    /**
022     * Collaboration Allowlist Entries URL Template.
023     */
024    public static final URLTemplate COLLABORATION_ALLOWLIST_ENTRIES_URL_TEMPLATE =
025        new URLTemplate("collaboration_whitelist_entries");
026
027    /**
028     * Collaboration Allowlist Entries URL Template with given ID.
029     */
030    public static final URLTemplate COLLABORATION_ALLOWLIST_ENTRY_URL_TEMPLATE =
031        new URLTemplate("collaboration_whitelist_entries/%s");
032
033    /**
034     * The default limit of entries per response.
035     */
036    private static final int DEFAULT_LIMIT = 100;
037
038    /**
039     * Constructs a BoxCollaborationAllowlist for a collaboration allowlist with a given ID.
040     *
041     * @param api the API connection to be used by the collaboration allowlist.
042     * @param id  the ID of the collaboration allowlist.
043     */
044    public BoxCollaborationAllowlist(BoxAPIConnection api, String id) {
045        super(api, id);
046    }
047
048    /**
049     * Creates a new Collaboration Allowlist for a domain.
050     *
051     * @param api       the API connection to be used by the resource.
052     * @param domain    the domain to be added to a collaboration allowlist for a Box Enterprise.
053     * @param direction an enum representing the direction of the collaboration allowlist. Can be set to
054     *                  inbound, outbound, or both.
055     * @return information about the collaboration allowlist created.
056     */
057    public static BoxCollaborationAllowlist.Info create(final BoxAPIConnection api, String domain,
058                                                        AllowlistDirection direction) {
059
060        URL url = COLLABORATION_ALLOWLIST_ENTRIES_URL_TEMPLATE.build(api.getBaseURL());
061        BoxJSONRequest request = new BoxJSONRequest(api, url, HttpMethod.POST);
062        JsonObject requestJSON = new JsonObject()
063            .add("domain", domain)
064            .add("direction", direction.toString());
065
066        request.setBody(requestJSON.toString());
067        BoxJSONResponse response = (BoxJSONResponse) request.send();
068        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
069        BoxCollaborationAllowlist domainAllowlist =
070            new BoxCollaborationAllowlist(api, responseJSON.get("id").asString());
071
072        return domainAllowlist.new Info(responseJSON);
073    }
074
075    /**
076     * Returns all the collaboration allowlisting with specified filters.
077     *
078     * @param api    the API connection to be used by the resource.
079     * @param fields the fields to retrieve.
080     * @return an iterable with all the collaboration allowlists met search conditions.
081     */
082    public static Iterable<BoxCollaborationAllowlist.Info> getAll(final BoxAPIConnection api, String... fields) {
083
084        return getAll(api, DEFAULT_LIMIT, fields);
085    }
086
087    /**
088     * Returns all the collaboration allowlisting with specified filters.
089     *
090     * @param api    the API connection to be used by the resource.
091     * @param limit  the limit of items per single response. The default value is 100.
092     * @param fields the fields to retrieve.
093     * @return an iterable with all the collaboration allowlists met search conditions.
094     */
095    public static Iterable<BoxCollaborationAllowlist.Info> getAll(final BoxAPIConnection api, int limit,
096                                                                  String... fields) {
097
098        QueryStringBuilder builder = new QueryStringBuilder();
099        if (fields.length > 0) {
100            builder.appendParam("fields", fields);
101        }
102
103        URL url = COLLABORATION_ALLOWLIST_ENTRIES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
104        return new BoxResourceIterable<BoxCollaborationAllowlist.Info>(api, url, limit) {
105
106            @Override
107            protected BoxCollaborationAllowlist.Info factory(JsonObject jsonObject) {
108                BoxCollaborationAllowlist allowlist = new BoxCollaborationAllowlist(
109                    api, jsonObject.get("id").asString());
110
111                return allowlist.new Info(jsonObject);
112            }
113        };
114    }
115
116    /**
117     * @return information about this {@link BoxCollaborationAllowlist}.
118     */
119    public BoxCollaborationAllowlist.Info getInfo() {
120        URL url = COLLABORATION_ALLOWLIST_ENTRY_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
121        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, HttpMethod.GET);
122        BoxJSONResponse response = (BoxJSONResponse) request.send();
123
124        return new Info(JsonObject.readFrom(response.getJSON()));
125    }
126
127    /**
128     * Deletes this collaboration allowlist.
129     */
130    public void delete() {
131        BoxAPIConnection api = this.getAPI();
132        URL url = COLLABORATION_ALLOWLIST_ENTRY_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
133
134        BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.DELETE);
135        BoxAPIResponse response = request.send();
136        response.disconnect();
137    }
138
139    /**
140     * Enumerates the direction of the collaboration allowlist.
141     */
142    public enum AllowlistDirection {
143        /**
144         * Allowlist inbound collaboration.
145         */
146        INBOUND("inbound"),
147
148        /**
149         * Allowlist outbound collaboration.
150         */
151        OUTBOUND("outbound"),
152
153        /**
154         * Allowlist both inbound and outbound collaboration.
155         */
156        BOTH("both");
157
158        private final String direction;
159
160        AllowlistDirection(String direction) {
161
162            this.direction = direction;
163        }
164
165        static AllowlistDirection fromDirection(String direction) {
166            if (direction.equals("inbound")) {
167                return AllowlistDirection.INBOUND;
168            } else if (direction.equals("outbound")) {
169                return AllowlistDirection.OUTBOUND;
170            } else if (direction.equals("both")) {
171                return AllowlistDirection.BOTH;
172            } else {
173                return null;
174            }
175        }
176
177        /**
178         * Returns a String containing the current direction of the collaboration allowlisting.
179         *
180         * @return a String containing information about the direction of the collaboration allowlisting.
181         */
182        public String toString() {
183
184            return this.direction;
185        }
186    }
187
188    /**
189     * Contains information about a BoxCollaborationAllowlist.
190     */
191    public class Info extends BoxResource.Info {
192        private String type;
193        private String domain;
194        private AllowlistDirection direction;
195        private BoxEnterprise enterprise;
196        private Date createdAt;
197        private Date modifiedAt;
198
199        /**
200         * Constructs an empty Info object.
201         */
202        public Info() {
203            super();
204        }
205
206        /**
207         * Constructs an Info object by parsing information from a JSON string.
208         *
209         * @param json the JSON string to parse.
210         */
211        public Info(String json) {
212            super(json);
213        }
214
215        Info(JsonObject jsonObject) {
216            super(jsonObject);
217        }
218
219        /**
220         * Gets the type of the collaboration allowlist.
221         *
222         * @return the type for the collaboration allowlist.
223         */
224        public String getType() {
225
226            return this.type;
227        }
228
229        /**
230         * Gets the domain added to the collaboration allowlist.
231         *
232         * @return the domain in the collaboration allowlist
233         */
234        public String getDomain() {
235
236            return this.domain;
237        }
238
239        /**
240         * Get the direction of the collaboration allowlist. Values can be inbound, outbound, or
241         * both.
242         *
243         * @return the direction set for the collaboration allowlist. Values can be inbound, outbound, or both.
244         */
245        public AllowlistDirection getDirection() {
246
247            return this.direction;
248        }
249
250        /**
251         * Gets the enterprise that the collaboration allowlist belongs to.
252         *
253         * @return the enterprise that the collaboration allowlist belongs to.
254         */
255        public BoxEnterprise getEnterprise() {
256
257            return this.enterprise;
258        }
259
260        /**
261         * Gets the time the collaboration allowlist was created.
262         *
263         * @return the time the collaboration allowlist was created.
264         */
265        public Date getCreatedAt() {
266
267            return this.createdAt;
268        }
269
270        /**
271         * Gets the time the collaboration allowlist was last modified.
272         *
273         * @return the time the collaboration allowlist was last modified.
274         */
275        public Date getModifiedAt() {
276
277            return this.modifiedAt;
278        }
279
280        @Override
281        public BoxCollaborationAllowlist getResource() {
282            return BoxCollaborationAllowlist.this;
283        }
284
285        @Override
286        protected void parseJSONMember(JsonObject.Member member) {
287            super.parseJSONMember(member);
288
289            String memberName = member.getName();
290            JsonValue value = member.getValue();
291            try {
292                if (memberName.equals("domain")) {
293                    this.domain = value.asString();
294
295                } else if (memberName.equals("type")) {
296                    this.type = value.asString();
297
298                } else if (memberName.equals("direction")) {
299                    this.direction = AllowlistDirection.fromDirection(value.asString());
300
301                } else if (memberName.equals("enterprise")) {
302                    JsonObject jsonObject = value.asObject();
303                    this.enterprise = new BoxEnterprise(jsonObject);
304
305                } else if (memberName.equals("created_at")) {
306                    this.createdAt = BoxDateFormat.parse(value.asString());
307
308                } else if (memberName.equals("modified_at")) {
309                    this.modifiedAt = BoxDateFormat.parse(value.asString());
310
311                }
312            } catch (Exception e) {
313                throw new BoxDeserializationException(memberName, value.toString(), e);
314            }
315        }
316    }
317}