Checkstyle enforcer
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWebUtil.java
1 package org.opendaylight.controller.web;
2
3 import java.util.Set;
4
5 import javax.servlet.http.HttpServletRequest;
6
7 import org.opendaylight.controller.containermanager.IContainerAuthorization;
8 import org.opendaylight.controller.sal.authorization.Resource;
9 import org.opendaylight.controller.sal.utils.GlobalConstants;
10 import org.opendaylight.controller.sal.utils.ServiceHelper;
11
12 public class DaylightWebUtil {
13     private static String defaultName = GlobalConstants.DEFAULT.toString();
14
15     /**
16      * Returns the container that this user is authorized to access. If the user is not authorized to the requested
17      * container, then this method will return the default container.
18      *
19      * @param request - HttpServletRequest object to retrieve username
20      * @param container - requested container
21      * @param bundle - respective bundle
22      * @return container name if cleared, else it will always be 'default'
23      */
24     public static String getAuthorizedContainer(HttpServletRequest request, String container, Object bundle) {
25         if (container == null) {
26             return defaultName;
27         }
28
29         String username = request.getUserPrincipal().getName();
30         IContainerAuthorization containerAuthorization = (IContainerAuthorization)
31                 ServiceHelper.getGlobalInstance(IContainerAuthorization.class, bundle);
32         if (containerAuthorization != null) {
33             Set<Resource> resources = containerAuthorization.getAllResourcesforUser(username);
34             for(Resource resource : resources) {
35                 String name = (String) resource.getResource();
36                 if(container.equals(name)) {
37                     return name;
38                 }
39             }
40         }
41         return defaultName;
42     }
43 }