1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
48  public class PortletSessionImpl implements LiferayPortletSession {
49  
50      public static final String getPortletScope(String portletName, long plid) {
51          StringBuilder sb = new StringBuilder();
52  
53          sb.append(PORTLET_SCOPE_NAMESPACE);
54          sb.append(portletName);
55          sb.append(LAYOUT_SEPARATOR);
56          sb.append(plid);
57  
58          return sb.toString();
59      }
60  
61      public static final String getPortletScopeName(
62          String portletName, long plid, String name) {
63  
64          StringBuilder sb = new StringBuilder();
65  
66          sb.append(getPortletScope(portletName, plid));
67          sb.append(StringPool.QUESTION);
68          sb.append(name);
69  
70          return sb.toString();
71      }
72  
73      public PortletSessionImpl(
74          HttpServletRequest request, String portletName,
75          PortletContext portletContext, String portalSessionId, long plid) {
76  
77          _request = request;
78          _portletName = portletName;
79          _portletContext = portletContext;
80          _creationTime = System.currentTimeMillis();
81          _lastAccessedTime = _creationTime;
82          _interval = getHttpSession().getMaxInactiveInterval();
83          _new = true;
84          _invalid = false;
85          _portalSessionId = portalSessionId;
86          _plid = plid;
87      }
88  
89      public Object getAttribute(String name) {
90          if (name == null) {
91              throw new IllegalArgumentException();
92          }
93  
94          if (_invalid) {
95              throw new IllegalStateException();
96          }
97  
98          return getAttribute(name, PortletSession.PORTLET_SCOPE);
99      }
100 
101     public Object getAttribute(String name, int scope) {
102         if (name == null) {
103             throw new IllegalArgumentException();
104         }
105 
106         if (_invalid) {
107             throw new IllegalStateException();
108         }
109 
110         if (scope == PortletSession.PORTLET_SCOPE) {
111             return getHttpSession().getAttribute(_getPortletScopeName(name));
112         }
113         else {
114             return getHttpSession().getAttribute(name);
115         }
116     }
117 
118     public Map<String, Object> getAttributeMap() {
119         return getAttributeMap(PortletSession.PORTLET_SCOPE);
120     }
121 
122     public Map<String, Object> getAttributeMap(int scope) {
123         Map<String, Object> map = new HashMap<String, Object>();
124 
125         Enumeration<String> enu = getAttributeNames(scope);
126 
127         while (enu.hasMoreElements()) {
128             String name = enu.nextElement();
129 
130             Object value = getAttribute(name);
131 
132             map.put(name, value);
133         }
134 
135         return map;
136     }
137 
138     public Enumeration<String> getAttributeNames() {
139         if (_invalid) {
140             throw new IllegalStateException();
141         }
142 
143         return getAttributeNames(PortletSession.PORTLET_SCOPE);
144     }
145 
146     public Enumeration<String> getAttributeNames(int scope) {
147         if (_invalid) {
148             throw new IllegalStateException();
149         }
150 
151         if (scope == PortletSession.PORTLET_SCOPE) {
152             List<String> attributeNames = new ArrayList<String>();
153 
154             String portletScope = getPortletScope(_portletName, _plid);
155 
156             Enumeration<String> enu = getHttpSession().getAttributeNames();
157 
158             while (enu.hasMoreElements()) {
159                 String name = enu.nextElement();
160 
161                 StringTokenizer st = new StringTokenizer(
162                     name, StringPool.QUESTION);
163 
164                 if (st.countTokens() == 2) {
165                     if (st.nextToken().equals(portletScope)) {
166                         attributeNames.add(st.nextToken());
167                     }
168                 }
169             }
170 
171             return Collections.enumeration(attributeNames);
172         }
173         else {
174             return getHttpSession().getAttributeNames();
175         }
176     }
177 
178     public long getCreationTime() {
179         if (_invalid) {
180             throw new IllegalStateException();
181         }
182 
183         return _creationTime;
184     }
185 
186     public HttpSession getHttpSession() {
187         if (_session == null) {
188             return _request.getSession();
189         }
190         else {
191             return _session;
192         }
193     }
194 
195     public String getId() {
196         return getHttpSession().getId();
197     }
198 
199     public long getLastAccessedTime() {
200         return _lastAccessedTime;
201     }
202 
203     public int getMaxInactiveInterval() {
204         return _interval;
205     }
206 
207     public String getPortalSessionId() {
208         return _portalSessionId;
209     }
210 
211     public PortletContext getPortletContext() {
212         return _portletContext;
213     }
214 
215     public void invalidate() {
216         if (_invalid) {
217             throw new IllegalStateException();
218         }
219 
220         getHttpSession().invalidate();
221 
222         _invalid = true;
223     }
224 
225     public boolean isNew() {
226         if (_invalid) {
227             throw new IllegalStateException();
228         }
229 
230         return _new;
231     }
232 
233     public boolean isValid() {
234         return !_invalid;
235     }
236 
237     public void removeAttribute(String name) {
238         if (name == null) {
239             throw new IllegalArgumentException();
240         }
241 
242         if (_invalid) {
243             throw new IllegalStateException();
244         }
245 
246         removeAttribute(name, PortletSession.PORTLET_SCOPE);
247     }
248 
249     public void removeAttribute(String name, int scope) {
250         if (name == null) {
251             throw new IllegalArgumentException();
252         }
253 
254         if (_invalid) {
255             throw new IllegalStateException();
256         }
257 
258         if (scope == PortletSession.PORTLET_SCOPE) {
259             getHttpSession().removeAttribute(_getPortletScopeName(name));
260         }
261         else {
262             getHttpSession().removeAttribute(name);
263         }
264     }
265 
266     public void setAttribute(String name, Object value) {
267         if (name == null) {
268             throw new IllegalArgumentException();
269         }
270 
271         if (_invalid) {
272             throw new IllegalStateException();
273         }
274 
275         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
276     }
277 
278     public void setAttribute(String name, Object value, int scope) {
279         if (name == null) {
280             throw new IllegalArgumentException();
281         }
282 
283         if (_invalid) {
284             throw new IllegalStateException();
285         }
286 
287         if (scope == PortletSession.PORTLET_SCOPE) {
288             getHttpSession().setAttribute(_getPortletScopeName(name), value);
289         }
290         else {
291             getHttpSession().setAttribute(name, value);
292         }
293     }
294 
295     public void setHttpSession(HttpSession session) {
296         _session = session;
297     }
298 
299     public void setLastAccessedTime(long lastAccessedTime) {
300         _lastAccessedTime = lastAccessedTime;
301         _new = false;
302     }
303 
304     public void setMaxInactiveInterval(int interval) {
305         _interval = interval;
306     }
307 
308     private String _getPortletScopeName(String name) {
309         return getPortletScopeName(_portletName, _plid, name);
310     }
311 
312     private HttpServletRequest _request;
313     private HttpSession _session;
314     private String _portletName;
315     private PortletContext _portletContext;
316     private long _creationTime;
317     private long _lastAccessedTime;
318     private int _interval;
319     private boolean _new;
320     private boolean _invalid;
321     private String _portalSessionId;
322     private long _plid;
323 
324 }