From c9bcbc48d1cc67c9134b1ba5556eac960e8d92e1 Mon Sep 17 00:00:00 2001 From: Nabil Adouani Date: Wed, 21 Aug 2019 16:16:00 +0200 Subject: [PATCH] #210 Fix the error handling when dealing with stream service --- .../thp/cortex/controllers/StreamCtrl.scala | 7 +- project/Dependencies.scala | 2 +- www/package.json | 2 +- .../app/core/services/common/StreamService.js | 77 +++++++++---------- www/src/app/pages/main/main.module.js | 7 +- 5 files changed, 49 insertions(+), 46 deletions(-) diff --git a/app/org/thp/cortex/controllers/StreamCtrl.scala b/app/org/thp/cortex/controllers/StreamCtrl.scala index e5aab089c..2059af71f 100644 --- a/app/org/thp/cortex/controllers/StreamCtrl.scala +++ b/app/org/thp/cortex/controllers/StreamCtrl.scala @@ -21,7 +21,7 @@ import org.thp.cortex.services.StreamActor.StreamMessages import org.elastic4play.Timed import org.elastic4play.controllers._ -import org.elastic4play.services.{AuxSrv, EventSrv, MigrationSrv} +import org.elastic4play.services.{AuxSrv, EventSrv, MigrationSrv, UserSrv} @Singleton class StreamCtrl( @@ -32,6 +32,7 @@ class StreamCtrl( authenticated: Authenticated, renderer: Renderer, eventSrv: EventSrv, + userSrv: UserSrv, auxSrv: AuxSrv, migrationSrv: MigrationSrv, components: ControllerComponents, @@ -45,6 +46,7 @@ class StreamCtrl( authenticated: Authenticated, renderer: Renderer, eventSrv: EventSrv, + userSrv: UserSrv, auxSrv: AuxSrv, migrationSrv: MigrationSrv, components: ControllerComponents, @@ -59,6 +61,7 @@ class StreamCtrl( authenticated, renderer, eventSrv, + userSrv, auxSrv, migrationSrv, components, @@ -94,7 +97,7 @@ class StreamCtrl( Future.successful(BadRequest("Invalid stream id")) } else { val futureStatus = authenticated.expirationStatus(request) match { - case ExpirationError if !migrationSrv.isMigrating ⇒ authenticated.getFromApiKey(request).map(_ ⇒ OK) + case ExpirationError if !migrationSrv.isMigrating ⇒ userSrv.getInitialUser(request).recoverWith { case _ => authenticated.getFromApiKey(request)}.map(_ ⇒ OK) case _: ExpirationWarning ⇒ Future.successful(220) case _ ⇒ Future.successful(OK) } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index c6f1b3467..b8337e407 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -18,7 +18,7 @@ object Dependencies { val reflections = "org.reflections" % "reflections" % "0.9.11" val zip4j = "net.lingala.zip4j" % "zip4j" % "1.3.2" - val elastic4play = "org.thehive-project" %% "elastic4play" % "1.11.4" + val elastic4play = "org.thehive-project" %% "elastic4play" % "1.11.5-SNAPSHOT" val dockerClient = "com.spotify" % "docker-client" % "8.14.4" } diff --git a/www/package.json b/www/package.json index 54df46419..56138bc12 100755 --- a/www/package.json +++ b/www/package.json @@ -75,4 +75,4 @@ "webpack": "^3.5.0", "webpack-dev-server": "^2.2.0" } -} \ No newline at end of file +} diff --git a/www/src/app/core/services/common/StreamService.js b/www/src/app/core/services/common/StreamService.js index 53cad16e3..7c98593c8 100644 --- a/www/src/app/core/services/common/StreamService.js +++ b/www/src/app/core/services/common/StreamService.js @@ -3,7 +3,7 @@ import lo from 'lodash'; import angular from 'angular'; -export default function(app) { +export default function (app) { function StreamSrv( $rootScope, $http, @@ -15,15 +15,15 @@ export default function(app) { this.isPolling = false; this.streamId = null; - this.init = function() { + this.init = function () { this.streamId = null; this.requestStream(); }; - this.runCallbacks = function(id, objectType, message) { + this.runCallbacks = function (id, objectType, message) { $rootScope.$broadcast('stream:' + id + '-' + objectType, message); }; - this.handleStreamResponse = function(data) { + this.handleStreamResponse = function (data) { if (!data || data.length === 0) { return; } @@ -37,9 +37,8 @@ export default function(app) { let rootId = message.base.rootId; let objectType = message.base.objectType; let rootIdWithObjectType = rootId + '|' + objectType; - let secondaryObjectTypes = message.summary - ? lo.without(lo.keys(message.summary), objectType) - : []; + let secondaryObjectTypes = message.summary ? + lo.without(lo.keys(message.summary), objectType) : []; if (rootId in byRootIds) { byRootIds[rootId].push(message); @@ -94,7 +93,7 @@ export default function(app) { this.runCallbacks('any', 'any', data); }; - this.poll = function() { + this.poll = function () { // Skip polling is a poll is already running if (this.streamId === null || this.isPolling === true) { return; @@ -104,46 +103,46 @@ export default function(app) { this.isPolling = true; // Poll stream changes - $http.get('./api/stream/' + this.streamId).then( - response => { - // Flag polling end - this.isPolling = false; - - // Handle stream data and callbacks - this.handleStreamResponse(response.data); - - // Check if the session will expire soon - // TODO - // if (status === 220) { - // AfkSrv.prompt().then(function () { - // UserSrv.getUserInfo(AuthService.currentUser.id).then( - // function () {}, - // function (response) { - // NotificationService.error('StreamSrv', response.data, response.status); - // } - // ); - // }); - // } - this.poll(); - }, - (data, status) => { + $http.get('./api/stream/' + this.streamId) + .then( + response => { + // Flag polling end + this.isPolling = false; + + // Handle stream data and callbacks + this.handleStreamResponse(response.data); + + // Check if the session will expire soon + // TODO + // if (status === 220) { + // AfkSrv.prompt().then(function () { + // UserSrv.getUserInfo(AuthService.currentUser.id).then( + // function () {}, + // function (response) { + // NotificationService.error('StreamSrv', response.data, response.status); + // } + // ); + // }); + // } + this.poll(); + }) + .catch(err => { // Initialize the stream; this.isPolling = false; - if (status !== 404) { - NotificationService.error('StreamSrv', data, status); + if (err.status !== 404) { + NotificationService.error('StreamSrv', err.data, err.status); - if (status === 401) { + if (err.status === 401) { return; } } this.init(); - } - ); + }); }; - this.requestStream = function() { + this.requestStream = function () { if (this.streamId !== null) { return; } @@ -166,7 +165,7 @@ export default function(app) { *
  • scope {Object}
  • *
  • callback {Function}
  • */ - this.addListener = function(config) { + this.addListener = function (config) { if (!config.scope) { $log.error('No scope provided, use the old listen method', config); this.listen(config.rootId, config.objectType, config.callback); @@ -182,4 +181,4 @@ export default function(app) { } app.service('StreamSrv', StreamSrv); -} +} \ No newline at end of file diff --git a/www/src/app/pages/main/main.module.js b/www/src/app/pages/main/main.module.js index 2ddf517c5..16a648741 100755 --- a/www/src/app/pages/main/main.module.js +++ b/www/src/app/pages/main/main.module.js @@ -21,7 +21,7 @@ const mainPageModule = angular url: '/', component: 'mainPage', resolve: { - currentUser: ($q, $state, AuthService) => { + currentUser: ($q, AuthService, NotificationService) => { 'ngInject'; let deferred = $q.defer(); @@ -29,7 +29,8 @@ const mainPageModule = angular AuthService.current() .then(userData => deferred.resolve(userData)) .catch(err => { - deferred.reject(err); + NotificationService.handleError('Application', err.data, err.status); + return deferred.reject(err) }); return deferred.promise; @@ -55,4 +56,4 @@ const mainPageModule = angular } }); -export default mainPageModule; +export default mainPageModule; \ No newline at end of file