001package com.box.sdk; 002 003import static com.box.sdk.BinaryBodyUtils.writeStream; 004 005import com.eclipsesource.json.Json; 006import com.eclipsesource.json.JsonObject; 007import com.eclipsesource.json.JsonValue; 008import java.io.OutputStream; 009import java.net.URL; 010import java.text.ParseException; 011import java.util.Date; 012 013/** Represents a particular version of a file on Box. */ 014@BoxResourceType("file_version") 015public class BoxFileVersion extends BoxResource { 016 /** Content URL Template. */ 017 public static final URLTemplate CONTENT_URL_TEMPLATE = 018 new URLTemplate("files/%s/content?version=%s"); 019 /** Version URL Template. */ 020 public static final URLTemplate VERSION_URL_TEMPLATE = new URLTemplate("files/%s/versions/%s"); 021 022 /** The default limit of entries per response. */ 023 public static final long DEFAULT_LIMIT = 1000; 024 025 private String fileID; 026 027 private String versionID; 028 private String sha1; 029 private String name; 030 private long size; 031 private String uploaderDisplayName; 032 private Date createdAt; 033 private Date modifiedAt; 034 private BoxUser.Info modifiedBy; 035 private Date trashedAt; 036 private BoxUser.Info trashedBy; 037 private Date restoredAt; 038 private BoxUser.Info restoredBy; 039 private Date purgedAt; 040 private Long versionNumber; 041 042 /** 043 * Constructs a BoxFileVersion from a JSON string. 044 * 045 * @param api the API connection to be used by the file. 046 * @param json the JSON encoded file version. 047 * @param fileID the ID of the file. 048 */ 049 public BoxFileVersion(BoxAPIConnection api, String json, String fileID) { 050 this(api, Json.parse(json).asObject(), fileID); 051 } 052 053 BoxFileVersion(BoxAPIConnection api, JsonObject jsonObject, String fileID) { 054 super(api, jsonObject.get("id").asString()); 055 056 this.fileID = fileID; 057 this.parseJSON(jsonObject); 058 } 059 060 /** 061 * Method used to update fields with values received from API. 062 * 063 * @param jsonObject JSON-encoded info about File Version object. 064 */ 065 private void parseJSON(JsonObject jsonObject) { 066 for (JsonObject.Member member : jsonObject) { 067 JsonValue value = member.getValue(); 068 if (value.isNull()) { 069 continue; 070 } 071 072 try { 073 String memberName = member.getName(); 074 if (memberName.equals("id")) { 075 this.versionID = value.asString(); 076 } else if (memberName.equals("sha1")) { 077 this.sha1 = value.asString(); 078 } else if (memberName.equals("name")) { 079 this.name = value.asString(); 080 } else if (memberName.equals("size")) { 081 this.size = Double.valueOf(value.toString()).longValue(); 082 } else if (memberName.equals("uploader_display_name")) { 083 this.uploaderDisplayName = value.asString(); 084 } else if (memberName.equals("created_at")) { 085 this.createdAt = BoxDateFormat.parse(value.asString()); 086 } else if (memberName.equals("modified_at")) { 087 this.modifiedAt = BoxDateFormat.parse(value.asString()); 088 } else if (memberName.equals("trashed_at")) { 089 this.trashedAt = BoxDateFormat.parse(value.asString()); 090 } else if (memberName.equals("trashed_by")) { 091 JsonObject userJSON = value.asObject(); 092 String userID = userJSON.get("id").asString(); 093 BoxUser user = new BoxUser(getAPI(), userID); 094 this.trashedBy = user.new Info(userJSON); 095 } else if (memberName.equals("modified_by")) { 096 JsonObject userJSON = value.asObject(); 097 String userID = userJSON.get("id").asString(); 098 BoxUser user = new BoxUser(getAPI(), userID); 099 this.modifiedBy = user.new Info(userJSON); 100 } else if (memberName.equals("restored_at")) { 101 this.restoredAt = BoxDateFormat.parse(value.asString()); 102 } else if (memberName.equals("restored_by")) { 103 JsonObject userJSON = value.asObject(); 104 String userID = userJSON.get("id").asString(); 105 BoxUser user = new BoxUser(getAPI(), userID); 106 this.restoredBy = user.new Info(userJSON); 107 } else if (memberName.equals("purged_at")) { 108 this.purgedAt = BoxDateFormat.parse(value.asString()); 109 } else if (memberName.equals("version_number")) { 110 this.versionNumber = Long.parseLong(value.asString()); 111 } 112 } catch (ParseException e) { 113 assert false : "A ParseException indicates a bug in the SDK."; 114 } 115 } 116 } 117 118 /** @return the file id this file version belongs to. */ 119 public String getFileID() { 120 return this.fileID; 121 } 122 123 /** 124 * Used if no or wrong file id was set with constructor. 125 * 126 * @param fileID the file id this file version belongs to. 127 */ 128 public void setFileID(String fileID) { 129 this.fileID = fileID; 130 } 131 132 /** 133 * Gets the version ID of this version of the file. 134 * 135 * @return the version ID of this version of the file. 136 */ 137 public String getVersionID() { 138 return this.versionID; 139 } 140 141 /** 142 * Gets the SHA1 hash of this version of the file. 143 * 144 * @return the SHA1 hash of this version of the file. 145 */ 146 public String getSha1() { 147 return this.sha1; 148 } 149 150 /** 151 * Gets the name of this version of the file. 152 * 153 * @return the name of this version of the file. 154 */ 155 public String getName() { 156 return this.name; 157 } 158 159 /** 160 * Gets the size of this version of the file. 161 * 162 * @return the size of this version of the file. 163 */ 164 public long getSize() { 165 return this.size; 166 } 167 168 /** 169 * Gets the time that this version of the file was created. 170 * 171 * @return the time that this version of the file was created. 172 */ 173 public Date getCreatedAt() { 174 return this.createdAt; 175 } 176 177 /** 178 * Gets the user's name at the time of upload. 179 * 180 * @return the time user's name at the time of upload. 181 */ 182 public String getUploaderDisplayName() { 183 return this.uploaderDisplayName; 184 } 185 186 /** 187 * Gets the time that this version of the file was modified. 188 * 189 * @return the time that this version of the file was modified. 190 */ 191 public Date getModifiedAt() { 192 return this.modifiedAt; 193 } 194 195 /** 196 * Gets the time that this version of the file was deleted. 197 * 198 * @return the time that this version of the file was deleted. 199 */ 200 public Date getTrashedAt() { 201 return this.trashedAt; 202 } 203 204 /** 205 * Gets information about the user who trashed this version of the file. 206 * 207 * @return info about the user who trashed this version of the file. 208 */ 209 public BoxUser.Info getTrashedBy() { 210 return this.trashedBy; 211 } 212 213 /** 214 * Gets information about the user who last modified this version of the file. 215 * 216 * @return info about the user who last modified this version of the file. 217 */ 218 public BoxUser.Info getModifiedBy() { 219 return this.modifiedBy; 220 } 221 222 /** 223 * Gets the time that this version of the file was restored. 224 * 225 * @return the time that this version of the file was restored. 226 */ 227 public Date getRestoredAt() { 228 return this.restoredAt; 229 } 230 231 /** 232 * Gets information about the user who restored this version of the file. 233 * 234 * @return info about the user who restored this version of the file. 235 */ 236 public BoxUser.Info getRestoredBy() { 237 return this.restoredBy; 238 } 239 240 /** 241 * Gets the time that this version of the file was purged. 242 * 243 * @return the time that this version of the file was purged. 244 */ 245 public Date getPurgedAt() { 246 return this.purgedAt; 247 } 248 249 /** 250 * Returns version number. 251 * 252 * @return version number 253 */ 254 public Long getVersionNumber() { 255 return versionNumber; 256 } 257 258 /** Deletes this version of the file. */ 259 public void delete() { 260 URL url = VERSION_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.fileID, this.getID()); 261 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 262 request.send().close(); 263 } 264 265 /** 266 * Downloads this version of the file to a given OutputStream. 267 * 268 * @param output the stream to where the file will be written. 269 */ 270 public void download(OutputStream output) { 271 this.download(output, null); 272 } 273 274 /** 275 * Downloads this version of the file to a given OutputStream while reporting the progress to a 276 * ProgressListener. 277 * 278 * @param output the stream to where the file will be written. 279 * @param listener a listener for monitoring the download's progress. 280 */ 281 public void download(OutputStream output, ProgressListener listener) { 282 URL url = CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.fileID, this.getID()); 283 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 284 writeStream(request.send(), output, listener); 285 } 286 287 /** Promotes this version of the file to be the latest version. */ 288 public void promote() { 289 URL url = VERSION_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.fileID, "current"); 290 291 JsonObject jsonObject = new JsonObject(); 292 jsonObject.add("type", "file_version"); 293 jsonObject.add("id", this.getID()); 294 295 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST"); 296 request.setBody(jsonObject.toString()); 297 try (BoxJSONResponse response = request.send()) { 298 this.parseJSON(Json.parse(response.getJSON()).asObject()); 299 } 300 } 301}