1   /**
2    * Copyright (c) 2000-2008 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.StringMaker;
27  import com.liferay.portal.kernel.util.StringPool;
28  
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.Enumeration;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.StringTokenizer;
36  
37  import javax.portlet.PortletContext;
38  import javax.portlet.PortletSession;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpSession;
42  
43  /**
44   * <a href="PortletSessionImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class PortletSessionImpl implements LiferayPortletSession {
50  
51      public static final String PORTLET_SCOPE_NAMESPACE = "javax.portlet.p.";
52  
53      public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
54  
55      public static final String getPortletScope(String portletName, long plid) {
56          StringMaker sm = new StringMaker();
57  
58          sm.append(PORTLET_SCOPE_NAMESPACE);
59          sm.append(portletName);
60          sm.append(LAYOUT_SEPARATOR);
61          sm.append(plid);
62  
63          return sm.toString();
64      }
65  
66      public static final String getPortletScopeName(
67          String portletName, long plid, String name) {
68  
69          StringMaker sm = new StringMaker();
70  
71          sm.append(getPortletScope(portletName, plid));
72          sm.append(StringPool.QUESTION);
73          sm.append(name);
74  
75          return sm.toString();
76      }
77  
78      public PortletSessionImpl(
79          HttpServletRequest req, String portletName, PortletContext ctx,
80          String portalSessionId, long plid) {
81  
82          _req = req;
83          _portletName = portletName;
84          _ctx = ctx;
85          _creationTime = System.currentTimeMillis();
86          _lastAccessedTime = _creationTime;
87          _interval = getHttpSession().getMaxInactiveInterval();
88          _new = true;
89          _invalid = false;
90          _portalSessionId = portalSessionId;
91          _plid = plid;
92      }
93  
94      public Object getAttribute(String name) {
95          if (name == null) {
96              throw new IllegalArgumentException();
97          }
98  
99          if (_invalid) {
100             throw new IllegalStateException();
101         }
102 
103         return getAttribute(name, PortletSession.PORTLET_SCOPE);
104     }
105 
106     public Object getAttribute(String name, int scope) {
107         if (name == null) {
108             throw new IllegalArgumentException();
109         }
110 
111         if (_invalid) {
112             throw new IllegalStateException();
113         }
114 
115         if (scope == PortletSession.PORTLET_SCOPE) {
116             return getHttpSession().getAttribute(_getPortletScopeName(name));
117         }
118         else {
119             return getHttpSession().getAttribute(name);
120         }
121     }
122 
123     public Map<String, Object> getAttributeMap() {
124         return getAttributeMap(PortletSession.PORTLET_SCOPE);
125     }
126 
127     public Map<String, Object> getAttributeMap(int scope) {
128         Map<String, Object> map = new HashMap<String, Object>();
129 
130         Enumeration<String> enu = getAttributeNames(scope);
131 
132         while (enu.hasMoreElements()) {
133             String name = enu.nextElement();
134 
135             Object value = getAttribute(name);
136 
137             map.put(name, value);
138         }
139 
140         return map;
141     }
142 
143     public Enumeration<String> getAttributeNames() {
144         if (_invalid) {
145             throw new IllegalStateException();
146         }
147 
148         return getAttributeNames(PortletSession.PORTLET_SCOPE);
149     }
150 
151     public Enumeration<String> getAttributeNames(int scope) {
152         if (_invalid) {
153             throw new IllegalStateException();
154         }
155 
156         if (scope == PortletSession.PORTLET_SCOPE) {
157             List<String> attributeNames = new ArrayList<String>();
158 
159             String portletScope = getPortletScope(_portletName, _plid);
160 
161             Enumeration<String> enu = getHttpSession().getAttributeNames();
162 
163             while (enu.hasMoreElements()) {
164                 String name = enu.nextElement();
165 
166                 StringTokenizer st = new StringTokenizer(
167                     name, StringPool.QUESTION);
168 
169                 if (st.countTokens() == 2) {
170                     if (st.nextToken().equals(portletScope)) {
171                         attributeNames.add(st.nextToken());
172                     }
173                 }
174             }
175 
176             return Collections.enumeration(attributeNames);
177         }
178         else {
179             return getHttpSession().getAttributeNames();
180         }
181     }
182 
183     public long getCreationTime() {
184         if (_invalid) {
185             throw new IllegalStateException();
186         }
187 
188         return _creationTime;
189     }
190 
191     public HttpSession getHttpSession() {
192         if (_ses == null) {
193             return _req.getSession();
194         }
195         else {
196             return _ses;
197         }
198     }
199 
200     public String getId() {
201         return getHttpSession().getId();
202     }
203 
204     public long getLastAccessedTime() {
205         return _lastAccessedTime;
206     }
207 
208     public int getMaxInactiveInterval() {
209         return _interval;
210     }
211 
212     public String getPortalSessionId() {
213         return _portalSessionId;
214     }
215 
216     public PortletContext getPortletContext() {
217         return _ctx;
218     }
219 
220     public void invalidate() {
221         if (_invalid) {
222             throw new IllegalStateException();
223         }
224 
225         getHttpSession().invalidate();
226 
227         _invalid = true;
228     }
229 
230     public boolean isNew() {
231         if (_invalid) {
232             throw new IllegalStateException();
233         }
234 
235         return _new;
236     }
237 
238     public boolean isValid() {
239         return !_invalid;
240     }
241 
242     public void removeAttribute(String name) {
243         if (name == null) {
244             throw new IllegalArgumentException();
245         }
246 
247         if (_invalid) {
248             throw new IllegalStateException();
249         }
250 
251         removeAttribute(name, PortletSession.PORTLET_SCOPE);
252     }
253 
254     public void removeAttribute(String name, int scope) {
255         if (name == null) {
256             throw new IllegalArgumentException();
257         }
258 
259         if (_invalid) {
260             throw new IllegalStateException();
261         }
262 
263         if (scope == PortletSession.PORTLET_SCOPE) {
264             getHttpSession().removeAttribute(_getPortletScopeName(name));
265         }
266         else {
267             getHttpSession().removeAttribute(name);
268         }
269     }
270 
271     public void setAttribute(String name, Object value) {
272         if (name == null) {
273             throw new IllegalArgumentException();
274         }
275 
276         if (_invalid) {
277             throw new IllegalStateException();
278         }
279 
280         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
281     }
282 
283     public void setAttribute(String name, Object value, int scope) {
284         if (name == null) {
285             throw new IllegalArgumentException();
286         }
287 
288         if (_invalid) {
289             throw new IllegalStateException();
290         }
291 
292         if (scope == PortletSession.PORTLET_SCOPE) {
293             getHttpSession().setAttribute(_getPortletScopeName(name), value);
294         }
295         else {
296             getHttpSession().setAttribute(name, value);
297         }
298     }
299 
300     public void setHttpSession(HttpSession ses) {
301         _ses = ses;
302     }
303 
304     public void setLastAccessedTime(long lastAccessedTime) {
305         _lastAccessedTime = lastAccessedTime;
306         _new = false;
307     }
308 
309     public void setMaxInactiveInterval(int interval) {
310         _interval = interval;
311     }
312 
313     private String _getPortletScopeName(String name) {
314         return getPortletScopeName(_portletName, _plid, name);
315     }
316 
317     private HttpServletRequest _req;
318     private HttpSession _ses;
319     private String _portletName;
320     private PortletContext _ctx;
321     private long _creationTime;
322     private long _lastAccessedTime;
323     private int _interval;
324     private boolean _new;
325     private boolean _invalid;
326     private String _portalSessionId;
327     private long _plid;
328 
329 }