1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.portals.bridges.util;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21
22 import javax.portlet.PortletRequest;
23 import javax.portlet.PortletSession;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpSession;
26
27 /***
28 * Proxy for a Servlet HttpSession to wrap a PortletSession, providing only access to PORTLET_SCOPE session attributes
29 * and hiding the APPLICATION_SCOPE attributes from the Servlet.
30 * <br/>
31 * This Proxy can be used to isolate two instances of the same Portlet dispatching to Servlets so they don't overwrite or read
32 * each others session attributes.
33 * <br/>
34 * Caveat: APPLICATION_SCOPE sessions attributes cannot be used anymore (directly) for inter-portlet communication,
35 * or when using Servlets directly which also need to "attach" to the PORTLET_SCOPE session attributes.<br/>
36 * The {@link PortletWindowUtils} class can help out with that though.
37
38 * @see PortletWindowUtils
39 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
40 * @version $Id: ServletPortletSessionProxy.java 510753 2007-02-23 01:28:50Z ate $
41 *
42 */
43 public class ServletPortletSessionProxy implements InvocationHandler
44 {
45 HttpSession servletSession;
46 PortletSession portletSession;
47
48 public static HttpSession createProxy(HttpServletRequest request)
49 {
50 HttpSession servletSession = request.getSession();
51 PortletRequest portletRequest = (PortletRequest) request.getAttribute("javax.portlet.request");
52 if (portletRequest != null)
53 {
54 PortletSession portletSession = portletRequest.getPortletSession();
55 servletSession = (HttpSession) Proxy.newProxyInstance(servletSession.getClass().getClassLoader(),
56 servletSession.getClass().getInterfaces(), new ServletPortletSessionProxy(servletSession,
57 portletSession));
58 }
59 return servletSession;
60 }
61
62 private ServletPortletSessionProxy(HttpSession servletSession, PortletSession portletSession)
63 {
64 this.servletSession = servletSession;
65 this.portletSession = portletSession;
66 }
67
68 /***
69 * (non-Javadoc)
70 *
71 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
72 * java.lang.reflect.Method, java.lang.Object[])
73 */
74 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
75 {
76 Object retval = null;
77 if (("getAttribute".equals(m.getName()) || "getValue".equals(m.getName())) && args.length == 1 && args[0] instanceof String)
78 {
79 retval = portletSession.getAttribute((String)args[0]);
80 }
81 else if (("setAttribute".equals(m.getName()) || "putValue".equals(m.getName())) && args.length == 2 && args[0] instanceof String)
82 {
83 portletSession.setAttribute((String)args[0], args[1]);
84 }
85 else if (("removeAttribute".equals(m.getName()) || "removeValue".equals(m.getName())) && args.length == 1 && args[0] instanceof String)
86 {
87 portletSession.removeAttribute((String)args[0]);
88 }
89 else if (("getAttributeNames".equals(m.getName()) || "getValueNames".equals(m.getName())) && args.length == 0)
90 {
91
92 retval = portletSession.getAttributeNames();
93 }
94 else
95 {
96 retval = m.invoke(servletSession, args);
97 }
98 return retval;
99 }
100 }