-
Notifications
You must be signed in to change notification settings - Fork 2
/
dcache-namespace.html
495 lines (453 loc) · 15.6 KB
/
dcache-namespace.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../promise-polyfill/promise-polyfill-lite.html">
<link rel="import" href="../util-precondition/preconditionjs-import.html">
<!--
`dcache-namespace`
dcache-namespace can be used to perform an ajax call to create a new directory,
move or rename a file using dcache restful-api.
### usage:
```html
<dcache-namespace id="namespace" auth="..."></dcache-namespace>
```
...
```javascript
const url = ...;
const path = ...;
const dest = ...;
this.$.namespace.mv({url: url, path: path, destination: dest});
```
_A model adapted from iron-request element of Google Polymer._
@demo demo/index.html
-->
<dom-module id="dcache-namespace">
<script>
class DcacheNamespace extends Polymer.Element
{
static get is()
{
return "dcache-namespace";
}
static get properties()
{
return {
/**
* A reference to the XMLHttpRequest instance used to generate the
* network request.
*
* @type {XMLHttpRequest}
*/
xhr: {
type: Object,
notify: true,
readOnly: true,
value: function() {
return new XMLHttpRequest();
}
},
/**
* A reference to the parsed response body, if the `xhr` has completely
* resolved.
*
* @type {*}
* @default null
*/
response: {
type: Object,
notify: true,
readOnly: true,
value: function() {
return null;
}
},
/**
* A reference to the status code, if the `xhr` has completely resolved.
*/
status: {
type: Number,
notify: true,
readOnly: true,
value: 0
},
/**
* A promise that resolves when the `xhr` response comes back, or rejects
* if there is an error before the `xhr` completes.
*
* @type {Promise}
*/
promise: {
type: Object,
readOnly: true,
notify: true,
value: function() {
return new Promise(function (resolve, reject) {
this.resolvePromise = resolve;
this.rejectPromise = reject;
}.bind(this));
},
},
/**
* Errored will be true if the browser fired an error event from the
* XHR object (mainly network errors).
*/
errored: {
type: Boolean,
notify: true,
readOnly: true,
value: false
},
/**
*
* @type {String}
* @default {window.btoa(nobody:nopassword)}
*/
auth: {
type: String,
value: 'YW5vbnltb3VzOm5vcGFzc3dvcmQ='
}
}
}
ready()
{
super.ready();
this._ensureAttribute('hidden', true);
}
/**
* Succeeded is true if the request succeeded. The request succeeded if it
* loaded without error, wasn't aborted, and the status code is ≥ 200, and
* < 300.
*
* @return {boolean}
*/
get succeeded()
{
if (this.errored) {
return false;
}
const status = this.xhr.status;
if (status >= 200 && status < 300) {
return true;
} else {
return false;
}
}
/*
* @return {Promise}
*/
_send(options, action)
{
const xhr = this.xhr;
const auth = this.auth;
let body;
if (action == 'set-label') {
body = '{"action": "set-label", "label": "'+ options.label +'"}';
}
if (action == 'rm-label') {
body = '{"action": "rm-label", "label": "'+ options.label +'"}';
}
if (action == 'mkdir') {
body = '{"action": "mkdir", "name": "'+ options.name +'"}';
} else if (action == 'mv') {
body = '{"action": "mv", "destination": "'+ options.destination +'"}';
} else if (action == 'delete') {
body = null;
options.method = 'DELETE';
} else if (action == "qos") {
body = '{"action": "qos", "target": "'+ options.target +'"}';
} else if (action == "getqos") {
body = null;
}
xhr.open(
options.method || 'POST',
options.url,
true
);
xhr.responseType = 'json';
xhr.addEventListener('error', function (error) {
this._setErrored(true);
this._updateStatus();
this.rejectPromise(error);
}.bind(this));
xhr.addEventListener('loadend', function () {
this._updateStatus();
this._setResponse(this.parseResponse());
if (!this.succeeded) {
this.rejectPromise(new Error(`Error! ${this.xhr.response.errors[0].status}:
${this.xhr.response.errors[0].message}.`));
return;
}
this.resolvePromise(this);
}.bind(this));
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Authorization", auth);
xhr.setRequestHeader("Suppress-WWW-Authenticate", "suppressed");
xhr.setRequestHeader("Accept", "application/json");
xhr.send(body);
return this.promise;
}
/*
* ### Performs a request to add a new label to a file.
* @param {{
* options: (object)}} options -
* `url`: (String) The url with the dcache restful-api path
* `path`: (String) The full path to where of a file
* `label`: (String) Label valued.
* */
setlabel(options) {
try {
precondition.checkArgument(options.path != undefined || options.label != null,
"Please provide the value of the label.");
let opt = {};
opt.url = this._appendFullPathToUrl(options.url, options.path);
opt.label = options.label;
this._send(opt, "set-label");
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs a request to remove the label from a file.
* @param {{
* options: (object)}} options -
* `url`: (String) The url with the dcache restful-api path
* `path`: (String) The full path to where of a file
* `label`: (String) Label valued.
* */
removelabel(options) {
try {
precondition.checkArgument(options.path != undefined || options.label != null,
"Please provide the value of the label.");
let opt = {};
opt.url = this._appendFullPathToUrl(options.url, options.path);
opt.label = options.label;
this._send(opt, "rm-label");
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs an AJAX request to create a new folder.
*
* @param {{
* options: (object)}} options -
* `url`: The url of dcache restful-api.
*
* `path`: (String) The full path to where the new directory
* should be created. This must be url-encoded.
*
* `name`: The name of the new directory.
*
*/
mkdir (options)
{
try {
precondition.checkArgument(options.name!=undefined || options.name!=null,
"Please provide the name of the new directory.");
precondition.checkArgument(options.name.trim().length!=0, "Directory name cannot be empty.");
let opt = {};
opt.url = this._appendFullPathToUrl(options.url, options.path);
opt.name = options.name;
this._send(opt, "mkdir");
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs an AJAX request to rename or move a file.
*
* @param {{
* options: (object)}} options -
* `url`: (String) The url with the dcache restful-api path
*
* `path`: (String) The full path to where the file you want to rename or
* move is located. This must be url-encoded.
*
* `destination`: (String) Specify the new name of the file or the full path
* to where the file should be moved.
*
*/
mv(options)
{
try {
precondition.checkArgument(options.destination!=undefined || options.destination!=null,
"Please provide the destination.");
precondition.checkArgument(options.destination.trim().length!=0, "Destination cannot be empty.");
let opt = {};
opt.url = this._appendFullPathToUrl(options.url, options.path);
opt.destination = options.destination;
this._send(opt, "mv");
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs an AJAX request to delete a file.
*
* @param {{
* options: (object)}} options -
* `url`: The url with the dcache restful-api path
*
* `path`: The full path to where the file you want to delete is
* located. This must be url-encoded.
*/
rm(options)
{
try {
let opt = {};
opt.url = this._appendFullPathToUrl(options.url, options.path);
this._send(opt, "delete");
} catch(e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs an AJAX request to get the metadata of a file.
*
* @param {{
* options: (object)}} options -
* `url`: (String) The url of dcache restful-api.
*
* `path`: (String) The path to the file you want to get it metadata.
* This must be url-encoded.
*
* `children`: (Boolean) Specify whether to list the children of the specified
* file path. Default to false
*
* `locality`: (Boolean) If true, the locality of the file children will be
* added to the returned file metadata. Default to false
*
* `limit`: (Number) Default to null
*
* `offset`: (Number) Default to null
*
*/
ls(options)
{
//TODO: Factor this out.
try {
precondition.checkArgument(options.url!=undefined || options.url!=null,
"Please specify the url.");
precondition.checkArgument((options.path!=undefined || options.path!=null)
&& options.path.startsWith('/'), "Please make sure that the path start with forward slash.");
let query = '';
query = options.path == null ? '/': options.path;
query = query.endsWith('/') ? query : query + '/';
query = options.children == true ? query + '?children=true': query + '?children=false';
query = options.locality == true ? query + '&locality=true': query + '&locality=false';
query = options.limit == null ? query: query+'&limit='+options.limit;
query = options.offset == null ? query: query+'&offset='+options.offset;
let url = options.url.endsWith('/') ?
options.url+'api/v1/namespace'+query : options.url+'/api/v1/namespace'+query;
let opt = {method:'GET', url: url}
this._send(opt, 'ls');
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs an AJAX request to modify the qos of a file.
*
* @param {{
* options: (object)}} options -
* `url`: (String) The url of dcache restful-api with the path to where
* the file in which you want to modify is qos.
*
* `path`: (String) The full path of the file that will be modified.
* This must be url-encoded.
*
* `target`: (String) The qos target to modify to.
*/
qos(options)
{
try {
precondition.checkArgument(options.target!=undefined || options.target!=null,
"Please specify the target.");
precondition.checkArgument(options.target.trim().length!==0, "The target cannot be empty.");
let opt = {};
opt.url = this._appendFullPathToUrl(options.url, options.path);
opt.target = options.target;
this._send(opt, 'qos');
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/*
* ### Performs an AJAX request to modify the qos of a file.
*
* @param {{
* options: (object)}} options -
* `url`: (String) The url of dcache restful-api with the path to where
* the file in which you want to get is current qos.
*
* `path`: (String) The full path of the file that the current qos will
* will be returned. This must be url-encoded.
*
*/
getqos(options)
{
try {
let opt = {};
opt.method = 'GET';
opt.url = this._appendFullPathToUrl(options.url, options.path) +"?qos=true";
this._send(opt, 'qos');
} catch (e) {
this.rejectPromise(new Error("Could not proceed with the request. " + e.message));
}
}
/**
* Attempts to parse the response body of the XHR. If parsing succeeds,
* the value returned will be deserialized based on the `responseType`
* set on the XHR.
*
* @return {*} The parsed response,
* or undefined if there was an empty response or parsing failed.
*/
parseResponse()
{
const xhr = this.xhr;
const responseType = xhr.responseType || xhr._responseType;
const preferResponseText = !this.xhr.responseType;
try {
if (preferResponseText || xhr.response === undefined) {
try {
return JSON.parse(xhr.responseText);
} catch (_) {
return null;
}
}
return xhr.response;
} catch (e) {
this.rejectPromise(new Error('Could not parse response. ' + e.message));
}
}
/**
* Updates the status code.
*/
_updateStatus()
{
this._setStatus(this.xhr.status);
}
/**
* append the file path to the url to form a full url.
*
* @return {String}
*/
_appendFullPathToUrl(url, path)
{
precondition.checkArgument(url!=undefined || url!=null, "Please specify the url.");
precondition.checkArgument(url.trim().length !==0, "The url cannot be empty.");
precondition.checkArgument(path!=undefined || path!=null, "Please specify the file full path.");
precondition.checkArgument(path.trim().length !==0, "The path cannot be an empty.");
let urlWithFullPath = "/";
if (url.endsWith('/') && path.startsWith('/')) {
urlWithFullPath = url + path.slice(1);
} else if (!url.endsWith('/') && !path.startsWith('/')) {
urlWithFullPath = url + "/" + path;
} else {
urlWithFullPath = url + path;
}
return urlWithFullPath;
}
}
window.customElements.define(DcacheNamespace.is, DcacheNamespace);
</script>
</dom-module>