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