View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }