View Javadoc

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 getGroups(final HttpServletRequest request)
23      {
24          if (request == null)
25          {
26              return Collections.EMPTY_LIST;
27          }
28  
29          Collection groups = (Collection) request.getAttribute(GROUP_CACHE_KEY);
30  
31          if (groups != null)
32          {
33              return groups;
34          }
35  
36          final User remoteUser = (User) SecurityConfigFactory.getInstance().getAuthenticator().getUser(request);
37  
38          if (remoteUser == null)
39          {
40              return Collections.EMPTY_LIST;
41          }
42  
43          groups = remoteUser.getGroups();
44          request.setAttribute(GROUP_CACHE_KEY, groups);
45          return groups;
46      }
47  }