1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.security.users;
18
19 import java.security.Principal;
20 import java.util.Iterator;
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23 import java.util.prefs.BackingStoreException;
24 import java.util.prefs.Preferences;
25
26 import javax.security.auth.Subject;
27
28 import org.apache.jetspeed.security.User;
29 import org.apache.jetspeed.security.UserPrincipal;
30
31 /***
32 * User state.
33 *
34 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35 * @version $Id: JetspeedUserBean.java 348264 2005-11-22 22:06:45Z taylor $
36 */
37 public class JetspeedUserBean
38 {
39 private String principal;
40 private Map attributes = new LinkedHashMap();
41
42 public JetspeedUserBean(User user)
43 {
44 Principal userPrincipal = createPrincipal(user.getSubject(), UserPrincipal.class);
45 this.principal = userPrincipal.getName();
46 try
47 {
48 Preferences userAttributes = user.getUserAttributes();
49 String[] keys = userAttributes.keys();
50 for (int ix = 0; ix < keys.length; ix++)
51 {
52 attributes.put(keys[ix], userAttributes.get(keys[ix], null));
53 }
54 }
55 catch (BackingStoreException e)
56 {
57 }
58 }
59
60 /***
61 * @return Returns the principal.
62 */
63 public String getPrincipal()
64 {
65 return principal;
66 }
67 /***
68 * @param principal The principal to set.
69 */
70 public void setPrincipal(String principal)
71 {
72 this.principal = principal;
73 }
74
75 public Principal createPrincipal(Subject subject, Class classe)
76 {
77 Principal principal = null;
78 Iterator principals = subject.getPrincipals().iterator();
79 while (principals.hasNext())
80 {
81 Principal p = (Principal) principals.next();
82 if (classe.isInstance(p))
83 {
84 principal = p;
85 break;
86 }
87 }
88 return principal;
89 }
90
91 /***
92 * @return Returns the attributes.
93 */
94 public Map getAttributes()
95 {
96 return attributes;
97 }
98 }