dlestrat    2004/05/16 13:08:28

  Added:       portal/src/java/org/apache/jetspeed/userinfo
                        UserInfoManager.java
               portal/src/java/org/apache/jetspeed/userinfo/impl
                        UserInfoManagerImpl.java
               portal/src/java/org/apache/jetspeed/userinfo/containers
                        userinfo.container.groovy
  Log:
  Continuing PLT 17 aka. User Information implementation.

  New component now maps the user attributes to the user attributes specified for a 
portlet application.
  
  Revision  Changes    Path
  1.1                  
jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/userinfo/UserInfoManager.java
  
  Index: UserInfoManager.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.userinfo;
  
  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
  import org.apache.jetspeed.request.RequestContext;
  
  /**
   * <p>The [EMAIL PROTECTED] UserInfoManager} retrieve the Map that will be set as a 
   * <code>(PortletRequest.USER_INFO</code> request attribute for a specific
   * portlet application</p>
   * <p>The portlet specification defines user info as follow (PLT 17):</p>
   * <p>Portlets can obtain an unmodifiable Map object containing the user attributes,
   * of user associated with the current request, from the request attributes.
   * The Map object can be retrieved using the USER_INFO constant defined in the
   * PortletRequest interface. If the request is done in the context of an
   * un-authenticated user, calls to the getAttribute method of the request 
   * using the USER_INFO constant must return null. If the user is
   * authenticated and there are no user attributes available, the Map must
   * be an empty Map. The Map object must contain a String name value pair for each 
available user
   * attribute. The Map object should only contain user attributes that have been 
mapped
   * during deployment.</p>
   * <p>Portlets can obtain an unmodifiable Map object containing the user attributes, 
of user
   * associated with the current request, from the request attributes. The Map object 
can be
   * retrieved using the USER_INFO constant defined in the PortletRequest interface. 
If the
   * request is done in the context of an un-authenticated user, calls to the 
getAttribute
   * method of the request using the USER_INFO constant must return null. If the user 
is
   * authenticated and there are no user attributes available, the Map must be an 
empty Map.
   * The Map object must contain a String name value pair for each available user 
attribute.
   * The Map object should only contain user attributes that have been mapped during
   * deployment.</p>
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">David Le Strat</a>
   */
  public interface UserInfoManager
  {
      
      /**
       * <p>Provide the user info map of user attributes for a given portlet 
application.</p>
       * <p>The MutablePortletApplication can be retrieved from a Fragment through:</p>
       * <p><code>MutablePortletApplication pa = 
getPortletApplication(portletFragment);</code></p>
       * @param pa The portlet application.
       * @param context The request context.
       * @return The portlet request context updated with the
       *         [EMAIL PROTECTED] PortletRequest.USER_INFO} map.
       */
      RequestContext setUserInfoMap(MutablePortletApplication pa, RequestContext 
context);
  }
  
  
  
  1.1                  
jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/userinfo/impl/UserInfoManagerImpl.java
  
  Index: UserInfoManagerImpl.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.userinfo.impl;
  
  import java.security.Principal;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.prefs.Preferences;
  import java.util.prefs.BackingStoreException;
  
  import javax.portlet.PortletRequest;
  import javax.security.auth.Subject;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import org.apache.jetspeed.components.portletregistry.PortletRegistryComponent;
  import org.apache.jetspeed.components.portletregistry.PortletRegistryHelper;
  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
  import org.apache.jetspeed.om.common.UserAttribute;
  import org.apache.jetspeed.om.page.Fragment;
  import org.apache.jetspeed.request.RequestContext;
  import org.apache.jetspeed.security.User;
  import org.apache.jetspeed.security.UserManager;
  import org.apache.jetspeed.security.UserPrincipal;
  import org.apache.jetspeed.security.SecurityException;
  import org.apache.jetspeed.security.SecurityHelper;
  import org.apache.jetspeed.userinfo.UserInfoManager;
  
  
  /**
   * <p>Implements the [EMAIL PROTECTED] org.apache.jetspeed.userinfo.UserInfoManager} 
interface.</p>
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">David Le Strat</a>
   */
  public class UserInfoManagerImpl implements UserInfoManager
  {
  
      /** Logger */
      private static final Log log = LogFactory.getLog(UserInfoManagerImpl.class);
  
      // TODO Same caching issue as usual.  We should look into JCS. That wil do for 
now.
      /** Map used to cache user info maps for each mapped portlet application. */
      private static Map userInfoMapCache;
  
      /** <p>The default user attributes property set.</p> */
      static String USER_INFO_PROPERTY_SET = "userinfo";
  
      /** The user information property set. */
      String userInfoPropertySet;
      /** The user manager */
      UserManager userMgr;
      /** The portlet registry. */
      PortletRegistryComponent registry;
      /** The portlet application being processed. */
      String paName;
  
      /**
       * <p>Constructor providing access to the [EMAIL PROTECTED] UserManager}.</p>
       * @param userMgr The user manager.
       * @param registry The portlet registry component.
       */
      public UserInfoManagerImpl(UserManager userMgr, PortletRegistryComponent 
registry)
      {
          this.userMgr = userMgr;
          this.registry = registry;
          this.userInfoPropertySet = USER_INFO_PROPERTY_SET;
          initUserInfoMapCache();
      }
  
      /**
       * <p>Constructor providing access to the [EMAIL PROTECTED] UserManager} and 
specifying which 
       * property set to use for user information.</p>
       * @param userMgr The user manager.
       * @param registry The portlet registry component.
       * @param userInfoPropertySet The user information property set.
       *  
       */
      public UserInfoManagerImpl(UserManager userMgr, PortletRegistryComponent 
registry, String userInfoPropertySet)
      {
          this.userMgr = userMgr;
          this.registry = registry;
          this.userInfoPropertySet = userInfoPropertySet;
          initUserInfoMapCache();
      }
  
      /**
       * @see 
org.apache.jetspeed.userinfo.UserInfoManager#setUserInfoMap(org.apache.jetspeed.om.page.Fragment,
 org.apache.jetspeed.request.RequestContext)
       */
      public RequestContext setUserInfoMap(MutablePortletApplication pa, 
RequestContext context)
      {
          // Check if user info map is in cache.
          if (userInfoMapCache.containsKey(paName))
          {
              context.setAttribute(PortletRequest.USER_INFO, 
userInfoMapCache.get(paName));
              return context;
          }
          // Not in cache, map user info.
          Preferences userPrefs = getUserPreferences(context);
          if (null == userPrefs)
          {
              context.setAttribute(PortletRequest.USER_INFO, null);
              log.debug(PortletRequest.USER_INFO + " is set to null");
              return context;
          }
          if (null == pa)
          {
              context.setAttribute(PortletRequest.USER_INFO, null);
              log.debug(PortletRequest.USER_INFO + " is set to null");
              return context;
          }
          Preferences userInfoPrefs = userPrefs.node(userInfoPropertySet);
          Collection portletUserAttributes = pa.getUserAttributes();
          Map userInfoMap = mapUserInfo(userInfoPrefs, portletUserAttributes);
          if (null == userInfoMap)
          {
              context.setAttribute(PortletRequest.USER_INFO, null);
              log.debug(PortletRequest.USER_INFO + " is set to null");
              return context;
          }
          context.setAttribute(PortletRequest.USER_INFO, userInfoMapCache.get(paName));
          return context;
      }
  
      /**
       * <p>Maps the user info properties retrieved from the user preferences
       * to the user info attribute declared in the portlet.xml descriptor.</p>
       * @param userInfoPrefs The user info preferences.
       * @param portletUserAttributes The declarative portlet user attributes.
       * @return The user info map.
       */
      private Map mapUserInfo(Preferences userInfoPrefs, Collection 
portletUserAttributes)
      {
          if ((null == portletUserAttributes) || (portletUserAttributes.size() == 0))
          {
              return null;
          }
  
          Map userInfoMap = new HashMap();
          String[] propertyKeys = null;
          try
          {
              propertyKeys = userInfoPrefs.keys();
              if ((null != propertyKeys) && log.isDebugEnabled())
                  log.debug("Found " + propertyKeys.length + " children for " + 
userInfoPrefs.absolutePath());
          }
          catch (BackingStoreException bse)
          {
              log.error("BackingStoreException: " + bse.toString());
          }
          if (null == propertyKeys)
          {
              return null;
          }
  
          Iterator iter = portletUserAttributes.iterator();
          while (iter.hasNext())
          {
              UserAttribute currentAttribute = (UserAttribute) iter.next();
              if (null != currentAttribute)
              {
                  for (int i = 0; i < propertyKeys.length; i++)
                  {
                      if ((currentAttribute.getName()).equals(propertyKeys[i]))
                      {
                          userInfoMap.put(propertyKeys[i], 
userInfoPrefs.get(propertyKeys[i], null));
                      }
                  }
              }
          }
  
          userInfoMapCache.put(paName, userInfoMap);
  
          return userInfoMap;
      }
  
      /**
       * <p>Gets the user preferences from the user's request.</p>
       * <p>If no user is logged in, return null.</p>
       * @param context The request context.
       * @return The user preferences.
       */
      private Preferences getUserPreferences(RequestContext context)
      {
          Preferences userPrefs = null;
          Subject subject = context.getSubject();
          if (null != subject)
          {
              Principal userPrincipal = SecurityHelper.getPrincipal(subject, 
UserPrincipal.class);
              if (null != userPrincipal)
              {
                  log.debug("Got user principal: " + userPrincipal.getName());
                  try
                  {
                      if (userMgr.userExists(userPrincipal.getName()))
                      {
                          User user = userMgr.getUser(userPrincipal.getName());
                          userPrefs = user.getPreferences();
                      }
                  }
                  catch (SecurityException sex)
                  {
                      log.warn("Unexpected SecurityException in UserInfoManager", sex);
                  }
              }
          }
          return userPrefs;
      }
  
      /**
       * <p>Gets the portlet application from the provided portlet fragment.</p>
       * @param portletFragment The portlet fragment.
       * @return The portlet application.
       */
      private MutablePortletApplication getPortletApplication(Fragment portletFragment)
      {
          paName = PortletRegistryHelper.parseAppName(portletFragment.getName());
          return registry.getPortletApplication(paName);
      }
  
      private void initUserInfoMapCache()
      {
          if (null == userInfoMapCache)
          {
              userInfoMapCache = new HashMap();
          }
      }
  
  }
  
  
  
  1.1                  
jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/userinfo/containers/userinfo.container.groovy
  
  Index: userinfo.container.groovy
  ===================================================================
  /* ========================================================================

   * Copyright 2004 The Apache Software Foundation

   *

   * Licensed under the Apache License, Version 2.0 (the "License");

   * you may not use this file except in compliance with the License.

   * You may obtain a copy of the License at

   *

   *     http://www.apache.org/licenses/LICENSE-2.0

   *

   * Unless required by applicable law or agreed to in writing, software

   * distributed under the License is distributed on an "AS IS" BASIS,

   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

   * See the License for the specific language governing permissions and

   * limitations under the License.

   * ========================================================================

   */

  import org.picocontainer.defaults.DefaultPicoContainer

  import org.picocontainer.Parameter

  import org.picocontainer.defaults.ConstantParameter

  import org.picocontainer.defaults.ComponentParameter

  import org.picocontainer.defaults.ConstructorComponentAdapter

  

  import org.apache.jetspeed.components.persistence.store.PersistenceStoreContainer

  

  import org.apache.jetspeed.components.portletregistry.PortletRegistryComponent

  import org.apache.jetspeed.security.UserManager

  import org.apache.jetspeed.userinfo.UserInfoManager

  import org.apache.jetspeed.userinfo.impl.UserInfoManagerImpl

  

  import java.io.File

  

  /**

   * This is the standard assembly for a Security

   * component.  We want the Security component to be exposed

   * at as high the container hierarchy as possibly so, if a

   * parent container is provided, we will regsiter to the parent

   * and use it as the container for the Security.

   */

  

  if(parent != null)

  {

        container = new DefaultPicoContainer(parent)

        parent.registerComponentImplementation(UserInfoManager, UserInfoManagerImpl, 
new Parameter[] {new ComponentParameter(UserManager), new 
ComponentParameter(PortletRegistryComponent)} )

  }

  else

  {

        container = new DefaultPicoContainer()

      container.registerComponentImplementation(UserInfoManager, UserInfoManagerImpl, 
new Parameter[] {new ComponentParameter(UserManager), new 
ComponentParameter(PortletRegistryComponent)} )

  }     

        

  // This will be an empty container if "parent" was not null

  return container

  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to