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