001package com.box.sdk; 002 003import java.net.MalformedURLException; 004import java.net.URL; 005 006import com.eclipsesource.json.JsonObject; 007 008/** 009 * The class represents one instance of a file representation. 010 */ 011public class Representation { 012 013 /** 014 * Used to validate if the hints header has (near) valid value. 015 */ 016 protected static final String X_REP_HINTS_PATTERN = "^(?:\\[[a-z0-9_]+(?:\\?[a-z0-9_]+\\=[a-z0-9_]+(?:" 017 + "\\|[a-z0-9_]+)*(?:&[a-z0-9_]+\\=[a-z0-9_]+(?:\\|[a-z0-9_]+)*)*)?(?:,[a-z0-9_]+(?:\\?[a-z0-9_]+\\=[a-z0-9_]+" 018 + "(?:\\|[a-z0-9_]+)*(?:&[a-z0-9_]+\\=[a-z0-9_]+(?:\\|[a-z0-9_]+)*)*)?)*\\])+$"; 019 020 private String representation; 021 private JsonObject properties; 022 private JsonObject metadata; 023 private Info info; 024 private Content content; 025 private Status status; 026 027 /** 028 * Construct a representation from JsonObject. 029 * @param representationJson representaion entry 030 */ 031 public Representation(JsonObject representationJson) { 032 for (JsonObject.Member member : representationJson) { 033 if (member.getName().equals("representation")) { 034 this.representation = member.getValue().asString(); 035 } else if (member.getName().equals("properties")) { 036 this.properties = member.getValue().asObject(); 037 } else if (member.getName().equals("metadata")) { 038 this.metadata = member.getValue().asObject(); 039 } else if (member.getName().equals("info")) { 040 this.info = new Info(member.getValue().asObject()); 041 } else if (member.getName().equals("content")) { 042 this.content = new Content(member.getValue().asObject()); 043 } else if (member.getName().equals("status")) { 044 this.status = new Status(member.getValue().asObject()); 045 } 046 } 047 } 048 049 /** 050 * Get the extension of the format, but occasionally a name of a standard (potentially de facto) format 051 * or a proprietary format that Box supports. 052 * 053 * @return representation name 054 */ 055 public String getRepresentation() { 056 return this.representation; 057 } 058 059 /** 060 * Get representation's set of static properties to distinguish between subtypes of a given representation, 061 * for example, different sizes of jpg's. Each representation has its own set of properties. 062 * @return properties of representation as JsonObject 063 */ 064 public JsonObject getProperties() { 065 return this.properties; 066 } 067 068 /** 069 * Get representation's metadata. 070 * 071 * @return metadataas JsonObject 072 */ 073 public JsonObject getMetadata() { 074 return this.metadata; 075 } 076 077 /** 078 * Get Info which has an opaque URL which will return status information about the file. 079 * It may change over time and should not be hard-coded or cached. 080 * @return info 081 */ 082 public Info getInfo() { 083 return this.info; 084 } 085 086 /** 087 * Get representation's content which includes a url template. 088 * @return content 089 */ 090 public Content getContent() { 091 return this.content; 092 } 093 094 /** 095 * A string with one of the following values: 'none', 'pending', 'viewable', 'error' and 'success'. 096 * @return status 097 */ 098 public Status getStatus() { 099 return this.status; 100 } 101 102 /** 103 * Representation's info URL. 104 */ 105 public class Info { 106 107 private URL url; 108 109 /** 110 * Construct Representation's info. 111 * @param members json object 112 */ 113 public Info(JsonObject members) { 114 for (JsonObject.Member member : members) { 115 if (member.getName().equals("url")) { 116 try { 117 this.url = new URL(member.getValue().asString()); 118 } catch (MalformedURLException e) { 119 throw new BoxAPIException("Couldn't parse info.url for a file representation", e); 120 } 121 } 122 } 123 } 124 125 /** 126 * An opaque URL which will return status information about the file. 127 * @return url 128 */ 129 public URL getUrl() { 130 return this.url; 131 } 132 } 133 134 /** 135 * Representation's content. 136 */ 137 public class Content { 138 139 private String urlTemplate; 140 141 /** 142 * Construct a representation's content. 143 * @param members json object 144 */ 145 public Content(JsonObject members) { 146 for (JsonObject.Member member : members) { 147 if (member.getName().equals("url_template")) { 148 this.urlTemplate = member.getValue().asString(); 149 } 150 } 151 } 152 153 /** 154 * Get an opaque URL template to the content, which follows RFC 6570. There is an asset_path variable that 155 * should be replaced with a valid path. Valid paths are different for each representation subtype. 156 * It may change over time and should not be hard-coded or cached. 157 * @return url template 158 */ 159 public String getUrlTemplate() { 160 return this.urlTemplate; 161 } 162 } 163 164 /** 165 * Representation's status. 166 */ 167 public class Status { 168 169 private String state; 170 171 /** 172 * Construct a status object for a representation. 173 * @param members of status object 174 */ 175 public Status(JsonObject members) { 176 for (JsonObject.Member member : members) { 177 if (member.getName().equals("state")) { 178 this.state = member.getValue().asString(); 179 } 180 } 181 } 182 183 /** 184 * A string with one of the following values: 'none', 'pending', 'viewable', 'error' and 'success'. 185 * none - the unknown or initial state. 186 * pending - content is being generated but is not ready yet. 187 * viewable - like pending, though indicates that enough content is available to be useful. 188 * error - an error happened and this content is not available. 189 * success - all of the content is available and complete. 190 * @return state 191 */ 192 public String getState() { 193 return this.state; 194 } 195 } 196}