From 09dbc93915294d3a5a44813b81c5ba42d9f26068 Mon Sep 17 00:00:00 2001 From: Harish Gokavarapu Date: Mon, 28 Aug 2017 16:11:37 -0700 Subject: [PATCH 1/4] started --- src/main/java/com/box/sdk/BoxFile.java | 3 +- src/main/java/com/box/sdk/Representation.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/box/sdk/Representation.java diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index 5130f999f..24706a9c3 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -40,7 +40,7 @@ public class BoxFile extends BoxItem { "created_by", "modified_by", "owned_by", "shared_link", "parent", "item_status", "version_number", "comment_count", "permissions", "tags", "lock", "extension", "is_package", "file_version", "collections", - "watermark_info", "metadata"}; + "watermark_info", "metadata", "representations"}; /** * Used to specify what filetype to request for a file thumbnail. @@ -1020,6 +1020,7 @@ public class Info extends BoxItem.Info { private boolean isWatermarked; private JsonObject metadata; private Map> metadataMap; + private Representation representations; /** * Constructs an empty Info object. diff --git a/src/main/java/com/box/sdk/Representation.java b/src/main/java/com/box/sdk/Representation.java new file mode 100644 index 000000000..73fc7fa46 --- /dev/null +++ b/src/main/java/com/box/sdk/Representation.java @@ -0,0 +1,28 @@ +package com.box.sdk; + +/** + * + */ +public class Representation { + private String representation; + private Properties properties; + //private Metadata + + public class Properties { + private String dimensions; + private String paged; + private String thumb; + + public String getDimensions() { + return this.dimensions; + } + + public String getPaged() { + return this.paged; + } + + public String getThumb() { + return this.thumb; + } + } +} From f840991f07a77d2a62602a660a005b62723be1aa Mon Sep 17 00:00:00 2001 From: Harish Gokavarapu Date: Wed, 30 Aug 2017 13:16:10 -0700 Subject: [PATCH 2/4] reps api --- src/main/java/com/box/sdk/BoxFile.java | 17 +- src/main/java/com/box/sdk/BoxFolder.java | 4 +- src/main/java/com/box/sdk/Metadata.java | 12 + src/main/java/com/box/sdk/Representation.java | 264 +++++++++++++++++- .../{MetadataUtils.java => Parsers.java} | 22 +- src/test/java/com/box/sdk/BoxFileTest.java | 64 +++++ 6 files changed, 374 insertions(+), 9 deletions(-) rename src/main/java/com/box/sdk/internal/utils/{MetadataUtils.java => Parsers.java} (68%) diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index 24706a9c3..5bfd2263a 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -14,7 +14,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import com.box.sdk.internal.utils.MetadataUtils; +import com.box.sdk.internal.utils.Parsers; import com.eclipsesource.json.JsonArray; import com.eclipsesource.json.JsonObject; import com.eclipsesource.json.JsonValue; @@ -1020,7 +1020,7 @@ public class Info extends BoxItem.Info { private boolean isWatermarked; private JsonObject metadata; private Map> metadataMap; - private Representation representations; + private List representations; /** * Constructs an empty Info object. @@ -1158,6 +1158,14 @@ public Metadata getMetadata(String templateName, String scope) { } } + /** + * Get file's representations. + * @return list of representations + */ + public List getRepresentations() { + return this.representations; + } + @Override protected void parseJSONMember(JsonObject.Member member) { super.parseJSONMember(member); @@ -1196,7 +1204,10 @@ protected void parseJSONMember(JsonObject.Member member) { this.isWatermarked = jsonObject.get("is_watermarked").asBoolean(); } else if (memberName.equals("metadata")) { JsonObject jsonObject = value.asObject(); - this.metadataMap = MetadataUtils.parseAndPopulateMetadataMap(jsonObject); + this.metadataMap = Parsers.parseAndPopulateMetadataMap(jsonObject); + } else if (memberName.equals("representations")) { + JsonObject jsonObject = value.asObject(); + this.representations = Parsers.parseRepresentations(jsonObject); } } diff --git a/src/main/java/com/box/sdk/BoxFolder.java b/src/main/java/com/box/sdk/BoxFolder.java index a3738b767..3a19f7262 100644 --- a/src/main/java/com/box/sdk/BoxFolder.java +++ b/src/main/java/com/box/sdk/BoxFolder.java @@ -11,7 +11,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import com.box.sdk.internal.utils.MetadataUtils; +import com.box.sdk.internal.utils.Parsers; import com.eclipsesource.json.JsonArray; import com.eclipsesource.json.JsonObject; import com.eclipsesource.json.JsonValue; @@ -1071,7 +1071,7 @@ protected void parseJSONMember(JsonObject.Member member) { this.isWatermarked = jsonObject.get("is_watermarked").asBoolean(); } else if (memberName.equals("metadata")) { JsonObject jsonObject = value.asObject(); - this.metadataMap = MetadataUtils.parseAndPopulateMetadataMap(jsonObject); + this.metadataMap = Parsers.parseAndPopulateMetadataMap(jsonObject); } } diff --git a/src/main/java/com/box/sdk/Metadata.java b/src/main/java/com/box/sdk/Metadata.java index f1244ee4a..51ca8b055 100644 --- a/src/main/java/com/box/sdk/Metadata.java +++ b/src/main/java/com/box/sdk/Metadata.java @@ -149,6 +149,18 @@ public Metadata add(String path, String value) { return this; } + /** + * Adds a new metadata value. + * @param path the path that designates the key. Must be prefixed with a "/". + * @param value the value. + * @return this metadata object. + */ + public Metadata add(String path, float value) { + this.values.add(this.pathToProperty(path), value); + this.addOp("add", path, value); + return this; + } + /** * Replaces an existing metadata value. * @param path the path that designates the key. Must be prefixed with a "/". diff --git a/src/main/java/com/box/sdk/Representation.java b/src/main/java/com/box/sdk/Representation.java index 73fc7fa46..82d10634c 100644 --- a/src/main/java/com/box/sdk/Representation.java +++ b/src/main/java/com/box/sdk/Representation.java @@ -1,28 +1,288 @@ package com.box.sdk; +import java.net.MalformedURLException; +import java.net.URL; + +import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonValue; + /** - * + * The class represents one instance of a file representation. */ public class Representation { private String representation; private Properties properties; - //private Metadata + private Metadata metadata; + private String assetPath; + private Info info; + private Content content; + private Status status; + + /** + * Construct a representation from JsonObject. + * @param representationJson representaion entry + */ + public Representation(JsonObject representationJson) { + for (JsonObject.Member member : representationJson) { + if (member.getName().equals("representation")) { + this.representation = member.getValue().asString(); + } else if (member.getName().equals("properties")) { + this.properties = new Properties(member.getValue().asObject()); + } else if (member.getName().equals("metadata")) { + this.metadata = new Metadata(member.getValue().asObject()); + } else if (member.getName().equals("assetPath")) { + this.assetPath = member.getValue().asString(); + } else if (member.getName().equals("info")) { + this.info = new Info(member.getValue().asObject()); + } else if (member.getName().equals("content")) { + this.content = new Content(member.getValue().asObject()); + } else if (member.getName().equals("status")) { + this.status = new Status(member.getValue().asObject()); + } + } + } + + /** + * Get the extension of the format, but occasionally a name of a standard (potentially de facto) format + * or a proprietary format that Box supports. + * + * @return representation name + */ + public String getRepresentation() { + return this.representation; + } + + /** + * Get representation's set of static properties to distinguish between subtypes of a given representation, + * for example, different sizes of jpg's. Each representation has its own set of properties. + + * @return properties of representation + */ + public Properties getProperties() { + return this.properties; + } + + /** + * Get representation's metadata. + * + * @return metadata + */ + public Metadata getMetadata() { + return this.metadata; + } + /** + * Get representation's asset path. + * + * @return The values used to substitute for asset_path in the content.url_template. + */ + public String getAssetPath() { + return this.assetPath; + } + + /** + * Get Info which has an opaque URL which will return status information about the file. + * It may change over time and should not be hard-coded or cached. + * @return info + */ + public Info getInfo() { + return this.info; + } + + /** + * Get representation's content which includes a url template. + * @return content + */ + public Content getContent() { + return this.content; + } + + /** + * A string with one of the following values: 'none', 'pending', 'viewable', 'error' and 'success'. + * @return status + */ + public Status getStatus() { + return this.status; + } + + /** + * A set of static properties to distinguish between subtypes of a given representation, + * for example, different sizes of jpg's. Each representation has its own set of properties. + */ public class Properties { private String dimensions; private String paged; private String thumb; + /** + * Construct a representation's properties. + * @param members json object + */ + public Properties(JsonObject members) { + for (JsonObject.Member member : members) { + if (member.getName().equals("dimensions")) { + this.dimensions = member.getValue().asString(); + } else if (member.getName().equals("paged")) { + this.paged = member.getValue().asString(); + } else if (member.getName().equals("thumb")) { + this.thumb = member.getValue().asString(); + } + } + } + + /** + * Get dimensions of representation. + * @return dimensions + */ public String getDimensions() { return this.dimensions; } + /** + * Get whether or not multiple pages are supported or not. + * @return paged value + */ public String getPaged() { return this.paged; } + /** + * When true, down-sampling options are used to produce a better image. + * @return thumb value + */ public String getThumb() { return this.thumb; } } + + /** + * Representation's metadata which is a set of dynamic properties about this specific representation of this + * specific file. Metadata is different for each representation subtype. + */ + public class Metadata { + private int pages; + private JsonObject jsonObject; + + /** + * Construct a representation's metadata. + * @param members json object + */ + public Metadata(JsonObject members) { + for (JsonObject.Member member : members) { + if (member.getName().equals("pages")) { + this.pages = member.getValue().asInt(); + } + } + this.jsonObject = members; + } + + /** + * No. of pages in a multi-page representation. + * @return no. of pages + */ + public int getPages() { + return this.pages; + } + + /** + * Returns a json value for any field in a repreentation's metadata. + * @param field the field that designates the key + * @return the metadata property value. + */ + public JsonValue get(String field) { + return this.jsonObject.get(field); + } + } + + /** + * Representation's info URL. + */ + public class Info { + private URL url; + + /** + * Construct Representation's info. + * @param members json object + */ + public Info(JsonObject members) { + for (JsonObject.Member member : members) { + if (member.getName().equals("url")) { + try { + this.url = new URL(member.getValue().asString()); + } catch (MalformedURLException e) { + throw new BoxAPIException("Couldn't parse info.url for a file representation", e); + } + } + } + } + + /** + * An opaque URL which will return status information about the file. + * @return url + */ + public URL getUrl() { + return this.url; + } + } + + /** + * Representation's content. + */ + public class Content { + private String urlTemplate; + + /** + * Construct a representation's content. + * @param members json object + */ + public Content(JsonObject members) { + for (JsonObject.Member member : members) { + if (member.getName().equals("url_template")) { + this.urlTemplate = member.getValue().asString(); + } + } + } + + /** + * Get an opaque URL template to the content, which follows RFC 6570. There is an asset_path variable that + * should be replaced with a valid path. Valid paths are different for each representation subtype. + * It may change over time and should not be hard-coded or cached. + * @return url template + */ + public String getUrlTemplate() { + return this.urlTemplate; + } + } + + /** + * Representation's status. + */ + public class Status { + private String state; + + /** + * Construct a status object for a representation. + * @param members of status object + */ + public Status(JsonObject members) { + for (JsonObject.Member member : members) { + if (member.getName().equals("state")) { + this.state = member.getValue().asString(); + } + } + } + + /** + * A string with one of the following values: 'none', 'pending', 'viewable', 'error' and 'success'. + * none - the unknown or initial state. + * pending - content is being generated but is not ready yet. + * viewable - like pending, though indicates that enough content is available to be useful. + * error - an error happened and this content is not available. + * success - all of the content is available and complete. + * @return state + */ + public String getState() { + return this.state; + } + } } diff --git a/src/main/java/com/box/sdk/internal/utils/MetadataUtils.java b/src/main/java/com/box/sdk/internal/utils/Parsers.java similarity index 68% rename from src/main/java/com/box/sdk/internal/utils/MetadataUtils.java rename to src/main/java/com/box/sdk/internal/utils/Parsers.java index c1e95ddb6..615b8e6fd 100644 --- a/src/main/java/com/box/sdk/internal/utils/MetadataUtils.java +++ b/src/main/java/com/box/sdk/internal/utils/Parsers.java @@ -1,20 +1,24 @@ package com.box.sdk.internal.utils; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import com.box.sdk.Metadata; +import com.box.sdk.Representation; import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonValue; /** * Utility class for constructing metadata map from json object. */ -public class MetadataUtils { +public class Parsers { /** * Only static members. */ - protected MetadataUtils() { + protected Parsers() { } /** @@ -47,4 +51,18 @@ public static Map> parseAndPopulateMetadataMap(Jso } return metadataMap; } + + /** + * Parse representations from a file object response. + * @param jsonObject representations json object in get response for /files/file-id?fields=representations + * @return list of representations + */ + public static List parseRepresentations(JsonObject jsonObject) { + List representations = new ArrayList(); + for (JsonValue representationJson : jsonObject.get("entries").asArray()) { + Representation representation = new Representation(representationJson.asObject()); + representations.add(representation); + } + return representations; + } } diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index f6f95752d..44a7b189e 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.security.DigestInputStream; @@ -391,7 +392,70 @@ public void testGetAllMetadateParseAllFieldsCorrectly() { Assert.assertEquals(secondEntryDescription, entry.get("/description")); Assert.assertEquals(secondEntryTemplate, entry.getTemplateName()); Assert.assertEquals(secondEntryScope, entry.getScope()); + } + + @Test + @Category(UnitTest.class) + public void getRepresentationsUnitTest() throws MalformedURLException { + final JsonObject fakeResponse = JsonObject.readFrom("{" + + "\"etag\": \"1\"," + + "\"id\": \"0\"," + + "\"type\": \"file\"," + + "\"representations\": {" + + " \"entries\": [" + + " {" + + " \"content\": {" + + " \"url_template\": \".../{+asset_path}\"" + + " }," + + " \"info\": {" + + " \"url\": \"http://dummy.com\"" + + " }," + + " \"metadata\": {" + + " \"pages\": 10" + + " }," + + " \"properties\": {" + + " \"dimensions\": \"2048x2048\"," + + " \"paged\": \"true\"," + + " \"thumb\": \"false\"" + + " }," + + " \"representation\": \"png\"," + + " \"status\": {" + + " \"state\": \"success\"" + + " }" + + " }" + + " ]" + + "}" + + "}"); + BoxAPIConnection api = new BoxAPIConnection(""); + api.setRequestInterceptor(JSONRequestInterceptor.respondWith(fakeResponse)); + BoxFile file = new BoxFile(api, "0"); + List representations = file.getInfo("representations").getRepresentations(); + Assert.assertEquals("There should be only one representation", 1, representations.size()); + Assert.assertEquals("There should content.url_template exists with valid value", + ".../{+asset_path}", representations.get(0).getContent().getUrlTemplate()); + Assert.assertEquals("There should info.url exists with valid value", + new URL("http://dummy.com"), representations.get(0).getInfo().getUrl()); + Assert.assertEquals("There should metadata.pages has exact value", + 10, representations.get(0).getMetadata().getPages()); + Assert.assertEquals("There should properties.dimensions exists with valid value", + "2048x2048", representations.get(0).getProperties().getDimensions()); + Assert.assertEquals("There should properties.paged exists with valid value", + "true", representations.get(0).getProperties().getPaged()); + Assert.assertEquals("There should properties.thumb exists with valid value", + "false", representations.get(0).getProperties().getThumb()); + Assert.assertEquals("There should representation exists with valid value", + "png", representations.get(0).getRepresentation()); + Assert.assertEquals("There should status.state exists with valid value", + "success", representations.get(0).getStatus().getState()); + } + @Test + @Category(IntegrationTest.class) + public void getRepresentationsIntegrationTest() throws MalformedURLException { + BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken()); + BoxFile file = new BoxFile(api, "135907614435"); + List representations = file.getInfo("representations").getRepresentations(); + Assert.assertTrue("There should be at least one representation", representations.size() > 0); } @Test From e3b84d744c7ed12744c43cf128d222165ac20d37 Mon Sep 17 00:00:00 2001 From: Harish Gokavarapu Date: Wed, 30 Aug 2017 14:39:17 -0700 Subject: [PATCH 3/4] feedback --- src/main/java/com/box/sdk/Representation.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/box/sdk/Representation.java b/src/main/java/com/box/sdk/Representation.java index 82d10634c..0a7adaa50 100644 --- a/src/main/java/com/box/sdk/Representation.java +++ b/src/main/java/com/box/sdk/Representation.java @@ -10,6 +10,7 @@ * The class represents one instance of a file representation. */ public class Representation { + private String representation; private Properties properties; private Metadata metadata; @@ -55,7 +56,6 @@ public String getRepresentation() { /** * Get representation's set of static properties to distinguish between subtypes of a given representation, * for example, different sizes of jpg's. Each representation has its own set of properties. - * @return properties of representation */ public Properties getProperties() { @@ -110,6 +110,7 @@ public Status getStatus() { * for example, different sizes of jpg's. Each representation has its own set of properties. */ public class Properties { + private String dimensions; private String paged; private String thumb; @@ -160,6 +161,7 @@ public String getThumb() { * specific file. Metadata is different for each representation subtype. */ public class Metadata { + private int pages; private JsonObject jsonObject; @@ -198,6 +200,7 @@ public JsonValue get(String field) { * Representation's info URL. */ public class Info { + private URL url; /** @@ -229,6 +232,7 @@ public URL getUrl() { * Representation's content. */ public class Content { + private String urlTemplate; /** @@ -258,6 +262,7 @@ public String getUrlTemplate() { * Representation's status. */ public class Status { + private String state; /** From 93e1d3f7dd8e8932964de8ba6a4153807735ec8b Mon Sep 17 00:00:00 2001 From: Harish Gokavarapu Date: Fri, 1 Sep 2017 17:41:58 -0700 Subject: [PATCH 4/4] grouping --- src/main/java/com/box/sdk/BoxFile.java | 30 +++++ src/main/java/com/box/sdk/Representation.java | 127 +++--------------- src/test/java/com/box/sdk/BoxFileTest.java | 51 +++++-- 3 files changed, 82 insertions(+), 126 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxFile.java b/src/main/java/com/box/sdk/BoxFile.java index 5bfd2263a..d3e0c217d 100644 --- a/src/main/java/com/box/sdk/BoxFile.java +++ b/src/main/java/com/box/sdk/BoxFile.java @@ -7,11 +7,14 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.EnumSet; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; import com.box.sdk.internal.utils.Parsers; @@ -439,6 +442,33 @@ public BoxFile.Info getInfo(String... fields) { return new Info(response.getJSON()); } + /** + * Gets information about this item including a specified set of representations. + * @see X-Rep-Hints Header + * + * @param representationHints hints for representations to be retrieved + * @param fields the fields to retrieve. + * @return info about this item containing only the specified fields, including representations. + */ + public BoxFile.Info getInfoWithRepresentations(String representationHints, String... fields) { + if (representationHints.matches(Representation.X_REP_HINTS_PATTERN)) { + //Since the user intends to get representations, add it to fields, even if user has missed it + Set fieldsSet = new HashSet(Arrays.asList(fields)); + fieldsSet.add("representations"); + String queryString = new QueryStringBuilder().appendParam("fields", + fieldsSet.toArray(new String[fieldsSet.size()])).toString(); + URL url = FILE_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID()); + + BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); + request.addHeader("X-Rep-Hints", representationHints); + BoxJSONResponse response = (BoxJSONResponse) request.send(); + return new Info(response.getJSON()); + } else { + throw new BoxAPIException("Represention hints is not valid." + + " Refer documention on how to construct X-Rep-Hints Header"); + } + } + /** * Updates the information about this file with any info fields that have been modified locally. * diff --git a/src/main/java/com/box/sdk/Representation.java b/src/main/java/com/box/sdk/Representation.java index 0a7adaa50..2543f1f2f 100644 --- a/src/main/java/com/box/sdk/Representation.java +++ b/src/main/java/com/box/sdk/Representation.java @@ -4,17 +4,22 @@ import java.net.URL; import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonValue; /** * The class represents one instance of a file representation. */ public class Representation { + /** + * Used to validate if the hints header has (near) valid value. + */ + protected static final String X_REP_HINTS_PATTERN = "^(?:\\[[a-z0-9_]+(?:\\?[a-z0-9_]+\\=[a-z0-9_]+(?:" + + "\\|[a-z0-9_]+)*(?:&[a-z0-9_]+\\=[a-z0-9_]+(?:\\|[a-z0-9_]+)*)*)?(?:,[a-z0-9_]+(?:\\?[a-z0-9_]+\\=[a-z0-9_]+" + + "(?:\\|[a-z0-9_]+)*(?:&[a-z0-9_]+\\=[a-z0-9_]+(?:\\|[a-z0-9_]+)*)*)?)*\\])+$"; + private String representation; - private Properties properties; - private Metadata metadata; - private String assetPath; + private JsonObject properties; + private JsonObject metadata; private Info info; private Content content; private Status status; @@ -28,11 +33,9 @@ public Representation(JsonObject representationJson) { if (member.getName().equals("representation")) { this.representation = member.getValue().asString(); } else if (member.getName().equals("properties")) { - this.properties = new Properties(member.getValue().asObject()); + this.properties = member.getValue().asObject(); } else if (member.getName().equals("metadata")) { - this.metadata = new Metadata(member.getValue().asObject()); - } else if (member.getName().equals("assetPath")) { - this.assetPath = member.getValue().asString(); + this.metadata = member.getValue().asObject(); } else if (member.getName().equals("info")) { this.info = new Info(member.getValue().asObject()); } else if (member.getName().equals("content")) { @@ -56,30 +59,21 @@ public String getRepresentation() { /** * Get representation's set of static properties to distinguish between subtypes of a given representation, * for example, different sizes of jpg's. Each representation has its own set of properties. - * @return properties of representation + * @return properties of representation as JsonObject */ - public Properties getProperties() { + public JsonObject getProperties() { return this.properties; } /** * Get representation's metadata. * - * @return metadata + * @return metadataas JsonObject */ - public Metadata getMetadata() { + public JsonObject getMetadata() { return this.metadata; } - /** - * Get representation's asset path. - * - * @return The values used to substitute for asset_path in the content.url_template. - */ - public String getAssetPath() { - return this.assetPath; - } - /** * Get Info which has an opaque URL which will return status information about the file. * It may change over time and should not be hard-coded or cached. @@ -105,97 +99,6 @@ public Status getStatus() { return this.status; } - /** - * A set of static properties to distinguish between subtypes of a given representation, - * for example, different sizes of jpg's. Each representation has its own set of properties. - */ - public class Properties { - - private String dimensions; - private String paged; - private String thumb; - - /** - * Construct a representation's properties. - * @param members json object - */ - public Properties(JsonObject members) { - for (JsonObject.Member member : members) { - if (member.getName().equals("dimensions")) { - this.dimensions = member.getValue().asString(); - } else if (member.getName().equals("paged")) { - this.paged = member.getValue().asString(); - } else if (member.getName().equals("thumb")) { - this.thumb = member.getValue().asString(); - } - } - } - - /** - * Get dimensions of representation. - * @return dimensions - */ - public String getDimensions() { - return this.dimensions; - } - - /** - * Get whether or not multiple pages are supported or not. - * @return paged value - */ - public String getPaged() { - return this.paged; - } - - /** - * When true, down-sampling options are used to produce a better image. - * @return thumb value - */ - public String getThumb() { - return this.thumb; - } - } - - /** - * Representation's metadata which is a set of dynamic properties about this specific representation of this - * specific file. Metadata is different for each representation subtype. - */ - public class Metadata { - - private int pages; - private JsonObject jsonObject; - - /** - * Construct a representation's metadata. - * @param members json object - */ - public Metadata(JsonObject members) { - for (JsonObject.Member member : members) { - if (member.getName().equals("pages")) { - this.pages = member.getValue().asInt(); - } - } - this.jsonObject = members; - } - - /** - * No. of pages in a multi-page representation. - * @return no. of pages - */ - public int getPages() { - return this.pages; - } - - /** - * Returns a json value for any field in a repreentation's metadata. - * @param field the field that designates the key - * @return the metadata property value. - */ - public JsonValue get(String field) { - return this.jsonObject.get(field); - } - } - /** * Representation's info URL. */ diff --git a/src/test/java/com/box/sdk/BoxFileTest.java b/src/test/java/com/box/sdk/BoxFileTest.java index 44a7b189e..613a629f3 100644 --- a/src/test/java/com/box/sdk/BoxFileTest.java +++ b/src/test/java/com/box/sdk/BoxFileTest.java @@ -431,30 +431,53 @@ public void getRepresentationsUnitTest() throws MalformedURLException { BoxFile file = new BoxFile(api, "0"); List representations = file.getInfo("representations").getRepresentations(); Assert.assertEquals("There should be only one representation", 1, representations.size()); - Assert.assertEquals("There should content.url_template exists with valid value", + Assert.assertEquals("content.url_template should exist with valid value", ".../{+asset_path}", representations.get(0).getContent().getUrlTemplate()); - Assert.assertEquals("There should info.url exists with valid value", + Assert.assertEquals("info.url should exist with valid value", new URL("http://dummy.com"), representations.get(0).getInfo().getUrl()); - Assert.assertEquals("There should metadata.pages has exact value", - 10, representations.get(0).getMetadata().getPages()); - Assert.assertEquals("There should properties.dimensions exists with valid value", - "2048x2048", representations.get(0).getProperties().getDimensions()); - Assert.assertEquals("There should properties.paged exists with valid value", - "true", representations.get(0).getProperties().getPaged()); - Assert.assertEquals("There should properties.thumb exists with valid value", - "false", representations.get(0).getProperties().getThumb()); - Assert.assertEquals("There should representation exists with valid value", + Assert.assertEquals("metadata.pages should have exact value", + 10, representations.get(0).getMetadata().get("pages").asInt()); + Assert.assertEquals("properties.dimensions should exist with valid value", + "2048x2048", representations.get(0).getProperties().get("dimensions").asString()); + Assert.assertEquals("properties.paged should exist with valid value", + "true", representations.get(0).getProperties().get("paged").asString()); + Assert.assertEquals("properties.thumb should exist with valid value", + "false", representations.get(0).getProperties().get("thumb").asString()); + Assert.assertEquals("representation should exist with valid value", "png", representations.get(0).getRepresentation()); - Assert.assertEquals("There should status.state exists with valid value", + Assert.assertEquals("status.state should exist with valid value", "success", representations.get(0).getStatus().getState()); } + @Test + @Category(UnitTest.class) + public void getRepresentationsShouldThrowExceptionWhenHintsIsInvalid() throws MalformedURLException { + BoxAPIConnection api = new BoxAPIConnection(""); + BoxFile file = new BoxFile(api, "0"); + try { + List representations = file.getInfoWithRepresentations("png", + "representations").getRepresentations(); + } catch (Exception e) { + Assert.assertTrue("BoxAPIException should be thrown", e instanceof BoxAPIException); + } + } + @Test @Category(IntegrationTest.class) - public void getRepresentationsIntegrationTest() throws MalformedURLException { + public void getInfoWithRepresentationsIntegrationTestWithSimpleHint() throws MalformedURLException { BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken()); BoxFile file = new BoxFile(api, "135907614435"); - List representations = file.getInfo("representations").getRepresentations(); + List representations = file.getInfoWithRepresentations("[png]").getRepresentations(); + Assert.assertTrue("There should be at least one representation", representations.size() > 0); + } + + @Test + @Category(IntegrationTest.class) + public void getInfoWithRepresentationsIntegrationTestWithComplexHint() throws MalformedURLException { + BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken()); + BoxFile file = new BoxFile(api, "135907614435"); + List representations = file.getInfoWithRepresentations( + "[jpg,png?dimensions=1024x1024][pdf]").getRepresentations(); Assert.assertTrue("There should be at least one representation", representations.size() > 0); }