-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathJSTACK.Comm.js
More file actions
234 lines (209 loc) · 9.03 KB
/
JSTACK.Comm.js
File metadata and controls
234 lines (209 loc) · 9.03 KB
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
/*
The MIT License
Copyright (c) 2012 Universidad Politecnica de Madrid
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// JSTACK Communication Module
// ---------------------------
// This module provides functions to send GET, POST, PUT and DELETE HTTP requests
// to OpenStack components. It is compatible with the token-based authentication
// proposed by Keystone.
JSTACK.Comm = (function (JS, undefined) {
"use strict";
var send, get, head, post, put, patch, del, getEndpoint, checkToken;
// Private functions
// -----------------
// Function `_send` is internally used to make detailed low-level requests
// to components.
send = function (method, url, data, token, callBackOK, callbackError, headers, skip_token) {
var xhr, body, result;
callbackError = callbackError || function(resp) {
//console.log("Error: ", resp);
};
callBackOK = callBackOK || function(resp, headers) {
//console.log("OK: ", resp, headers);
};
// This function receives a `method` that can be "GET", "POST", "PUT", or
// "DELETE". It also receives the `url` to which it has to send the request,
// the `data` to be sent, that has to be a JSON object, the ´token´ to
// authenticate the request, and success and error callbacks.
xhr = new XMLHttpRequest();
xhr.open(method, url, true);
if (token !== undefined) {
xhr.setRequestHeader('X-Auth-Token', token);
}
var hasContent = false;
var hasAccept = false;
if (headers) {
for (var head in headers) {
if (head === "Content-Type") hasContent = true;
if (head === "Accept") hasAccept = true;
xhr.setRequestHeader(head, headers[head]);
//console.log("Header set: ", head, " - ", headers[head]);
}
}
if (data && !hasContent) {
xhr.setRequestHeader("Content-Type", "application/json");
}
if (!hasAccept) {
xhr.setRequestHeader("Accept", "application/json");
}
xhr.onerror = function(error) {
callbackError({message:"Error", body:error});
}
xhr.onreadystatechange = function () {
// This resolves an error with Zombie.js
if (flag) {
return;
}
if (xhr.readyState === 4) {
flag = true;
switch (xhr.status) {
// In case of successful response it calls the `callbackOK` function.
case 100:
case 200:
case 201:
case 202:
case 203:
case 204:
case 205:
case 206:
case 207:
result = undefined;
if (xhr.responseText !== undefined && xhr.responseText !== '') {
if (xhr.getResponseHeader('content-type') === 'text/plain; charset=utf-8') {
result = xhr.responseText;
} else {
result = JSON.parse(xhr.responseText);
}
}
callBackOK(result, xhr.getAllResponseHeaders(), xhr.getResponseHeader('x-subject-token'));
break;
// In case of error it sends an error message to `callbackError`.
case 401:
if (skip_token) {
callbackError({message:xhr.status + " Error", body:xhr.responseText});
} else {
checkToken(function () {
callbackError({message:xhr.status + " Error", body:xhr.responseText});
});
}
default:
callbackError({message:xhr.status + " Error", body:xhr.responseText});
}
}
};
var flag = false;
if (data !== undefined) {
body = JSON.stringify(data);
try {
xhr.send(body);
} catch (e) {
//callbackError(e.message);
return;
}
} else {
try {
xhr.send();
} catch (e) {
//callbackError(e.message);
return;
}
}
};
// Public functions
// ----------------
// * Function *get* receives the `url`, the authentication token
// (which is optional), and callbacks. It sends a HTTP GET request,
// so it does not send any data.
get = function (url, token, callbackOK, callbackError, headers, skip_token) {
send("get", url, undefined, token, callbackOK, callbackError, headers, skip_token);
};
// * Function *head* receives the `url`, the authentication token
// (which is optional), and callbacks. It sends a HTTP HEAD request,
// so it does not send any data.
head = function (url, token, callbackOK, callbackError, headers) {
send("head", url, undefined, token, callbackOK, callbackError);
};
// * Function *post* receives the `url`, the authentication token
// (which is optional), the data to be sent (a JSON Object), and
// callbacks. It sends a HTTP POST request.
post = function (url, data, token, callbackOK, callbackError, headers) {
send("POST", url, data, token, callbackOK, callbackError);
};
// * Function *put* receives the same parameters as post. It sends
// a HTTP PUT request.
put = function (url, data, token, callbackOK, callbackError, headers) {
send("PUT", url, data, token, callbackOK, callbackError, headers);
};
// * Function *patch* receives the same parameters as post. It sends
// a HTTP PATC request.
patch = function (url, data, token, callbackOK, callbackError, headers) {
headers["Content-Type"] = 'application/openstack-images-v2.1-json-patch';
send("PATCH", url, data, token, callbackOK, callbackError, headers);
};
// * Function *del* receives the same paramaters as get. It sends a
// HTTP DELETE request.
del = function (url, token, callbackOK, callbackError, headers) {
send("DELETE", url, undefined, token, callbackOK, callbackError);
};
checkToken = function (callback) {
console.log('Unauthorize response. Checking token with Keystone ...');
JSTACK.Keystone.validatetoken(function(r) {
console.log('Valid token. Perhaps there is a issue in the service authentication');
callback();
}, function (e){
console.log('Invalid Token. Logging out... ', e);
Fiware.signOut('cloud');
});
};
getEndpoint = function (serv, region, type) {
var endpoint;
if (JSTACK.Keystone.params.version === 3) {
type = type.split('URL')[0];
for (var e in serv.endpoints) {
if ((serv.endpoints[e].region === region || serv.endpoints[e].region_id === region) &&
serv.endpoints[e].interface === type) {
endpoint = serv.endpoints[e].url;
break;
}
}
} else {
for (var e in serv.endpoints) {
if (serv.endpoints[e].region === region) {
endpoint = serv.endpoints[e][type];
break;
}
}
}
//if (!endpoint) endpoint = serv.endpoints[0][type];
return endpoint;
};
// Public Functions and Variables
// ------------------------------
// This is the list of available public functions and variables
return {
// Functions:
get : get,
head : head,
post : post,
put : put,
patch: patch,
del : del,
getEndpoint: getEndpoint
};
}(JSTACK));