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