OpenDaylight Controller functional modules.
[controller.git] / opendaylight / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / WebSecurityContextRepository.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 package org.opendaylight.controller.northbound.commons;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.security.core.context.SecurityContext;
17 import org.springframework.security.web.context.HttpRequestResponseHolder;
18 import org.springframework.security.web.context.SecurityContextRepository;
19
20 import org.opendaylight.controller.sal.utils.ServiceHelper;
21 import org.opendaylight.controller.usermanager.IUserManager;
22
23 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
24
25 public class WebSecurityContextRepository implements SecurityContextRepository {
26
27     private static final Logger logger = LoggerFactory
28             .getLogger(WebSecurityContextRepository.class);
29
30     WebSecurityContextRepository() {
31     }
32
33     @Override
34     public SecurityContext loadContext(
35             HttpRequestResponseHolder requestResponseHolder) {
36
37         SecurityContextRepository contextRepo = (SecurityContextRepository) getUserManagerRef()
38                 .getSecurityContextRepo();
39         return contextRepo.loadContext(requestResponseHolder);
40     }
41
42     @Override
43     public void saveContext(SecurityContext context,
44             HttpServletRequest request, HttpServletResponse response) {
45         SecurityContextRepository contextRepo = (SecurityContextRepository) getUserManagerRef()
46                 .getSecurityContextRepo();
47         contextRepo.saveContext(context, request, response);
48     }
49
50     private IUserManager getUserManagerRef() {
51         IUserManager userManager = (IUserManager) ServiceHelper
52                 .getGlobalInstance(IUserManager.class, this);
53         if (userManager != null) {
54             return userManager;
55         } else {
56             logger.error("UserManager Ref is null. ");
57             throw new InternalServerErrorException("UserManager Ref is null. ");
58         }
59     }
60
61     @Override
62     public boolean containsContext(HttpServletRequest request) {
63         SecurityContextRepository contextRepo = (SecurityContextRepository) getUserManagerRef()
64                 .getSecurityContextRepo();
65         return contextRepo.containsContext(request);
66     }
67
68 }