1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.StringPool;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.StringTokenizer;
28  
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletSession;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpSession;
34  
35  /**
36   * <a href="PortletSessionImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class PortletSessionImpl implements LiferayPortletSession {
41  
42      public static final String getPortletScope(String portletName, long plid) {
43          StringBundler sb = new StringBundler(4);
44  
45          sb.append(PORTLET_SCOPE_NAMESPACE);
46          sb.append(portletName);
47          sb.append(LAYOUT_SEPARATOR);
48          sb.append(plid);
49  
50          return sb.toString();
51      }
52  
53      public static final String getPortletScopeName(
54          String portletName, long plid, String name) {
55  
56          return getPortletScope(portletName, plid).concat(
57              StringPool.QUESTION).concat(name);
58      }
59  
60      public PortletSessionImpl(
61          HttpServletRequest request, String portletName,
62          PortletContext portletContext, String portalSessionId, long plid) {
63  
64          _request = request;
65          _portletName = portletName;
66          _portletContext = portletContext;
67          _creationTime = System.currentTimeMillis();
68          _lastAccessedTime = _creationTime;
69          _interval = getHttpSession().getMaxInactiveInterval();
70          _new = true;
71          _invalid = false;
72          _portalSessionId = portalSessionId;
73          _plid = plid;
74      }
75  
76      public Object getAttribute(String name) {
77          if (name == null) {
78              throw new IllegalArgumentException();
79          }
80  
81          if (_invalid) {
82              throw new IllegalStateException();
83          }
84  
85          return getAttribute(name, PortletSession.PORTLET_SCOPE);
86      }
87  
88      public Object getAttribute(String name, int scope) {
89          if (name == null) {
90              throw new IllegalArgumentException();
91          }
92  
93          if (_invalid) {
94              throw new IllegalStateException();
95          }
96  
97          if (scope == PortletSession.PORTLET_SCOPE) {
98              return getHttpSession().getAttribute(_getPortletScopeName(name));
99          }
100         else {
101             return getHttpSession().getAttribute(name);
102         }
103     }
104 
105     public Map<String, Object> getAttributeMap() {
106         return getAttributeMap(PortletSession.PORTLET_SCOPE);
107     }
108 
109     public Map<String, Object> getAttributeMap(int scope) {
110         Map<String, Object> map = new HashMap<String, Object>();
111 
112         Enumeration<String> enu = getAttributeNames(scope);
113 
114         while (enu.hasMoreElements()) {
115             String name = enu.nextElement();
116 
117             Object value = getAttribute(name);
118 
119             map.put(name, value);
120         }
121 
122         return map;
123     }
124 
125     public Enumeration<String> getAttributeNames() {
126         if (_invalid) {
127             throw new IllegalStateException();
128         }
129 
130         return getAttributeNames(PortletSession.PORTLET_SCOPE);
131     }
132 
133     public Enumeration<String> getAttributeNames(int scope) {
134         if (_invalid) {
135             throw new IllegalStateException();
136         }
137 
138         if (scope == PortletSession.PORTLET_SCOPE) {
139             List<String> attributeNames = new ArrayList<String>();
140 
141             String portletScope = getPortletScope(_portletName, _plid);
142 
143             Enumeration<String> enu = getHttpSession().getAttributeNames();
144 
145             while (enu.hasMoreElements()) {
146                 String name = enu.nextElement();
147 
148                 StringTokenizer st = new StringTokenizer(
149                     name, StringPool.QUESTION);
150 
151                 if (st.countTokens() == 2) {
152                     if (st.nextToken().equals(portletScope)) {
153                         attributeNames.add(st.nextToken());
154                     }
155                 }
156             }
157 
158             return Collections.enumeration(attributeNames);
159         }
160         else {
161             return getHttpSession().getAttributeNames();
162         }
163     }
164 
165     public long getCreationTime() {
166         if (_invalid) {
167             throw new IllegalStateException();
168         }
169 
170         return _creationTime;
171     }
172 
173     public HttpSession getHttpSession() {
174         if (_session == null) {
175             return _request.getSession();
176         }
177         else {
178             return _session;
179         }
180     }
181 
182     public String getId() {
183         return getHttpSession().getId();
184     }
185 
186     public long getLastAccessedTime() {
187         return _lastAccessedTime;
188     }
189 
190     public int getMaxInactiveInterval() {
191         return _interval;
192     }
193 
194     public String getPortalSessionId() {
195         return _portalSessionId;
196     }
197 
198     public PortletContext getPortletContext() {
199         return _portletContext;
200     }
201 
202     public void invalidate() {
203         if (_invalid) {
204             throw new IllegalStateException();
205         }
206 
207         getHttpSession().invalidate();
208 
209         _invalid = true;
210     }
211 
212     public boolean isNew() {
213         if (_invalid) {
214             throw new IllegalStateException();
215         }
216 
217         return _new;
218     }
219 
220     public boolean isValid() {
221         return !_invalid;
222     }
223 
224     public void removeAttribute(String name) {
225         if (name == null) {
226             throw new IllegalArgumentException();
227         }
228 
229         if (_invalid) {
230             throw new IllegalStateException();
231         }
232 
233         removeAttribute(name, PortletSession.PORTLET_SCOPE);
234     }
235 
236     public void removeAttribute(String name, int scope) {
237         if (name == null) {
238             throw new IllegalArgumentException();
239         }
240 
241         if (_invalid) {
242             throw new IllegalStateException();
243         }
244 
245         if (scope == PortletSession.PORTLET_SCOPE) {
246             getHttpSession().removeAttribute(_getPortletScopeName(name));
247         }
248         else {
249             getHttpSession().removeAttribute(name);
250         }
251     }
252 
253     public void setAttribute(String name, Object value) {
254         if (name == null) {
255             throw new IllegalArgumentException();
256         }
257 
258         if (_invalid) {
259             throw new IllegalStateException();
260         }
261 
262         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
263     }
264 
265     public void setAttribute(String name, Object value, int scope) {
266         if (name == null) {
267             throw new IllegalArgumentException();
268         }
269 
270         if (_invalid) {
271             throw new IllegalStateException();
272         }
273 
274         if (scope == PortletSession.PORTLET_SCOPE) {
275             getHttpSession().setAttribute(_getPortletScopeName(name), value);
276         }
277         else {
278             getHttpSession().setAttribute(name, value);
279         }
280     }
281 
282     public void setHttpSession(HttpSession session) {
283         _session = session;
284     }
285 
286     public void setLastAccessedTime(long lastAccessedTime) {
287         _lastAccessedTime = lastAccessedTime;
288         _new = false;
289     }
290 
291     public void setMaxInactiveInterval(int interval) {
292         _interval = interval;
293     }
294 
295     private String _getPortletScopeName(String name) {
296         return getPortletScopeName(_portletName, _plid, name);
297     }
298 
299     private HttpServletRequest _request;
300     private HttpSession _session;
301     private String _portletName;
302     private PortletContext _portletContext;
303     private long _creationTime;
304     private long _lastAccessedTime;
305     private int _interval;
306     private boolean _new;
307     private boolean _invalid;
308     private String _portalSessionId;
309     private long _plid;
310 
311 }