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.userinfo.impl;
18  
19  import java.security.Principal;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.prefs.Preferences;
25  
26  import javax.security.auth.Subject;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.jetspeed.om.common.UserAttributeRef;
31  import org.apache.jetspeed.request.RequestContext;
32  import org.apache.jetspeed.security.SecurityException;
33  import org.apache.jetspeed.security.SecurityHelper;
34  import org.apache.jetspeed.security.User;
35  import org.apache.jetspeed.security.UserManager;
36  import org.apache.jetspeed.security.UserPrincipal;
37  import org.apache.jetspeed.userinfo.UserAttributeRetrievalException;
38  import org.apache.jetspeed.userinfo.UserAttributeSource;
39  
40  /***
41   * Default implementation of a UserAttribute source Provides users attributes from standard prefs implementation
42   * 
43   * @author <a href="mailto:KeithGarry.Boyce@bcbsma.com">Keith Garry Boyce</a>
44   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
45   * @version $Id: $
46   */
47  public class UserManagerUserAttributeSourceImpl implements UserAttributeSource
48  {
49  
50      /*** Logger */
51      private static final Log log = LogFactory.getLog(UserManagerUserAttributeSourceImpl.class);
52  
53      /*** The user manager */
54      private UserManager userManager;
55  
56      /***
57       * @param userManager
58       *            The userManager to set.
59       */
60      public void setUserManager(UserManager userManager)
61      {
62          this.userManager = userManager;
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see org.jetspeed.userinfo.UserAttributeSource#getUserAttributeMap(javax.security.auth.Subject, java.util.Set)
69       */
70      public Map getUserAttributeMap(Subject subject, Collection userAttributeRefs, RequestContext context)
71              throws UserAttributeRetrievalException
72      {
73  
74          Map userAttributeMap = new HashMap();
75          Principal userPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class);
76          if (null != userPrincipal)
77          {
78              log.debug("Got user principal: " + userPrincipal.getName());
79              try
80              {
81                  if (userManager.userExists(userPrincipal.getName()))
82                  {
83                      User user = userManager.getUser(userPrincipal.getName());
84                      Preferences userInfoPrefs = user.getPreferences();
85                      for (Iterator iter = userAttributeRefs.iterator(); iter.hasNext();)
86                      {
87                          UserAttributeRef currentAttributeRef = (UserAttributeRef) iter.next();
88                          Object value = userInfoPrefs.get(currentAttributeRef.getName(), null);
89                          if (value != null)
90                          {
91                              userAttributeMap.put(currentAttributeRef.getName(), value);
92                          }
93  
94                      }
95                  }
96              }
97              catch (SecurityException sex)
98              {
99                  log.warn("Unexpected SecurityException in UserInfoManager", sex);
100             }
101         }
102 
103         return userAttributeMap;
104     }
105 
106 }