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