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 * Represents a collaboration whitelist between a user and a Box Enterprise. Collaboration Whitelist enables a Box
013 * Enterprise(only available if you have Box Governance) to manage a set of approved users 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_whitelist_exempt_target")
021public class BoxCollaborationWhitelistExemptTarget extends BoxResource {
022    /**
023     * Collaboration Whitelist Exempt Target Entries URL Template.
024     */
025    public static final URLTemplate COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRIES_URL_TEMPLATE =
026            new URLTemplate("collaboration_whitelist_exempt_targets");
027
028    /**
029     * Collaboration Whitelist Exempt Target Entries URL Template with given ID.
030     */
031    public static final URLTemplate COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE =
032            new URLTemplate("collaboration_whitelist_exempt_targets/%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 BoxCollaborationWhitelistExemptTarget for a collaboration whitelist with a give ID.
041     *
042     * @param api   the API connection to be used by the collaboration whitelist.
043     * @param id    the ID of the collaboration whitelist.
044     */
045    public BoxCollaborationWhitelistExemptTarget(BoxAPIConnection api, String id) {
046
047        super(api, id);
048    }
049
050    /**
051     * Creates a collaboration whitelist for a Box User with a given ID.
052     * @param api       the API connection to be used by the collaboration whitelist.
053     * @param userID    the ID of the Box User to add to the collaboration whitelist.
054     * @return          information about the collaboration whitelist created for user.
055     */
056    public static BoxCollaborationWhitelistExemptTarget.Info create(final BoxAPIConnection api, String userID) {
057        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRIES_URL_TEMPLATE.build(api.getBaseURL());
058        BoxJSONRequest request = new BoxJSONRequest(api, url, HttpMethod.POST);
059        JsonObject requestJSON = new JsonObject()
060                .add("user", new JsonObject()
061                    .add("type", "user")
062                    .add("id", userID));
063
064        request.setBody(requestJSON.toString());
065        BoxJSONResponse response = (BoxJSONResponse) request.send();
066        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
067        BoxCollaborationWhitelistExemptTarget userWhitelist = new BoxCollaborationWhitelistExemptTarget(api,
068                responseJSON.get("id").asString());
069
070        return userWhitelist.new Info(responseJSON);
071    }
072
073    /**
074     * Retrieves information for a collaboration whitelist for a given whitelist ID.
075     *
076     * @return information about this {@link BoxCollaborationWhitelistExemptTarget}.
077     */
078    public BoxCollaborationWhitelistExemptTarget.Info getInfo() {
079        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE.build(this.getAPI().getBaseURL(),
080                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 for user with default limit set to 100.
089     *
090     * @param api       the API connection to be use by the resource.
091     * @param fields    the fields to retrieve.
092     * @return  an iterable with all the collaboration whitelists for users met search conditions.
093     */
094    public static Iterable<BoxCollaborationWhitelistExemptTarget.Info> getAll(final BoxAPIConnection api,
095                                                                              String ... fields) {
096        return getAll(api, DEFAULT_LIMIT, fields);
097    }
098
099    /**
100     * Returns all the collaboration whitelisting for user with specified filters.
101     * @param api       the API connection to be used by the resource.
102     * @param limit     the number of collaboration whitelists to retrieve.
103     * @param fields    the fields to retrieve.
104     * @return  an iterable with all the collaboration whitelists for users met search conditions.
105     */
106    public static Iterable<BoxCollaborationWhitelistExemptTarget.Info> getAll(final BoxAPIConnection api, int limit,
107                                                                               String ... fields) {
108        QueryStringBuilder builder = new QueryStringBuilder();
109        if (fields.length > 0) {
110            builder.appendParam("fields", fields);
111        }
112
113        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRIES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(),
114                builder.toString());
115        return new BoxResourceIterable<BoxCollaborationWhitelistExemptTarget.Info>(api, url, limit) {
116
117            @Override
118            protected BoxCollaborationWhitelistExemptTarget.Info factory(JsonObject jsonObject) {
119                BoxCollaborationWhitelistExemptTarget userWhitelist = new BoxCollaborationWhitelistExemptTarget(
120                        api, jsonObject.get("id").asString());
121
122                return userWhitelist.new Info(jsonObject);
123            }
124        };
125    }
126
127    /**
128     * Deletes this collaboration whitelist entry for user.
129     */
130    public void delete() {
131        BoxAPIConnection api = this.getAPI();
132        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE.build(api.getBaseURL(),
133                this.getID());
134
135        BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.DELETE);
136        BoxAPIResponse response = request.send();
137        response.disconnect();
138    }
139    /**
140     * Contains information about a BoxCollaborationWhitelistExemptTarget.
141     */
142    public class Info extends BoxResource.Info {
143        private String type;
144        private BoxUser.Info user;
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 for user.
171         *
172         * @return the type of the collaboration whitelist for user.
173         */
174        public String getType() {
175
176            return this.type;
177        }
178        /**
179         * Gets the user added to the collaboration whitelist.
180         *
181         * @return the user in the collaboration whitelist.
182         */
183        public BoxUser.Info getUser() {
184
185            return this.user;
186        }
187
188        /**
189         * Gets the enterprise that the collaboration whitelist for user belongs to.
190         *
191         * @return the enterprise that the collaboration whitelist for user belongs to.
192         */
193        public BoxEnterprise getEnterprise() {
194
195            return this.enterprise;
196        }
197
198        /**
199         * Gets the time the collaboration whitelist was created for user.
200         *
201         * @return the time the collaboration whitelist was created for user.
202         */
203        public Date getCreatedAt() {
204
205            return this.createdAt;
206        }
207
208        /**
209         * Gets the last modified time of the collaboration whitelist for user.
210         *
211         * @return the last modified time of the collaboration whitelist for user.
212         */
213        public Date getModifiedAt() {
214
215            return this.modifiedAt;
216        }
217
218        @Override
219        public BoxCollaborationWhitelistExemptTarget getResource() {
220            return BoxCollaborationWhitelistExemptTarget.this;
221        }
222
223        @Override
224        protected void parseJSONMember(JsonObject.Member member) {
225            super.parseJSONMember(member);
226
227            String memberName = member.getName();
228            JsonValue value = member.getValue();
229            try {
230                if (memberName.equals("user")) {
231                    JsonObject userJSON = value.asObject();
232                    String userID = userJSON.get("id").asString();
233                    BoxUser user = new BoxUser(getAPI(), userID);
234                    this.user = user.new Info(userJSON);
235
236                } else if (memberName.equals("type")) {
237                    this.type = value.asString();
238
239                } else if (memberName.equals("enterprise")) {
240                    JsonObject jsonObject = value.asObject();
241                    this.enterprise = new BoxEnterprise(jsonObject);
242
243                } else if (memberName.equals("created_at")) {
244                    this.createdAt = BoxDateFormat.parse(value.asString());
245
246                } else if (memberName.equals("modified_at")) {
247                    this.modifiedAt = BoxDateFormat.parse(value.asString());
248                }
249            } catch (ParseException e) {
250                assert false : "Error in parsing BoxCollaborationWhitelistExemptTarget JSON object";
251            }
252        }
253    }
254}