+
+ public String getContainer() {
+ return container;
+ }
+
+ public String getPrefix() {
+ return prefix;
+ }
+
+ public void setPrefix(String prefix) {
+ this.prefix = prefix;
+ }
+
+ public void populate(Response response) {
+ String header = response.getHeader("Last-Modified");
+ if (header != null)
+ lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
+
+ header = response.getHeader("X-Container-Bytes-Used");
+ if (header != null)
+ bytesUsed = Long.valueOf(header);
+
+ subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
+ files.clear();
+ JSONValue json = JSONParser.parseStrict(response.getText());
+ JSONArray array = json.isArray();
+ if (array != null) {
+ for (int i=0; i<array.size(); i++) {
+ JSONObject o = array.get(i).isObject();
+ if (o != null) {
+ String contentType = unmarshallString(o, "content_type");
+ if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
+ Folder f = new Folder();
+ f.populate(this, o, container);
+ subfolders.add(f);
+ }
+ else {
+ File file = new File();
+ file.populate(this, o, container);
+ files.add(file);
+ }
+ }
+ }
+ }
+ }
+
+ public void populate(Folder parent, JSONObject o, String aContainer) {
+ this.parent = parent;
+ String path = null;
+ if (o.containsKey("subdir")) {
+ path = unmarshallString(o, "subdir");
+ }
+ else {
+ path = unmarshallString(o, "name");
+ lastModified = unmarshallDate(o, "last_modified");
+ }
+ if (path.endsWith("/"))
+ path = path.substring(0, path.length() - 1);
+ if (path.contains("/"))
+ name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
+ else
+ name = path;
+ if (aContainer != null) {
+ container = aContainer;
+ prefix = path;
+ }
+ else {
+ container = name;
+ prefix = "";
+ }
+ }
+
+ public static Folder createFromResponse(Response response, Folder result) {
+ Folder f = null;
+ if (result == null)
+ f = new Folder();
+ else
+ f = result;
+
+ f.populate(response);
+ return f;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other instanceof Folder) {
+ Folder o = (Folder) other;
+ return (container + prefix).equals(o.getContainer() + o.getPrefix());
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return (container + prefix).hashCode();
+ }
+
+ public Set<File> getFiles() {
+ return files;
+ }
+
+ public Folder getParent() {
+ return parent;
+ }
+
+ public String getUri() {
+ return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
+ }