001package com.box.sdk; 002 003import java.lang.reflect.Constructor; 004import java.lang.reflect.InvocationTargetException; 005import java.util.Collections; 006import java.util.Map; 007import java.util.concurrent.ConcurrentHashMap; 008 009import com.eclipsesource.json.JsonObject; 010 011/** 012 * The abstract base class for all resource types (files, folders, comments, collaborations, etc.) used by the API. 013 * 014 * <p>Every API resource has an ID and a {@link BoxAPIConnection} that it uses to communicate with the API. Some 015 * resources also have an associated {@link Info} class that contains information about the resource.</p> 016 */ 017public abstract class BoxResource { 018 019 /** 020 * @see #initResourceClassByType() 021 */ 022 private static final Map<String, Class<? extends BoxResource>> RESOURCE_CLASS_BY_TYPE = initResourceClassByType(); 023 024 private final BoxAPIConnection api; 025 private final String id; 026 027 /** 028 * Constructs a BoxResource for a resource with a given ID. 029 * 030 * @param api the API connection to be used by the resource. 031 * @param id the ID of the resource. 032 */ 033 public BoxResource(BoxAPIConnection api, String id) { 034 this.api = api; 035 this.id = id; 036 } 037 038 /** 039 * @return Builds {@link Map} between String {@link #getResourceType(Class)} and {@link BoxResource} type. 040 */ 041 private static Map<String, Class<? extends BoxResource>> initResourceClassByType() { 042 Map<String, Class<? extends BoxResource>> result = 043 new ConcurrentHashMap<String, Class<? extends BoxResource>>(); 044 result.put(getResourceType(BoxFolder.class), BoxFolder.class); 045 result.put(getResourceType(BoxFile.class), BoxFile.class); 046 result.put(getResourceType(BoxComment.class), BoxComment.class); 047 result.put(getResourceType(BoxCollaboration.class), BoxCollaboration.class); 048 result.put(getResourceType(BoxTask.class), BoxTask.class); 049 result.put(getResourceType(BoxTaskAssignment.class), BoxTaskAssignment.class); 050 result.put(getResourceType(BoxUser.class), BoxUser.class); 051 result.put(getResourceType(BoxGroup.class), BoxGroup.class); 052 result.put(getResourceType(BoxGroupMembership.class), BoxGroupMembership.class); 053 result.put(getResourceType(BoxEvent.class), BoxEvent.class); 054 result.put(getResourceType(BoxWebHook.class), BoxWebHook.class); 055 result.put(getResourceType(BoxCollection.class), BoxCollection.class); 056 result.put(getResourceType(BoxDevicePin.class), BoxDevicePin.class); 057 result.put(getResourceType(BoxRetentionPolicy.class), BoxRetentionPolicy.class); 058 result.put(getResourceType(BoxRetentionPolicyAssignment.class), BoxRetentionPolicyAssignment.class); 059 result.put(getResourceType(BoxFileVersionRetention.class), BoxFileVersionRetention.class); 060 result.put(getResourceType(BoxLegalHoldPolicy.class), BoxLegalHoldPolicy.class); 061 result.put(getResourceType(BoxLegalHoldAssignment.class), BoxLegalHoldAssignment.class); 062 result.put(getResourceType(BoxFileVersionLegalHold.class), BoxFileVersionLegalHold.class); 063 result.put(getResourceType(BoxFileUploadSession.class), BoxFileUploadSession.class); 064 result.put(getResourceType(BoxWebLink.class), BoxWebLink.class); 065 result.put(getResourceType(BoxStoragePolicy.class), BoxStoragePolicy.class); 066 result.put(getResourceType(BoxStoragePolicyAssignment.class), BoxStoragePolicyAssignment.class); 067 result.put(getResourceType(BoxFolderLock.class), BoxFolderLock.class); 068 result.put(getResourceType(BoxFileRequest.class), BoxFileRequest.class); 069 070 return Collections.unmodifiableMap(result); 071 } 072 073 /** 074 * Resolves {@link BoxResourceType} for a provided {@link BoxResource} {@link Class}. 075 * 076 * @param clazz 077 * {@link BoxResource} type 078 * @return resolved {@link BoxResourceType#value()} 079 */ 080 public static String getResourceType(Class<? extends BoxResource> clazz) { 081 BoxResourceType resource = clazz.getAnnotation(BoxResourceType.class); 082 if (resource == null) { 083 throw new IllegalArgumentException("Provided BoxResource type does not have @BoxResourceType annotation."); 084 } 085 return resource.value(); 086 } 087 088 static BoxResource.Info parseInfo(BoxAPIConnection api, JsonObject jsonObject) { 089 String type = jsonObject.get("type").asString(); 090 String id = jsonObject.get("id").asString(); 091 092 try { 093 Class<? extends BoxResource> resourceClass = RESOURCE_CLASS_BY_TYPE.get(type); 094 Constructor<? extends BoxResource> resourceConstructor = 095 resourceClass.getConstructor(BoxAPIConnection.class, String.class); 096 097 Class<?> infoClass = resourceClass.getClassLoader().loadClass(resourceClass.getCanonicalName() + "$Info"); 098 Constructor<?> infoConstructor = infoClass.getDeclaredConstructor(resourceClass, JsonObject.class); 099 100 BoxResource resource = resourceConstructor.newInstance(api, id); 101 return (BoxResource.Info) infoConstructor.newInstance(resource, jsonObject); 102 103 } catch (ClassNotFoundException e) { 104 return null; 105 } catch (NoSuchMethodException e) { 106 return null; 107 } catch (IllegalAccessException e) { 108 throw new BoxAPIException("Can not create BoxResource.Info instance:", e); 109 } catch (InvocationTargetException e) { 110 throw new BoxAPIException("Can not create BoxResource.Info instance:", e); 111 } catch (InstantiationException e) { 112 throw new BoxAPIException("Can not create BoxResource.Info instance:", e); 113 } 114 } 115 116 /** 117 * Gets the API connection used by this resource. 118 * @return the API connection used by this resource. 119 */ 120 public BoxAPIConnection getAPI() { 121 return this.api; 122 } 123 124 /** 125 * Gets the ID of this resource. 126 * @return the ID of this resource. 127 */ 128 public String getID() { 129 return this.id; 130 } 131 132 /** 133 * Indicates whether this BoxResource is equal to another BoxResource. Two BoxResources are equal if they have the 134 * same type and ID. 135 * @param other the other BoxResource to compare. 136 * @return true if the type and IDs of the two resources are equal; otherwise false. 137 */ 138 @Override 139 public boolean equals(Object other) { 140 if (other == null) { 141 return false; 142 } 143 144 if (this.getClass().equals(other.getClass())) { 145 BoxResource otherResource = (BoxResource) other; 146 return this.getID().equals(otherResource.getID()); 147 } 148 149 return false; 150 } 151 152 /** 153 * Returns a hash code value for this BoxResource. 154 * @return a hash code value for this BoxResource. 155 */ 156 @Override 157 public int hashCode() { 158 return this.getID().hashCode(); 159 } 160 161 /** 162 * Contains information about a BoxResource. 163 */ 164 public abstract class Info extends BoxJSONObject { 165 /** 166 * Constructs an empty Info object. 167 */ 168 public Info() { 169 super(); 170 } 171 172 /** 173 * Constructs an Info object by parsing information from a JSON string. 174 * @param json the JSON string to parse. 175 */ 176 public Info(String json) { 177 super(json); 178 } 179 180 /** 181 * Constructs an Info object using an already parsed JSON object. 182 * @param jsonObject the parsed JSON object. 183 */ 184 Info(JsonObject jsonObject) { 185 super(jsonObject); 186 } 187 188 /** 189 * Gets the ID of the resource associated with this Info. 190 * @return the ID of the associated resource. 191 */ 192 public String getID() { 193 return BoxResource.this.getID(); 194 } 195 196 /** 197 * Gets the resource associated with this Info. 198 * @return the associated resource. 199 */ 200 public abstract BoxResource getResource(); 201 } 202}