[jboss-cvs] jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer ...

Chris Laprun chris.laprun at jboss.com
Thu Aug 24 20:56:30 EDT 2006


  User: claprun 
  Date: 06/08/24 20:56:30

  Modified:    wsrp/src/main/org/jboss/portal/wsrp/consumer    Tag:
                        JBoss_Portal_Branch_2_4
                        ProducerSessionInformation.java
                        InvocationHandler.java SessionHandler.java
  Log:
  - Re-wrote cookie/session management. Now handles cookies outside of initCookie calls. Still needs more testing.
  - Added ProducerSessionInformationTestCase and other-tests in build.xml for WSRP.
  - Minor improvements.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.2.2.2   +115 -28   jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/ProducerSessionInformation.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ProducerSessionInformation.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/ProducerSessionInformation.java,v
  retrieving revision 1.2.2.1
  retrieving revision 1.2.2.2
  diff -u -b -r1.2.2.1 -r1.2.2.2
  --- ProducerSessionInformation.java	21 Aug 2006 23:07:20 -0000	1.2.2.1
  +++ ProducerSessionInformation.java	25 Aug 2006 00:56:30 -0000	1.2.2.2
  @@ -22,62 +22,69 @@
   
   package org.jboss.portal.wsrp.consumer;
   
  +import org.apache.commons.httpclient.Cookie;
   import org.jboss.logging.Logger;
   import org.jboss.portal.common.util.ParameterValidation;
  +import org.jboss.portal.common.util.Tools;
   import org.jboss.portal.wsrp.WSRPConstants;
   import org.jboss.portal.wsrp.core.SessionContext;
   
   import java.util.HashMap;
  +import java.util.List;
   import java.util.Map;
   
   /**
    * Records session and cookie information for a producer.
    *
    * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
  - * @version $Revision: 1.2.2.1 $
  + * @version $Revision: 1.2.2.2 $
    * @since 2.4 (May 30, 2006)
    */
   public class ProducerSessionInformation
   {
      private Logger log = Logger.getLogger(ProducerSessionInformation.class);
   
  -   private boolean userCookiesInitialized = false;
  +   private boolean initCookieDone = false;
      private boolean perGroupCookies = false;
   
  -   /** group id -> cookie */
  +   /** group id -> Cookie[] */
      private Map groupCookies;
   
      /** portlet handle -> SessionInfo */
      private Map portletSessions;
   
  -   private String userCookie;
  +   private Cookie[] userCookie;
   
      public String getUserCookie()
      {
  -      if (isPerGroupCookies())
  +      if (userCookie == null)
         {
  -         throw new IllegalStateException("Cannot get user cookie when cookie protocol is perGroup.");
  -      }
  -      return userCookie;
  +         return null;
      }
   
  -   public void setUserCookie(String userCookie)
  +      userCookie = purgeExpiredCookies(userCookie);
  +      if (userCookie.length == 0)
      {
  -      if (isPerGroupCookies())
  -      {
  -         throw new IllegalStateException("Cannot set user cookie when cookie protocol is perGroup.");
  +         setInitCookieDone(false);
  +      }
  +      return outputToExternalForm(userCookie);
         }
  +
  +   public void setUserCookie(Cookie[] userCookie)
  +   {
  +      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(userCookie, "cookies");
  +
         this.userCookie = userCookie;
      }
   
  -   public boolean isUserCookiesInitialized()
  +   public boolean isInitCookieDone()
      {
  -      return userCookiesInitialized;
  +      return initCookieDone;
      }
   
  -   public void setUserCookiesInitialized(boolean userCookiesInitialized)
  +   public void setInitCookieDone(boolean initCookieDone)
      {
  -      this.userCookiesInitialized = userCookiesInitialized;
  +      this.initCookieDone = initCookieDone;
      }
   
      public boolean isPerGroupCookies()
  @@ -90,12 +97,11 @@
         this.perGroupCookies = perGroupCookies;
      }
   
  -   public void addGroupCookie(String groupId, String cookie)
  +   public void setGroupCookieFor(String groupId, Cookie[] cookies)
      {
         if (!isPerGroupCookies())
         {
            throw new IllegalStateException("Cannot add group cookie when cookie protocol is perUser.");
  -
         }
   
         if (groupId == null)
  @@ -103,6 +109,8 @@
            throw new IllegalArgumentException("Cannot set cookie for a null portlet group id!");
         }
   
  +      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(cookies, "cookies");
  +
         if (groupCookies == null)
         {
            groupCookies = new HashMap();
  @@ -110,26 +118,33 @@
   
         if (groupCookies.containsKey(groupId))
         {
  -         throw new IllegalArgumentException("Trying to set a cookie for an existing group: " + groupId);
  +         log.debug("Trying to set a cookie for an existing group: " + groupId);
         }
   
  -      groupCookies.put(groupId, cookie);
  +      groupCookies.put(groupId, cookies);
      }
   
      public String getGroupCookieFor(String groupId)
      {
  -      if (!isPerGroupCookies())
  +      if (groupCookies == null)
         {
  -         throw new IllegalStateException("Cannot get group cookie when cookie protocol is perUser.");
  -
  +         return null;
         }
   
  -      if (groupCookies == null)
  +      // purge expired cookies
  +      Cookie[] cookies = (Cookie[])groupCookies.get(groupId);
  +      cookies = purgeExpiredCookies(cookies);
  +
  +      // if there are no non-expired cookies left, we will need to re-init them
  +      if (cookies.length == 0)
         {
  -         throw new IllegalStateException("No group cookies have been initialized yet.");
  +         setInitCookieDone(false);
         }
   
  -      return (String)groupCookies.get(groupId);
  +      // update cookies for the considered group id
  +      groupCookies.put(groupId, cookies);
  +
  +      return outputToExternalForm(cookies);
      }
   
      public void clearGroupCookies()
  @@ -169,19 +184,91 @@
         SessionInfo session = (SessionInfo)portletSessions.get(portletHandle);
         if (session != null)
         {
  -         return session.getSessionId();
  +         String id = session.getSessionId();
  +         // if the returned id is null, the session is invalid so we remove it
  +         if (id == null)
  +         {
  +            portletSessions.remove(portletHandle);
  +         }
  +
  +         return id;
         }
         return null;
      }
   
      public void removeSessionIdForPortlet(String portletHandle)
      {
  -      if (portletHandle != null)
  +      if (portletHandle != null && portletSessions != null)
         {
            portletSessions.remove(portletHandle);
         }
      }
   
  +   public int getNumberOfSessions()
  +   {
  +      if (portletSessions != null)
  +      {
  +         return portletSessions.size();
  +      }
  +      else
  +      {
  +         return 0;
  +      }
  +   }
  +
  +   public void replaceUserCookiesWith(ProducerSessionInformation currentSessionInfo)
  +   {
  +      if (currentSessionInfo.userCookie != null && currentSessionInfo.userCookie.length > 0)
  +      {
  +         this.userCookie = currentSessionInfo.userCookie;
  +      }
  +   }
  +
  +   /**
  +    * Create a String representation of the specified array of Cookies.
  +    *
  +    * @param cookies the cookies to be output to external form
  +    * @return a String representation of the cookies, ready to be sent over the wire.
  +    */
  +   private String outputToExternalForm(Cookie[] cookies)
  +   {
  +      if (cookies != null && cookies.length != 0)
  +      {
  +         StringBuffer sb = new StringBuffer(64);
  +         for (int i = 0; i < cookies.length; i++)
  +         {
  +            sb.append(cookies[i].toExternalForm());
  +            if (i != cookies.length - 1)
  +            {
  +               sb.append(";");
  +            }
  +         }
  +         return sb.toString();
  +      }
  +      return null;
  +   }
  +
  +   /**
  +    * Purges the expired cookies in the specified array.
  +    *
  +    * @param cookies the cookies to be purged
  +    * @return an array of Cookies containing only still valid cookies
  +    */
  +   private Cookie[] purgeExpiredCookies(Cookie[] cookies)
  +   {
  +      List cleanCookies = Tools.toList(cookies);
  +
  +      for (int i = 0; i < cookies.length; i++)
  +      {
  +         Cookie cookie = cookies[i];
  +         if (cookie.isExpired())
  +         {
  +            cleanCookies.remove(cookie);
  +         }
  +      }
  +      return (Cookie[])cleanCookies.toArray(new Cookie[0]);
  +   }
  +
      private class SessionInfo
      {
         private SessionContext sessionContext;
  
  
  
  1.2.2.2   +5 -3      jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/InvocationHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: InvocationHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/InvocationHandler.java,v
  retrieving revision 1.2.2.1
  retrieving revision 1.2.2.2
  diff -u -b -r1.2.2.1 -r1.2.2.2
  --- InvocationHandler.java	21 Aug 2006 23:07:20 -0000	1.2.2.1
  +++ InvocationHandler.java	25 Aug 2006 00:56:30 -0000	1.2.2.2
  @@ -40,7 +40,7 @@
   
   /**
    * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
  - * @version $Revision: 1.2.2.1 $
  + * @version $Revision: 1.2.2.2 $
    * @since 2.4 (May 31, 2006)
    */
   public abstract class InvocationHandler
  @@ -113,6 +113,8 @@
               sessionHandler.initPortletGroupIdIfNeeded(invocation);
   
               response = performRequest(request);
  +
  +            sessionHandler.updateCookiesIfNeeded(invocation);
            }
            catch (Exception e)
            {
  @@ -120,8 +122,8 @@
            }
            finally
            {
  -            // we're done: reset current group id
  -            sessionHandler.resetPortletGroupIdIfNeeded();
  +            // we're done: reset currently held information
  +            sessionHandler.resetCurrentlyHeldInformation();
            }
         }
         return response;
  
  
  
  1.5.2.2   +15 -10    jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/SessionHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SessionHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/SessionHandler.java,v
  retrieving revision 1.5.2.1
  retrieving revision 1.5.2.2
  diff -u -b -r1.5.2.1 -r1.5.2.2
  --- SessionHandler.java	21 Aug 2006 23:07:20 -0000	1.5.2.1
  +++ SessionHandler.java	25 Aug 2006 00:56:30 -0000	1.5.2.2
  @@ -44,7 +44,7 @@
    * Manages session informations on behalf of a consumer.
    *
    * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
  - * @version $Revision: 1.5.2.1 $
  + * @version $Revision: 1.5.2.2 $
    * @since 2.4 (May 31, 2006)
    */
   public class SessionHandler
  @@ -89,7 +89,7 @@
         if (requiresGroupInitCookie())
         {
            WSRPPortletInfo info = consumer.getPortletInfo(invocation);
  -         RequestHeaderClientHandler.setCurrentGroupId(info.getGroupId());
  +         RequestHeaderClientHandler.setCurrentInfo(info.getGroupId(), getProducerSessionInformation(invocation, true));
         }
      }
   
  @@ -98,12 +98,10 @@
         return requiresInitCookie() && CookieProtocol.perGroup.equals(requiresInitCookie);
      }
   
  -   public void resetPortletGroupIdIfNeeded()
  +   /** Resets the information held by RequestHeaderClientHandler for the current interaction. */
  +   public void resetCurrentlyHeldInformation()
      {
  -      if (requiresGroupInitCookie())
  -      {
  -         RequestHeaderClientHandler.setCurrentGroupId(null);
  -      }
  +      RequestHeaderClientHandler.resetCurrentInfo();
      }
   
      public void initCookieIfNeeded(PortletInvocation invocation) throws ServiceDescriptionUnavailableException
  @@ -129,7 +127,7 @@
   
         // check if we have already initialized cookies for this user
         ProducerSessionInformation sessionInformation = getProducerSessionInformation(invocation);
  -      if (sessionInformation.isUserCookiesInitialized())
  +      if (sessionInformation.isInitCookieDone())
         {
            return;
         }
  @@ -161,11 +159,11 @@
            }
            finally
            {
  -            resetPortletGroupIdIfNeeded();
  +            resetCurrentlyHeldInformation();
            }
         }
   
  -      sessionInformation.setUserCookiesInitialized(true);
  +      sessionInformation.setInitCookieDone(true);
      }
   
      private void initCookie(InitCookie initCookie, PortletInvocation invocation, boolean retryIfFails)
  @@ -209,6 +207,13 @@
         }
      }
   
  +   void updateCookiesIfNeeded(PortletInvocation invocation)
  +   {
  +      ProducerSessionInformation sessionInfo = getProducerSessionInformation(invocation, true);
  +      ProducerSessionInformation currentSessionInfo = RequestHeaderClientHandler.getCurrentProducerSessionInformation();
  +      sessionInfo.replaceUserCookiesWith(currentSessionInfo);
  +   }
  +
      ProducerSessionInformation getProducerSessionInformation(PortletInvocation invocation)
      {
         return getProducerSessionInformation(invocation, true);
  
  
  



More information about the jboss-cvs-commits mailing list