View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /* 
17  
18   */
19  
20  package org.apache.pluto.core.impl;
21  
22  import java.util.Enumeration;
23  import java.util.Vector;
24  
25  import javax.portlet.PortletContext;
26  import javax.portlet.PortletSession;
27  import javax.portlet.PortletSessionUtil;
28  
29  import org.apache.pluto.factory.PortletObjectAccess;
30  import org.apache.pluto.om.window.PortletWindow;
31  
32  public class PortletSessionImpl implements PortletSession, javax.servlet.http.HttpSession
33  {
34      private final int DEFAULT_SCOPE = PortletSession.PORTLET_SCOPE;
35  
36      private javax.servlet.http.HttpSession httpSession;
37      private PortletContext portletContext = null;
38  
39      private PortletWindow portletWindow;
40  
41      public PortletSessionImpl(PortletWindow portletWindow,
42                                javax.servlet.http.HttpSession httpSession)
43      {
44          this.portletWindow = portletWindow;
45          this.httpSession = httpSession;
46      }
47  
48      // javax.portlet.PortletSession and javax.servlet.http.HttpSession implementation -------------
49      public Object getAttribute(String name)
50      {
51          return this.getAttribute(name, DEFAULT_SCOPE);
52      }
53  
54      public Enumeration getAttributeNames()
55      {
56          return this.getAttributeNames(DEFAULT_SCOPE);
57      }
58  
59      public long getCreationTime() throws java.lang.IllegalStateException
60      {
61          return httpSession.getCreationTime();
62      }
63  
64      public String getId() throws java.lang.IllegalStateException
65      {
66          return httpSession.getId();
67      }
68  
69      public long getLastAccessedTime() throws java.lang.IllegalStateException
70      {
71          return httpSession.getLastAccessedTime();
72      }
73  
74      public int getMaxInactiveInterval()
75      {
76          return httpSession.getMaxInactiveInterval();
77      }
78  
79      public void invalidate() throws java.lang.IllegalStateException
80      {
81          httpSession.invalidate();
82      }
83  
84      public boolean isNew() throws java.lang.IllegalStateException
85      {
86          return httpSession.isNew();
87      }
88  
89      public void removeAttribute(String name)
90      {
91          this.removeAttribute(name, DEFAULT_SCOPE);
92      }
93  
94      public void setAttribute(String name, Object value)
95      {
96          this.setAttribute(name, value, DEFAULT_SCOPE);
97      }
98  
99      public void setMaxInactiveInterval(int interval)
100     {
101         httpSession.setMaxInactiveInterval(interval);
102     }
103     // --------------------------------------------------------------------------------------------
104 
105     // javax.portlet.PortletSession implementation ------------------------------------------------
106     public java.lang.Object getAttribute(String name, int scope) throws java.lang.IllegalStateException
107     {
108         if (name == null)
109         {
110             throw new IllegalArgumentException("name must not be null");
111         }
112         if (scope==PortletSession.APPLICATION_SCOPE)
113         {
114             return httpSession.getAttribute(name);
115         }
116         else
117         {
118             Object attribute = httpSession.getAttribute("javax.portlet.p."+portletWindow.getId()+"?"+name);
119             if (attribute == null)
120             {
121                 // not sure, if this should be done for all attributes or only javax.servlet.
122                 attribute = httpSession.getAttribute(name);
123             }
124             return attribute;
125         }
126     }
127 
128     public java.util.Enumeration getAttributeNames(int scope)
129     {
130         if (scope==PortletSession.APPLICATION_SCOPE)
131         {
132             return httpSession.getAttributeNames();
133         }
134         else
135         {
136             Enumeration attributes = httpSession.getAttributeNames();
137 
138             Vector portletAttributes = new Vector();
139 
140 	        /* Fix that ONLY attributes of PORTLET_SCOPE are returned. */
141             int prefix_length = "javax.portlet.p.".length();
142             String portletWindowId = portletWindow.getId().toString();
143             
144             while (attributes.hasMoreElements())
145             {
146                 String attribute = (String)attributes.nextElement();
147 
148                 int attributeScope = PortletSessionUtil.decodeScope(attribute);
149                 
150                 if (attributeScope == PortletSession.PORTLET_SCOPE && attribute.startsWith(portletWindowId, prefix_length))
151                 {
152                 	String portletAttribute = PortletSessionUtil.decodeAttributeName(attribute);
153                 	
154                 	if (portletAttribute!=null)
155                 	{ // it is in the portlet's namespace
156                 		portletAttributes.add(portletAttribute);
157                 	}
158                 }
159            }
160 
161            return portletAttributes.elements();
162         }
163     }
164 
165     public void removeAttribute(String name, int scope) throws java.lang.IllegalStateException
166     {
167         if (name == null)
168         {
169             throw new IllegalArgumentException("name must not be null");
170         }
171         if (scope == PortletSession.APPLICATION_SCOPE)
172         {
173             httpSession.removeAttribute(name);
174         }
175         else
176         {
177             httpSession.removeAttribute("javax.portlet.p."+portletWindow.getId()+"?"+name);
178         }
179     }
180 
181     public void setAttribute(java.lang.String name, java.lang.Object value, int scope) throws IllegalStateException
182     {
183         if (name == null)
184         {
185             throw new IllegalArgumentException("name must not be null");
186         }
187         if (scope==PortletSession.APPLICATION_SCOPE)
188         {
189             httpSession.setAttribute(name,value);
190         }
191         else
192         {
193             httpSession.setAttribute("javax.portlet.p."+portletWindow.getId()+"?"+name, value);
194         }
195     }
196 
197     public PortletContext getPortletContext()
198     {
199         return getInternalPortletContext();
200     }
201     // --------------------------------------------------------------------------------------------
202 
203     // javax.servlet.http.HttpSession implementation ----------------------------------------------
204     public javax.servlet.ServletContext getServletContext()
205     {
206         // TBD, open issue. it would be good if we could also implement the ServletContext interface at the PortletContextImpl
207         return httpSession.getServletContext();
208     }
209 
210     public javax.servlet.http.HttpSessionContext getSessionContext()
211     {
212         return httpSession.getSessionContext();
213     }
214 
215     public Object getValue(String name)
216     {
217         return this.getAttribute(name, DEFAULT_SCOPE);
218     }
219 
220     public String[] getValueNames()
221     {
222         // TBD
223         return null;
224     }
225 
226     public void putValue(String name, Object value)
227     {
228         this.setAttribute(name, value, DEFAULT_SCOPE);
229     }
230 
231     public void removeValue(String name)
232     {
233         this.removeAttribute(name, DEFAULT_SCOPE);
234     }
235     // --------------------------------------------------------------------------------------------
236 
237     // internal methods ---------------------------------------------------------------------------
238     private synchronized PortletContext getInternalPortletContext()
239     {
240         if (this.portletContext == null)
241         {
242             this.portletContext = PortletObjectAccess.getPortletContext(
243                                                                        getServletContext(), 
244                                                                        portletWindow.getPortletEntity().getPortletApplicationEntity().getPortletApplicationDefinition()
245                                                                        );
246         }
247         return this.portletContext;
248     }
249     // --------------------------------------------------------------------------------------------
250 }