Fix NPEs on switch disconnect in cluster mode
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWeb.java
index 296a8fbbd6c684bc0c441b5b05900842d3e0c7ea..24f0b4d80c77eb52cbff414f1900614b0521eaac 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -12,7 +11,9 @@ package org.opendaylight.controller.web;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
 
 import org.opendaylight.controller.configuration.IConfigurationService;
 import org.opendaylight.controller.sal.authorization.UserLevel;
@@ -20,7 +21,6 @@ import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.controller.sal.utils.StatusCode;
 import org.opendaylight.controller.usermanager.IUserManager;
-import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -31,73 +31,111 @@ import org.springframework.web.bind.annotation.ResponseBody;
 @RequestMapping("/")
 public class DaylightWeb {
     @RequestMapping(value = "")
-    public String index(Model model) {
-       IUserManager userManager = (IUserManager) ServiceHelper
+    public String index(Model model, HttpServletRequest request) {
+        IUserManager userManager = (IUserManager) ServiceHelper
                 .getGlobalInstance(IUserManager.class, this);
         if (userManager == null) {
-               return "User Manager is not available";
+            return "User Manager is not available";
         }
-       
-        String username = SecurityContextHolder.getContext().getAuthentication().getName();
+
+        String username = request.getUserPrincipal().getName();
+
         model.addAttribute("username", username);
-        model.addAttribute("role", userManager.getUserLevel(username).toNumber());
-        
+        model.addAttribute("role", userManager.getUserLevel(username)
+                .toNumber());
+
         return "main";
     }
 
     @RequestMapping(value = "web.json")
     @ResponseBody
-    public Map<String, Map<String, Object>> bundles() {
-        Object[] instances = ServiceHelper.getGlobalInstances(IDaylightWeb.class,
-                this, null);
+    public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
+        Object[] instances = ServiceHelper.getGlobalInstances(
+                IDaylightWeb.class, this, null);
         Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
         Map<String, Object> entry;
         IDaylightWeb bundle;
-        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
-        IUserManager userManger = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
+        String username = request.getUserPrincipal().getName();
+        IUserManager userManger = (IUserManager) ServiceHelper
+                .getGlobalInstance(IUserManager.class, this);
         for (Object instance : instances) {
             bundle = (IDaylightWeb) instance;
-            if (userManger != null &&
-                       bundle.isAuthorized(userManger.getUserLevel(userName))) {
-                   entry = new HashMap<String, Object>();
-                   entry.put("name", bundle.getWebName());
-                   entry.put("order", bundle.getWebOrder());
-                   bundles.put(bundle.getWebId(), entry);
+            if (userManger != null
+                    && bundle.isAuthorized(userManger.getUserLevel(username))) {
+                entry = new HashMap<String, Object>();
+                entry.put("name", bundle.getWebName());
+                entry.put("order", bundle.getWebOrder());
+                bundles.put(bundle.getWebId(), entry);
             }
         }
         return bundles;
     }
-    
+
     @RequestMapping(value = "save", method = RequestMethod.POST)
     @ResponseBody
-    public String save() {
-       String username = SecurityContextHolder.getContext().getAuthentication().getName();
-       IUserManager userManager = (IUserManager) ServiceHelper
+    public String save(HttpServletRequest request) {
+        String username = request.getUserPrincipal().getName();
+        IUserManager userManager = (IUserManager) ServiceHelper
                 .getGlobalInstance(IUserManager.class, this);
-        if (userManager == null) return "User Manager is not available";
-        
+        if (userManager == null) {
+            return "User Manager is not available";
+        }
+
         UserLevel level = userManager.getUserLevel(username);
         if (level == UserLevel.NETWORKOPERATOR) {
-               return "Save not permitted for Operator";
+            return "Save not permitted for Operator";
+        }
+
+        Status status = new Status(StatusCode.UNAUTHORIZED,
+                "Operation not allowed for current user");
+        if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) {
+            IConfigurationService configService = (IConfigurationService) ServiceHelper
+                    .getGlobalInstance(IConfigurationService.class, this);
+            if (configService != null) {
+                status = configService.saveConfigurations();
+            }
         }
-        
-        Status status = new Status(StatusCode.UNAUTHORIZED, 
-                       "Operation not allowed for current user");
-           if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) {
-               IConfigurationService configService = (IConfigurationService) ServiceHelper
-                       .getGlobalInstance(IConfigurationService.class, this);
-               if (configService != null) {
-                       status = configService.saveConfigurations();
-               }
-           }
-        
+
         return status.getDescription();
     }
-    
+
+    @RequestMapping(value = "logout")
+    public String login(Map<String, Object> model,
+            final HttpServletRequest request) {
+
+        IUserManager userManager = (IUserManager) ServiceHelper
+                .getGlobalInstance(IUserManager.class, this);
+        if (userManager == null) {
+            return "User Manager is not available";
+        }
+        String username = request.getUserPrincipal().getName();
+        HttpSession session = request.getSession(false);
+        if (session != null) {
+            if (username != null) {
+                userManager.userLogout(username);
+            }
+            session.invalidate();
+
+        }
+        return "redirect:" + "/";
+    }
+
     @RequestMapping(value = "login")
-       public String login(Map<String, Object> model, final HttpServletResponse response) {
-                response.setHeader("X-Page-Location", "/login");
-               return "login";
-       }
+    public String login(Model model, final HttpServletRequest request,
+            final HttpServletResponse response) {
+        // response.setHeader("X-Page-Location", "/login");
+        IUserManager userManager = (IUserManager) ServiceHelper
+                .getGlobalInstance(IUserManager.class, this);
+        if (userManager == null) {
+            return "User Manager is not available";
+        }
+
+        String username = request.getUserPrincipal().getName();
+
+        model.addAttribute("username", username);
+        model.addAttribute("role", userManager.getUserLevel(username)
+                .toNumber());
+        return "forward:" + "/";
+    }
 
-}
\ No newline at end of file
+}