Merge pull request #159 from jcabrerizo/feature/session-timeout-messages

Manage backend errors including expired sessions
diff --git a/ui-modules/utils/server-status/server-status.js b/ui-modules/utils/server-status/server-status.js
index 9d25c1a..e82d544 100644
--- a/ui-modules/utils/server-status/server-status.js
+++ b/ui-modules/utils/server-status/server-status.js
@@ -36,14 +36,15 @@
 export function BrServerStatusDirective() {
     return {
         restrict: 'A',
-        controller: ['$rootScope', '$scope', '$http', '$cookies', '$interval', '$uibModal', controller]
+        controller: ['$rootScope', '$scope', '$http', '$cookies', '$interval', '$uibModal', '$log', controller]
     };
 
-    function controller($rootScope, $scope, $http, $cookies, $interval, $uibModal) {
+    function controller($rootScope, $scope, $http, $cookies, $interval, $uibModal, $log) {
         let cookie = DEFAULT_COOKIE;
         let intervalId = $interval(checkStatus, REFRESH_INTERVAL);
         $scope.$on('$destroy', () => ($interval.cancel(intervalId)));
         let modalInstance = null;
+        var previousState = null;
 
         function checkStatus() {
             cookie = $cookies.getObject(COOKIE_KEY) || DEFAULT_COOKIE;
@@ -55,7 +56,28 @@
             let state = BrServerStatusModalController.STATES.OK;
             let stateData = null;
             if (error) {
-                state = BrServerStatusModalController.STATES.NO_CONNECTION;
+                stateData = response.data;
+
+                if (stateData && stateData.SESSION_AGE_EXCEEDED) {
+                    state = BrServerStatusModalController.STATES.SESSION_AGE_EXCEEDED;
+                } else if (stateData && stateData.SESSION_INVALIDATED) {
+                    state = BrServerStatusModalController.STATES.SESSION_INVALIDATED;
+                }else if(response.status === 404) {
+                    state = BrServerStatusModalController.STATES.NO_CONNECTION;
+                }else if(response.status === 401 || response.status === 403 ) {
+                    state = BrServerStatusModalController.STATES.USER_NOT_AUTHORIZED;
+                }else {
+                    if (previousState === null || previousState == BrServerStatusModalController.STATES.OK){
+                        state = BrServerStatusModalController.STATES.OTHER_ERROR;
+                    } else {
+                        // we're now getting a new server error, possibly because the old error has expired
+                        // but changing the message for the user would be confusing so don't do that!
+                        // eg we get a 405 after a 307 (which the browser handles automatically) if redirected to Google for login
+                        $log.info("Server responded \"" + stateData + "\" after previous problem \"" + previousState + "\"");
+                        // no update
+                        state = previousState;
+                    }
+                }
                 stateData = response;
             } else {
                 stateData = response.data;
@@ -69,6 +91,7 @@
                     state = BrServerStatusModalController.STATES.UNHEALTHY;
                 }
             }
+            previousState = state;
             $rootScope.$broadcast('br-server-state-update', {state: state, stateData: stateData});
             if (state !== BrServerStatusModalController.STATES.OK && !cookie.dismissed && cookie.dismissedSate !== state) {
                 openModal(state, stateData);
@@ -114,7 +137,11 @@
                 STOPPING: 'STOPPING',
                 NOT_HA_MASTER: 'NOT-HA-MASTER',
                 NO_CONNECTION: 'NO-CONNECTION',
-                UNHEALTHY: 'UNHEALTHY'
+                UNHEALTHY: 'UNHEALTHY',
+                SESSION_INVALIDATED: 'SESSION_INVALIDATED',
+                SESSION_AGE_EXCEEDED: 'SESSION_AGE_EXCEEDED',
+                OTHER_ERROR: 'OTHER_ERROR',
+                USER_NOT_AUTHORIZED: 'USER_NOT_AUTHORIZED'
             };
             static $inject = ['$scope', '$uibModalInstance', 'state', 'stateData'];
 
diff --git a/ui-modules/utils/server-status/server-status.template.html b/ui-modules/utils/server-status/server-status.template.html
index eb54018..2de6b42 100644
--- a/ui-modules/utils/server-status/server-status.template.html
+++ b/ui-modules/utils/server-status/server-status.template.html
@@ -21,7 +21,9 @@
     <h3 class="modal-title" ng-switch-when="STARTING">Server starting up&hellip;</h3>
     <h3 class="modal-title" ng-switch-when="STOPPING">Server shutting down&hellip;</h3>
     <h3 class="modal-title" ng-switch-when="NOT-HA-MASTER">This server is not the high availability master</h3>
-    <h3 class="modal-title" ng-switch-when="NO-CONNECTION">Connecting to server&hellip;</h3>
+    <h3 class="modal-title" ng-switch-when="NO-CONNECTION|OTHER_ERROR" ng-switch-when-separator="|">Connecting to server&hellip;</h3>
+    <h3 class="modal-title" ng-switch-when="SESSION_INVALIDATED|SESSION_AGE_EXCEEDED" ng-switch-when-separator="|">Session invalid</h3>
+    <h3 class="modal-title" ng-switch-when="USER_NOT_AUTHORIZED">User not authorized</h3>
     <h3 class="modal-title" ng-switch-when="OK">Server up and running</h3>
     <h3 class="modal-title" ng-switch-default>This server has errors</h3>
 </div>
@@ -68,13 +70,27 @@
             </tbody>
         </table>
     </div>
-    <div ng-switch-when="NO-CONNECTION">
+    <div ng-switch-when="NO-CONNECTION|OTHER_ERROR" ng-switch-when-separator="|">
         <p>Cannot connect to API server. Please check your network connection. Try closing and re-opening the window and login again in the app. If the problem persists, please contact your administrator.</p>
     </div>
     <div ng-switch-when="UNHEALTHY">
         <p>Your server has errors and is not healthy.</p>
         <p>Please check with your system administrator.</p>
     </div>
+    <div ng-switch-when="USER_NOT_AUTHORIZED">
+        <p>The current user is not authorized.</p>
+    </div>
+    <div ng-switch-when="SESSION_INVALIDATED|SESSION_AGE_EXCEEDED" ng-switch-when-separator="|">
+        <p>Your last session has expired.</p>
+        <p>Please login again.</p>
+    </div>
+    <div ng-switch-when="OTHER_ERROR">
+        <p>An unexpected error has occurred.</p>
+        <p>Please try to login again and if the problem persists, please check with your system administrator.</p>
+        <form action="/brooklyn-ui-logout" method="post">
+            <button type="submit" class="btn btn-success"><i class="fa fa-sign-out fa-fw"></i> Logout</button>
+        </form>
+    </div>
     <div ng-switch-when="OK">
         <p>The server is now ready to use.</p>
     </div>