| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 1 | /* |
| Laurence Maultsby | ea969fd | 2016-10-31 16:32:53 -0400 | [diff] [blame] | 2 | * |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 3 | * Copyright 2016 RIFT.IO Inc |
| 4 | * |
| 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 |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | * |
| 17 | */ |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 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'; |
| 23 | |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 24 | var API_SERVER = rw.getSearchParams(window.location).api_server; |
| Bob Gallagher | 4ef8c40 | 2017-03-17 08:25:37 -0400 | [diff] [blame] | 25 | let NODE_PORT = rw.getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000); |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 26 | |
| 27 | var Utils = {}; |
| 28 | |
| 29 | Utils.DescriptorModelMeta = null; |
| 30 | |
| 31 | var INACTIVITY_TIMEOUT = 600000; |
| 32 | |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 33 | Utils.isMultiplexerLoaded = function() { |
| 34 | if (window.multiplexer) { |
| 35 | return true; |
| 36 | } |
| 37 | return false; |
| 38 | }; |
| 39 | |
| 40 | Utils.setupMultiplexClient = function() { |
| 41 | var sockjs_url = '/multiplex'; |
| 42 | |
| 43 | var sockjs = new SockJS(sockjs_url); |
| 44 | |
| 45 | var loadChecker = function() { |
| 46 | try { |
| 47 | window.multiplexer = new WebSocketMultiplex(sockjs); |
| 48 | console.log('WebSocketMultiplex loaded'); |
| 49 | } catch (e) { |
| 50 | // caught an error, retry in someTime |
| 51 | console.log('WebSocketMultiplex not loaded yet. will try again in 1 second:', e); |
| 52 | setTimeout(function() { |
| 53 | loadChecker(); |
| 54 | }, 1000); |
| 55 | } |
| 56 | } |
| 57 | loadChecker(); |
| 58 | }; |
| 59 | |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 60 | Utils.checkAndResolveSocketRequest = function(data, resolve, reject, successCallback) { |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 61 | const checker = () => { |
| 62 | if (!Utils.isMultiplexerLoaded()) { |
| 63 | setTimeout(() => { |
| 64 | checker(); |
| 65 | }, 500); |
| 66 | } else { |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 67 | if (!successCallback) { |
| 68 | resolve(data.id); |
| 69 | } else { |
| 70 | //resolve handled in callback |
| 71 | successCallback(data.id) |
| 72 | } |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | checker(); |
| 77 | }; |
| 78 | |
| 79 | Utils.bootstrapApplication = function() { |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 80 | return new Promise((resolve, reject) => { |
| 81 | Promise.all([appConfiguration.get()]).then(function(results) { |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 82 | INACTIVITY_TIMEOUT = results[0]['inactivity-timeout']; |
| 83 | resolve(); |
| 84 | }, function(error) { |
| 85 | console.log("Error bootstrapping application ", error); |
| 86 | reject(); |
| 87 | }); |
| 88 | }); |
| 89 | }; |
| 90 | |
| 91 | Utils.getDescriptorModelMeta = function() { |
| 92 | return new Promise(function(resolve, reject) { |
| 93 | if (!Utils.DescriptorModelMeta) { |
| 94 | $.ajax({ |
| 95 | url: '/descriptor-model-meta?api_server=' + API_SERVER, |
| 96 | type: 'GET', |
| 97 | beforeSend: Utils.addAuthorizationStub, |
| 98 | success: function(data) { |
| 99 | Utils.DescriptorModelMeta = data; |
| 100 | Utils.DescriptorModelMetaLoaded = true; |
| 101 | resolve(data); |
| 102 | }, |
| 103 | error: function(error) { |
| 104 | console.log("There was an error getting the schema: ", error); |
| 105 | reject(error); |
| 106 | } |
| 107 | }).fail(function(xhr) { |
| 108 | console.log("There was an error getting the schema: ", xhr); |
| 109 | Utils.checkAuthentication(xhr.status); |
| 110 | }); |
| 111 | } else { |
| 112 | resolve(Utils.DescriptorModelMeta); |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | Utils.addAuthorizationStub = function(xhr) { |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 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); |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 121 | }; |
| 122 | |
| 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']; |
| 143 | } else { |
| 144 | return [(number / Math.pow(10, 27)).toPrecision(toPrecision), 'YB']; |
| 145 | } |
| 146 | } |
| 147 | |
| 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']; |
| 168 | } else { |
| 169 | return [(number / Math.pow(10, 27)).toPrecision(toPrecision), 'YP']; |
| 170 | } |
| 171 | } |
| 172 | Utils.loginHash = "#/login"; |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 173 | |
| 174 | Utils.clearAuthentication = function() { |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 175 | var self = this; |
| 176 | window.sessionStorage.removeItem("auth"); |
| 177 | AuthActions.notAuthenticated(); |
| 178 | window.sessionStorage.setItem("locationRefHash", window.location.hash); |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 179 | var reloadURL = ''; |
| 180 | $.ajax({ |
| 181 | url: '//' + window.location.hostname + ':' + window.location.port + '/session?api_server=' + API_SERVER + '&hash=' + encodeURIComponent(window.location.hash), |
| 182 | type: 'DELETE', |
| 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; |
| 190 | |
| 191 | window.location.replace(reloadURL); |
| 192 | }, |
| 193 | error: function(data) { |
| 194 | console.log('Problem logging user out'); |
| 195 | } |
| 196 | }); |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 197 | } |
| 198 | Utils.isNotAuthenticated = function(windowLocation, callback) { |
| 199 | var self = this; |
| 200 | self.detectInactivity(); |
| 201 | if (!window.sessionStorage.getItem("auth")) { |
| 202 | Utils.clearAuthentication(); |
| 203 | } |
| 204 | } |
| 205 | Utils.isDetecting = false; |
| 206 | Utils.detectInactivity = function(callback, duration) { |
| 207 | var self = this; |
| 208 | if (!self.isDetecting) { |
| 209 | var cb = function() { |
| 210 | self.clearAuthentication(); |
| 211 | if (callback) { |
| 212 | callback(); |
| 213 | } |
| 214 | }; |
| 215 | var isInactive; |
| 216 | var timeout = duration || INACTIVITY_TIMEOUT; |
| 217 | var setInactive = function() { |
| 218 | isInactive = setTimeout(cb, timeout); |
| 219 | }; |
| 220 | var reset = function() { |
| 221 | clearTimeout(isInactive); |
| 222 | setInactive(); |
| 223 | } |
| 224 | setInactive(); |
| 225 | window.addEventListener('mousemove', reset); |
| 226 | window.addEventListener("keypress", reset); |
| 227 | self.isDetecting = true; |
| 228 | } |
| 229 | } |
| 230 | Utils.checkAuthentication = function(statusCode, cb) { |
| 231 | var self = this; |
| 232 | if (statusCode == 401) { |
| 233 | if (cb) { |
| 234 | cb(); |
| 235 | } |
| 236 | window.sessionStorage.removeItem("auth") |
| 237 | self.isNotAuthenticated(window.location) |
| 238 | return true; |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | Utils.isAuthenticationCached = function() { |
| 244 | var self = this; |
| 245 | if (window.sessionStorage.getItem("auth")) { |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | Utils.getHostNameFromURL = function(url) { |
| 252 | var match = url.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([^?#]*)(\?[^#]*|)(#.*|)$/); |
| 253 | return match && match[3]; |
| 254 | } |
| 255 | |
| 256 | Utils.webSocketProtocol = function() { |
| 257 | if (this.wsProto) { |
| 258 | return this.wsProto; |
| 259 | } else { |
| 260 | if (window.location.protocol == 'http:') { |
| 261 | this.wsProto = 'ws:' |
| 262 | } else { |
| 263 | this.wsProto = 'wss:' |
| 264 | } |
| 265 | } |
| 266 | return this.wsProto; |
| 267 | } |
| 268 | |
| 269 | Utils.arrayIntersperse = (arr, sep) => { |
| 270 | if (arr.length === 0) { |
| 271 | return []; |
| 272 | } |
| 273 | |
| 274 | return arr.slice(1).reduce((xs, x, i) => { |
| 275 | return xs.concat([sep, x]); |
| 276 | }, [arr[0]]); |
| 277 | } |
| 278 | |
| KIRAN KASHALKAR | f07c844 | 2016-11-15 17:58:11 -0500 | [diff] [blame] | 279 | Utils.cleanImageDataURI = (imageString, type, id) => { |
| 280 | if (/\bbase64\b/g.test(imageString)) { |
| 281 | return 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); |
| 288 | } |
| 289 | if(type == 'nsd' || type == 'vnfd') { |
| 290 | return require('style/img/catalog-'+type+'-default.svg'); |
| 291 | } |
| 292 | return require('style/img/catalog-default.svg'); |
| 293 | } |
| 294 | |
| Laurence Maultsby | 1fbd777 | 2017-05-17 11:26:53 -0400 | [diff] [blame] | 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']; |
| 301 | } |
| 302 | } |
| 303 | return displayMsg |
| 304 | } |
| 305 | |
| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 306 | Utils.rpcError = (rpcResult) => { |
| 307 | try { |
| 308 | let info = JSON.parse(rpcResult); |
| 309 | let rpcError = info.body || info.errorMessage.body || info.errorMessage.error; |
| 310 | if (rpcError) { |
| 311 | if (typeof rpcError === 'string') { |
| 312 | const index = rpcError.indexOf('{'); |
| 313 | if (index >= 0) { |
| 314 | return JSON.parse(rpcError.substr(index)); |
| 315 | } |
| 316 | } else { |
| 317 | return rpcError; |
| 318 | } |
| 319 | } |
| 320 | } catch (e) { |
| 321 | } |
| 322 | console.log('invalid rpc error: ', rpcResult); |
| 323 | return null; |
| 324 | } |
| 325 | |
| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 326 | module.exports = Utils; |