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