OpenDaylight Controller functional modules.
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / ControllerLogoutHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9
10 package org.opendaylight.controller.web;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.springframework.security.core.Authentication;
19 import org.springframework.security.web.authentication.logout.LogoutHandler;
20
21 import org.opendaylight.controller.sal.utils.ServiceHelper;
22 import org.opendaylight.controller.usermanager.IUserManager;
23
24 public class ControllerLogoutHandler implements LogoutHandler {
25
26     private static final Logger logger = LoggerFactory
27             .getLogger(ControllerLogoutHandler.class);
28
29     @Override
30     public void logout(HttpServletRequest request,
31             HttpServletResponse response, Authentication authentication) {
32         if (authentication != null) {
33             String userName = authentication.getName();
34             if (userName != null) {
35                 IUserManager userManager = (IUserManager) ServiceHelper
36                         .getGlobalInstance(IUserManager.class, this);
37                 if (userManager != null) {
38                     userManager.userLogout(userName);
39                     HttpSession session = request.getSession();
40                     userManager.getSessionManager().invalidateSessions(userName, session.getId());
41                     
42                 } else
43                     logger
44                             .error("UserMgr ref is null. Logout is not done cleanly");
45
46             } else
47                 logger
48                         .error("User name is null in authentication. Logout is not done cleanly");
49         }
50
51     }
52
53 }