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 * Represents a collaboration whitelist between a user and a Box Enterprise. Collaboration Whitelist enables a Box
011 * Enterprise(only available if you have Box Governance) to manage a set of approved users that can collaborate
012 * with an enterprise.
013 *
014 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
015 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
016 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
017 */
018@BoxResourceType("collaboration_whitelist_exempt_target")
019@Deprecated
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     *
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     * Returns all the collaboration whitelisting for user with default limit set to 100.
075     *
076     * @param api    the API connection to be use by the resource.
077     * @param fields the fields to retrieve.
078     * @return an iterable with all the collaboration whitelists for users met search conditions.
079     */
080    public static Iterable<BoxCollaborationWhitelistExemptTarget.Info> getAll(final BoxAPIConnection api,
081                                                                              String... fields) {
082        return getAll(api, DEFAULT_LIMIT, fields);
083    }
084
085    /**
086     * Returns all the collaboration whitelisting for user with specified filters.
087     *
088     * @param api    the API connection to be used by the resource.
089     * @param limit  the number of collaboration whitelists to retrieve.
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, int limit,
094                                                                              String... fields) {
095        QueryStringBuilder builder = new QueryStringBuilder();
096        if (fields.length > 0) {
097            builder.appendParam("fields", fields);
098        }
099
100        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRIES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(),
101            builder.toString());
102        return new BoxResourceIterable<BoxCollaborationWhitelistExemptTarget.Info>(api, url, limit) {
103
104            @Override
105            protected BoxCollaborationWhitelistExemptTarget.Info factory(JsonObject jsonObject) {
106                BoxCollaborationWhitelistExemptTarget userWhitelist = new BoxCollaborationWhitelistExemptTarget(
107                    api, jsonObject.get("id").asString());
108
109                return userWhitelist.new Info(jsonObject);
110            }
111        };
112    }
113
114    /**
115     * Retrieves information for a collaboration whitelist for a given whitelist ID.
116     *
117     * @return information about this {@link BoxCollaborationWhitelistExemptTarget}.
118     */
119    public BoxCollaborationWhitelistExemptTarget.Info getInfo() {
120        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE.build(this.getAPI().getBaseURL(),
121            this.getID());
122        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, HttpMethod.GET);
123        BoxJSONResponse response = (BoxJSONResponse) request.send();
124
125        return new Info(JsonObject.readFrom(response.getJSON()));
126    }
127
128    /**
129     * Deletes this collaboration whitelist entry for user.
130     */
131    public void delete() {
132        BoxAPIConnection api = this.getAPI();
133        URL url = COLLABORATION_WHITELIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE.build(api.getBaseURL(),
134            this.getID());
135
136        BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.DELETE);
137        BoxAPIResponse response = request.send();
138        response.disconnect();
139    }
140
141    /**
142     * Contains information about a BoxCollaborationWhitelistExemptTarget.
143     */
144    public class Info extends BoxResource.Info {
145        private String type;
146        private BoxUser.Info user;
147        private BoxEnterprise enterprise;
148        private Date createdAt;
149        private Date modifiedAt;
150
151        /**
152         * Constructs an empty Info object.
153         */
154        public Info() {
155            super();
156        }
157
158        /**
159         * Constructs an Info object by parsing information from a JSON string.
160         *
161         * @param json the JSON string to parse.
162         */
163        public Info(String json) {
164            super(json);
165        }
166
167        Info(JsonObject jsonObject) {
168            super(jsonObject);
169        }
170
171        /**
172         * Gets the type of the collaboration whitelist for user.
173         *
174         * @return the type of the collaboration whitelist for user.
175         */
176        public String getType() {
177
178            return this.type;
179        }
180
181        /**
182         * Gets the user added to the collaboration whitelist.
183         *
184         * @return the user in the collaboration whitelist.
185         */
186        public BoxUser.Info getUser() {
187
188            return this.user;
189        }
190
191        /**
192         * Gets the enterprise that the collaboration whitelist for user belongs to.
193         *
194         * @return the enterprise that the collaboration whitelist for user belongs to.
195         */
196        public BoxEnterprise getEnterprise() {
197
198            return this.enterprise;
199        }
200
201        /**
202         * Gets the time the collaboration whitelist was created for user.
203         *
204         * @return the time the collaboration whitelist was created for user.
205         */
206        public Date getCreatedAt() {
207
208            return this.createdAt;
209        }
210
211        /**
212         * Gets the last modified time of the collaboration whitelist for user.
213         *
214         * @return the last modified time of the collaboration whitelist for user.
215         */
216        public Date getModifiedAt() {
217
218            return this.modifiedAt;
219        }
220
221        @Override
222        public BoxCollaborationWhitelistExemptTarget getResource() {
223            return BoxCollaborationWhitelistExemptTarget.this;
224        }
225
226        @Override
227        protected void parseJSONMember(JsonObject.Member member) {
228            super.parseJSONMember(member);
229
230            String memberName = member.getName();
231            JsonValue value = member.getValue();
232            try {
233                if (memberName.equals("user")) {
234                    JsonObject userJSON = value.asObject();
235                    String userID = userJSON.get("id").asString();
236                    BoxUser user = new BoxUser(getAPI(), userID);
237                    this.user = user.new Info(userJSON);
238
239                } else if (memberName.equals("type")) {
240                    this.type = value.asString();
241
242                } else if (memberName.equals("enterprise")) {
243                    JsonObject jsonObject = value.asObject();
244                    this.enterprise = new BoxEnterprise(jsonObject);
245
246                } else if (memberName.equals("created_at")) {
247                    this.createdAt = BoxDateFormat.parse(value.asString());
248
249                } else if (memberName.equals("modified_at")) {
250                    this.modifiedAt = BoxDateFormat.parse(value.asString());
251                }
252            } catch (Exception e) {
253                throw new BoxDeserializationException(memberName, value.toString(), e);
254            }
255        }
256    }
257}