root / src / pithos / content / connection.js @ 29:ec9750e9cceb
History | View | Annotate | Download (7.4 kB)
1 |
connection_obj = null;
|
---|---|
2 |
|
3 |
function ConnectionObject() { |
4 |
// public properties
|
5 |
this.token = 'ERROR: no token'; |
6 |
this.username = 'ERROR: no username'; |
7 |
this.login = 'ERROR: no login'; |
8 |
this.email = 'ERROR: no email'; |
9 |
this.rest_url = 'http://pithos.grnet.gr/pithos/rest'; |
10 |
|
11 |
// private properties
|
12 |
var user_homepage = null; |
13 |
var shared_data = null; |
14 |
var current = this; |
15 |
|
16 |
// public methods
|
17 |
this.get_shared_data = function() { |
18 |
if (shared_data == null) { |
19 |
shared_data = new Array();
|
20 |
|
21 |
var d = this.load_directory_sync(user_homepage.shared); |
22 |
|
23 |
for (var i in d.files) { |
24 |
shared_data.push(d.files[i].URI); |
25 |
} |
26 |
for (var i in d.folders) { |
27 |
shared_data.push(d.folders[i].URI); |
28 |
} |
29 |
} |
30 |
|
31 |
return shared_data;
|
32 |
} |
33 |
|
34 |
this.refresh_shared_data = function() { |
35 |
shared_data = null;
|
36 |
return this.get_shared_data(); |
37 |
} |
38 |
|
39 |
this.user_homepage = function() { |
40 |
if(user_homepage == null) { |
41 |
user_homepage = new UserHomePage();
|
42 |
} |
43 |
|
44 |
return user_homepage;
|
45 |
} |
46 |
|
47 |
this.load_directory_sync = function(uri) { |
48 |
var d = cache.get(uri);
|
49 |
if (d == undefined) { |
50 |
if (isRemoteDirectory(uri)) {
|
51 |
// remote dir, do a json call
|
52 |
var req = this.rest_request_sync(uri); |
53 |
return (new Directory(uri, JSON.parse(req.responseText))); |
54 |
} else {
|
55 |
// local dir
|
56 |
return (new Directory(uri, load_local_directory(uri))); |
57 |
} |
58 |
} |
59 |
|
60 |
return d;
|
61 |
} |
62 |
|
63 |
this.load_file_metadata = function(uri) { |
64 |
var fm_obj = null; |
65 |
|
66 |
if ( isRemoteDirectory(uri) ) {
|
67 |
var req = this.rest_request_get(uri); |
68 |
|
69 |
} else if ( isLocalDirectory(uri) ) { |
70 |
fm_obj = loadLocalFile(uri); |
71 |
} |
72 |
|
73 |
return fm_obj;
|
74 |
} |
75 |
|
76 |
this.create_request_get = function(rest_req) { |
77 |
if ( isRemoteDirectory(rest_req) ) {
|
78 |
var gmtstring = (new Date()).toGMTString(); |
79 |
var sign_url = rest_req.replace(this.rest_url, ""); |
80 |
var hash = sign(this.token, sign_url, gmtstring); |
81 |
|
82 |
return rest_req + ('?Date=' + encodeURIComponent(gmtstring) + '&Authorization=' + this.username + '%20' + encodeURIComponent(hash)); |
83 |
} else {
|
84 |
if ( isUnix() ) {
|
85 |
return 'file://' + rest_req; |
86 |
} |
87 |
if ( isWindows() ) {
|
88 |
return 'file:///' + rest_req.replace('\\','/'); |
89 |
} |
90 |
} |
91 |
|
92 |
return rest_req;
|
93 |
} |
94 |
|
95 |
this.rest_request_head = function(rest_req) { |
96 |
return rest_request_generic(rest_req, "HEAD", null, false, null, null, null); |
97 |
} |
98 |
|
99 |
this.rest_request_post = function(rest_req, data) { |
100 |
return rest_request_generic(rest_req, "POST", data, false, null, null, null); |
101 |
} |
102 |
|
103 |
this.rest_request_get = function(rest_req) { |
104 |
return this.rest_request_sync(rest_req); |
105 |
} |
106 |
|
107 |
this.rest_request_sync = function(rest_req) { |
108 |
return rest_request_generic(rest_req, "GET", null, false, null, null, null); |
109 |
} |
110 |
|
111 |
this.rest_request_async = function(rest_req, on_load, on_progress, on_error) { |
112 |
return rest_request_generic(rest_req, "GET", null, true, on_load, on_progress, on_error); |
113 |
} |
114 |
|
115 |
// empty implementations of async events
|
116 |
// for easy programming
|
117 |
this.onLoadDefault = function(e) { } |
118 |
this.onProgressDefault = function(e) { } |
119 |
this.onErrorDefault = function(e) { } |
120 |
|
121 |
// private methods
|
122 |
function rest_request_generic(rest_req, method, data, async, on_load, on_progress, on_error) { |
123 |
var url = rest_req;
|
124 |
var sign_url = rest_req.replace(current.rest_url, ''); |
125 |
var gmtstring = (new Date()).toGMTString(); |
126 |
|
127 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
128 |
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); |
129 |
req.open(method, url, async); |
130 |
if (async) {
|
131 |
req.onload = on_load; |
132 |
req.onprogress = on_progress; |
133 |
req.onerror = on_error; |
134 |
} |
135 |
req.setRequestHeader('Authorization', current.username + " " + sign(current.token, sign_url, gmtstring)); |
136 |
req.setRequestHeader('X-GSS-Date', gmtstring);
|
137 |
|
138 |
if (method == "GET" || method == "HEAD") { |
139 |
req.send(null);
|
140 |
} |
141 |
if (method == "POST") { |
142 |
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
143 |
req.setRequestHeader("Content-length", data.length);
|
144 |
req.setRequestHeader("Connection", "close"); |
145 |
req.send(data); |
146 |
} |
147 |
|
148 |
return req;
|
149 |
} |
150 |
|
151 |
function check_null_result(r, d) { |
152 |
var res = r.exec(d);
|
153 |
if (res == null) { return 'ERROR: no connection data'; } |
154 |
|
155 |
return res[1]; |
156 |
} |
157 |
|
158 |
function sign(token, thepath, gmtdate) { |
159 |
var rawdatastring = "GET" + gmtdate + thepath; |
160 |
var signature = gss_sign(token, rawdatastring);
|
161 |
|
162 |
return (signature);
|
163 |
} |
164 |
|
165 |
|
166 |
// Allow access to cookie database of browser
|
167 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
168 |
var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager); |
169 |
var menum = cookieManager.enumerator;
|
170 |
while (menum.hasMoreElements()){
|
171 |
var cookie = menum.getNext().QueryInterface(Components.interfaces.nsICookie);
|
172 |
if ( (cookie.host == '.pithos.grnet.gr') && (cookie.name == '_gss_a') ) { |
173 |
var tokens = cookie.value.split('|'); |
174 |
this.email = 'ERROR: no connection data'; |
175 |
this.username = tokens[0].replace('%40', '@'); |
176 |
this.login = this.username.split('@')[0]; |
177 |
this.token = tokens[1]; |
178 |
break;
|
179 |
} |
180 |
} |
181 |
|
182 |
|
183 |
//document.getElementById('label-username').value = this.username + ' (' + this.login + ')';
|
184 |
$mb = jQuery.noConflict();
|
185 |
$mb('#label-username').val(this.username + ' (' + this.login + ')'); |
186 |
} |
187 |
|
188 |
function UserHomePage() { |
189 |
// public properties
|
190 |
this.fileroot = 'ERROR: fileroot not defined'; |
191 |
this.trash = 'ERROR: trash is not defined'; |
192 |
this.shared = 'ERROR: shared is not defined'; |
193 |
this.others = 'ERROR: others is not defined'; |
194 |
this.tags = 'ERROR: tags is not defined'; |
195 |
this.groups = 'ERROR: groups is not defined'; |
196 |
this.announcement = 'ERROR: announcement is not defined'; |
197 |
this.creationdate = 0; |
198 |
this.modificationDate = 0; |
199 |
this.totalFiles = 0; |
200 |
this.totalBytes = 0; |
201 |
this.bytesRemaining = 0; |
202 |
|
203 |
this.fileroot_object = function() { |
204 |
// TODO: do some checking here
|
205 |
return connection_obj.load_directory_sync(this.fileroot); |
206 |
} |
207 |
|
208 |
this.others_object = function() { |
209 |
|
210 |
} |
211 |
|
212 |
this.tags_list = function() { |
213 |
|
214 |
} |
215 |
|
216 |
// private properties
|
217 |
|
218 |
// public methods
|
219 |
|
220 |
// private methods
|
221 |
|
222 |
// run initialization
|
223 |
var req = connection_obj.rest_request_sync(connection_obj.rest_url + '/' + connection_obj.username + '/'); |
224 |
var homepage_json = JSON.parse(req.responseText);
|
225 |
|
226 |
this.fileroot = homepage_json.fileroot + '/'; |
227 |
this.trash = homepage_json.trash + '/'; |
228 |
this.shared = homepage_json.shared + '/'; |
229 |
this.others = homepage_json.others + '/'; |
230 |
this.tags = homepage_json.tags + '/'; |
231 |
this.groups = homepage_json.groups + '/'; |
232 |
this.creationdate = homepage_json.creationDate;
|
233 |
this.modificationDate = homepage_json.modificationDate;
|
234 |
this.totalFiles = homepage_json.quota.totalFiles;
|
235 |
this.totalBytes = homepage_json.quota.totalBytes;
|
236 |
this.bytesRemaining = homepage_json.quota.bytesRemaining;
|
237 |
this.announcement = homepage_json.announcement;
|
238 |
|
239 |
$mb = jQuery.noConflict();
|
240 |
$mb('#label-total-files').val(this.totalFiles + ' files'); |
241 |
$mb('#label-used-quota').val(Math.round(((this.totalBytes) / 1024) / 1024) + ' MB used'); |
242 |
$mb('#label-free-quota').val(Math.round((this.bytesRemaining / 1024) / 1024) + ' MB free'); |
243 |
$mb("#label-system-message").val(this.announcement); |
244 |
} |
245 |
|
246 |
/*
|
247 |
[{"username":"ebstest@grnet-hq.admin.grnet.gr","uri":"http://pithos.grnet.gr/pithos/rest/aaitest@grnet-hq.admin.grnet.gr/others/ebstest@grnet-hq.admin.grnet.gr"},{"username":"louridas@grnet-hq.admin.grnet.gr","uri":"http://pithos.grnet.gr/pithos/rest/aaitest@grnet-hq.admin.grnet.gr/others/louridas@grnet-hq.admin.grnet.gr"}]
|
248 |
*/
|