1   package com.atlassian.seraph.util;
2   
3   import com.atlassian.seraph.config.SecurityConfigFactory;
4   
5   import com.opensymphony.user.User;
6   
7   import java.util.Collection;
8   import java.util.Collections;
9   
10  import javax.servlet.http.HttpServletRequest;
11  
12  /**
13   * A cache of request (via the remote user) -> group lookups
14   */
15  public class GroupCache
16  {
17      public static final String GROUP_CACHE_KEY = "atlassian.core.util.groups.cache.key";
18  
19      /**
20       * Get a list of the groups for the current remote user. These groups are cache in a request attribute.
21       */
22      public static Collection<String> getGroups(final HttpServletRequest request)
23      {
24          if (request == null)
25          {
26              return Collections.emptySet();
27          }
28  
29          {
30              @SuppressWarnings("unchecked")
31              final Collection<String> groups = (Collection<String>) request.getAttribute(GROUP_CACHE_KEY);
32              if (groups != null)
33              {
34                  return groups;
35              }
36          }
37  
38          final User remoteUser = (User) SecurityConfigFactory.getInstance().getAuthenticator().getUser(request);
39          if (remoteUser == null)
40          {
41              return Collections.emptySet();
42          }
43  
44          @SuppressWarnings("unchecked")
45          final Collection<String> groups = remoteUser.getGroups();
46          request.setAttribute(GROUP_CACHE_KEY, groups);
47          return groups;
48      }
49  }