Skip to content

Commit c9bcbc4

Browse files
committed
#210 Fix the error handling when dealing with stream service
1 parent fc71898 commit c9bcbc4

5 files changed

Lines changed: 49 additions & 46 deletions

File tree

app/org/thp/cortex/controllers/StreamCtrl.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.thp.cortex.services.StreamActor.StreamMessages
2121

2222
import org.elastic4play.Timed
2323
import org.elastic4play.controllers._
24-
import org.elastic4play.services.{AuxSrv, EventSrv, MigrationSrv}
24+
import org.elastic4play.services.{AuxSrv, EventSrv, MigrationSrv, UserSrv}
2525

2626
@Singleton
2727
class StreamCtrl(
@@ -32,6 +32,7 @@ class StreamCtrl(
3232
authenticated: Authenticated,
3333
renderer: Renderer,
3434
eventSrv: EventSrv,
35+
userSrv: UserSrv,
3536
auxSrv: AuxSrv,
3637
migrationSrv: MigrationSrv,
3738
components: ControllerComponents,
@@ -45,6 +46,7 @@ class StreamCtrl(
4546
authenticated: Authenticated,
4647
renderer: Renderer,
4748
eventSrv: EventSrv,
49+
userSrv: UserSrv,
4850
auxSrv: AuxSrv,
4951
migrationSrv: MigrationSrv,
5052
components: ControllerComponents,
@@ -59,6 +61,7 @@ class StreamCtrl(
5961
authenticated,
6062
renderer,
6163
eventSrv,
64+
userSrv,
6265
auxSrv,
6366
migrationSrv,
6467
components,
@@ -94,7 +97,7 @@ class StreamCtrl(
9497
Future.successful(BadRequest("Invalid stream id"))
9598
} else {
9699
val futureStatus = authenticated.expirationStatus(request) match {
97-
case ExpirationError if !migrationSrv.isMigrating authenticated.getFromApiKey(request).map(_ OK)
100+
case ExpirationError if !migrationSrv.isMigrating userSrv.getInitialUser(request).recoverWith { case _ => authenticated.getFromApiKey(request)}.map(_ OK)
98101
case _: ExpirationWarning Future.successful(220)
99102
case _ Future.successful(OK)
100103
}

project/Dependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Dependencies {
1818

1919
val reflections = "org.reflections" % "reflections" % "0.9.11"
2020
val zip4j = "net.lingala.zip4j" % "zip4j" % "1.3.2"
21-
val elastic4play = "org.thehive-project" %% "elastic4play" % "1.11.4"
21+
val elastic4play = "org.thehive-project" %% "elastic4play" % "1.11.5-SNAPSHOT"
2222
val dockerClient = "com.spotify" % "docker-client" % "8.14.4"
2323
}
2424

www/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@
7575
"webpack": "^3.5.0",
7676
"webpack-dev-server": "^2.2.0"
7777
}
78-
}
78+
}

www/src/app/core/services/common/StreamService.js

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lo from 'lodash';
44
import angular from 'angular';
55

6-
export default function(app) {
6+
export default function (app) {
77
function StreamSrv(
88
$rootScope,
99
$http,
@@ -15,15 +15,15 @@ export default function(app) {
1515
this.isPolling = false;
1616
this.streamId = null;
1717

18-
this.init = function() {
18+
this.init = function () {
1919
this.streamId = null;
2020
this.requestStream();
2121
};
22-
this.runCallbacks = function(id, objectType, message) {
22+
this.runCallbacks = function (id, objectType, message) {
2323
$rootScope.$broadcast('stream:' + id + '-' + objectType, message);
2424
};
2525

26-
this.handleStreamResponse = function(data) {
26+
this.handleStreamResponse = function (data) {
2727
if (!data || data.length === 0) {
2828
return;
2929
}
@@ -37,9 +37,8 @@ export default function(app) {
3737
let rootId = message.base.rootId;
3838
let objectType = message.base.objectType;
3939
let rootIdWithObjectType = rootId + '|' + objectType;
40-
let secondaryObjectTypes = message.summary
41-
? lo.without(lo.keys(message.summary), objectType)
42-
: [];
40+
let secondaryObjectTypes = message.summary ?
41+
lo.without(lo.keys(message.summary), objectType) : [];
4342

4443
if (rootId in byRootIds) {
4544
byRootIds[rootId].push(message);
@@ -94,7 +93,7 @@ export default function(app) {
9493
this.runCallbacks('any', 'any', data);
9594
};
9695

97-
this.poll = function() {
96+
this.poll = function () {
9897
// Skip polling is a poll is already running
9998
if (this.streamId === null || this.isPolling === true) {
10099
return;
@@ -104,46 +103,46 @@ export default function(app) {
104103
this.isPolling = true;
105104

106105
// Poll stream changes
107-
$http.get('./api/stream/' + this.streamId).then(
108-
response => {
109-
// Flag polling end
110-
this.isPolling = false;
111-
112-
// Handle stream data and callbacks
113-
this.handleStreamResponse(response.data);
114-
115-
// Check if the session will expire soon
116-
// TODO
117-
// if (status === 220) {
118-
// AfkSrv.prompt().then(function () {
119-
// UserSrv.getUserInfo(AuthService.currentUser.id).then(
120-
// function () {},
121-
// function (response) {
122-
// NotificationService.error('StreamSrv', response.data, response.status);
123-
// }
124-
// );
125-
// });
126-
// }
127-
this.poll();
128-
},
129-
(data, status) => {
106+
$http.get('./api/stream/' + this.streamId)
107+
.then(
108+
response => {
109+
// Flag polling end
110+
this.isPolling = false;
111+
112+
// Handle stream data and callbacks
113+
this.handleStreamResponse(response.data);
114+
115+
// Check if the session will expire soon
116+
// TODO
117+
// if (status === 220) {
118+
// AfkSrv.prompt().then(function () {
119+
// UserSrv.getUserInfo(AuthService.currentUser.id).then(
120+
// function () {},
121+
// function (response) {
122+
// NotificationService.error('StreamSrv', response.data, response.status);
123+
// }
124+
// );
125+
// });
126+
// }
127+
this.poll();
128+
})
129+
.catch(err => {
130130
// Initialize the stream;
131131
this.isPolling = false;
132132

133-
if (status !== 404) {
134-
NotificationService.error('StreamSrv', data, status);
133+
if (err.status !== 404) {
134+
NotificationService.error('StreamSrv', err.data, err.status);
135135

136-
if (status === 401) {
136+
if (err.status === 401) {
137137
return;
138138
}
139139
}
140140

141141
this.init();
142-
}
143-
);
142+
});
144143
};
145144

146-
this.requestStream = function() {
145+
this.requestStream = function () {
147146
if (this.streamId !== null) {
148147
return;
149148
}
@@ -166,7 +165,7 @@ export default function(app) {
166165
* <li>scope {Object}</li>
167166
* <li>callback {Function}</li>
168167
*/
169-
this.addListener = function(config) {
168+
this.addListener = function (config) {
170169
if (!config.scope) {
171170
$log.error('No scope provided, use the old listen method', config);
172171
this.listen(config.rootId, config.objectType, config.callback);
@@ -182,4 +181,4 @@ export default function(app) {
182181
}
183182

184183
app.service('StreamSrv', StreamSrv);
185-
}
184+
}

www/src/app/pages/main/main.module.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ const mainPageModule = angular
2121
url: '/',
2222
component: 'mainPage',
2323
resolve: {
24-
currentUser: ($q, $state, AuthService) => {
24+
currentUser: ($q, AuthService, NotificationService) => {
2525
'ngInject';
2626

2727
let deferred = $q.defer();
2828

2929
AuthService.current()
3030
.then(userData => deferred.resolve(userData))
3131
.catch(err => {
32-
deferred.reject(err);
32+
NotificationService.handleError('Application', err.data, err.status);
33+
return deferred.reject(err)
3334
});
3435

3536
return deferred.promise;
@@ -55,4 +56,4 @@ const mainPageModule = angular
5556
}
5657
});
5758

58-
export default mainPageModule;
59+
export default mainPageModule;

0 commit comments

Comments
 (0)