3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import AuthActions
from '../widgets/login/loginAuthActions';
19 import $ from 'jquery';
20 import rw
from './rw';
21 import appConfiguration
from './appConfiguration'
22 import SockJS
from 'sockjs-client';
24 var API_SERVER
= rw
.getSearchParams(window
.location
).api_server
;
25 let NODE_PORT
= rw
.getSearchParams(window
.location
).api_port
|| ((window
.location
.protocol
== 'https:') ? 8443 : 8000);
29 Utils
.DescriptorModelMeta
= null;
31 var INACTIVITY_TIMEOUT
= 600000;
33 Utils
.isMultiplexerLoaded = function() {
34 if (window
.multiplexer
) {
40 Utils
.setupMultiplexClient = function() {
41 var sockjs_url
= '/multiplex';
43 var sockjs
= new SockJS(sockjs_url
);
45 var loadChecker = function() {
47 window
.multiplexer
= new WebSocketMultiplex(sockjs
);
48 console
.log('WebSocketMultiplex loaded');
50 // caught an error, retry in someTime
51 console
.log('WebSocketMultiplex not loaded yet. will try again in 1 second:', e
);
52 setTimeout(function() {
60 Utils
.checkAndResolveSocketRequest = function(data
, resolve
, reject
, successCallback
) {
61 const checker
= () => {
62 if (!Utils
.isMultiplexerLoaded()) {
67 if (!successCallback
) {
70 //resolve handled in callback
71 successCallback(data
.id
)
79 Utils
.bootstrapApplication = function() {
80 return new Promise((resolve
, reject
) => {
81 Promise
.all([appConfiguration
.get()]).then(function(results
) {
82 INACTIVITY_TIMEOUT
= results
[0]['inactivity-timeout'];
85 console
.log("Error bootstrapping application ", error
);
91 Utils
.getDescriptorModelMeta = function() {
92 return new Promise(function(resolve
, reject
) {
93 if (!Utils
.DescriptorModelMeta
) {
95 url
: '/descriptor-model-meta?api_server=' + API_SERVER
,
97 beforeSend
: Utils
.addAuthorizationStub
,
98 success: function(data
) {
99 Utils
.DescriptorModelMeta
= data
;
100 Utils
.DescriptorModelMetaLoaded
= true;
103 error: function(error
) {
104 console
.log("There was an error getting the schema: ", error
);
107 }).fail(function(xhr
) {
108 console
.log("There was an error getting the schema: ", xhr
);
109 Utils
.checkAuthentication(xhr
.status
);
112 resolve(Utils
.DescriptorModelMeta
);
117 Utils
.addAuthorizationStub = function(xhr
) {
118 // NO-OP now that we are dealing with it on the server
119 // var Auth = window.sessionStorage.getItem("auth");
120 // xhr.setRequestHeader('Authorization', 'Basic ' + Auth);
123 Utils
.getByteDataWithUnitPrefix = function(number
, precision
) {
124 var toPrecision
= precision
|| 3;
125 if (number
< Math
.pow(10, 3)) {
126 return [number
, 'B'];
127 } else if (number
< Math
.pow(10, 6)) {
128 return [(number
/ Math
.pow(10, 3)).toPrecision(toPrecision
), 'KB'];
129 } else if (number
< Math
.pow(10, 9)) {
130 return [(number
/ Math
.pow(10, 6)).toPrecision(toPrecision
), 'MB'];
131 } else if (number
< Math
.pow(10, 12)) {
132 return [(number
/ Math
.pow(10, 9)).toPrecision(toPrecision
), 'GB'];
133 } else if (number
< Math
.pow(10, 15)) {
134 return [(number
/ Math
.pow(10, 12)).toPrecision(toPrecision
), 'TB'];
135 } else if (number
< Math
.pow(10, 18)) {
136 return [(number
/ Math
.pow(10, 15)).toPrecision(toPrecision
), 'PB'];
137 } else if (number
< Math
.pow(10, 21)) {
138 return [(number
/ Math
.pow(10, 18)).toPrecision(toPrecision
), 'EB'];
139 } else if (number
< Math
.pow(10, 24)) {
140 return [(number
/ Math
.pow(10, 21)).toPrecision(toPrecision
), 'ZB'];
141 } else if (number
< Math
.pow(10, 27)) {
142 return [(number
/ Math
.pow(10, 24)).toPrecision(toPrecision
), 'ZB'];
144 return [(number
/ Math
.pow(10, 27)).toPrecision(toPrecision
), 'YB'];
148 Utils
.getPacketDataWithUnitPrefix = function(number
, precision
) {
149 var toPrecision
= precision
|| 3;
150 if (number
< Math
.pow(10, 3)) {
151 return [number
, 'P'];
152 } else if (number
< Math
.pow(10, 6)) {
153 return [(number
/ Math
.pow(10, 3)).toPrecision(toPrecision
), 'KP'];
154 } else if (number
< Math
.pow(10, 9)) {
155 return [(number
/ Math
.pow(10, 6)).toPrecision(toPrecision
), 'MP'];
156 } else if (number
< Math
.pow(10, 12)) {
157 return [(number
/ Math
.pow(10, 9)).toPrecision(toPrecision
), 'GP'];
158 } else if (number
< Math
.pow(10, 15)) {
159 return [(number
/ Math
.pow(10, 12)).toPrecision(toPrecision
), 'TP'];
160 } else if (number
< Math
.pow(10, 18)) {
161 return [(number
/ Math
.pow(10, 15)).toPrecision(toPrecision
), 'PP'];
162 } else if (number
< Math
.pow(10, 21)) {
163 return [(number
/ Math
.pow(10, 18)).toPrecision(toPrecision
), 'EP'];
164 } else if (number
< Math
.pow(10, 24)) {
165 return [(number
/ Math
.pow(10, 21)).toPrecision(toPrecision
), 'ZP'];
166 } else if (number
< Math
.pow(10, 27)) {
167 return [(number
/ Math
.pow(10, 24)).toPrecision(toPrecision
), 'ZP'];
169 return [(number
/ Math
.pow(10, 27)).toPrecision(toPrecision
), 'YP'];
172 Utils
.loginHash
= "#/login";
174 Utils
.clearAuthentication = function() {
176 window
.sessionStorage
.removeItem("auth");
177 AuthActions
.notAuthenticated();
178 window
.sessionStorage
.setItem("locationRefHash", window
.location
.hash
);
181 url
: '//' + window
.location
.hostname
+ ':' + window
.location
.port
+ '/session?api_server=' + API_SERVER
+ '&hash=' + encodeURIComponent(window
.location
.hash
),
183 success: function(data
) {
184 console
.log('User logged out');
185 reloadURL
= data
['url'] + '?post_logout_redirect_uri=' +
186 window
.location
.protocol
+ '//' +
187 window
.location
.hostname
+ ':' +
188 window
.location
.port
+
189 '/?api_server=' + API_SERVER
;
191 window
.location
.replace(reloadURL
);
193 error: function(data
) {
194 console
.log('Problem logging user out');
198 Utils
.isNotAuthenticated = function(windowLocation
, callback
) {
200 self
.detectInactivity();
201 if (!window
.sessionStorage
.getItem("auth")) {
202 Utils
.clearAuthentication();
205 Utils
.isDetecting
= false;
206 Utils
.detectInactivity = function(callback
, duration
) {
208 if (!self
.isDetecting
) {
209 var cb = function() {
210 self
.clearAuthentication();
216 var timeout
= duration
|| INACTIVITY_TIMEOUT
;
217 var setInactive = function() {
218 isInactive
= setTimeout(cb
, timeout
);
220 var reset = function() {
221 clearTimeout(isInactive
);
225 window
.addEventListener('mousemove', reset
);
226 window
.addEventListener("keypress", reset
);
227 self
.isDetecting
= true;
230 Utils
.checkAuthentication = function(statusCode
, cb
) {
232 if (statusCode
== 401) {
236 window
.sessionStorage
.removeItem("auth")
237 self
.isNotAuthenticated(window
.location
)
243 Utils
.isAuthenticationCached = function() {
245 if (window
.sessionStorage
.getItem("auth")) {
251 Utils
.getHostNameFromURL = function(url
) {
252 var match
= url
.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([^?#]*)(\?[^#]*|)(#.*|)$/);
253 return match
&& match
[3];
256 Utils
.webSocketProtocol = function() {
260 if (window
.location
.protocol
== 'http:') {
263 this.wsProto
= 'wss:'
269 Utils
.arrayIntersperse
= (arr
, sep
) => {
270 if (arr
.length
=== 0) {
274 return arr
.slice(1).reduce((xs
, x
, i
) => {
275 return xs
.concat([sep
, x
]);
279 Utils
.cleanImageDataURI
= (imageString
, type
, id
) => {
280 if (/\bbase64\b/g.test(imageString
)) {
282 } else if (/<\?xml\b/g.test(imageString
)) {
283 const imgStr
= imageString
.substring(imageString
.indexOf('<?xml'));
284 return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(imgStr
);
285 } else if (/\.(svg|png|gif|jpeg|jpg)$/.test(imageString
)) {
286 return '/composer/assets/logos/' + type
+ '/' + id
+ '/' + imageString
;
287 // return require('../images/logos/' + imageString);
289 if(type
== 'nsd' || type
== 'vnfd') {
290 return require('style/img/catalog-'+type
+'-default.svg');
292 return require('style/img/catalog-default.svg');
295 Utils
.parseError
= (error
) => {
296 let displayMsg
= JSON
.parse(error
);
297 if (displayMsg
.errorMessage
&& displayMsg
.errorMessage
.body
) {
298 displayMsg
= displayMsg
.errorMessage
.body
;
299 if(displayMsg
['last-error'] && displayMsg
['last-error']['rpc-error'] && displayMsg
['last-error']['rpc-error']['error-message']) {
300 displayMsg
= displayMsg
['last-error']['rpc-error']['error-message'];
306 Utils
.rpcError
= (rpcResult
) => {
308 let info
= JSON
.parse(rpcResult
);
309 let rpcError
= info
.body
|| info
.errorMessage
.body
|| info
.errorMessage
.error
;
311 if (typeof rpcError
=== 'string') {
312 const index
= rpcError
.indexOf('{');
314 return JSON
.parse(rpcError
.substr(index
));
322 console
.log('invalid rpc error: ', rpcResult
);
326 module
.exports
= Utils
;