Skip to content

Commit bc87147

Browse files
authored
Representations api (#465)
Utility method to pass x-rep-hints header as part of request for getting representations
1 parent c18e9c9 commit bc87147

File tree

3 files changed

+82
-126
lines changed

3 files changed

+82
-126
lines changed

src/main/java/com/box/sdk/BoxFile.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import java.net.MalformedURLException;
88
import java.net.URL;
99
import java.util.ArrayList;
10+
import java.util.Arrays;
1011
import java.util.Collection;
1112
import java.util.Date;
1213
import java.util.EnumSet;
14+
import java.util.HashSet;
1315
import java.util.List;
1416
import java.util.Map;
17+
import java.util.Set;
1518
import java.util.concurrent.TimeUnit;
1619

1720
import com.box.sdk.internal.utils.Parsers;
@@ -439,6 +442,33 @@ public BoxFile.Info getInfo(String... fields) {
439442
return new Info(response.getJSON());
440443
}
441444

445+
/**
446+
* Gets information about this item including a specified set of representations.
447+
* @see <a href=https://developer.box.com/reference#section-x-rep-hints-header>X-Rep-Hints Header</a>
448+
*
449+
* @param representationHints hints for representations to be retrieved
450+
* @param fields the fields to retrieve.
451+
* @return info about this item containing only the specified fields, including representations.
452+
*/
453+
public BoxFile.Info getInfoWithRepresentations(String representationHints, String... fields) {
454+
if (representationHints.matches(Representation.X_REP_HINTS_PATTERN)) {
455+
//Since the user intends to get representations, add it to fields, even if user has missed it
456+
Set<String> fieldsSet = new HashSet<String>(Arrays.asList(fields));
457+
fieldsSet.add("representations");
458+
String queryString = new QueryStringBuilder().appendParam("fields",
459+
fieldsSet.toArray(new String[fieldsSet.size()])).toString();
460+
URL url = FILE_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
461+
462+
BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
463+
request.addHeader("X-Rep-Hints", representationHints);
464+
BoxJSONResponse response = (BoxJSONResponse) request.send();
465+
return new Info(response.getJSON());
466+
} else {
467+
throw new BoxAPIException("Represention hints is not valid."
468+
+ " Refer documention on how to construct X-Rep-Hints Header");
469+
}
470+
}
471+
442472
/**
443473
* Updates the information about this file with any info fields that have been modified locally.
444474
*

src/main/java/com/box/sdk/Representation.java

Lines changed: 15 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
import java.net.URL;
55

66
import com.eclipsesource.json.JsonObject;
7-
import com.eclipsesource.json.JsonValue;
87

98
/**
109
* The class represents one instance of a file representation.
1110
*/
1211
public class Representation {
1312

13+
/**
14+
* Used to validate if the hints header has (near) valid value.
15+
*/
16+
protected static final String X_REP_HINTS_PATTERN = "^(?:\\[[a-z0-9_]+(?:\\?[a-z0-9_]+\\=[a-z0-9_]+(?:"
17+
+ "\\|[a-z0-9_]+)*(?:&[a-z0-9_]+\\=[a-z0-9_]+(?:\\|[a-z0-9_]+)*)*)?(?:,[a-z0-9_]+(?:\\?[a-z0-9_]+\\=[a-z0-9_]+"
18+
+ "(?:\\|[a-z0-9_]+)*(?:&[a-z0-9_]+\\=[a-z0-9_]+(?:\\|[a-z0-9_]+)*)*)?)*\\])+$";
19+
1420
private String representation;
15-
private Properties properties;
16-
private Metadata metadata;
17-
private String assetPath;
21+
private JsonObject properties;
22+
private JsonObject metadata;
1823
private Info info;
1924
private Content content;
2025
private Status status;
@@ -28,11 +33,9 @@ public Representation(JsonObject representationJson) {
2833
if (member.getName().equals("representation")) {
2934
this.representation = member.getValue().asString();
3035
} else if (member.getName().equals("properties")) {
31-
this.properties = new Properties(member.getValue().asObject());
36+
this.properties = member.getValue().asObject();
3237
} else if (member.getName().equals("metadata")) {
33-
this.metadata = new Metadata(member.getValue().asObject());
34-
} else if (member.getName().equals("assetPath")) {
35-
this.assetPath = member.getValue().asString();
38+
this.metadata = member.getValue().asObject();
3639
} else if (member.getName().equals("info")) {
3740
this.info = new Info(member.getValue().asObject());
3841
} else if (member.getName().equals("content")) {
@@ -56,30 +59,21 @@ public String getRepresentation() {
5659
/**
5760
* Get representation's set of static properties to distinguish between subtypes of a given representation,
5861
* for example, different sizes of jpg's. Each representation has its own set of properties.
59-
* @return properties of representation
62+
* @return properties of representation as JsonObject
6063
*/
61-
public Properties getProperties() {
64+
public JsonObject getProperties() {
6265
return this.properties;
6366
}
6467

6568
/**
6669
* Get representation's metadata.
6770
*
68-
* @return metadata
71+
* @return metadataas JsonObject
6972
*/
70-
public Metadata getMetadata() {
73+
public JsonObject getMetadata() {
7174
return this.metadata;
7275
}
7376

74-
/**
75-
* Get representation's asset path.
76-
*
77-
* @return The values used to substitute for asset_path in the content.url_template.
78-
*/
79-
public String getAssetPath() {
80-
return this.assetPath;
81-
}
82-
8377
/**
8478
* Get Info which has an opaque URL which will return status information about the file.
8579
* It may change over time and should not be hard-coded or cached.
@@ -105,97 +99,6 @@ public Status getStatus() {
10599
return this.status;
106100
}
107101

108-
/**
109-
* A set of static properties to distinguish between subtypes of a given representation,
110-
* for example, different sizes of jpg's. Each representation has its own set of properties.
111-
*/
112-
public class Properties {
113-
114-
private String dimensions;
115-
private String paged;
116-
private String thumb;
117-
118-
/**
119-
* Construct a representation's properties.
120-
* @param members json object
121-
*/
122-
public Properties(JsonObject members) {
123-
for (JsonObject.Member member : members) {
124-
if (member.getName().equals("dimensions")) {
125-
this.dimensions = member.getValue().asString();
126-
} else if (member.getName().equals("paged")) {
127-
this.paged = member.getValue().asString();
128-
} else if (member.getName().equals("thumb")) {
129-
this.thumb = member.getValue().asString();
130-
}
131-
}
132-
}
133-
134-
/**
135-
* Get dimensions of representation.
136-
* @return dimensions
137-
*/
138-
public String getDimensions() {
139-
return this.dimensions;
140-
}
141-
142-
/**
143-
* Get whether or not multiple pages are supported or not.
144-
* @return paged value
145-
*/
146-
public String getPaged() {
147-
return this.paged;
148-
}
149-
150-
/**
151-
* When true, down-sampling options are used to produce a better image.
152-
* @return thumb value
153-
*/
154-
public String getThumb() {
155-
return this.thumb;
156-
}
157-
}
158-
159-
/**
160-
* Representation's metadata which is a set of dynamic properties about this specific representation of this
161-
* specific file. Metadata is different for each representation subtype.
162-
*/
163-
public class Metadata {
164-
165-
private int pages;
166-
private JsonObject jsonObject;
167-
168-
/**
169-
* Construct a representation's metadata.
170-
* @param members json object
171-
*/
172-
public Metadata(JsonObject members) {
173-
for (JsonObject.Member member : members) {
174-
if (member.getName().equals("pages")) {
175-
this.pages = member.getValue().asInt();
176-
}
177-
}
178-
this.jsonObject = members;
179-
}
180-
181-
/**
182-
* No. of pages in a multi-page representation.
183-
* @return no. of pages
184-
*/
185-
public int getPages() {
186-
return this.pages;
187-
}
188-
189-
/**
190-
* Returns a json value for any field in a repreentation's metadata.
191-
* @param field the field that designates the key
192-
* @return the metadata property value.
193-
*/
194-
public JsonValue get(String field) {
195-
return this.jsonObject.get(field);
196-
}
197-
}
198-
199102
/**
200103
* Representation's info URL.
201104
*/

src/test/java/com/box/sdk/BoxFileTest.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -431,30 +431,53 @@ public void getRepresentationsUnitTest() throws MalformedURLException {
431431
BoxFile file = new BoxFile(api, "0");
432432
List<Representation> representations = file.getInfo("representations").getRepresentations();
433433
Assert.assertEquals("There should be only one representation", 1, representations.size());
434-
Assert.assertEquals("There should content.url_template exists with valid value",
434+
Assert.assertEquals("content.url_template should exist with valid value",
435435
".../{+asset_path}", representations.get(0).getContent().getUrlTemplate());
436-
Assert.assertEquals("There should info.url exists with valid value",
436+
Assert.assertEquals("info.url should exist with valid value",
437437
new URL("http://dummy.com"), representations.get(0).getInfo().getUrl());
438-
Assert.assertEquals("There should metadata.pages has exact value",
439-
10, representations.get(0).getMetadata().getPages());
440-
Assert.assertEquals("There should properties.dimensions exists with valid value",
441-
"2048x2048", representations.get(0).getProperties().getDimensions());
442-
Assert.assertEquals("There should properties.paged exists with valid value",
443-
"true", representations.get(0).getProperties().getPaged());
444-
Assert.assertEquals("There should properties.thumb exists with valid value",
445-
"false", representations.get(0).getProperties().getThumb());
446-
Assert.assertEquals("There should representation exists with valid value",
438+
Assert.assertEquals("metadata.pages should have exact value",
439+
10, representations.get(0).getMetadata().get("pages").asInt());
440+
Assert.assertEquals("properties.dimensions should exist with valid value",
441+
"2048x2048", representations.get(0).getProperties().get("dimensions").asString());
442+
Assert.assertEquals("properties.paged should exist with valid value",
443+
"true", representations.get(0).getProperties().get("paged").asString());
444+
Assert.assertEquals("properties.thumb should exist with valid value",
445+
"false", representations.get(0).getProperties().get("thumb").asString());
446+
Assert.assertEquals("representation should exist with valid value",
447447
"png", representations.get(0).getRepresentation());
448-
Assert.assertEquals("There should status.state exists with valid value",
448+
Assert.assertEquals("status.state should exist with valid value",
449449
"success", representations.get(0).getStatus().getState());
450450
}
451451

452+
@Test
453+
@Category(UnitTest.class)
454+
public void getRepresentationsShouldThrowExceptionWhenHintsIsInvalid() throws MalformedURLException {
455+
BoxAPIConnection api = new BoxAPIConnection("");
456+
BoxFile file = new BoxFile(api, "0");
457+
try {
458+
List<Representation> representations = file.getInfoWithRepresentations("png",
459+
"representations").getRepresentations();
460+
} catch (Exception e) {
461+
Assert.assertTrue("BoxAPIException should be thrown", e instanceof BoxAPIException);
462+
}
463+
}
464+
452465
@Test
453466
@Category(IntegrationTest.class)
454-
public void getRepresentationsIntegrationTest() throws MalformedURLException {
467+
public void getInfoWithRepresentationsIntegrationTestWithSimpleHint() throws MalformedURLException {
455468
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
456469
BoxFile file = new BoxFile(api, "135907614435");
457-
List<Representation> representations = file.getInfo("representations").getRepresentations();
470+
List<Representation> representations = file.getInfoWithRepresentations("[png]").getRepresentations();
471+
Assert.assertTrue("There should be at least one representation", representations.size() > 0);
472+
}
473+
474+
@Test
475+
@Category(IntegrationTest.class)
476+
public void getInfoWithRepresentationsIntegrationTestWithComplexHint() throws MalformedURLException {
477+
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
478+
BoxFile file = new BoxFile(api, "135907614435");
479+
List<Representation> representations = file.getInfoWithRepresentations(
480+
"[jpg,png?dimensions=1024x1024][pdf]").getRepresentations();
458481
Assert.assertTrue("There should be at least one representation", representations.size() > 0);
459482
}
460483

0 commit comments

Comments
 (0)