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 * Represents a collaboration allowlist between a user and a Box Enterprise. Collaboration Allowlist 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_allowlist_exempt_target")
020public class BoxCollaborationAllowlistExemptTarget extends BoxResource {
021    /**
022     * Collaboration Allowlist Exempt Target Entries URL Template.
023     */
024    public static final URLTemplate COLLABORATION_ALLOWLIST_EXEMPT_TARGET_ENTRIES_URL_TEMPLATE =
025        new URLTemplate("collaboration_whitelist_exempt_targets");
026
027    /**
028     * Collaboration Allowlist Exempt Target Entries URL Template with given ID.
029     */
030    public static final URLTemplate COLLABORATION_ALLOWLIST_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 BoxCollaborationAllowlistExemptTarget for a collaboration allowlist with a give ID.
040     *
041     * @param api the API connection to be used by the collaboration allowlist.
042     * @param id  the ID of the collaboration allowlist.
043     */
044    public BoxCollaborationAllowlistExemptTarget(BoxAPIConnection api, String id) {
045
046        super(api, id);
047    }
048
049    /**
050     * Creates a collaboration allowlist for a Box User with a given ID.
051     *
052     * @param api    the API connection to be used by the collaboration allowlist.
053     * @param userID the ID of the Box User to add to the collaboration allowlist.
054     * @return information about the collaboration allowlist created for user.
055     */
056    public static BoxCollaborationAllowlistExemptTarget.Info create(final BoxAPIConnection api, String userID) {
057        URL url = COLLABORATION_ALLOWLIST_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        try (BoxJSONResponse response = request.send()) {
066            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
067            BoxCollaborationAllowlistExemptTarget userAllowlist = new BoxCollaborationAllowlistExemptTarget(api,
068                responseJSON.get("id").asString());
069
070            return userAllowlist.new Info(responseJSON);
071        }
072    }
073
074    /**
075     * Returns all the collaboration allowlisting for user with default limit set to 100.
076     *
077     * @param api    the API connection to be use by the resource.
078     * @param fields the fields to retrieve.
079     * @return an iterable with all the collaboration allowlists for users met search conditions.
080     */
081    public static Iterable<BoxCollaborationAllowlistExemptTarget.Info> getAll(final BoxAPIConnection api,
082                                                                              String... fields) {
083        return getAll(api, DEFAULT_LIMIT, fields);
084    }
085
086    /**
087     * Returns all the collaboration allowlisting for user with specified filters.
088     *
089     * @param api    the API connection to be used by the resource.
090     * @param limit  the number of collaboration allowlists to retrieve.
091     * @param fields the fields to retrieve.
092     * @return an iterable with all the collaboration allowlists for users met search conditions.
093     */
094    public static Iterable<BoxCollaborationAllowlistExemptTarget.Info> getAll(final BoxAPIConnection api, int limit,
095                                                                              String... fields) {
096        QueryStringBuilder builder = new QueryStringBuilder();
097        if (fields.length > 0) {
098            builder.appendParam("fields", fields);
099        }
100
101        URL url = COLLABORATION_ALLOWLIST_EXEMPT_TARGET_ENTRIES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(),
102            builder.toString());
103        return new BoxResourceIterable<BoxCollaborationAllowlistExemptTarget.Info>(api, url, limit) {
104
105            @Override
106            protected BoxCollaborationAllowlistExemptTarget.Info factory(JsonObject jsonObject) {
107                BoxCollaborationAllowlistExemptTarget userAllowlist = new BoxCollaborationAllowlistExemptTarget(
108                    api, jsonObject.get("id").asString());
109
110                return userAllowlist.new Info(jsonObject);
111            }
112        };
113    }
114
115    /**
116     * Retrieves information for a collaboration allowlist for a given allowlist ID.
117     *
118     * @return information about this {@link BoxCollaborationAllowlistExemptTarget}.
119     */
120    public BoxCollaborationAllowlistExemptTarget.Info getInfo() {
121        URL url = COLLABORATION_ALLOWLIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE.build(this.getAPI().getBaseURL(),
122            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 entry for user.
131     */
132    public void delete() {
133        BoxAPIConnection api = this.getAPI();
134        URL url = COLLABORATION_ALLOWLIST_EXEMPT_TARGET_ENTRY_URL_TEMPLATE.build(api.getBaseURL(),
135            this.getID());
136
137        BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.DELETE);
138        request.send().close();
139    }
140
141    /**
142     * Contains information about a BoxCollaborationAllowlistExemptTarget.
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 allowlist for user.
173         *
174         * @return the type of the collaboration allowlist for user.
175         */
176        public String getType() {
177
178            return this.type;
179        }
180
181        /**
182         * Gets the user added to the collaboration allowlist.
183         *
184         * @return the user in the collaboration allowlist.
185         */
186        public BoxUser.Info getUser() {
187
188            return this.user;
189        }
190
191        /**
192         * Gets the enterprise that the collaboration allowlist for user belongs to.
193         *
194         * @return the enterprise that the collaboration allowlist for user belongs to.
195         */
196        public BoxEnterprise getEnterprise() {
197
198            return this.enterprise;
199        }
200
201        /**
202         * Gets the time the collaboration allowlist was created for user.
203         *
204         * @return the time the collaboration allowlist 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 allowlist for user.
213         *
214         * @return the last modified time of the collaboration allowlist for user.
215         */
216        public Date getModifiedAt() {
217
218            return this.modifiedAt;
219        }
220
221        @Override
222        public BoxCollaborationAllowlistExemptTarget getResource() {
223            return BoxCollaborationAllowlistExemptTarget.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}