Skip to content

Commit

Permalink
TheHive-Project#210 Fix the error handling when dealing with stream s…
Browse files Browse the repository at this point in the history
…ervice
  • Loading branch information
nadouani committed Aug 21, 2019
1 parent fc71898 commit c9bcbc4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
7 changes: 5 additions & 2 deletions app/org/thp/cortex/controllers/StreamCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -32,6 +32,7 @@ class StreamCtrl(
authenticated: Authenticated,
renderer: Renderer,
eventSrv: EventSrv,
userSrv: UserSrv,
auxSrv: AuxSrv,
migrationSrv: MigrationSrv,
components: ControllerComponents,
Expand All @@ -45,6 +46,7 @@ class StreamCtrl(
authenticated: Authenticated,
renderer: Renderer,
eventSrv: EventSrv,
userSrv: UserSrv,
auxSrv: AuxSrv,
migrationSrv: MigrationSrv,
components: ControllerComponents,
Expand All @@ -59,6 +61,7 @@ class StreamCtrl(
authenticated,
renderer,
eventSrv,
userSrv,
auxSrv,
migrationSrv,
components,
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

2 changes: 1 addition & 1 deletion www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@
"webpack": "^3.5.0",
"webpack-dev-server": "^2.2.0"
}
}
}
77 changes: 38 additions & 39 deletions www/src/app/core/services/common/StreamService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lo from 'lodash';
import angular from 'angular';

export default function(app) {
export default function (app) {
function StreamSrv(
$rootScope,
$http,
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -166,7 +165,7 @@ export default function(app) {
* <li>scope {Object}</li>
* <li>callback {Function}</li>
*/
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);
Expand All @@ -182,4 +181,4 @@ export default function(app) {
}

app.service('StreamSrv', StreamSrv);
}
}
7 changes: 4 additions & 3 deletions www/src/app/pages/main/main.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ const mainPageModule = angular
url: '/',
component: 'mainPage',
resolve: {
currentUser: ($q, $state, AuthService) => {
currentUser: ($q, AuthService, NotificationService) => {
'ngInject';

let deferred = $q.defer();

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;
Expand All @@ -55,4 +56,4 @@ const mainPageModule = angular
}
});

export default mainPageModule;
export default mainPageModule;

0 comments on commit c9bcbc4

Please sign in to comment.