Skip to content

Commit 4490a70

Browse files
author
Anders Breid
authored
Add status bar on login page, centralized status handling (#215)
* Add status bar on login page, centralized status handling - A status bar may now be shown on the login page if back end goes down - Status handling has been moved to its own file - Functionality previously handled in ajax request has now been moved to its own functions - Several new getters and setters - Moved global variables to file global-variables.js - Add message when back end status is slow to update - Better messages when failing to login
1 parent 8a93049 commit 4490a70

File tree

8 files changed

+282
-242
lines changed

8 files changed

+282
-242
lines changed

src/main/resources/static/js/common.js

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
var router = new Navigo(null, true, '#');
2-
var frontendServiceUrl = $('#frontendServiceUrl').text();
3-
var frontendServiceBackEndPath = "/backend";
4-
var timerInterval;
5-
var ldapEnabled = true;
6-
7-
// Start ## getters and setters
8-
9-
function isLdapEnabled(){
10-
return Boolean(ldapEnabled);
11-
}
12-
13-
function setLdapEnabled(value){
14-
ldapEnabled = Boolean(value);
15-
}
16-
17-
function getCurrentUser() {
18-
return sessionStorage.getItem("currentUser");
19-
}
20-
21-
function setCurrentUser(user) {
22-
sessionStorage.removeItem("currentUser");
23-
sessionStorage.setItem("currentUser", user);
24-
}
25-
26-
// End ## getters and setters
27-
281
function stringContainsSubstring(string, substring) {
292
var isSubstring = string.indexOf(substring) !== -1;
303
return isSubstring;
@@ -60,7 +33,7 @@ AjaxHttpSender.prototype.sendAjax = function (contextPath, type, data, callback,
6033
if (!dataType) {
6134
dataType = "json";
6235
}
63-
url = addBackendParameter(frontendServiceUrl+contextPath);
36+
var url = addBackendParameter(getFrontEndServiceUrl() + contextPath);
6437
$.ajax({
6538
url: url,
6639
type: type,
@@ -134,35 +107,29 @@ function isStringDefined(value) {
134107
// Start ## Routing ##
135108
var routes = {};
136109
routes["subscriptions"] = function () {
137-
window.clearInterval(timerInterval);
138110
updateBackEndInstanceList();
139111
$(".app-header").removeClass("header-bar-hidden");
140112
$(".main").load("subscriptionpage.html");
141113
};
142114
routes["test-rules"] = function () {
143-
window.clearInterval(timerInterval);
144115
updateBackEndInstanceList();
145116
$(".app-header").removeClass("header-bar-hidden");
146117
$(".main").load("testRules.html");
147118
};
148119
routes["ei-info"] = function () {
149-
window.clearInterval(timerInterval);
150120
updateBackEndInstanceList();
151121
$(".app-header").removeClass("header-bar-hidden");
152122
$(".main").load("eiInfo.html");
153123
};
154124
routes["switch-backend"] = function () {
155-
window.clearInterval(timerInterval);
156125
$(".app-header").addClass("header-bar-hidden");
157126
$(".main").load("switch-backend.html");
158127
};
159128
routes["add-backend"] = function () {
160-
window.clearInterval(timerInterval);
161129
$(".app-header").addClass("header-bar-hidden");
162130
$(".main").load("add-instances.html");
163131
};
164132
routes["login"] = function () {
165-
window.clearInterval(timerInterval);
166133
updateBackEndInstanceList();
167134
$(".app-header").removeClass("header-bar-hidden");
168135
$(".main").load("login.html");
@@ -193,7 +160,7 @@ function navigateToRoute(route) {
193160
// Start ## Load Back end list ##
194161
function updateBackEndInstanceList() {
195162
$.ajax({
196-
url: frontendServiceUrl + frontendServiceBackEndPath,
163+
url: getFrontEndServiceUrl() + getFrontendServiceBackEndPath(),
197164
type: "GET",
198165
contentType: 'application/json; charset=utf-8',
199166
cache: false,
@@ -301,8 +268,7 @@ function doIfSecurityOff() {
301268
function checkBackendSecured() {
302269
var callback = {
303270
success: function (responseData, textStatus) {
304-
var response = JSON.parse(ko.toJSON(responseData));
305-
var ldapStatus = response.security;
271+
var ldapStatus = responseData.security;
306272
setLdapEnabled(ldapStatus);
307273
if (isLdapEnabled()) {
308274
checkLoggedInUser();
@@ -315,8 +281,7 @@ function checkBackendSecured() {
315281
}
316282
};
317283
var ajaxHttpSender = new AjaxHttpSender();
318-
var contextPath = "/auth";
319-
ajaxHttpSender.sendAjax(contextPath, "GET", null, callback);
284+
ajaxHttpSender.sendAjax(backendEndpoints.AUTH, "GET", null, callback);
320285
}
321286

322287
function checkLoggedInUser() {
@@ -330,34 +295,7 @@ function checkLoggedInUser() {
330295
}
331296
};
332297
var ajaxHttpSender = new AjaxHttpSender();
333-
var contextPath = "/auth/login";
334-
ajaxHttpSender.sendAjax(contextPath, "GET", null, callback);
298+
ajaxHttpSender.sendAjax(backendEndpoints.LOGIN, "GET", null, callback);
335299
}
336300

337301
// End ## Login and Security ##
338-
339-
// Start ## Status Indicator ##
340-
var statusType = {
341-
success: "alert-success",
342-
info: "alert-info",
343-
warning: "alert-warning",
344-
danger: "alert-danger"
345-
};
346-
347-
var statusText = {
348-
backend_down: "<strong>Back end is down!</strong> Wait for it go up or switch to another back end before continuing!",
349-
test_rules_disabled: "<strong>Test Rule service is disabled!</strong> To enable it set the backend property [testaggregated.enabled] as [true]"
350-
};
351-
352-
function addStatusIndicator(statusType, statusText) {
353-
var statusIndicator = $(".content")[0].previousElementSibling;
354-
if (statusIndicator != null) {
355-
$($(".content")[0].previousElementSibling).remove();
356-
}
357-
$(".content").before("<div class=\"subscription-alert alert " + statusType + "\">" + statusText + "</div>");
358-
}
359-
360-
function removeStatusIndicator() {
361-
$($(".content")[0].previousElementSibling).remove();
362-
}
363-
// End ## Status Indicator ##

src/main/resources/static/js/eiInfo.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -142,51 +142,15 @@ jQuery(document).ready(function () {
142142
}
143143
};
144144
var ajaxHttpSender = new AjaxHttpSender();
145-
var contextPath = "/information";
146-
ajaxHttpSender.sendAjax(contextPath, "GET", null, callback);
145+
ajaxHttpSender.sendAjax(backendEndpoints.INFORMATION, "GET", null, callback);
147146
}
148147

149148
createFrontEndGeneralInfo();
150149
getInstanceInfo();
151150

152151
// Check EI Backend Server Status ########################################
153-
var backendStatus;
154-
var currentBackendStatus = false;
155-
function checkBackendStatus() {
156-
var callback = {
157-
error: function (XMLHttpRequest, textStatus, errorThrown) {
158-
if (XMLHttpRequest.status == 401) {
159-
doIfUserLoggedOut();
160-
removeStatusIndicator();
161-
currentBackendStatus = true;
162-
} else {
163-
doIfSecurityOff();
164-
addStatusIndicator(statusType.danger, statusText.backend_down);
165-
currentBackendStatus = false;
166-
}
167-
},
168-
success: function (data, textStatus) {
169-
checkBackendSecured();
170-
removeStatusIndicator();
171-
currentBackendStatus = true;
172-
},
173-
complete: function () {
174-
if(backendStatus != undefined && backendStatus != currentBackendStatus) {
175-
reloadRoute();
176-
}
177-
backendStatus = currentBackendStatus;
178-
}
179-
};
180-
var ajaxHttpSender = new AjaxHttpSender();
181-
var contentType = "application/string; charset=utf-8";
182-
var datatype = "text";
183-
var contextPath = "/auth/checkStatus";
184-
ajaxHttpSender.sendAjax(contextPath, "GET", null, callback, contentType, datatype);
185-
}
186-
checkBackendStatus();
187152

188-
// Check if EI Backend Server is online every X seconds
189-
timerInterval = window.setInterval(function () { checkBackendStatus(); }, 15000);
153+
checkBackendStatus();
190154

191155
// END OF EI Backend Server check #########################################
192156
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
var router = new Navigo(null, true, '#');
2+
var frontendServiceUrl = $('#frontendServiceUrl').text();
3+
var frontendServiceBackEndPath = "/backend";
4+
var ldapEnabled = true;
5+
var table;
6+
7+
var backendEndpoints = {
8+
AUTH: "/auth",
9+
LOGIN: "/auth/login",
10+
CHECK_STATUS: "/auth/checkStatus",
11+
SUBSCRIPTIONS: "/subscriptions",
12+
DOWNLOAD: "/download/",
13+
DOWNLOAD_SUBSCRIPTIONS_TEMPLATE: "/download/subscriptionsTemplate",
14+
INFORMATION: "/information",
15+
TEST_RULES_PAGE_ENABLED: "/rules/rule-check/testRulePageEnabled",
16+
CHECK_AGGREGATION: "/rules/rule-check/aggregation"
17+
};
18+
19+
// Status handling variables
20+
var PAGES_FOR_STATUS_INDICATION = "subscriptions test-rules ei-info login";
21+
var statusType = {
22+
SUCCESS: "alert-success",
23+
INFO: "alert-info",
24+
WARNING: "alert-warning",
25+
DANGER: "alert-danger"
26+
};
27+
var statusText = {
28+
BACKEND_DOWN: "<strong>Back-end is down!</strong> Wait for it go up or switch to another back-end before continuing!",
29+
UNKNOWN_BACK_END_STATUS: "<strong>Back-end status is unknown!</strong> Wait for it to update or switch to another back-end before continuing!",
30+
TEST_RULES_DISABLED: "<strong>Test Rule service is disabled!</strong> To enable it set the backend property [testaggregated.enabled] as [true]"
31+
};
32+
33+
var backEndStatus = true;
34+
var backEndStatusUpdated = false;
35+
var previousBackEndStatus;
36+
var backEnsStatusTimerInterval;
37+
// End Status variables
38+
39+
// Start ## getters and setters
40+
41+
function isLdapEnabled(){
42+
return Boolean(ldapEnabled);
43+
}
44+
45+
function setLdapEnabled(value){
46+
ldapEnabled = Boolean(value);
47+
}
48+
49+
function getCurrentUser() {
50+
return sessionStorage.getItem("currentUser");
51+
}
52+
53+
function setCurrentUser(user) {
54+
sessionStorage.removeItem("currentUser");
55+
sessionStorage.setItem("currentUser", user);
56+
}
57+
58+
function getFrontEndServiceUrl() {
59+
return frontendServiceUrl;
60+
}
61+
62+
function getFrontendServiceBackEndPath() {
63+
return frontendServiceBackEndPath;
64+
}
65+
66+
// End ## getters and setters
67+
68+
// Start ## getters and setters status handling
69+
70+
function isBackEndStatusOk() {
71+
return Boolean(backEndStatus);
72+
}
73+
74+
function isBackEndStatusUpdated() {
75+
return Boolean(backEndStatusUpdated);
76+
}
77+
78+
function setBackEndStatusOk(value) {
79+
backEndStatus = Boolean(value);
80+
backEndStatusUpdated = true;
81+
}
82+
83+
function isBackEndStatusChanged() {
84+
if (previousBackEndStatus === undefined) {
85+
previousBackEndStatus = backEndStatus;
86+
return false;
87+
}
88+
var statusChanged = previousBackEndStatus !== backEndStatus;
89+
previousBackEndStatus = backEndStatus;
90+
return Boolean(statusChanged);
91+
}
92+
93+
function setBackEndStatusTimerInterval() {
94+
if (backEnsStatusTimerInterval === undefined) {
95+
backEnsStatusTimerInterval = window.setInterval(function () { updateBackendStatus(); }, 15000);
96+
}
97+
}
98+
99+
function getWhiteListedPages() {
100+
return String(PAGES_FOR_STATUS_INDICATION);
101+
}
102+
103+
// End ## getters and setters status handling

src/main/resources/static/js/login.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ jQuery(document).ready(function () {
4242
navigateToRoute('subscriptions');
4343
},
4444
error: function (XMLHttpRequest, textStatus, errorThrown) {
45-
if (XMLHttpRequest.status == 401) {
46-
window.logMessages("Bad credentials");
45+
var statusCode = XMLHttpRequest.status;
46+
47+
if (statusCode == 401) {
48+
window.logMessages("Error Status code: 401 'Unauthorized or bad credentials'");
4749
$('#loginError').text("Invalid username and/or password!");
48-
$('#loginError').addClass("is-invalid");
49-
$('#loginError').show();
50+
} else if (statusCode == 500 && !isBackEndStatusOk()) {
51+
window.logMessages("Error back-end is not reachable!");
52+
$('#loginError').text("Back-end might be unavailable!");
5053
} else {
51-
window.logMessages("Unknown login error");
52-
$('#loginError').text("Unknown login error!");
53-
$('#loginError').addClass("is-invalid");
54-
$('#loginError').show();
54+
window.logMessages("Error Status Code: " + statusCode + " 'Unknown server error'");
5555
}
56+
57+
$('#loginError').addClass("is-invalid");
58+
$('#loginError').show();
5659
},
5760
complete: function () {
5861
}
@@ -66,4 +69,10 @@ jQuery(document).ready(function () {
6669
ko.cleanNode(observableObject);
6770
var model = new loginModel();
6871
ko.applyBindings(model, observableObject);
72+
73+
// Check EI Backend Server Status ########################################
74+
75+
checkBackendStatus();
76+
77+
// END OF EI Backend Server check #########################################
6978
});

0 commit comments

Comments
 (0)