KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > atlassian > seraph > util > GroupCache


1 package com.atlassian.seraph.util;
2
3 import com.atlassian.seraph.config.SecurityConfigFactory;
4 import com.opensymphony.user.User;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.security.Principal JavaDoc;
10
11 /**
12  * A cache of request (via the remote user) -> group lookups
13  */

14 public class GroupCache
15 {
16     public static final String JavaDoc GROUP_CACHE_KEY = "atlassian.core.util.groups.cache.key";
17
18     /**
19      * Get a list of the groups for the current remote user.
20      *
21      * These groups are cache in a request attribute.
22      */

23     public static Collection JavaDoc getGroups(HttpServletRequest JavaDoc request)
24     {
25         if (request == null)
26             return Collections.EMPTY_LIST;
27
28         Collection JavaDoc groups = (Collection JavaDoc) request.getAttribute(GROUP_CACHE_KEY);
29
30         if (groups != null)
31             return groups;
32
33         User remoteUser = (User) SecurityConfigFactory.getInstance().getAuthenticator().getUser(request);
34
35         if (remoteUser == null)
36             return Collections.EMPTY_LIST;
37
38         groups = remoteUser.getGroups();
39         request.setAttribute(GROUP_CACHE_KEY, groups);
40         return groups;
41     }
42 }
43
Popular Tags