2 * Copyright (c) 2011 Greek Research and Technology Network
5 package gr.grnet.pithos.web.client.foldertree;
7 import com.google.gwt.http.client.Header;
8 import com.google.gwt.http.client.Response;
9 import com.google.gwt.i18n.client.DateTimeFormat;
10 import com.google.gwt.json.client.JSONArray;
11 import com.google.gwt.json.client.JSONNumber;
12 import com.google.gwt.json.client.JSONObject;
13 import com.google.gwt.json.client.JSONParser;
14 import com.google.gwt.json.client.JSONString;
15 import com.google.gwt.json.client.JSONValue;
16 import java.util.Date;
18 public abstract class Resource {
22 public String getUri() {
26 protected static String unmarshallString(JSONObject obj, String key){
27 if(obj.get(key) != null) {
28 JSONString s = obj.get(key).isString();
30 return s.stringValue();
35 protected static int unmarshallInt(JSONObject obj, String key){
36 if(obj.get(key) != null)
37 if(obj.get(key).isNumber() != null)
38 return (int) obj.get(key).isNumber().getValue();
42 protected static long unmarshallLong(JSONObject obj, String key){
43 if(obj.get(key) != null) {
44 JSONNumber value = obj.get(key).isNumber();
46 return (long) value.doubleValue();
51 protected static boolean unmarshallBoolean(JSONObject obj, String key){
52 if(obj.get(key) != null)
53 if(obj.get(key).isBoolean() != null)
54 return obj.get(key).isBoolean().booleanValue();
58 protected static Date unmarshallTimestamp(JSONObject obj, String key){
59 if(obj.get(key) != null) {
60 JSONString s = obj.get(key).isString();
62 return new Date(Long.valueOf(s.stringValue()).longValue());
67 protected static Date unmarshallDate(JSONObject obj, String key){
68 if(obj.get(key) != null) {
69 JSONString s = obj.get(key).isString();
71 return DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss").parse(s.stringValue());
76 public static native String getDate(Long ms)/*-{
77 return (new Date(ms)).toUTCString();
80 public abstract String getLastModifiedSince();
82 public static <T> T createFromResponse(Class<T> aClass, Response response) {
83 JSONValue json = JSONParser.parseStrict(response.getText());
85 if (aClass.equals(AccountResource.class)) {
86 AccountResource a = new AccountResource();
87 JSONArray array = json.isArray();
89 for (int i=0; i<array.size(); i++) {
90 JSONObject o = array.get(i).isObject();
92 ContainerResource container = new ContainerResource();
93 container.setName(unmarshallString(o, "name"));
94 container.setCount(unmarshallLong(o, "count"));
95 container.setBytes(unmarshallLong(o, "bytes"));
96 container.setCreated(unmarshallTimestamp(o, "created"));
97 container.setLastModified(unmarshallDate(o, "last_modified"));
98 a.getContainers().add(container);