1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.StringTokenizer;
35  
36  import javax.portlet.PortletContext;
37  import javax.portlet.PortletSession;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpSession;
41  
42  /**
43   * <a href="PortletSessionImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class PortletSessionImpl implements LiferayPortletSession {
48  
49      public static final String PORTLET_SCOPE_NAMESPACE = "javax.portlet.p.";
50  
51      public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
52  
53      public static final String getPortletScope(String portletName, long plid) {
54          StringBuilder sb = new StringBuilder();
55  
56          sb.append(PORTLET_SCOPE_NAMESPACE);
57          sb.append(portletName);
58          sb.append(LAYOUT_SEPARATOR);
59          sb.append(plid);
60  
61          return sb.toString();
62      }
63  
64      public static final String getPortletScopeName(
65          String portletName, long plid, String name) {
66  
67          StringBuilder sb = new StringBuilder();
68  
69          sb.append(getPortletScope(portletName, plid));
70          sb.append(StringPool.QUESTION);
71          sb.append(name);
72  
73          return sb.toString();
74      }
75  
76      public PortletSessionImpl(
77          HttpServletRequest request, String portletName,
78          PortletContext portletContext, String portalSessionId, long plid) {
79  
80          _request = request;
81          _portletName = portletName;
82          _portletContext = portletContext;
83          _creationTime = System.currentTimeMillis();
84          _lastAccessedTime = _creationTime;
85          _interval = getHttpSession().getMaxInactiveInterval();
86          _new = true;
87          _invalid = false;
88          _portalSessionId = portalSessionId;
89          _plid = plid;
90      }
91  
92      public Object getAttribute(String name) {
93          if (name == null) {
94              throw new IllegalArgumentException();
95          }
96  
97          if (_invalid) {
98              throw new IllegalStateException();
99          }
100 
101         return getAttribute(name, PortletSession.PORTLET_SCOPE);
102     }
103 
104     public Object getAttribute(String name, int scope) {
105         if (name == null) {
106             throw new IllegalArgumentException();
107         }
108 
109         if (_invalid) {
110             throw new IllegalStateException();
111         }
112 
113         if (scope == PortletSession.PORTLET_SCOPE) {
114             return getHttpSession().getAttribute(_getPortletScopeName(name));
115         }
116         else {
117             return getHttpSession().getAttribute(name);
118         }
119     }
120 
121     public Map<String, Object> getAttributeMap() {
122         return getAttributeMap(PortletSession.PORTLET_SCOPE);
123     }
124 
125     public Map<String, Object> getAttributeMap(int scope) {
126         Map<String, Object> map = new HashMap<String, Object>();
127 
128         Enumeration<String> enu = getAttributeNames(scope);
129 
130         while (enu.hasMoreElements()) {
131             String name = enu.nextElement();
132 
133             Object value = getAttribute(name);
134 
135             map.put(name, value);
136         }
137 
138         return map;
139     }
140 
141     public Enumeration<String> getAttributeNames() {
142         if (_invalid) {
143             throw new IllegalStateException();
144         }
145 
146         return getAttributeNames(PortletSession.PORTLET_SCOPE);
147     }
148 
149     public Enumeration<String> getAttributeNames(int scope) {
150         if (_invalid) {
151             throw new IllegalStateException();
152         }
153 
154         if (scope == PortletSession.PORTLET_SCOPE) {
155             List<String> attributeNames = new ArrayList<String>();
156 
157             String portletScope = getPortletScope(_portletName, _plid);
158 
159             Enumeration<String> enu = getHttpSession().getAttributeNames();
160 
161             while (enu.hasMoreElements()) {
162                 String name = enu.nextElement();
163 
164                 StringTokenizer st = new StringTokenizer(
165                     name, StringPool.QUESTION);
166 
167                 if (st.countTokens() == 2) {
168                     if (st.nextToken().equals(portletScope)) {
169                         attributeNames.add(st.nextToken());
170                     }
171                 }
172             }
173 
174             return Collections.enumeration(attributeNames);
175         }
176         else {
177             return getHttpSession().getAttributeNames();
178         }
179     }
180 
181     public long getCreationTime() {
182         if (_invalid) {
183             throw new IllegalStateException();
184         }
185 
186         return _creationTime;
187     }
188 
189     public HttpSession getHttpSession() {
190         if (_session == null) {
191             return _request.getSession();
192         }
193         else {
194             return _session;
195         }
196     }
197 
198     public String getId() {
199         return getHttpSession().getId();
200     }
201 
202     public long getLastAccessedTime() {
203         return _lastAccessedTime;
204     }
205 
206     public int getMaxInactiveInterval() {
207         return _interval;
208     }
209 
210     public String getPortalSessionId() {
211         return _portalSessionId;
212     }
213 
214     public PortletContext getPortletContext() {
215         return _portletContext;
216     }
217 
218     public void invalidate() {
219         if (_invalid) {
220             throw new IllegalStateException();
221         }
222 
223         getHttpSession().invalidate();
224 
225         _invalid = true;
226     }
227 
228     public boolean isNew() {
229         if (_invalid) {
230             throw new IllegalStateException();
231         }
232 
233         return _new;
234     }
235 
236     public boolean isValid() {
237         return !_invalid;
238     }
239 
240     public void removeAttribute(String name) {
241         if (name == null) {
242             throw new IllegalArgumentException();
243         }
244 
245         if (_invalid) {
246             throw new IllegalStateException();
247         }
248 
249         removeAttribute(name, PortletSession.PORTLET_SCOPE);
250     }
251 
252     public void removeAttribute(String name, int scope) {
253         if (name == null) {
254             throw new IllegalArgumentException();
255         }
256 
257         if (_invalid) {
258             throw new IllegalStateException();
259         }
260 
261         if (scope == PortletSession.PORTLET_SCOPE) {
262             getHttpSession().removeAttribute(_getPortletScopeName(name));
263         }
264         else {
265             getHttpSession().removeAttribute(name);
266         }
267     }
268 
269     public void setAttribute(String name, Object value) {
270         if (name == null) {
271             throw new IllegalArgumentException();
272         }
273 
274         if (_invalid) {
275             throw new IllegalStateException();
276         }
277 
278         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
279     }
280 
281     public void setAttribute(String name, Object value, int scope) {
282         if (name == null) {
283             throw new IllegalArgumentException();
284         }
285 
286         if (_invalid) {
287             throw new IllegalStateException();
288         }
289 
290         if (scope == PortletSession.PORTLET_SCOPE) {
291             getHttpSession().setAttribute(_getPortletScopeName(name), value);
292         }
293         else {
294             getHttpSession().setAttribute(name, value);
295         }
296     }
297 
298     public void setHttpSession(HttpSession session) {
299         _session = session;
300     }
301 
302     public void setLastAccessedTime(long lastAccessedTime) {
303         _lastAccessedTime = lastAccessedTime;
304         _new = false;
305     }
306 
307     public void setMaxInactiveInterval(int interval) {
308         _interval = interval;
309     }
310 
311     private String _getPortletScopeName(String name) {
312         return getPortletScopeName(_portletName, _plid, name);
313     }
314 
315     private HttpServletRequest _request;
316     private HttpSession _session;
317     private String _portletName;
318     private PortletContext _portletContext;
319     private long _creationTime;
320     private long _lastAccessedTime;
321     private int _interval;
322     private boolean _new;
323     private boolean _invalid;
324     private String _portalSessionId;
325     private long _plid;
326 
327 }