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 domain and a Box Enterprise. Collaboration 012 * Allowlist enables a Box Enterprise(only available if you have Box Governance) to manage a set of 013 * approved domains 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_entry") 021public class BoxCollaborationAllowlist extends BoxResource { 022 /** Collaboration Allowlist Entries URL Template. */ 023 public static final URLTemplate COLLABORATION_ALLOWLIST_ENTRIES_URL_TEMPLATE = 024 new URLTemplate("collaboration_whitelist_entries"); 025 026 /** Collaboration Allowlist Entries URL Template with given ID. */ 027 public static final URLTemplate COLLABORATION_ALLOWLIST_ENTRY_URL_TEMPLATE = 028 new URLTemplate("collaboration_whitelist_entries/%s"); 029 030 /** The default limit of entries per response. */ 031 private static final int DEFAULT_LIMIT = 100; 032 033 /** 034 * Constructs a BoxCollaborationAllowlist for a collaboration allowlist with a given ID. 035 * 036 * @param api the API connection to be used by the collaboration allowlist. 037 * @param id the ID of the collaboration allowlist. 038 */ 039 public BoxCollaborationAllowlist(BoxAPIConnection api, String id) { 040 super(api, id); 041 } 042 043 /** 044 * Creates a new Collaboration Allowlist for a domain. 045 * 046 * @param api the API connection to be used by the resource. 047 * @param domain the domain to be added to a collaboration allowlist for a Box Enterprise. 048 * @param direction an enum representing the direction of the collaboration allowlist. Can be set 049 * to inbound, outbound, or both. 050 * @return information about the collaboration allowlist created. 051 */ 052 public static BoxCollaborationAllowlist.Info create( 053 final BoxAPIConnection api, String domain, AllowlistDirection direction) { 054 055 URL url = COLLABORATION_ALLOWLIST_ENTRIES_URL_TEMPLATE.build(api.getBaseURL()); 056 BoxJSONRequest request = new BoxJSONRequest(api, url, HttpMethod.POST); 057 JsonObject requestJSON = 058 new JsonObject().add("domain", domain).add("direction", direction.toString()); 059 060 request.setBody(requestJSON.toString()); 061 try (BoxJSONResponse response = request.send()) { 062 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 063 BoxCollaborationAllowlist domainAllowlist = 064 new BoxCollaborationAllowlist(api, responseJSON.get("id").asString()); 065 066 return domainAllowlist.new Info(responseJSON); 067 } 068 } 069 070 /** 071 * Returns all the collaboration allowlisting with specified filters. 072 * 073 * @param api the API connection to be used by the resource. 074 * @param fields the fields to retrieve. 075 * @return an iterable with all the collaboration allowlists met search conditions. 076 */ 077 public static Iterable<BoxCollaborationAllowlist.Info> getAll( 078 final BoxAPIConnection api, String... fields) { 079 080 return getAll(api, DEFAULT_LIMIT, fields); 081 } 082 083 /** 084 * Returns all the collaboration allowlisting with specified filters. 085 * 086 * @param api the API connection to be used by the resource. 087 * @param limit the limit of items per single response. The default value is 100. 088 * @param fields the fields to retrieve. 089 * @return an iterable with all the collaboration allowlists met search conditions. 090 */ 091 public static Iterable<BoxCollaborationAllowlist.Info> getAll( 092 final BoxAPIConnection api, int limit, String... fields) { 093 094 QueryStringBuilder builder = new QueryStringBuilder(); 095 if (fields.length > 0) { 096 builder.appendParam("fields", fields); 097 } 098 099 URL url = 100 COLLABORATION_ALLOWLIST_ENTRIES_URL_TEMPLATE.buildWithQuery( 101 api.getBaseURL(), builder.toString()); 102 return new BoxResourceIterable<BoxCollaborationAllowlist.Info>(api, url, limit) { 103 104 @Override 105 protected BoxCollaborationAllowlist.Info factory(JsonObject jsonObject) { 106 BoxCollaborationAllowlist allowlist = 107 new BoxCollaborationAllowlist(api, jsonObject.get("id").asString()); 108 109 return allowlist.new Info(jsonObject); 110 } 111 }; 112 } 113 114 /** @return information about this {@link BoxCollaborationAllowlist}. */ 115 public BoxCollaborationAllowlist.Info getInfo() { 116 URL url = 117 COLLABORATION_ALLOWLIST_ENTRY_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 118 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, HttpMethod.GET); 119 try (BoxJSONResponse response = request.send()) { 120 return new Info(Json.parse(response.getJSON()).asObject()); 121 } 122 } 123 124 /** Deletes this collaboration allowlist. */ 125 public void delete() { 126 BoxAPIConnection api = this.getAPI(); 127 URL url = COLLABORATION_ALLOWLIST_ENTRY_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); 128 129 BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.DELETE); 130 request.send().close(); 131 } 132 133 /** Enumerates the direction of the collaboration allowlist. */ 134 public enum AllowlistDirection { 135 /** Allowlist inbound collaboration. */ 136 INBOUND("inbound"), 137 138 /** Allowlist outbound collaboration. */ 139 OUTBOUND("outbound"), 140 141 /** Allowlist both inbound and outbound collaboration. */ 142 BOTH("both"); 143 144 private final String direction; 145 146 AllowlistDirection(String direction) { 147 148 this.direction = direction; 149 } 150 151 static AllowlistDirection fromDirection(String direction) { 152 if (direction.equals("inbound")) { 153 return AllowlistDirection.INBOUND; 154 } else if (direction.equals("outbound")) { 155 return AllowlistDirection.OUTBOUND; 156 } else if (direction.equals("both")) { 157 return AllowlistDirection.BOTH; 158 } else { 159 return null; 160 } 161 } 162 163 /** 164 * Returns a String containing the current direction of the collaboration allowlisting. 165 * 166 * @return a String containing information about the direction of the collaboration 167 * allowlisting. 168 */ 169 public String toString() { 170 171 return this.direction; 172 } 173 } 174 175 /** Contains information about a BoxCollaborationAllowlist. */ 176 public class Info extends BoxResource.Info { 177 private String type; 178 private String domain; 179 private AllowlistDirection direction; 180 private BoxEnterprise enterprise; 181 private Date createdAt; 182 private Date modifiedAt; 183 184 /** Constructs an empty Info object. */ 185 public Info() { 186 super(); 187 } 188 189 /** 190 * Constructs an Info object by parsing information from a JSON string. 191 * 192 * @param json the JSON string to parse. 193 */ 194 public Info(String json) { 195 super(json); 196 } 197 198 Info(JsonObject jsonObject) { 199 super(jsonObject); 200 } 201 202 /** 203 * Gets the type of the collaboration allowlist. 204 * 205 * @return the type for the collaboration allowlist. 206 */ 207 public String getType() { 208 209 return this.type; 210 } 211 212 /** 213 * Gets the domain added to the collaboration allowlist. 214 * 215 * @return the domain in the collaboration allowlist 216 */ 217 public String getDomain() { 218 219 return this.domain; 220 } 221 222 /** 223 * Get the direction of the collaboration allowlist. Values can be inbound, outbound, or both. 224 * 225 * @return the direction set for the collaboration allowlist. Values can be inbound, outbound, 226 * or both. 227 */ 228 public AllowlistDirection getDirection() { 229 230 return this.direction; 231 } 232 233 /** 234 * Gets the enterprise that the collaboration allowlist belongs to. 235 * 236 * @return the enterprise that the collaboration allowlist belongs to. 237 */ 238 public BoxEnterprise getEnterprise() { 239 240 return this.enterprise; 241 } 242 243 /** 244 * Gets the time the collaboration allowlist was created. 245 * 246 * @return the time the collaboration allowlist was created. 247 */ 248 public Date getCreatedAt() { 249 250 return this.createdAt; 251 } 252 253 /** 254 * Gets the time the collaboration allowlist was last modified. 255 * 256 * @return the time the collaboration allowlist was last modified. 257 */ 258 public Date getModifiedAt() { 259 260 return this.modifiedAt; 261 } 262 263 @Override 264 public BoxCollaborationAllowlist getResource() { 265 return BoxCollaborationAllowlist.this; 266 } 267 268 @Override 269 protected void parseJSONMember(JsonObject.Member member) { 270 super.parseJSONMember(member); 271 272 String memberName = member.getName(); 273 JsonValue value = member.getValue(); 274 try { 275 if (memberName.equals("domain")) { 276 this.domain = value.asString(); 277 278 } else if (memberName.equals("type")) { 279 this.type = value.asString(); 280 281 } else if (memberName.equals("direction")) { 282 this.direction = AllowlistDirection.fromDirection(value.asString()); 283 284 } else if (memberName.equals("enterprise")) { 285 JsonObject jsonObject = value.asObject(); 286 this.enterprise = new BoxEnterprise(jsonObject); 287 288 } else if (memberName.equals("created_at")) { 289 this.createdAt = BoxDateFormat.parse(value.asString()); 290 291 } else if (memberName.equals("modified_at")) { 292 this.modifiedAt = BoxDateFormat.parse(value.asString()); 293 } 294 } catch (Exception e) { 295 throw new BoxDeserializationException(memberName, value.toString(), e); 296 } 297 } 298 } 299}