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