Author: ate
Date: Fri Jun  8 17:56:12 2007
New Revision: 545678

URL: http://svn.apache.org/viewvc?view=rev&rev=545678
Log:
Improved the ServletPortletSessionProxy so it now can also be used for 
"reconnecting" direct servlet based requests to the PORTLET_SCOPE 
PortletSession attributes. 

Modified:
    
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/util/ServletPortletSessionProxy.java

Modified: 
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/util/ServletPortletSessionProxy.java
URL: 
http://svn.apache.org/viewvc/portals/bridges/trunk/common/src/java/org/apache/portals/bridges/util/ServletPortletSessionProxy.java?view=diff&rev=545678&r1=545677&r2=545678
==============================================================================
--- 
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/util/ServletPortletSessionProxy.java
 (original)
+++ 
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/util/ServletPortletSessionProxy.java
 Fri Jun  8 17:56:12 2007
@@ -19,6 +19,8 @@
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
+import java.util.Enumeration;
+import java.util.NoSuchElementException;
 
 import javax.portlet.PortletRequest;
 import javax.portlet.PortletSession;
@@ -26,7 +28,7 @@
 import javax.servlet.http.HttpSession;
 
 /**
- * Proxy for a Servlet HttpSession to wrap a PortletSession, providing only 
access to PORTLET_SCOPE session attributes
+ * Proxy for a Servlet HttpSession to attach to a PortletSession, providing 
only access to PORTLET_SCOPE session attributes
  * and hiding the APPLICATION_SCOPE attributes from the Servlet.
  * <br/>
  * This Proxy can be used to isolate two instances of the same Portlet 
dispatching to Servlets so they don't overwrite or read
@@ -43,8 +45,56 @@
  */
 public class ServletPortletSessionProxy implements InvocationHandler
 {
+    private static class PortletSessionAttributeNamesEnumeration implements 
Enumeration
+    {
+        private Enumeration namesEnumeration;
+        private String      portletWindowPrefix;
+        
+        private String nextName;
+        private boolean done;
+        
+        public PortletSessionAttributeNamesEnumeration(Enumeration 
namesEnumeration, String portletWindowPrefix)
+        {
+            this.namesEnumeration = namesEnumeration;
+            this.portletWindowPrefix = portletWindowPrefix;
+            hasMoreElements();
+        }
+        
+        public boolean hasMoreElements()
+        {
+            if (!done)
+            {
+                if (nextName == null)
+                {
+                    while (namesEnumeration.hasMoreElements())
+                    {
+                        String name = (String)namesEnumeration.nextElement();
+                        if ( name.startsWith(portletWindowPrefix))
+                        {
+                            nextName = 
name.substring(portletWindowPrefix.length());
+                            break;
+                        }
+                    }
+                    done = nextName == null;
+                }
+            }
+            return !done;
+        }
+
+        public Object nextElement()
+        {
+            if (done)
+            {
+                throw new NoSuchElementException();
+            }
+            String name = nextName;
+            nextName = null;
+            return name;
+        }
+    }
+    
     HttpSession servletSession;
-    PortletSession portletSession;
+    String portletWindowPrefix;
 
     public static HttpSession createProxy(HttpServletRequest request)
     {
@@ -53,17 +103,23 @@
         if (portletRequest != null)
         {
             PortletSession portletSession = portletRequest.getPortletSession();
-            servletSession = (HttpSession) 
Proxy.newProxyInstance(servletSession.getClass().getClassLoader(),
-                    servletSession.getClass().getInterfaces(), new 
ServletPortletSessionProxy(servletSession,
-                            portletSession));
+            servletSession = (HttpSession)createProxy(request, 
PortletWindowUtils.getPortletWindowId(portletSession));
         }
         return servletSession;
     }
 
-    private ServletPortletSessionProxy(HttpSession servletSession, 
PortletSession portletSession)
+    public static HttpSession createProxy(HttpServletRequest request, String 
portletWindowId)
+    {
+        HttpSession servletSession = request.getSession();
+        return (HttpSession) 
Proxy.newProxyInstance(servletSession.getClass().getClassLoader(),
+                servletSession.getClass().getInterfaces(), new 
ServletPortletSessionProxy(request.getSession(),
+                        portletWindowId));
+    }
+
+    private ServletPortletSessionProxy(HttpSession servletSession, String 
portletWindowId)
     {
         this.servletSession = servletSession;
-        this.portletSession = portletSession;
+        this.portletWindowPrefix = 
PortletWindowUtils.getApplicationScopeSessionAttributeName(portletWindowId,"");
     }
 
     /**
@@ -77,20 +133,19 @@
         Object retval = null;
         if (("getAttribute".equals(m.getName()) || 
"getValue".equals(m.getName())) && args.length == 1 && args[0] instanceof 
String)
         {
-            retval = portletSession.getAttribute((String)args[0]);
+            retval = 
servletSession.getAttribute(portletWindowPrefix+(String)args[0]);
         }
         else if (("setAttribute".equals(m.getName()) || 
"putValue".equals(m.getName())) && args.length == 2 && args[0] instanceof 
String)
         {
-            portletSession.setAttribute((String)args[0], args[1]);
+            
servletSession.setAttribute(portletWindowPrefix+(String)args[0],args[1]);
         }
         else if (("removeAttribute".equals(m.getName()) || 
"removeValue".equals(m.getName())) && args.length == 1 && args[0] instanceof 
String)
         {
-            portletSession.removeAttribute((String)args[0]);
+            
servletSession.removeAttribute(portletWindowPrefix+(String)args[0]);
         }
         else if (("getAttributeNames".equals(m.getName()) || 
"getValueNames".equals(m.getName())) && args.length == 0)
         {
-            
-            retval = portletSession.getAttributeNames();
+            retval = new 
PortletSessionAttributeNamesEnumeration(servletSession.getAttributeNames(), 
portletWindowPrefix);
         }
         else
         {



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

Reply via email to