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 069 return Collections.unmodifiableMap(result); 070 } 071 072 /** 073 * Resolves {@link BoxResourceType} for a provided {@link BoxResource} {@link Class}. 074 * 075 * @param clazz 076 * {@link BoxResource} type 077 * @return resolved {@link BoxResourceType#value()} 078 */ 079 public static String getResourceType(Class<? extends BoxResource> clazz) { 080 BoxResourceType resource = clazz.getAnnotation(BoxResourceType.class); 081 if (resource == null) { 082 throw new IllegalArgumentException("Provided BoxResource type does not have @BoxResourceType annotation."); 083 } 084 return resource.value(); 085 } 086 087 static BoxResource.Info parseInfo(BoxAPIConnection api, JsonObject jsonObject) { 088 String type = jsonObject.get("type").asString(); 089 String id = jsonObject.get("id").asString(); 090 091 try { 092 Class<? extends BoxResource> resourceClass = RESOURCE_CLASS_BY_TYPE.get(type); 093 Constructor<? extends BoxResource> resourceConstructor = 094 resourceClass.getConstructor(BoxAPIConnection.class, String.class); 095 096 Class<?> infoClass = resourceClass.getClassLoader().loadClass(resourceClass.getCanonicalName() + "$Info"); 097 Constructor<?> infoConstructor = infoClass.getDeclaredConstructor(resourceClass, JsonObject.class); 098 099 BoxResource resource = resourceConstructor.newInstance(api, id); 100 return (BoxResource.Info) infoConstructor.newInstance(resource, jsonObject); 101 102 } catch (ClassNotFoundException e) { 103 return null; 104 } catch (NoSuchMethodException e) { 105 return null; 106 } catch (IllegalAccessException e) { 107 throw new BoxAPIException("Can not create BoxResource.Info instance:", e); 108 } catch (InvocationTargetException e) { 109 throw new BoxAPIException("Can not create BoxResource.Info instance:", e); 110 } catch (InstantiationException e) { 111 throw new BoxAPIException("Can not create BoxResource.Info instance:", e); 112 } 113 } 114 115 /** 116 * Gets the API connection used by this resource. 117 * @return the API connection used by this resource. 118 */ 119 public BoxAPIConnection getAPI() { 120 return this.api; 121 } 122 123 /** 124 * Gets the ID of this resource. 125 * @return the ID of this resource. 126 */ 127 public String getID() { 128 return this.id; 129 } 130 131 /** 132 * Indicates whether this BoxResource is equal to another BoxResource. Two BoxResources are equal if they have the 133 * same type and ID. 134 * @param other the other BoxResource to compare. 135 * @return true if the type and IDs of the two resources are equal; otherwise false. 136 */ 137 @Override 138 public boolean equals(Object other) { 139 if (other == null) { 140 return false; 141 } 142 143 if (this.getClass().equals(other.getClass())) { 144 BoxResource otherResource = (BoxResource) other; 145 return this.getID().equals(otherResource.getID()); 146 } 147 148 return false; 149 } 150 151 /** 152 * Returns a hash code value for this BoxResource. 153 * @return a hash code value for this BoxResource. 154 */ 155 @Override 156 public int hashCode() { 157 return this.getID().hashCode(); 158 } 159 160 /** 161 * Contains information about a BoxResource. 162 */ 163 public abstract class Info extends BoxJSONObject { 164 /** 165 * Constructs an empty Info object. 166 */ 167 public Info() { 168 super(); 169 } 170 171 /** 172 * Constructs an Info object by parsing information from a JSON string. 173 * @param json the JSON string to parse. 174 */ 175 public Info(String json) { 176 super(json); 177 } 178 179 /** 180 * Constructs an Info object using an already parsed JSON object. 181 * @param jsonObject the parsed JSON object. 182 */ 183 Info(JsonObject jsonObject) { 184 super(jsonObject); 185 } 186 187 /** 188 * Gets the ID of the resource associated with this Info. 189 * @return the ID of the associated resource. 190 */ 191 public String getID() { 192 return BoxResource.this.getID(); 193 } 194 195 /** 196 * Gets the resource associated with this Info. 197 * @return the associated resource. 198 */ 199 public abstract BoxResource getResource(); 200 } 201}