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