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/** 012 * Represents a collaboration whitelist between a domain and a Box Enterprise. Collaboration Whitelist enables a Box 013 * Enterprise(only available if you have Box Governance) to manage a set of approved domains that can collaborate 014 * with an enterprise. 015 * 016 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked 017 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error 018 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> 019 */ 020@BoxResourceType("collaboration_whitelist_entry") 021public class BoxCollaborationWhitelist extends BoxResource { 022 /** 023 * Collaboration Whitelist Entries URL Template. 024 */ 025 public static final URLTemplate COLLABORATION_WHITELIST_ENTRIES_URL_TEMPLATE = 026 new URLTemplate("collaboration_whitelist_entries"); 027 028 /** 029 * Collaboration Whitelist Entries URL Template with given ID. 030 */ 031 public static final URLTemplate COLLABORATION_WHITELIST_ENTRY_URL_TEMPLATE = 032 new URLTemplate("collaboration_whitelist_entries/%s"); 033 034 /** 035 * The default limit of entries per response. 036 */ 037 private static final int DEFAULT_LIMIT = 100; 038 039 /** 040 * Constructs a BoxCollaborationWhitelist for a collaboration whitelist with a given ID. 041 * 042 * @param api the API connection to be used by the collaboration whitelist. 043 * @param id the ID of the collaboration whitelist. 044 */ 045 public BoxCollaborationWhitelist(BoxAPIConnection api, String id) { 046 super(api, id); 047 } 048 049 /** 050 * Creates a new Collaboration Whitelist for a domain. 051 * @param api the API connection to be used by the resource. 052 * @param domain the domain to be added to a collaboration whitelist for a Box Enterprise. 053 * @param direction an enum representing the direction of the collaboration whitelist. Can be set to 054 * inbound, outbound, or both. 055 * @return information about the collaboration whitelist created. 056 */ 057 public static BoxCollaborationWhitelist.Info create(final BoxAPIConnection api, String domain, 058 WhitelistDirection direction) { 059 060 URL url = COLLABORATION_WHITELIST_ENTRIES_URL_TEMPLATE.build(api.getBaseURL()); 061 BoxJSONRequest request = new BoxJSONRequest(api, url, HttpMethod.POST); 062 JsonObject requestJSON = new JsonObject() 063 .add("domain", domain) 064 .add("direction", direction.toString()); 065 066 request.setBody(requestJSON.toString()); 067 BoxJSONResponse response = (BoxJSONResponse) request.send(); 068 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 069 BoxCollaborationWhitelist domainWhitelist = 070 new BoxCollaborationWhitelist(api, responseJSON.get("id").asString()); 071 072 return domainWhitelist.new Info(responseJSON); 073 } 074 075 /** 076 * @return information about this {@link BoxCollaborationWhitelist}. 077 */ 078 public BoxCollaborationWhitelist.Info getInfo() { 079 URL url = COLLABORATION_WHITELIST_ENTRY_URL_TEMPLATE.build(this.getAPI().getBaseURL(), 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 with specified filters. 088 * @param api the API connection to be used by the resource. 089 * @param fields the fields to retrieve. 090 * @return an iterable with all the collaboration whitelists met search conditions. 091 */ 092 public static Iterable<BoxCollaborationWhitelist.Info> getAll(final BoxAPIConnection api, String ... fields) { 093 094 return getAll(api, DEFAULT_LIMIT, fields); 095 } 096 097 /** 098 * Returns all the collaboration whitelisting with specified filters. 099 * @param api the API connection to be used by the resource. 100 * @param limit the limit of items per single response. The default value is 100. 101 * @param fields the fields to retrieve. 102 * @return an iterable with all the collaboration whitelists met search conditions. 103 */ 104 public static Iterable<BoxCollaborationWhitelist.Info> getAll(final BoxAPIConnection api, int limit, 105 String ... fields) { 106 107 QueryStringBuilder builder = new QueryStringBuilder(); 108 if (fields.length > 0) { 109 builder.appendParam("fields", fields); 110 } 111 112 URL url = COLLABORATION_WHITELIST_ENTRIES_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString()); 113 return new BoxResourceIterable<BoxCollaborationWhitelist.Info>(api, url, limit) { 114 115 @Override 116 protected BoxCollaborationWhitelist.Info factory(JsonObject jsonObject) { 117 BoxCollaborationWhitelist whitelist = new BoxCollaborationWhitelist( 118 api, jsonObject.get("id").asString()); 119 120 return whitelist.new Info(jsonObject); 121 } 122 }; 123 } 124 125 /** 126 * Deletes this collaboration whitelist. 127 */ 128 public void delete() { 129 BoxAPIConnection api = this.getAPI(); 130 URL url = COLLABORATION_WHITELIST_ENTRY_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); 131 132 BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.DELETE); 133 BoxAPIResponse response = request.send(); 134 response.disconnect(); 135 } 136 137 /** 138 * Contains information about a BoxCollaborationWhitelist. 139 */ 140 public class Info extends BoxResource.Info { 141 private String type; 142 private String domain; 143 private WhitelistDirection direction; 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. 170 * 171 * @return the type for the collaboration whitelist. 172 */ 173 public String getType() { 174 175 return this.type; 176 } 177 178 /** 179 * Gets the domain added to the collaboration whitelist. 180 * 181 * @return the domain in the collaboration whitelist 182 */ 183 public String getDomain() { 184 185 return this.domain; 186 } 187 188 /** 189 * Get the direction of the collaboration whitelist. Values can be inbound, outbound, or 190 * both. 191 * 192 * @return the direction set for the collaboration whitelist. Values can be inbound, outbound, or both. 193 */ 194 public WhitelistDirection getDirection() { 195 196 return this.direction; 197 } 198 199 /** 200 * Gets the enterprise that the collaboration whitelist belongs to. 201 * 202 * @return the enterprise that the collaboration whitelist belongs to. 203 */ 204 public BoxEnterprise getEnterprise() { 205 206 return this.enterprise; 207 } 208 209 /** 210 * Gets the time the collaboration whitelist was created. 211 * 212 * @return the time the collaboration whitelist was created. 213 */ 214 public Date getCreatedAt() { 215 216 return this.createdAt; 217 } 218 219 /** 220 * Gets the time the collaboration whitelist was last modified. 221 * 222 * @return the time the collaboration whitelist was last modified. 223 */ 224 public Date getModifiedAt() { 225 226 return this.modifiedAt; 227 } 228 229 @Override 230 public BoxCollaborationWhitelist getResource() { 231 return BoxCollaborationWhitelist.this; 232 } 233 234 @Override 235 protected void parseJSONMember(JsonObject.Member member) { 236 super.parseJSONMember(member); 237 238 String memberName = member.getName(); 239 JsonValue value = member.getValue(); 240 try { 241 if (memberName.equals("domain")) { 242 this.domain = value.asString(); 243 244 } else if (memberName.equals("type")) { 245 this.type = value.asString(); 246 247 } else if (memberName.equals("direction")) { 248 this.direction = WhitelistDirection.fromDirection(value.asString()); 249 250 } else if (memberName.equals("enterprise")) { 251 JsonObject jsonObject = value.asObject(); 252 this.enterprise = new BoxEnterprise(jsonObject); 253 254 } else if (memberName.equals("created_at")) { 255 this.createdAt = BoxDateFormat.parse(value.asString()); 256 257 } else if (memberName.equals("modified_at")) { 258 this.modifiedAt = BoxDateFormat.parse(value.asString()); 259 260 } 261 } catch (Exception e) { 262 throw new BoxDeserializationException(memberName, value.toString(), e); 263 } 264 } 265 } 266 267 /** 268 * Enumerates the direction of the collaboration whitelist. 269 */ 270 public enum WhitelistDirection { 271 /** 272 * Whitelist inbound collaboration. 273 */ 274 INBOUND("inbound"), 275 276 /** 277 * Whitelist outbound collaboration. 278 */ 279 OUTBOUND("outbound"), 280 281 /** 282 * Whitelist both inbound and outbound collaboration. 283 */ 284 BOTH("both"); 285 286 private final String direction; 287 288 WhitelistDirection(String direction) { 289 290 this.direction = direction; 291 } 292 293 static WhitelistDirection fromDirection(String direction) { 294 if (direction.equals("inbound")) { 295 return WhitelistDirection.INBOUND; 296 } else if (direction.equals("outbound")) { 297 return WhitelistDirection.OUTBOUND; 298 } else if (direction.equals("both")) { 299 return WhitelistDirection.BOTH; 300 } else { 301 return null; 302 } 303 } 304 305 /** 306 * Returns a String containing the current direction of the collaboration whitelisting. 307 * @return a String containing information about the direction of the collaboration whitelisting. 308 */ 309 public String toString() { 310 311 return this.direction; 312 } 313 } 314}